Compare commits
2 Commits
e39a19da55
...
3435e4ce7a
Author | SHA1 | Date | |
---|---|---|---|
3435e4ce7a | |||
eb5ed5fe9e |
@ -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])
|
||||
|
@ -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:
|
||||
|
@ -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
|
||||
|
@ -32,3 +32,10 @@ The original code is in [src](./src/). Feel free to try to get this working.
|
||||
1. Bot should then connect to discord and start responding to commands defined in game.py
|
||||
|
||||
|
||||
## Roadmap
|
||||
|
||||
Things that would be nice to add:
|
||||
|
||||
* (yaml) Config file for tweaking various internals
|
||||
* Admin commands: table clear, record deletion, record update
|
||||
* Audit log of admin commands, and previous values for anything changed
|
||||
|
Loading…
Reference in New Issue
Block a user