From 3435e4ce7a4dcb292bd63ee71043c066712615eb Mon Sep 17 00:00:00 2001 From: c0de Date: Wed, 9 Nov 2022 11:48:31 -0600 Subject: [PATCH] Fix to the point of being runnable --- GhostBallBot/database/models.py | 4 ++-- GhostBallBot/game/game.py | 25 ++++++++++++++----------- GhostBallBot/game/guess.py | 9 +++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/GhostBallBot/database/models.py b/GhostBallBot/database/models.py index 390e693..e2a483e 100644 --- a/GhostBallBot/database/models.py +++ b/GhostBallBot/database/models.py @@ -69,7 +69,7 @@ class GuessModel(BaseModel): guess_id = UUIDField(primary_key=True) player = ForeignKeyField(PlayerModel, backref="guesses") - game_id = ForeignKeyField(GameModel, backref="guesses") + game = ForeignKeyField(GameModel, backref="guesses") guess = IntegerField(default=0) difference = IntegerField(null=True) @@ -80,4 +80,4 @@ def create_models(): """Create database tables""" with database: - database.create_tables([GameModel, GuessModel, PlayerModel()]) + database.create_tables([GameModel, GuessModel, PlayerModel]) diff --git a/GhostBallBot/game/game.py b/GhostBallBot/game/game.py index 4ca7294..d3436d7 100644 --- a/GhostBallBot/game/game.py +++ b/GhostBallBot/game/game.py @@ -152,9 +152,10 @@ class GameManager: # How many valid guesses got placed? guess_count = ( - Guess.select().where((Guess.game.game_id == self.game.game_id) - & (Guess.guess > 0) - ).count()) + Guess.select().join(Game) + .where((Guess.game.game_id == self.game.game_id) & (Guess.guess > 0)) + .count() + ) # Discard the game if there weren't enough players if guess_count < 3: @@ -170,9 +171,13 @@ class GameManager: ) pitch_value = await self.update_pitch_value() - guess_processor = ProcessGuess(game=self, pitch_value=pitch_value) + guess_processor = ProcessGuess(game=self, pitch_value=pitch_value, message=message) - message, closest_player_id, furthest_player_id = guess_processor.process_guesses() + ( + message, + closest_player_id, + furthest_player_id, + ) = guess_processor.process_guesses() message += ( f"\nCongrats <@{closest_player_id}>! You were the closest!\n" @@ -205,15 +210,13 @@ class GameManager: # Create the guess (or allow us to say update successful) _, created = Guess.get_or_create( - guess_id=uuid.uuid4(), - game_id=self.game.game_id, - player_id=player.player_id, - player_name=player.player_name, + guess_id=uuid.uuid4(), game_id=self.game.game_id, player_id=player.player_id ) Guess.update({"guess": value}).where( - (Guess.game_id == self.game.game_id) - & (Guess.player_id == self.message.author.id) + # (Guess.game_id == self.game.game_id) + # & + (Guess.player_id == self.message.author.id) ).execute() if created: diff --git a/GhostBallBot/game/guess.py b/GhostBallBot/game/guess.py index b34977b..1d8195d 100644 --- a/GhostBallBot/game/guess.py +++ b/GhostBallBot/game/guess.py @@ -7,6 +7,7 @@ from database.models import ( PlayerModel as Player, ) + class ProcessGuess: def __init__(self, game, **kwargs): self.game_manager = game @@ -31,7 +32,7 @@ class ProcessGuess: ) .join(Player) .where( - (Guess.game_id == self.game_manager.game.game_id) + (Guess.game == self.game_manager.game.game_id) & (Guess.guess > 0) & (Guess.player.player_id == Player.player_id) ) @@ -42,7 +43,7 @@ class ProcessGuess: def update_difference_value(self): # Update player's difference in guessed value Guess.update({"difference": self.difference}).where( - (Guess.game_id == self.game_manager.game.game_id) + (Guess.game.game_id == self.game_manager.game.game_id) & (Guess.player.player_id == self.guess.player.player_id) & (Guess.guess_id == self.guess.guess_id) ).execute() @@ -60,7 +61,7 @@ class ProcessGuess: def get_difference(self, guess=None): """Difference calculation, includes "loop over" effect""" if not guess: - guess = self.guess + guess = self.guess.guess difference = abs(guess - self.pitch_value) @@ -80,7 +81,7 @@ class ProcessGuess: def get_winner_loser(self): # Determine which guesses are closest and furthest from the pitch_value - guess_values = [record.guess for record in self.guesses] + guess_values = [record.guess for record in self.get_guesses()] # Closest to the pitch_value winner = min(guess_values, key=lambda guess: self.get_difference(guess)) # Furthest from the pitch_value