From 7101db5394298a566ba5ef453b8a54dfabccf3c2 Mon Sep 17 00:00:00 2001 From: c0de Date: Mon, 24 Oct 2022 19:13:34 -0500 Subject: [PATCH] We'll be fine with one database connection, and don't have to repeat ourselves --- GhostBallBot/game.py | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/GhostBallBot/game.py b/GhostBallBot/game.py index 896ae3d..dcc7c70 100644 --- a/GhostBallBot/game.py +++ b/GhostBallBot/game.py @@ -35,11 +35,19 @@ class Game: # Discord client instance self.discord = None + database.connect() + + def __exit__(self, exc_type, exc_val, exc_tb): + """ + Automagically close the database + when this class has ended execution + """ + database.close() + async def start(self): if self.is_running: return await self.message.channel.send("A game is already running") - database.connect() self.is_running = True # game.pitch_value is unknown at the start of the game @@ -48,8 +56,6 @@ class Game: server_id = self.message.guild.id ) - database.close() - await self.message.send("@flappy ball, pitch is in! Send me your guesses with !guess ") def __stopArgs__(self): @@ -71,8 +77,6 @@ class Game: if not pitch_value: return await self.message.channel.send(f"Invalid command <@{ str(self.message.author.id) }>!") - database.connect() - if has_batter: player_id = batter_id[3:] GuessModel.create( @@ -92,7 +96,6 @@ class Game: # stop and discard game self.is_running = False self.game = None - database.close() async def guess(self): if not self.is_running: @@ -102,8 +105,6 @@ class Game: if value < 1 or value > 1000: return await self.message.channel.send(f"Invalid value. It must be between 1 and 1000 inclusive") - database.connect() - GuessModel.create( game_id = self.game.game_id, player_id = self.message.author.id, @@ -111,13 +112,10 @@ class Game: guess = value ) - database.close() - return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}") async def points(self): - # database.connect() - + # TODO value = self.message.content.split() try: if len(value) > 1: @@ -125,8 +123,6 @@ class Game: except: return await self.message.channel.send("Invalid timestamp. Try again") - # database.close() - return await self.message.channel.send("Sorry, not implemented yet") async def help(self):