Compare commits
4 Commits
f4bd33b4d5
...
1a62747da1
Author | SHA1 | Date | |
---|---|---|---|
1a62747da1 | |||
efe58e0278 | |||
84e4975d27 | |||
3dfbb247da |
@ -23,7 +23,7 @@ from peewee import (
|
||||
|
||||
# User can provide path to database, or it will be put next to main.py
|
||||
DATABASE = os.environ.get("database_path", os.getcwd() + "/ghostball.db")
|
||||
database = SqliteDatabase(DATABASE)
|
||||
database = SqliteDatabase(DATABASE, pragmas={"foreign_keys": 1})
|
||||
|
||||
|
||||
class BaseModel(Model):
|
||||
@ -36,6 +36,22 @@ class BaseModel(Model):
|
||||
database = database
|
||||
|
||||
|
||||
class PlayerModel(BaseModel):
|
||||
"""Need to keep track of total player score"""
|
||||
|
||||
player_id = IntegerField(primary_key=True)
|
||||
player_name = CharField()
|
||||
|
||||
total_points = IntegerField(default=0)
|
||||
date_joined = DateTimeField(default=datetime.datetime.now)
|
||||
last_update = DateTimeField(null=True)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Should set the last update everytime the record is saved"""
|
||||
self.last_update = datetime.datetime.now()
|
||||
super().save(*args, **kwargs)
|
||||
|
||||
|
||||
class GameModel(BaseModel):
|
||||
"""Games that are ran"""
|
||||
|
||||
@ -50,18 +66,15 @@ class GameModel(BaseModel):
|
||||
class GuessModel(BaseModel):
|
||||
"""Guesses for a particular game"""
|
||||
|
||||
player_id = IntegerField(primary_key=True)
|
||||
guess_id = UUIDField(primary_key=True)
|
||||
|
||||
player = ForeignKeyField(PlayerModel, backref="guesses")
|
||||
game_id = ForeignKeyField(GameModel, backref="guesses")
|
||||
|
||||
player_name = CharField()
|
||||
guess = IntegerField(default=0)
|
||||
difference = IntegerField(null=True)
|
||||
date_guessed = DateTimeField(null=True)
|
||||
|
||||
# TODO: Add unique constraint for player_id and game_id
|
||||
# ie: one guess per player allowed per game
|
||||
|
||||
|
||||
def create_models():
|
||||
"""Create database tables"""
|
||||
|
||||
|
@ -31,7 +31,7 @@ class GhostBallClient(discord.Client):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
with game.Game() as self.game:
|
||||
with game.GameManager() as self.game:
|
||||
self.game.discord = self
|
||||
|
||||
async def on_ready(self):
|
||||
|
@ -11,10 +11,16 @@
|
||||
"""
|
||||
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
# import dateparser
|
||||
|
||||
from database.models import database, GameModel, GuessModel
|
||||
from database.models import (
|
||||
database,
|
||||
GameModel as Game,
|
||||
GuessModel as Guess,
|
||||
PlayerModel as Player,
|
||||
)
|
||||
|
||||
|
||||
async def check_is_running(method, start_new_game=True):
|
||||
@ -35,7 +41,7 @@ async def check_is_running(method, start_new_game=True):
|
||||
return await wrapper
|
||||
|
||||
|
||||
class Game:
|
||||
class GameManager:
|
||||
"""
|
||||
The game state class
|
||||
|
||||
@ -54,7 +60,7 @@ class Game:
|
||||
"help": self.help,
|
||||
}
|
||||
|
||||
self.game = GameModel
|
||||
self.game = Game
|
||||
|
||||
# Discord message
|
||||
self.message = None
|
||||
@ -86,9 +92,7 @@ class Game:
|
||||
self.is_running = True
|
||||
|
||||
# game.pitch_value is unknown at the start of the game
|
||||
self.game = GameModel.create(
|
||||
game_id=uuid.uuid4(), server_id=self.message.guild.id
|
||||
)
|
||||
self.game = Game.create(game_id=uuid.uuid4(), server_id=self.message.guild.id)
|
||||
|
||||
await self.message.send("Send me your guesses with !guess <number>")
|
||||
|
||||
@ -103,13 +107,8 @@ class Game:
|
||||
|
||||
return None, False, None, None
|
||||
|
||||
@check_is_running(stop, start_new_game=False)
|
||||
async def stop(self):
|
||||
"""
|
||||
Stop command - Stops the game if it is currently running,
|
||||
saves the pitch value, and displays differences
|
||||
"""
|
||||
|
||||
async def close_game(self):
|
||||
"""Update game state database for closing arguments"""
|
||||
# Determine arguments
|
||||
pitch_value, has_batter, batter_id, batter_guess = self.__stop_args__()
|
||||
if not pitch_value:
|
||||
@ -119,22 +118,98 @@ class Game:
|
||||
|
||||
if has_batter:
|
||||
player_id = batter_id[3:]
|
||||
GuessModel.create(
|
||||
Guess.create(
|
||||
game_id=self.game.game_id,
|
||||
player_id=player_id,
|
||||
player_name=self.discord.get_user(int(player_id).name),
|
||||
guess=int(batter_guess),
|
||||
)
|
||||
).save()
|
||||
|
||||
# Save the pitch value
|
||||
self.game.update({"pitch_value": pitch_value})
|
||||
|
||||
# TODO: Determine differences
|
||||
|
||||
await self.message.channel.send(
|
||||
"Difference calculation is not currently available"
|
||||
self.game.update(
|
||||
{
|
||||
Game.pitch_value: pitch_value,
|
||||
Game.date_ended: datetime.datetime.now(),
|
||||
}
|
||||
)
|
||||
|
||||
return pitch_value
|
||||
|
||||
@check_is_running(stop, start_new_game=False)
|
||||
async def stop(self):
|
||||
"""
|
||||
Stop command - Stops the game if it is currently running,
|
||||
saves the pitch value, and displays differences
|
||||
"""
|
||||
|
||||
pitch_value = await self.close_game()
|
||||
|
||||
# Start calculating difference
|
||||
|
||||
# Get all guesses for this game as a list of combo Guess + Player models,
|
||||
# excluding invalid results, from lowest to highest
|
||||
# http://docs.peewee-orm.com/en/latest/peewee/query_examples.html#joins-and-subqueries
|
||||
records = (
|
||||
Guess.select(
|
||||
Guess.guess, Player.player_id, Player.player_name, Player.total_points
|
||||
)
|
||||
.join(Player)
|
||||
.where(
|
||||
(Guess.game_id == self.game.game_id)
|
||||
& (Guess.guess > 0)
|
||||
& (Guess.player_id == Player.player_id)
|
||||
)
|
||||
.order_by(Guess.guess)
|
||||
)
|
||||
|
||||
# Minimum of 3 players
|
||||
if len(records) < 2:
|
||||
self.game = None
|
||||
self.is_running = False
|
||||
return await self.message.channel.send(
|
||||
("Play closed!\n" + "However, there were not enough participants.")
|
||||
)
|
||||
|
||||
message = (
|
||||
"Closed this play! Here are the results\n"
|
||||
+ "PLAYER | GUESS | DIFFERENCE | POINTS GAINED | TOTAL POINTS\n"
|
||||
)
|
||||
|
||||
# Determine which guesses are closest and furthest from the pitch_value
|
||||
guess_values = [record.guess for record in records]
|
||||
closest_guess_value = min(
|
||||
guess_values, key=lambda guess: abs(guess - pitch_value)
|
||||
)
|
||||
furthest_guess_value = max(
|
||||
guess_values, key=lambda guess: abs(guess - pitch_value)
|
||||
)
|
||||
|
||||
def get_difference_score():
|
||||
# TODO: Calculate score based on scoring table
|
||||
return 0
|
||||
|
||||
for record in records:
|
||||
difference = abs(record.guess - pitch_value)
|
||||
difference_score = get_difference_score()
|
||||
|
||||
# TODO: Update Guess.difference
|
||||
# TODO: Update Player.total_points
|
||||
|
||||
if record.guess == closest_guess_value:
|
||||
closest_player_id = record.player.player_id
|
||||
|
||||
if record.guess == furthest_guess_value:
|
||||
furthest_player_id = record.player.player_id
|
||||
|
||||
message += f"{record.player.player_name} | {record.guess} | {difference} | {difference_score}"
|
||||
|
||||
message += (
|
||||
f"Congrats <@{closest_player_id}>! You were the closest!\n"
|
||||
+ f"Sorry <@{furthest_player_id}>, you were way off"
|
||||
)
|
||||
|
||||
await self.message.channel.send(message)
|
||||
|
||||
# stop and discard game
|
||||
self.is_running = False
|
||||
self.game = None
|
||||
@ -152,21 +227,19 @@ class Game:
|
||||
"Invalid value. It must be between 1 and 1000 inclusive"
|
||||
)
|
||||
|
||||
# TODO: Check if the user tried to vote before, update their vote if so
|
||||
|
||||
player_guess, created = GuessModel.get_or_create(
|
||||
player_guess, created = Guess.get_or_create(
|
||||
game_id=self.game.game_id,
|
||||
player_id=self.message.author.id,
|
||||
player_name=self.message.author.name,
|
||||
)
|
||||
|
||||
player_guess.update(guess=value)
|
||||
if not created: # They updated their guess
|
||||
await self.message.channel.send(
|
||||
f"<@{ str(self.message.author.id) }> your guess has been updated"
|
||||
)
|
||||
player_guess.update({Guess.guess: value})
|
||||
if created:
|
||||
return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}")
|
||||
|
||||
return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}")
|
||||
return await self.message.channel.send(
|
||||
f"<@{ str(self.message.author.id) }> your guess has been updated"
|
||||
)
|
||||
|
||||
async def points(self):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user