jarajpu commited on
Commit
2847f48
·
1 Parent(s): a53fee8

DIS IPL App - Base

Browse files
Files changed (6) hide show
  1. app.py +246 -0
  2. match_outcomes.json +0 -0
  3. matches.json +156 -0
  4. predictions.csv +2 -0
  5. requirements.txt +3 -0
  6. users.json +17 -0
app.py ADDED
@@ -0,0 +1,246 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from datetime import datetime
3
+
4
+ import pandas as pd
5
+ import pytz
6
+ import streamlit as st
7
+
8
+ # File paths
9
+ predictions_csv = 'predictions.csv'
10
+ users_json = 'users.json'
11
+ matches_json = 'matches.json'
12
+ outcomes_json = 'match_outcomes.json'
13
+
14
+
15
+ # Initialize CSV and JSON files if they don't exist
16
+ def initialize_files():
17
+ # Initialize predictions CSV
18
+ try:
19
+ pd.read_csv(predictions_csv)
20
+ except FileNotFoundError:
21
+ df = pd.DataFrame(columns=['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points'])
22
+ df.to_csv(predictions_csv, index=False)
23
+
24
+
25
+ # Load users from JSON
26
+ def get_users():
27
+ try:
28
+ with open(users_json, 'r') as file:
29
+ users = json.load(file)
30
+ return list(users.keys())
31
+ except FileNotFoundError:
32
+ return []
33
+
34
+
35
+ # Load matches from JSON
36
+ def load_matches():
37
+ try:
38
+ with open(matches_json, 'r') as f:
39
+ return json.load(f)
40
+ except FileNotFoundError:
41
+ return []
42
+
43
+
44
+ def load_match_outcomes():
45
+ try:
46
+ with open(outcomes_json, 'r') as file:
47
+ return json.load(file)
48
+ except FileNotFoundError:
49
+ return []
50
+
51
+
52
+ # Get today's date in IST to load today's match
53
+ def get_current_date_ist():
54
+ tz_IST = pytz.timezone('Asia/Kolkata')
55
+ datetime_ist = datetime.now(tz_IST)
56
+ return datetime_ist.strftime('%Y-%m-%d')
57
+
58
+
59
+ # Function to get matches for today
60
+ def get_today_matches():
61
+ today = get_current_date_ist()
62
+ matches = load_matches()
63
+ today_matches = [match for match in matches if match['date'] == today]
64
+ return today_matches
65
+
66
+
67
+ # Submit prediction function
68
+ def submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid_points):
69
+ # Ensure predictions DataFrame is loaded or initialized correctly
70
+ try:
71
+ predictions = pd.read_csv(predictions_csv)
72
+ # Check if all expected columns are present, if not, reinitialize the DataFrame
73
+ expected_columns = ['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points']
74
+ if not all(column in predictions.columns for column in expected_columns):
75
+ raise ValueError("CSV file missing one or more columns; Reinitializing.")
76
+ except (FileNotFoundError, ValueError) as e:
77
+ predictions = pd.DataFrame(columns=expected_columns)
78
+
79
+ # Check for duplicate prediction for the same match by the same user
80
+ if user_name == "Select a user...":
81
+ st.warning("Please select a valid user.")
82
+ return
83
+ else:
84
+ existing_predictions = predictions[(predictions['user_name'] == user_name) & (predictions['match_id'] == match_id)]
85
+ if not existing_predictions.empty:
86
+ st.error("You've already submitted a prediction for this match.")
87
+ return
88
+
89
+ # Append new prediction
90
+ new_prediction = {
91
+ 'user_name': user_name,
92
+ 'match_id': match_id,
93
+ 'predicted_winner': predicted_winner,
94
+ 'predicted_motm': predicted_motm,
95
+ 'bid_points': bid_points
96
+ }
97
+
98
+ predictions = pd.concat([predictions, pd.DataFrame([new_prediction])], ignore_index=True)
99
+ predictions.to_csv(predictions_csv, index=False)
100
+ st.success("Prediction submitted successfully!")
101
+
102
+
103
+ # Streamlit UI
104
+ st.title("DIS IPL Match Predictions")
105
+
106
+ # Prediction form
107
+ with st.expander("Submit Prediction"):
108
+
109
+ # User selection
110
+ user_name = st.selectbox("Select User", ["Select a user..."] + get_users())
111
+
112
+ # Match selection
113
+ matches = get_today_matches()
114
+ if matches:
115
+ match_choice = st.selectbox("Select Today's Match", matches, format_func=lambda match: f"{match['teams'][0]} vs {match['teams'][1]}")
116
+ match_id = match_choice['match_id']
117
+ teams = match_choice['teams']
118
+ else:
119
+ st.write("No matches are scheduled for today.")
120
+ st.stop()
121
+
122
+ # Predictions
123
+ predicted_winner = st.selectbox("Predicted Winner", teams)
124
+ predicted_motm = st.text_input("Predicted Man of the Match")
125
+ bid_points = st.number_input("Bid Points", min_value=1, value=100)
126
+
127
+ # Submit button
128
+ if st.button("Submit Prediction"):
129
+ submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid_points)
130
+
131
+
132
+ # Show predictions
133
+ with st.expander("Predictions"):
134
+
135
+ # Display predictions
136
+ if st.button("Show Predictions"):
137
+ try:
138
+ df = pd.read_csv(predictions_csv)
139
+ st.dataframe(df)
140
+ except FileNotFoundError:
141
+ st.write("No predictions have been submitted yet.")
142
+
143
+
144
+ # Show leaderboard
145
+ with st.expander("Leaderboard"):
146
+
147
+ # Display leaderboard
148
+ if st.button("Show Leaderboard"):
149
+ try:
150
+ with open(users_json, 'r') as f:
151
+ users = json.load(f)
152
+ leaderboard = sorted(users.items(), key=lambda x: x[1], reverse=True)
153
+ df_leaderboard = pd.DataFrame(leaderboard, columns=['User', 'Points'])
154
+
155
+ # Add a 'Rank' column starting from 1
156
+ df_leaderboard['Rank'] = range(1, len(df_leaderboard) + 1)
157
+
158
+ # Reorder DataFrame columns so 'Rank' is first
159
+ df_leaderboard = df_leaderboard[['Rank', 'User', 'Points']]
160
+
161
+ st.dataframe(df_leaderboard)
162
+ except FileNotFoundError:
163
+ st.write("Leaderboard data not available.")
164
+
165
+
166
+
167
+
168
+ ADMIN_PASSPHRASE = "admin123"
169
+
170
+
171
+ def load_predictions():
172
+ # loading predictions from 'predictions.csv'
173
+ try:
174
+ return pd.read_csv(predictions_csv)
175
+ except FileNotFoundError:
176
+ return pd.DataFrame(columns=['user_name', 'match_id', 'predicted_winner', 'predicted_motm', 'bid_points'])
177
+
178
+
179
+ def load_users():
180
+ with open(users_json, 'r') as file:
181
+ return json.load(file)
182
+
183
+
184
+ def save_users(users):
185
+ with open(users_json, 'w') as file:
186
+ json.dump(users, file, indent=4)
187
+
188
+
189
+ def save_match_outcomes(outcomes):
190
+ with open(outcomes_json, 'w') as file:
191
+ json.dump(outcomes, file, indent=4)
192
+
193
+
194
+ def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match):
195
+ outcomes = load_match_outcomes() # Load existing match outcomes
196
+ predictions = load_predictions() # Load existing predictions
197
+ users = load_users() # Load existing user points
198
+
199
+ # Update match outcomes
200
+ match_outcome = next((outcome for outcome in outcomes if outcome['match_id'] == match_id), None)
201
+ if match_outcome:
202
+ match_outcome['winning_team'] = winning_team
203
+ match_outcome['man_of_the_match'] = man_of_the_match
204
+ else:
205
+ outcomes.append({
206
+ "match_id": match_id,
207
+ "winning_team": winning_team,
208
+ "man_of_the_match": man_of_the_match
209
+ })
210
+
211
+ # Update user points based on prediction accuracy
212
+ match_predictions = predictions[predictions['match_id'] == match_id]
213
+ for _, prediction in match_predictions.iterrows():
214
+ user_name = prediction['user_name']
215
+ # Initialize user points if not present
216
+ users[user_name] = users.get(user_name, 1000)
217
+
218
+ if prediction['predicted_winner'] == winning_team:
219
+ users[user_name] += prediction['bid_points'] * 2 # Correct team prediction
220
+ else:
221
+ users[user_name] -= prediction['bid_points'] # Deduct points for incorrect prediction
222
+
223
+ if prediction['predicted_motm'] == man_of_the_match:
224
+ users[user_name] += 100 # Correct man of the match prediction
225
+
226
+ # Save updated outcomes and user points
227
+ save_match_outcomes(outcomes)
228
+ save_users(users)
229
+
230
+
231
+ with st.sidebar.expander("Admin Panel", expanded=False):
232
+ admin_pass = st.text_input("Enter admin passphrase:", type="password")
233
+
234
+ if admin_pass == ADMIN_PASSPHRASE:
235
+ st.success("Authenticated")
236
+ matches = get_today_matches() # this function gets matches for today
237
+
238
+ match_id_selection = st.selectbox("Select Match ID", [match['match_id'] for match in matches])
239
+ winning_team = st.text_input("Winning Team")
240
+ man_of_the_match = st.text_input("Man of the Match")
241
+
242
+ if st.button("Submit Match Outcome"):
243
+ update_leaderboard_and_outcomes(match_id_selection, winning_team, man_of_the_match)
244
+ st.success("Match outcome submitted and leaderboard updated!")
245
+ else:
246
+ st.error("Not authenticated")
match_outcomes.json ADDED
File without changes
matches.json ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "match_id": "20240321_1",
4
+ "date": "2024-03-21",
5
+ "time": "7:30 PM",
6
+ "teams": ["CSK", "RCB"],
7
+ "venue": "Chennai"
8
+ },
9
+ {
10
+ "match_id": "20240322_1",
11
+ "date": "2024-03-22",
12
+ "time": "7:30 PM",
13
+ "teams": ["CSK", "RCB"],
14
+ "venue": "Chennai"
15
+ },
16
+ {
17
+ "match_id": "20240323_2",
18
+ "date": "2024-03-23",
19
+ "time": "3:30 PM",
20
+ "teams": ["PBKS", "DC"],
21
+ "venue": "Mohali"
22
+ },
23
+ {
24
+ "match_id": "20240323_3",
25
+ "date": "2024-03-23",
26
+ "time": "7:30 PM",
27
+ "teams": ["KKR", "SRH"],
28
+ "venue": "Kolkata"
29
+ },
30
+ {
31
+ "match_id": "20240324_4",
32
+ "date": "2024-03-24",
33
+ "time": "3:30 PM",
34
+ "teams": ["RR", "LSG"],
35
+ "venue": "Jaipur"
36
+ },
37
+ {
38
+ "match_id": "20240324_5",
39
+ "date": "2024-03-24",
40
+ "time": "7:30 PM",
41
+ "teams": ["GT", "MI"],
42
+ "venue": "Ahmedabad"
43
+ },
44
+ {
45
+ "match_id": "20240325_6",
46
+ "date": "2024-03-25",
47
+ "time": "7:30 PM",
48
+ "teams": ["RCB", "PBKS"],
49
+ "venue": "Bengaluru"
50
+ },
51
+ {
52
+ "match_id": "20240326_7",
53
+ "date": "2024-03-26",
54
+ "time": "7:30 PM",
55
+ "teams": ["CSK", "GT"],
56
+ "venue": "Chennai"
57
+ },
58
+ {
59
+ "match_id": "20240327_8",
60
+ "date": "2024-03-27",
61
+ "time": "7:30 PM",
62
+ "teams": ["SRH", "MI"],
63
+ "venue": "Hyderabad"
64
+ },
65
+ {
66
+ "match_id": "20240328_9",
67
+ "date": "2024-03-28",
68
+ "time": "7:30 PM",
69
+ "teams": ["RR", "DC"],
70
+ "venue": "Jaipur"
71
+ },
72
+ {
73
+ "match_id": "20240329_10",
74
+ "date": "2024-03-29",
75
+ "time": "7:30 PM",
76
+ "teams": ["RCB", "KKR"],
77
+ "venue": "Bengaluru"
78
+ },
79
+ {
80
+ "match_id": "20240330_11",
81
+ "date": "2024-03-30",
82
+ "time": "7:30 PM",
83
+ "teams": ["LSG", "PBKS"],
84
+ "venue": "Lucknow"
85
+ },
86
+ {
87
+ "match_id": "20240331_12",
88
+ "date": "2024-03-31",
89
+ "time": "3:30 PM",
90
+ "teams": ["GT", "SRH"],
91
+ "venue": "Ahmedabad"
92
+ },
93
+ {
94
+ "match_id": "20240331_13",
95
+ "date": "2024-03-31",
96
+ "time": "7:30 PM",
97
+ "teams": ["DC", "CSK"],
98
+ "venue": "Visakhapatnam"
99
+ },
100
+ {
101
+ "match_id": "20240401_14",
102
+ "date": "2024-04-01",
103
+ "time": "7:30 PM",
104
+ "teams": ["MI", "RR"],
105
+ "venue": "Mumbai"
106
+ },
107
+ {
108
+ "match_id": "20240402_15",
109
+ "date": "2024-04-02",
110
+ "time": "7:30 PM",
111
+ "teams": ["RCB", "LSG"],
112
+ "venue": "Bengaluru"
113
+ },
114
+ {
115
+ "match_id": "20240403_16",
116
+ "date": "2024-04-03",
117
+ "time": "7:30 PM",
118
+ "teams": ["DC", "KKR"],
119
+ "venue": "Visakhapatnam"
120
+ },
121
+ {
122
+ "match_id": "20240404_17",
123
+ "date": "2024-04-04",
124
+ "time": "7:30 PM",
125
+ "teams": ["GT", "PBKS"],
126
+ "venue": "Ahmedabad"
127
+ },
128
+ {
129
+ "match_id": "20240405_18",
130
+ "date": "2024-04-05",
131
+ "time": "7:30 PM",
132
+ "teams": ["SRH", "CSK"],
133
+ "venue": "Hyderabad"
134
+ },
135
+ {
136
+ "match_id": "20240406_19",
137
+ "date": "2024-04-06",
138
+ "time": "7:30 PM",
139
+ "teams": ["RR", "RCB"],
140
+ "venue": "Jaipur"
141
+ },
142
+ {
143
+ "match_id": "20240407_20",
144
+ "date": "2024-04-07",
145
+ "time": "3:30 PM",
146
+ "teams": ["MI", "DC"],
147
+ "venue": "Mumbai"
148
+ },
149
+ {
150
+ "match_id": "20240407_21",
151
+ "date": "2024-04-07",
152
+ "time": "7:30 PM",
153
+ "teams": ["LSG", "GT"],
154
+ "venue": "Lucknow"
155
+ }
156
+ ]
predictions.csv ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ user_name,match_id,predicted_winner,predicted_motm,bid_points
2
+ Jay,20240321_1,CSK,Dhoni,105
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ pandas
2
+ pytz
3
+ streamlit
users.json ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Archana": 1000,
3
+ "Arpit": 1000,
4
+ "Ganesh": 1000,
5
+ "Haaris": 1000,
6
+ "Harshit": 1000,
7
+ "Jay": 1000,
8
+ "Kichu": 1000,
9
+ "Megha": 1000,
10
+ "Naveein": 1000,
11
+ "Neha": 1000,
12
+ "Praveen": 1000,
13
+ "Rakesh": 1000,
14
+ "Sunil": 1000,
15
+ "Vaibhav": 1000,
16
+ "Vinay": 1000
17
+ }