jarajpu commited on
Commit
70680b5
·
1 Parent(s): 377f8ae

Adding 20% cap & points calculations

Browse files
Files changed (5) hide show
  1. README.md +5 -5
  2. app.py +115 -40
  3. matches.json +1 -8
  4. predictions.csv +1 -2
  5. users.json +16 -15
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: DIS IPL
3
  emoji: 🦀
4
  colorFrom: red
5
  colorTo: pink
@@ -12,7 +12,7 @@ license: apache-2.0
12
 
13
  # DIS IPL Match Predictions App
14
 
15
- Welcome to the IPL Match Predictions App! This app allows you to predict the outcomes of IPL matches and compete with your colleagues to win exciting prizes.
16
 
17
  ## Getting Started
18
 
@@ -32,13 +32,13 @@ Welcome to the IPL Match Predictions App! This app allows you to predict the out
32
 
33
  2. Navigate to the cloned repository directory:
34
 
35
- ```
36
  cd ipl-match-predictions-app
37
  ```
38
 
39
  3. Install the required Python dependencies using Pip:
40
 
41
- ```
42
  pip install -r requirements.txt
43
  ```
44
 
@@ -46,7 +46,7 @@ Welcome to the IPL Match Predictions App! This app allows you to predict the out
46
 
47
  1. After installing the dependencies, you can run the Streamlit app using the following command:
48
 
49
- ```
50
  streamlit run app.py
51
  ```
52
 
 
1
  ---
2
+ title: DIS IPL Predictions
3
  emoji: 🦀
4
  colorFrom: red
5
  colorTo: pink
 
12
 
13
  # DIS IPL Match Predictions App
14
 
15
+ Welcome to the DIS IPL Match Predictions App! This app allows you to predict the outcomes of IPL matches and compete with your colleagues to win exciting prizes.
16
 
17
  ## Getting Started
18
 
 
32
 
33
  2. Navigate to the cloned repository directory:
34
 
35
+ ```bash
36
  cd ipl-match-predictions-app
37
  ```
38
 
39
  3. Install the required Python dependencies using Pip:
40
 
41
+ ```bash
42
  pip install -r requirements.txt
43
  ```
44
 
 
46
 
47
  1. After installing the dependencies, you can run the Streamlit app using the following command:
48
 
49
+ ```bash
50
  streamlit run app.py
51
  ```
52
 
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import json
2
- from datetime import datetime
3
 
4
  import pandas as pd
5
  import pytz
@@ -64,8 +64,53 @@ def get_today_matches():
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)
@@ -77,10 +122,7 @@ def submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid
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.")
@@ -100,6 +142,19 @@ def submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid
100
  st.success("Prediction submitted successfully!")
101
 
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  # Streamlit UI
104
  st.title("DIS IPL Match Predictions")
105
 
@@ -109,6 +164,14 @@ with st.expander("Submit Prediction"):
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:
@@ -122,11 +185,12 @@ with st.expander("Submit Prediction"):
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
@@ -136,7 +200,7 @@ with st.expander("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
 
@@ -158,7 +222,10 @@ with st.expander("Leaderboard"):
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
 
@@ -192,40 +259,48 @@ def save_match_outcomes(outcomes):
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):
 
1
  import json
2
+ from datetime import datetime, timedelta
3
 
4
  import pandas as pd
5
  import pytz
 
64
  return today_matches
65
 
66
 
67
+ # Function to check if prediction submission is allowed
68
+ def is_submission_allowed(match_id):
69
+ matches = load_matches() # This loads matches correctly with IST times
70
+
71
+ for match in matches:
72
+ if match["match_id"] == match_id:
73
+ # Parse the match start time in IST
74
+ tz_IST = pytz.timezone('Asia/Kolkata')
75
+ match_datetime_str = f'{match["date"]} {match["time"]}'
76
+ # The match time string is like "2024-03-21 7:30 PM"
77
+ match_datetime = datetime.strptime(match_datetime_str, "%Y-%m-%d %I:%M %p")
78
+ match_datetime = tz_IST.localize(match_datetime) # Set the timezone to IST
79
+
80
+ # Get the current time in IST
81
+ current_datetime = datetime.now(tz_IST)
82
+
83
+ if current_datetime > match_datetime + timedelta(minutes=30):
84
+ return False
85
+ else:
86
+ return True
87
+ return False # If match_id not found, default to False
88
+
89
+
90
  # Submit prediction function
91
+ def submit_prediction(
92
+ user_name,
93
+ match_id,
94
+ predicted_winner,
95
+ predicted_motm,
96
+ bid_points,
97
+ max_bid_points
98
+ ):
99
+
100
+ # Validation for user selection
101
+ if user_name == "Select a user...":
102
+ st.warning("Please select a valid user.")
103
+ return
104
+
105
+ # Check if prediction submission is allowed for the match
106
+ if not is_submission_allowed(match_id):
107
+ st.error("Prediction submission time has passed. Predictions can't be submitted after match start.")
108
+ return
109
+
110
+ if bid_points > max_bid_points:
111
+ st.error(f"Your bid points exceed the 20% limit of your total points. Maximum allowed bid points: {max_bid_points}")
112
+ return
113
+
114
  # Ensure predictions DataFrame is loaded or initialized correctly
115
  try:
116
  predictions = pd.read_csv(predictions_csv)
 
122
  predictions = pd.DataFrame(columns=expected_columns)
123
 
124
  # Check for duplicate prediction for the same match by the same user
125
+ if user_name != "Select a user...":
 
 
 
126
  existing_predictions = predictions[(predictions['user_name'] == user_name) & (predictions['match_id'] == match_id)]
127
  if not existing_predictions.empty:
128
  st.error("You've already submitted a prediction for this match.")
 
142
  st.success("Prediction submitted successfully!")
143
 
144
 
145
+ def get_user_total_points(user_name):
146
+ with open(users_json, 'r') as file:
147
+ users = json.load(file)
148
+ return users.get(user_name, 0)
149
+
150
+
151
+ # Define the new function
152
+ def calculate_max_bid_points(user_name):
153
+ total_points = get_user_total_points(user_name)
154
+ max_bid_points = int(total_points * 0.20) # 20% of total points
155
+ return max_bid_points
156
+
157
+
158
  # Streamlit UI
159
  st.title("DIS IPL Match Predictions")
160
 
 
164
  # User selection
165
  user_name = st.selectbox("Select User", ["Select a user..."] + get_users())
166
 
167
+ # Initialize max_bid_points to None
168
+ max_bid_points = None
169
+
170
+ if user_name != "Select a user...":
171
+ max_bid_points = calculate_max_bid_points(user_name)
172
+ # Display the max bid points
173
+ st.write(f"Maximum bid points you can submit: {max_bid_points}")
174
+
175
  # Match selection
176
  matches = get_today_matches()
177
  if matches:
 
185
  # Predictions
186
  predicted_winner = st.selectbox("Predicted Winner", teams)
187
  predicted_motm = st.text_input("Predicted Man of the Match")
188
+ bid_points = st.number_input("Bid Points", min_value=1, value=100, format="%d")
189
 
190
  # Submit button
191
  if st.button("Submit Prediction"):
192
+ if user_name != "Select a user...":
193
+ submit_prediction(user_name, match_id, predicted_winner, predicted_motm, bid_points, max_bid_points)
194
 
195
 
196
  # Show predictions
 
200
  if st.button("Show Predictions"):
201
  try:
202
  df = pd.read_csv(predictions_csv)
203
+ st.dataframe(df, hide_index=True)
204
  except FileNotFoundError:
205
  st.write("No predictions have been submitted yet.")
206
 
 
222
  # Reorder DataFrame columns so 'Rank' is first
223
  df_leaderboard = df_leaderboard[['Rank', 'User', 'Points']]
224
 
225
+ # Reset index to remove the default index column
226
+ df_leaderboard.reset_index(drop=True, inplace=True)
227
+
228
+ st.dataframe(df_leaderboard, hide_index=True)
229
  except FileNotFoundError:
230
  st.write("Leaderboard data not available.")
231
 
 
259
 
260
 
261
  def update_leaderboard_and_outcomes(match_id, winning_team, man_of_the_match):
262
+ outcomes = load_match_outcomes() # Load existing match outcomes
263
+ predictions = load_predictions() # Load existing predictions
264
+ users = load_users() # Load existing user points
265
+
266
+ # Update match outcomes
267
+ match_outcome = next((outcome for outcome in outcomes if outcome['match_id'] == match_id), None)
268
+ if match_outcome:
269
+ match_outcome['winning_team'] = winning_team
270
+ match_outcome['man_of_the_match'] = man_of_the_match
271
+ else:
272
+ outcomes.append({
273
+ "match_id": match_id,
274
+ "winning_team": winning_team,
275
+ "man_of_the_match": man_of_the_match
276
+ })
277
+
278
+ # Update user points based on prediction accuracy
279
+ match_predictions = predictions[predictions['match_id'] == match_id]
280
+ for _, prediction in match_predictions.iterrows():
281
+ user_name = prediction['user_name']
282
+ # Initialize user points if not present
283
+ users[user_name] = users.get(user_name, 0)
284
+
285
+ # Points for correct or incorrect team prediction
286
+ if prediction['predicted_winner'] == winning_team:
287
+ users[user_name] += 1000 # Correct team prediction
288
+ # Check for double or nothing bid points
289
+ users[user_name] += prediction['bid_points'] * 2
290
  else:
291
+ users[user_name] -= 200 # Deduct points for incorrect team prediction
292
+ users[user_name] -= prediction['bid_points'] # Lose bid points for incorrect prediction
 
 
 
 
 
 
 
 
 
 
293
 
294
+ # Points for correct man of the match prediction
295
+ if prediction['predicted_motm'] == man_of_the_match:
296
+ users[user_name] += 200 # Correct man of the match prediction
297
+ # Bonus for getting both right
298
  if prediction['predicted_winner'] == winning_team:
299
+ users[user_name] += 200
 
 
 
 
 
300
 
301
+ # Save updated outcomes and user points
302
+ save_match_outcomes(outcomes)
303
+ save_users(users)
304
 
305
 
306
  with st.sidebar.expander("Admin Panel", expanded=False):
matches.json CHANGED
@@ -1,13 +1,6 @@
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"],
 
1
  [
2
  {
3
+ "match_id": "20240322_2",
 
 
 
 
 
 
 
4
  "date": "2024-03-22",
5
  "time": "7:30 PM",
6
  "teams": ["CSK", "RCB"],
predictions.csv CHANGED
@@ -1,2 +1 @@
1
- user_name,match_id,predicted_winner,predicted_motm,bid_points
2
- Jay,20240321_1,CSK,Dhoni,105
 
1
+ user_name,match_id,predicted_winner,predicted_motm,bid_points
 
users.json CHANGED
@@ -1,17 +1,18 @@
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
  }
 
1
  {
2
+ "Archana": 10000,
3
+ "Arpit": 10000,
4
+ "Ganesh": 10000,
5
+ "Haaris": 10000,
6
+ "Jay": 10000,
7
+ "Kichu": 10000,
8
+ "Megha": 10000,
9
+ "Naveein": 10000,
10
+ "Neha": 10000,
11
+ "Praveen": 10000,
12
+ "Rakesh": 10000,
13
+ "Sai": 10000,
14
+ "Sahil": 10000,
15
+ "Sunil": 10000,
16
+ "Vaibhav": 10000,
17
+ "Vinay": 10000
18
  }