We'll be fine with one database connection, and don't have to repeat ourselves
This commit is contained in:
parent
49d3672936
commit
7101db5394
@ -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 <number>")
|
||||
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user