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
|
# 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 = os.environ.get("database_path", os.getcwd() + "/ghostball.db")
|
||||||
database = SqliteDatabase(DATABASE)
|
database = SqliteDatabase(DATABASE, pragmas={"foreign_keys": 1})
|
||||||
|
|
||||||
|
|
||||||
class BaseModel(Model):
|
class BaseModel(Model):
|
||||||
@ -36,6 +36,22 @@ class BaseModel(Model):
|
|||||||
database = database
|
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):
|
class GameModel(BaseModel):
|
||||||
"""Games that are ran"""
|
"""Games that are ran"""
|
||||||
|
|
||||||
@ -50,18 +66,15 @@ class GameModel(BaseModel):
|
|||||||
class GuessModel(BaseModel):
|
class GuessModel(BaseModel):
|
||||||
"""Guesses for a particular game"""
|
"""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")
|
game_id = ForeignKeyField(GameModel, backref="guesses")
|
||||||
|
|
||||||
player_name = CharField()
|
|
||||||
guess = IntegerField(default=0)
|
guess = IntegerField(default=0)
|
||||||
difference = IntegerField(null=True)
|
difference = IntegerField(null=True)
|
||||||
date_guessed = DateTimeField(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():
|
def create_models():
|
||||||
"""Create database tables"""
|
"""Create database tables"""
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ class GhostBallClient(discord.Client):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
with game.Game() as self.game:
|
with game.GameManager() as self.game:
|
||||||
self.game.discord = self
|
self.game.discord = self
|
||||||
|
|
||||||
async def on_ready(self):
|
async def on_ready(self):
|
||||||
|
@ -11,10 +11,16 @@
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
import uuid
|
import uuid
|
||||||
|
import datetime
|
||||||
|
|
||||||
# import dateparser
|
# 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):
|
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
|
return await wrapper
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class GameManager:
|
||||||
"""
|
"""
|
||||||
The game state class
|
The game state class
|
||||||
|
|
||||||
@ -54,7 +60,7 @@ class Game:
|
|||||||
"help": self.help,
|
"help": self.help,
|
||||||
}
|
}
|
||||||
|
|
||||||
self.game = GameModel
|
self.game = Game
|
||||||
|
|
||||||
# Discord message
|
# Discord message
|
||||||
self.message = None
|
self.message = None
|
||||||
@ -86,9 +92,7 @@ class Game:
|
|||||||
self.is_running = True
|
self.is_running = True
|
||||||
|
|
||||||
# game.pitch_value is unknown at the start of the game
|
# game.pitch_value is unknown at the start of the game
|
||||||
self.game = GameModel.create(
|
self.game = Game.create(game_id=uuid.uuid4(), server_id=self.message.guild.id)
|
||||||
game_id=uuid.uuid4(), server_id=self.message.guild.id
|
|
||||||
)
|
|
||||||
|
|
||||||
await self.message.send("Send me your guesses with !guess <number>")
|
await self.message.send("Send me your guesses with !guess <number>")
|
||||||
|
|
||||||
@ -103,13 +107,8 @@ class Game:
|
|||||||
|
|
||||||
return None, False, None, None
|
return None, False, None, None
|
||||||
|
|
||||||
@check_is_running(stop, start_new_game=False)
|
async def close_game(self):
|
||||||
async def stop(self):
|
"""Update game state database for closing arguments"""
|
||||||
"""
|
|
||||||
Stop command - Stops the game if it is currently running,
|
|
||||||
saves the pitch value, and displays differences
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Determine arguments
|
# Determine arguments
|
||||||
pitch_value, has_batter, batter_id, batter_guess = self.__stop_args__()
|
pitch_value, has_batter, batter_id, batter_guess = self.__stop_args__()
|
||||||
if not pitch_value:
|
if not pitch_value:
|
||||||
@ -119,22 +118,98 @@ class Game:
|
|||||||
|
|
||||||
if has_batter:
|
if has_batter:
|
||||||
player_id = batter_id[3:]
|
player_id = batter_id[3:]
|
||||||
GuessModel.create(
|
Guess.create(
|
||||||
game_id=self.game.game_id,
|
game_id=self.game.game_id,
|
||||||
player_id=player_id,
|
player_id=player_id,
|
||||||
player_name=self.discord.get_user(int(player_id).name),
|
player_name=self.discord.get_user(int(player_id).name),
|
||||||
guess=int(batter_guess),
|
guess=int(batter_guess),
|
||||||
)
|
).save()
|
||||||
|
|
||||||
# Save the pitch value
|
# Save the pitch value
|
||||||
self.game.update({"pitch_value": pitch_value})
|
self.game.update(
|
||||||
|
{
|
||||||
# TODO: Determine differences
|
Game.pitch_value: pitch_value,
|
||||||
|
Game.date_ended: datetime.datetime.now(),
|
||||||
await self.message.channel.send(
|
}
|
||||||
"Difference calculation is not currently available"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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
|
# stop and discard game
|
||||||
self.is_running = False
|
self.is_running = False
|
||||||
self.game = None
|
self.game = None
|
||||||
@ -152,22 +227,20 @@ class Game:
|
|||||||
"Invalid value. It must be between 1 and 1000 inclusive"
|
"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 = Guess.get_or_create(
|
||||||
|
|
||||||
player_guess, created = GuessModel.get_or_create(
|
|
||||||
game_id=self.game.game_id,
|
game_id=self.game.game_id,
|
||||||
player_id=self.message.author.id,
|
player_id=self.message.author.id,
|
||||||
player_name=self.message.author.name,
|
player_name=self.message.author.name,
|
||||||
)
|
)
|
||||||
|
|
||||||
player_guess.update(guess=value)
|
player_guess.update({Guess.guess: value})
|
||||||
if not created: # They updated their guess
|
if created:
|
||||||
await self.message.channel.send(
|
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"
|
f"<@{ str(self.message.author.id) }> your guess has been updated"
|
||||||
)
|
)
|
||||||
|
|
||||||
return await self.message.add_reaction(emoji="\N{THUMBS UP SIGN}")
|
|
||||||
|
|
||||||
async def points(self):
|
async def points(self):
|
||||||
"""
|
"""
|
||||||
Points command - returns a table ordered from highest to lowest
|
Points command - returns a table ordered from highest to lowest
|
||||||
|
Loading…
Reference in New Issue
Block a user