From 1cf79ce861fb713a02b1f5f87d87b06abecd3f17 Mon Sep 17 00:00:00 2001 From: Sakimori Date: Thu, 31 Dec 2020 03:32:01 -0500 Subject: [PATCH] tidyed up some half-inning and game end weirdness, fixed end_delay, added game end pings and summary embeds in discord --- games.py | 1 + main_controller.py | 22 ++++++++++++++------ static/loader.js | 2 +- the_prestige.py | 50 +++++++++++++++++++++++++++++++++++++++++----- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/games.py b/games.py index c692aa2..805e4c4 100644 --- a/games.py +++ b/games.py @@ -149,6 +149,7 @@ class game(object): self.last_update = ({},0) #this is a ({outcome}, runs) tuple self.owner = None self.ready = False + self.victory_lap = False if length is not None: self.max_innings = length else: diff --git a/main_controller.py b/main_controller.py index fa60d28..0d92549 100644 --- a/main_controller.py +++ b/main_controller.py @@ -43,11 +43,11 @@ def update_loop(): if test_string == "Game not started.": #weather_emoji state["update_emoji"] = "🍿" #weather_text state["update_text"] = "Play blall!" #they also need a timestamp - state["delay"] -= 1 + state["start_delay"] -= 1 state["display_top_of_inning"] = state["top_of_inning"] - if state["delay"] <= 0: + if state["start_delay"] <= 0: if this_game.top_of_inning != state["top_of_inning"]: state["update_pause"] = 2 state["pitcher"] = "-" @@ -59,8 +59,15 @@ def update_loop(): if state["update_pause"] == 1: state["update_emoji"] = "🍿" if this_game.over: + state["display_inning"] -= 1 + state["display_top_of_inning"] = False winning_team = this_game.teams['home'].name if this_game.teams['home'].score > this_game.teams['away'].score else this_game.teams['away'].name - state["update_text"] = f"{winning_team} wins{' with a victory lap' if state['victory_lap'] else ''}!" + if this_game.victory_lap and winning_team == this_game.teams['home'].name: + state["update_text"] = f"{winning_team} wins with a victory lap!" + elif winning_team == this_game.teams['home'].name: + state["update_text"] = f"{winning_team} wins, shaming {this_game.teams['away'].name}!" + else: + state["update_text"] = f"{winning_team} wins!" state["pitcher"] = "-" state["batter"] = "-" elif this_game.top_of_inning: @@ -68,7 +75,7 @@ def update_loop(): else: if this_game.inning >= this_game.max_innings: if this_game.teams["home"].score > this_game.teams["away"].score: - state["victory_lap"] = True + this_game.victory_lap = True state["update_text"] = f"Bottom of {this_game.inning}. {this_game.teams['home'].name} batting!" elif state["update_pause"] != 1 and test_string != "Game not started.": @@ -103,9 +110,12 @@ def update_loop(): states_to_send[game_time] = state - if state["update_pause"] <= 1 and state["delay"] < 0: + if state["update_pause"] <= 1 and state["start_delay"] < 0: if this_game.over: - master_games_dic.pop(game_time) + state["update_pause"] = 2 + if state["end_delay"] < 0: + master_games_dic.pop(game_time) + state["end_delay"] -= 1 else: this_game.gamestate_update_full() diff --git a/static/loader.js b/static/loader.js index 738fa9b..6a26137 100644 --- a/static/loader.js +++ b/static/loader.js @@ -103,7 +103,7 @@ $(document).ready(function (){ $('#updateTarget .update_emoji').html(gamestate.update_emoji); $('#updateTarget .update_text').html(gamestate.update_text); - $('#updateTarget .batting').html((gamestate.top_of_inning ? gamestate.away_name : gamestate.home_name) + " batting."); + $('#updateTarget .batting').html((gamestate.display_top_of_inning ? gamestate.away_name : gamestate.home_name) + " batting."); gamediv.id = ""; }; diff --git a/the_prestige.py b/the_prestige.py index c4c95a9..c7da63f 100644 --- a/the_prestige.py +++ b/the_prestige.py @@ -355,6 +355,7 @@ def config(): 0000 ], "prefix" : ["m;", "m!"], + "simmadome_url" : "", "soulscream channel id" : 0, "game_freeze" : 0 } @@ -370,6 +371,8 @@ def config(): async def on_ready(): db.initialcheck() print(f"logged in as {client.user} with token {config()['token']}") + watch_task = asyncio.create_task(game_watcher()) + await watch_task @client.event async def on_reaction_add(reaction, user): @@ -557,9 +560,8 @@ async def watch_game(channel, newgame, user = None): out_emoji = discord.utils.get(client.emojis, id = 791578957241778226) in_emoji = discord.utils.get(client.emojis, id = 791578957244792832) - if user is not None: - await channel.send(f"Game for {user.mention}:") - embed = await channel.send("Starting...") + + await asyncio.sleep(1) weathers = games.all_weathers() newgame.weather = weathers[random.choice(list(weathers.keys()))] @@ -572,9 +574,13 @@ async def watch_game(channel, newgame, user = None): "victory_lap" : False, "weather_emoji" : newgame.weather.emoji, "weather_text" : newgame.weather.name, - "delay" : -1 + "start_delay" : 3, + "end_delay" : 3 } + await channel.send(f"{newgame.teams['away'].name} vs. {newgame.teams['home'].name}, starting at {config()['simmadome_url']}") + gamesarray.append((newgame, channel, user)) + main_controller.master_games_dic[str(time.time() * 1000.0)] = (newgame, state_init) #while not newgame.over or newgame.top_of_inning != top_of_inning: @@ -869,6 +875,40 @@ async def team_pages(msg, all_teams, search_term=None): except asyncio.TimeoutError: return +async def game_watcher(): + while True: + this_array = gamesarray.copy() + for i in range(0,len(this_array)): + game, channel, user = this_array[i] + if game.over: + title_string = f"{game.teams['away'].name} at {game.teams['home'].name} ended after {game.inning-1} innings" + if (game.inning - 1) > game.max_innings: #if extra innings + title_string += f" with {game.inning - (game.max_innings+1)} extra innings." + else: + title_string += "." + + winning_team = game.teams['home'].name if game.teams['home'].score > game.teams['away'].score else game.teams['away'].name + winstring = f"{game.teams['away'].score} to {game.teams['home'].score}\n" + if game.victory_lap and winning_team == game.teams['home'].name: + winstring += f"{winning_team} wins with a victory lap!" + elif winning_team == game.teams['home'].name: + winstring += f"{winning_team} wins, shaming {game.teams['away'].name}!" + else: + winstring += f"{winning_team} wins!" + + if user is not None: + await channel.send(f"{user.mention}'s game just ended.") + else: + await channel.send("A game started from this channel just ended.") + + final_embed = discord.Embed(color=discord.Color.dark_purple(), title=title_string) + final_embed.add_field(name="Final score:", value=winstring) + await channel.send(embed=final_embed) + gamesarray.pop(i) + break + + await asyncio.sleep(6) -client.run(config()["token"]) + +client.run(config()["token"]) \ No newline at end of file