We'll be fine with one database connection, and don't have to repeat ourselves

This commit is contained in:
c0de 2022-10-24 19:13:34 -05:00
parent 49d3672936
commit 7101db5394

View File

@ -35,11 +35,19 @@ class Game:
# Discord client instance # Discord client instance
self.discord = None 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): async def start(self):
if self.is_running: if self.is_running:
return await self.message.channel.send("A game is already running") return await self.message.channel.send("A game is already running")
database.connect()
self.is_running = True self.is_running = True
# game.pitch_value is unknown at the start of the game # game.pitch_value is unknown at the start of the game
@ -48,8 +56,6 @@ class Game:
server_id = self.message.guild.id server_id = self.message.guild.id
) )
database.close()
await self.message.send("@flappy ball, pitch is in! Send me your guesses with !guess <number>") await self.message.send("@flappy ball, pitch is in! Send me your guesses with !guess <number>")
def __stopArgs__(self): def __stopArgs__(self):
@ -71,8 +77,6 @@ class Game:
if not pitch_value: if not pitch_value:
return await self.message.channel.send(f"Invalid command <@{ str(self.message.author.id) }>!") return await self.message.channel.send(f"Invalid command <@{ str(self.message.author.id) }>!")
database.connect()
if has_batter: if has_batter:
player_id = batter_id[3:] player_id = batter_id[3:]
GuessModel.create( GuessModel.create(
@ -92,7 +96,6 @@ class Game:
# stop and discard game # stop and discard game
self.is_running = False self.is_running = False
self.game = None self.game = None
database.close()
async def guess(self): async def guess(self):
if not self.is_running: if not self.is_running:
@ -102,8 +105,6 @@ class Game:
if value < 1 or value > 1000: if value < 1 or value > 1000:
return await self.message.channel.send(f"Invalid value. It must be between 1 and 1000 inclusive") return await self.message.channel.send(f"Invalid value. It must be between 1 and 1000 inclusive")
database.connect()
GuessModel.create( GuessModel.create(
game_id = self.game.game_id, game_id = self.game.game_id,
player_id = self.message.author.id, player_id = self.message.author.id,
@ -111,13 +112,10 @@ class Game:
guess = value guess = value
) )
database.close()
return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}") return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}")
async def points(self): async def points(self):
# database.connect() # TODO
value = self.message.content.split() value = self.message.content.split()
try: try:
if len(value) > 1: if len(value) > 1:
@ -125,8 +123,6 @@ class Game:
except: except:
return await self.message.channel.send("Invalid timestamp. Try again") return await self.message.channel.send("Invalid timestamp. Try again")
# database.close()
return await self.message.channel.send("Sorry, not implemented yet") return await self.message.channel.send("Sorry, not implemented yet")
async def help(self): async def help(self):