Upload Live_Match.py

#3
by Shadvikram - opened
Files changed (1) hide show
  1. src/pages/Live_Match.py +87 -0
src/pages/Live_Match.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import random
3
+ import json
4
+ from streamlit_lottie import st_lottie
5
+
6
+ def load_lottie_file(path):
7
+ try:
8
+ with open(path, "r") as f:
9
+ return json.load(f)
10
+ except:
11
+ return None
12
+
13
+ st.set_page_config(page_title="Live Match")
14
+ st.title("๐ŸŽฎ Live Match")
15
+
16
+ required_keys = ["match_ready", "your_team", "opponent", "overs", "batting_first"]
17
+ if not all(k in st.session_state for k in required_keys):
18
+ st.error("โš  Match setup incomplete. Please go to the Match Setup page first.")
19
+ st.stop()
20
+
21
+ six_url = "shots_animations/6.json"
22
+ four_url = "shots_animations/4.json"
23
+ wicket_url = "shots_animations/wicket.json"
24
+
25
+ batting_first = st.session_state["batting_first"]
26
+ bowling_first = st.session_state["opponent"] if batting_first == st.session_state["your_team"] else st.session_state["your_team"]
27
+
28
+ if "innings" not in st.session_state:
29
+ st.session_state["innings"] = 1
30
+ st.session_state["team1_runs"] = 0
31
+ st.session_state["team1_balls"] = 0
32
+ st.session_state["team1_out"] = False
33
+ st.session_state["team2_runs"] = 0
34
+ st.session_state["team2_balls"] = 0
35
+ st.session_state["team2_out"] = False
36
+
37
+ innings = st.session_state["innings"]
38
+ batting_team = batting_first if innings == 1 else bowling_first
39
+ current_team = "team1" if innings == 1 else "team2"
40
+
41
+ runs = st.session_state[f"{current_team}_runs"]
42
+ balls = st.session_state[f"{current_team}_balls"]
43
+ out = st.session_state[f"{current_team}_out"]
44
+ max_balls = st.session_state["overs"] * 6
45
+
46
+ st.subheader(f"๐Ÿ Inning {innings}: {batting_team} Batting")
47
+ st.markdown(f"๐ŸŽฏ Score: *{runs}/{1 if out else 0}* | ๐Ÿงฎ Balls: *{balls}/{max_balls}*")
48
+
49
+ if not out and balls < max_balls:
50
+ shot = st.selectbox("Choose your shot", ["Straight Drive", "Pull", "Cut", "Scoop", "Defend"], key=f"shot_{innings}")
51
+ if st.button("Play Shot"):
52
+ outcome = random.choices(
53
+ ["Dot", "1", "2", "4", "6", "Wicket"],
54
+ weights=[10, 15, 10, 20, 15, 5], k=1
55
+ )[0]
56
+
57
+ if outcome == "Wicket":
58
+ st.error("You're OUT! ๐Ÿ")
59
+ st.session_state[f"{current_team}_out"] = True
60
+ anim = load_lottie_file(wicket_url)
61
+ if anim: st_lottie(anim, speed=1, width=400, height=400, key=f"wicket_{innings}")
62
+ else:
63
+ st.success(f"You played a {shot} and scored: {outcome}")
64
+ if outcome == "4":
65
+ anim = load_lottie_file(four_url)
66
+ if anim: st_lottie(anim, speed=1, width=400, height=400, key=f"four_{innings}")
67
+ elif outcome == "6":
68
+ anim = load_lottie_file(six_url)
69
+ if anim: st_lottie(anim, speed=1, width=400, height=400, key=f"six_{innings}")
70
+
71
+ st.session_state[f"{current_team}_runs"] += int(outcome) if outcome.isdigit() else 0
72
+ st.session_state[f"{current_team}_balls"] += 1
73
+ else:
74
+ if innings == 1:
75
+ st.info("๐Ÿ›‘ First Innings Over! Click to start the second innings.")
76
+ if st.button("Start Second Innings"):
77
+ st.session_state["innings"] = 2
78
+ else:
79
+ st.success("๐Ÿ Match Over! You can now head to the Scorecard page.")
80
+
81
+ with st.expander("๐ŸŒฆ Match Conditions"):
82
+ st.markdown(f"""
83
+ - ๐ŸŸ *Stadium*: {st.session_state["stadium"]}
84
+ - ๐ŸŒค *Weather*: {st.session_state["weather"]}
85
+ - ๐Ÿงฑ *Pitch Type*: {st.session_state["pitch"]}
86
+ - ๐ŸŽฏ *Bowling Style*: {st.session_state["bowling_type"]}
87
+ """)