coollsd commited on
Commit
21b7479
β€’
1 Parent(s): 6ac0994

Update sportbet.py

Browse files
Files changed (1) hide show
  1. sportbet.py +64 -57
sportbet.py CHANGED
@@ -176,70 +176,77 @@ class BetModal(discord.ui.Modal, title="Place Your Bet"):
176
  except ValueError as e:
177
  await interaction.response.send_message(str(e), ephemeral=False)
178
 
179
- async def monitor_game(self, interaction, bet_amount):
180
- game_start = datetime.fromisoformat(self.game_data['eventTime'].replace('Z', '+00:00'))
181
- event_id = self.game_data['id']
182
- fetch_scores = fetch_nhl_scores if self.league == "NHL" else fetch_nfl_scores
183
-
184
- sleep_duration = (game_start - datetime.now(timezone.utc)).total_seconds()
185
- if sleep_duration > 0:
186
- await asyncio.sleep(sleep_duration)
187
-
188
- user = await interaction.client.fetch_user(self.user_id)
189
- await user.send(f"Your team **{self.team}** has started playing!")
190
-
191
- previous_scores = {
192
- self.game_data['lowerTeam']['name']: self.game_data.get('lowerTeam', {}).get('score', 0),
193
- self.game_data['upperTeam']['name']: self.game_data.get('upperTeam', {}).get('score', 0)
194
- }
195
-
196
- while True:
197
- scores = await fetch_scores()
198
- game = None
199
- for section in scores.get('sectionList', []):
200
- for evt in section.get('events', []):
201
- if evt['id'] == event_id:
202
- game = evt
203
- break
204
- if game:
205
  break
206
- if not game:
207
- await asyncio.sleep(60)
208
- continue
209
-
210
- event_status = game.get('eventStatus', 2)
211
- current_scores = {
212
- game['lowerTeam']['name']: game['lowerTeam'].get('score', 0),
213
- game['upperTeam']['name']: game['upperTeam'].get('score', 0)
214
- }
215
- is_final = event_status == 3
216
-
217
- # Check for score updates
218
- for team, score in current_scores.items():
219
- if score > previous_scores.get(team, 0):
220
- team_name = self.game_data['lowerTeam']['longName'] if team == self.game_data['lowerTeam']['name'] else self.game_data['upperTeam']['longName']
221
- message = f"**{team_name}** SCORED! Current score: {current_scores[self.game_data['lowerTeam']['name']]} - {current_scores[self.game_data['upperTeam']['name']]}"
222
- await user.send(message)
223
-
224
- previous_scores = current_scores.copy()
225
-
226
- if is_final:
 
 
 
 
 
 
 
 
 
227
  away_score = current_scores.get(self.game_data['lowerTeam']['name'], 0)
228
  home_score = current_scores.get(self.game_data['upperTeam']['name'], 0)
229
  winner = self.game_data['lowerTeam']['name'] if away_score > home_score else self.game_data['upperTeam']['name']
230
 
231
- if winner == self.team:
232
- winnings = bet_amount * 2
233
- user_cash[self.user_id] += winnings
234
- await user.send(f"πŸŽ‰ **Congratulations!** Your team **{self.team}** won! You won **${winnings}**!")
235
- else:
236
- await user.send(f"😞 **Sorry!** Your team **{self.team}** lost. Better luck next time!")
237
 
238
- # Remove the completed bet
239
- user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if not (bet['league'] == self.league and bet['team'] == self.team and bet['game_data']['id'] == event_id)]
240
- break
241
 
242
- await asyncio.sleep(60) # Check every minute
243
 
244
  class SportBetView(discord.ui.View):
245
  def __init__(self):
 
176
  except ValueError as e:
177
  await interaction.response.send_message(str(e), ephemeral=False)
178
 
179
+ async def monitor_game(self, interaction, bet_amount):
180
+ game_start = datetime.fromisoformat(self.game_data['eventTime'].replace('Z', '+00:00'))
181
+ event_id = self.game_data['id']
182
+ fetch_scores = fetch_nhl_scores if self.league == "NHL" else fetch_nfl_scores
183
+
184
+ sleep_duration = (game_start - datetime.now(timezone.utc)).total_seconds()
185
+ if sleep_duration > 0:
186
+ await asyncio.sleep(sleep_duration)
187
+
188
+ user = await interaction.client.fetch_user(self.user_id)
189
+ await user.send(f"Your team **{self.team}** has started playing!")
190
+
191
+ previous_scores = {
192
+ self.game_data['lowerTeam']['name']: self.game_data.get('lowerTeam', {}).get('score', 0),
193
+ self.game_data['upperTeam']['name']: self.game_data.get('upperTeam', {}).get('score', 0)
194
+ }
195
+
196
+ while True:
197
+ scores = await fetch_scores()
198
+ game = None
199
+ for section in scores.get('sectionList', []):
200
+ for evt in section.get('events', []):
201
+ if evt['id'] == event_id:
202
+ game = evt
 
 
203
  break
204
+ if game:
205
+ break
206
+ if not game:
207
+ await asyncio.sleep(60)
208
+ continue
209
+
210
+ event_status = game.get('eventStatus', 2)
211
+ current_scores = {
212
+ game['lowerTeam']['name']: game['lowerTeam'].get('score', 0),
213
+ game['upperTeam']['name']: game['upperTeam'].get('score', 0)
214
+ }
215
+ is_final = event_status == 3 or game['lowerTeam'].get('isLoser') is not None or game['upperTeam'].get('isLoser') is not None
216
+
217
+ # Check for score updates
218
+ for team, score in current_scores.items():
219
+ if score > previous_scores.get(team, 0):
220
+ team_name = self.game_data['lowerTeam']['longName'] if team == self.game_data['lowerTeam']['name'] else self.game_data['upperTeam']['longName']
221
+ message = f"**{team_name}** SCORED! Current score: {current_scores[self.game_data['lowerTeam']['name']]} - {current_scores[self.game_data['upperTeam']['name']]}"
222
+ await user.send(message)
223
+
224
+ previous_scores = current_scores.copy()
225
+
226
+ if is_final:
227
+ winner = None
228
+ if game['lowerTeam'].get('isLoser') == True:
229
+ winner = game['upperTeam']['name']
230
+ elif game['upperTeam'].get('isLoser') == True:
231
+ winner = game['lowerTeam']['name']
232
+ else:
233
+ # Fallback to score comparison if isLoser is not set
234
  away_score = current_scores.get(self.game_data['lowerTeam']['name'], 0)
235
  home_score = current_scores.get(self.game_data['upperTeam']['name'], 0)
236
  winner = self.game_data['lowerTeam']['name'] if away_score > home_score else self.game_data['upperTeam']['name']
237
 
238
+ if winner == self.team:
239
+ winnings = bet_amount * 2
240
+ user_cash[self.user_id] += winnings
241
+ await user.send(f"πŸŽ‰ **Congratulations!** Your team **{self.team}** won! You won **${winnings}**!")
242
+ else:
243
+ await user.send(f"😞 **Sorry!** Your team **{self.team}** lost. Better luck next time!")
244
 
245
+ # Remove the completed bet
246
+ user_bets[self.user_id] = [bet for bet in user_bets[self.user_id] if not (bet['league'] == self.league and bet['team'] == self.team and bet['game_data']['id'] == event_id)]
247
+ break
248
 
249
+ await asyncio.sleep(60) # Check every minute
250
 
251
  class SportBetView(discord.ui.View):
252
  def __init__(self):