Fix to the point of being runnable

This commit is contained in:
c0de 2022-11-09 11:48:31 -06:00
parent eb5ed5fe9e
commit 3435e4ce7a
3 changed files with 21 additions and 17 deletions

View File

@ -69,7 +69,7 @@ class GuessModel(BaseModel):
guess_id = UUIDField(primary_key=True) guess_id = UUIDField(primary_key=True)
player = ForeignKeyField(PlayerModel, backref="guesses") player = ForeignKeyField(PlayerModel, backref="guesses")
game_id = ForeignKeyField(GameModel, backref="guesses") game = ForeignKeyField(GameModel, backref="guesses")
guess = IntegerField(default=0) guess = IntegerField(default=0)
difference = IntegerField(null=True) difference = IntegerField(null=True)
@ -80,4 +80,4 @@ def create_models():
"""Create database tables""" """Create database tables"""
with database: with database:
database.create_tables([GameModel, GuessModel, PlayerModel()]) database.create_tables([GameModel, GuessModel, PlayerModel])

View File

@ -152,9 +152,10 @@ class GameManager:
# How many valid guesses got placed? # How many valid guesses got placed?
guess_count = ( guess_count = (
Guess.select().where((Guess.game.game_id == self.game.game_id) Guess.select().join(Game)
& (Guess.guess > 0) .where((Guess.game.game_id == self.game.game_id) & (Guess.guess > 0))
).count()) .count()
)
# Discard the game if there weren't enough players # Discard the game if there weren't enough players
if guess_count < 3: if guess_count < 3:
@ -170,9 +171,13 @@ class GameManager:
) )
pitch_value = await self.update_pitch_value() 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 += ( message += (
f"\nCongrats <@{closest_player_id}>! You were the closest!\n" 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) # Create the guess (or allow us to say update successful)
_, created = Guess.get_or_create( _, created = Guess.get_or_create(
guess_id=uuid.uuid4(), guess_id=uuid.uuid4(), game_id=self.game.game_id, player_id=player.player_id
game_id=self.game.game_id,
player_id=player.player_id,
player_name=player.player_name,
) )
Guess.update({"guess": value}).where( Guess.update({"guess": value}).where(
(Guess.game_id == self.game.game_id) # (Guess.game_id == self.game.game_id)
& (Guess.player_id == self.message.author.id) # &
(Guess.player_id == self.message.author.id)
).execute() ).execute()
if created: if created:

View File

@ -7,6 +7,7 @@ from database.models import (
PlayerModel as Player, PlayerModel as Player,
) )
class ProcessGuess: class ProcessGuess:
def __init__(self, game, **kwargs): def __init__(self, game, **kwargs):
self.game_manager = game self.game_manager = game
@ -31,7 +32,7 @@ class ProcessGuess:
) )
.join(Player) .join(Player)
.where( .where(
(Guess.game_id == self.game_manager.game.game_id) (Guess.game == self.game_manager.game.game_id)
& (Guess.guess > 0) & (Guess.guess > 0)
& (Guess.player.player_id == Player.player_id) & (Guess.player.player_id == Player.player_id)
) )
@ -42,7 +43,7 @@ class ProcessGuess:
def update_difference_value(self): def update_difference_value(self):
# Update player's difference in guessed value # Update player's difference in guessed value
Guess.update({"difference": self.difference}).where( 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.player.player_id == self.guess.player.player_id)
& (Guess.guess_id == self.guess.guess_id) & (Guess.guess_id == self.guess.guess_id)
).execute() ).execute()
@ -60,7 +61,7 @@ class ProcessGuess:
def get_difference(self, guess=None): def get_difference(self, guess=None):
"""Difference calculation, includes "loop over" effect""" """Difference calculation, includes "loop over" effect"""
if not guess: if not guess:
guess = self.guess guess = self.guess.guess
difference = abs(guess - self.pitch_value) difference = abs(guess - self.pitch_value)
@ -80,7 +81,7 @@ class ProcessGuess:
def get_winner_loser(self): def get_winner_loser(self):
# Determine which guesses are closest and furthest from the pitch_value # 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 # Closest to the pitch_value
winner = min(guess_values, key=lambda guess: self.get_difference(guess)) winner = min(guess_values, key=lambda guess: self.get_difference(guess))
# Furthest from the pitch_value # Furthest from the pitch_value