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)
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])

View File

@ -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:

View File

@ -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