2022-09-24 02:41:03 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright 2022 - c0de <c0de@c0de.dev>
|
|
|
|
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
|
|
|
|
|
2022-11-11 03:03:38 +00:00
|
|
|
# pylint: disable=no-member
|
2022-10-25 01:53:51 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
A Context Manager / State Machine that keeps track of
|
|
|
|
a single game instance (there should only be one) in a
|
|
|
|
Discord channel
|
|
|
|
"""
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
import pdb
|
2022-10-31 03:24:51 +00:00
|
|
|
import math
|
2022-09-24 02:41:03 +00:00
|
|
|
import uuid
|
2022-10-26 04:17:57 +00:00
|
|
|
import datetime
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-25 01:53:51 +00:00
|
|
|
# import dateparser
|
2022-10-04 00:41:02 +00:00
|
|
|
|
2022-10-26 04:17:57 +00:00
|
|
|
from database.models import (
|
|
|
|
database,
|
|
|
|
GameModel as Game,
|
|
|
|
GuessModel as Guess,
|
|
|
|
PlayerModel as Player,
|
|
|
|
)
|
2022-10-31 03:24:51 +00:00
|
|
|
from game.guess import ProcessGuess
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-25 01:05:21 +00:00
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
def check_is_running(method):
|
2022-10-25 01:02:26 +00:00
|
|
|
"""
|
|
|
|
Decorator that determines if the game is running or not
|
|
|
|
"""
|
|
|
|
|
|
|
|
async def wrapper(self):
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
if self.is_running and self.new_game:
|
2022-10-25 01:02:26 +00:00
|
|
|
return await self.message.channel.send("A game is already running")
|
2022-10-25 01:53:51 +00:00
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
if not self.is_running and not self.new_game:
|
2022-10-25 01:53:51 +00:00
|
|
|
return await self.message.channel.send("There is no game running")
|
2022-10-25 01:02:26 +00:00
|
|
|
|
|
|
|
await method(self)
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
return wrapper
|
2022-10-25 01:00:39 +00:00
|
|
|
|
2022-10-25 01:05:21 +00:00
|
|
|
|
2022-10-26 04:17:57 +00:00
|
|
|
class GameManager:
|
2022-09-24 02:41:03 +00:00
|
|
|
"""
|
2022-10-25 01:00:39 +00:00
|
|
|
The game state class
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-25 01:00:39 +00:00
|
|
|
This represents a game that exists in a channel
|
|
|
|
"""
|
2022-10-25 00:55:50 +00:00
|
|
|
|
2022-10-25 01:53:51 +00:00
|
|
|
def __init__(self):
|
2022-09-24 02:41:03 +00:00
|
|
|
# Only one game should run at at time
|
|
|
|
self.is_running = False
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
self.new_game = True
|
|
|
|
|
|
|
|
self.commands = [
|
|
|
|
("braveball", self.start),
|
|
|
|
("resolve", self.stop),
|
|
|
|
("guess", self.guess),
|
|
|
|
("points", self.points),
|
|
|
|
("help", self.help),
|
|
|
|
]
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-26 04:17:57 +00:00
|
|
|
self.game = Game
|
2022-09-24 02:41:03 +00:00
|
|
|
|
|
|
|
# Discord message
|
|
|
|
self.message = None
|
|
|
|
|
|
|
|
# Discord client instance
|
|
|
|
self.discord = None
|
|
|
|
|
2022-10-25 01:53:51 +00:00
|
|
|
def __enter__(self):
|
|
|
|
"""
|
|
|
|
Allows use of `with Game() as game` for try/except statements
|
|
|
|
(https://peps.python.org/pep-0343/)
|
|
|
|
"""
|
|
|
|
|
2022-10-25 00:13:34 +00:00
|
|
|
database.connect()
|
2022-10-25 00:55:50 +00:00
|
|
|
return self
|
2022-10-25 00:13:34 +00:00
|
|
|
|
2022-10-25 00:55:50 +00:00
|
|
|
def __exit__(self, exception_type, exception_value, exception_traceback):
|
2022-10-25 00:13:34 +00:00
|
|
|
"""
|
2022-10-25 01:00:39 +00:00
|
|
|
Automagically close the database
|
|
|
|
when this class has ended execution
|
2022-10-25 00:13:34 +00:00
|
|
|
"""
|
|
|
|
database.close()
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
# @check_is_running
|
2022-09-24 02:41:03 +00:00
|
|
|
async def start(self):
|
2022-10-25 01:53:51 +00:00
|
|
|
"""
|
|
|
|
Start command - Starts a new game if there isn't already one running
|
|
|
|
"""
|
2022-10-27 04:36:41 +00:00
|
|
|
# print(dir(self.message))
|
|
|
|
|
2022-09-24 02:41:03 +00:00
|
|
|
self.is_running = True
|
2022-10-27 04:36:41 +00:00
|
|
|
self.new_game = False
|
2022-09-24 02:41:03 +00:00
|
|
|
|
|
|
|
# game.pitch_value is unknown at the start of the game
|
2022-10-27 04:36:41 +00:00
|
|
|
self.game = Game.create(game_id=uuid.uuid4(), server_id=self.message.channel.id)
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
await self.message.channel.send("Send me your guesses with !guess <number>")
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-25 01:53:51 +00:00
|
|
|
def __stop_args__(self):
|
2022-09-24 02:41:03 +00:00
|
|
|
pieces = self.message.content.split()
|
|
|
|
|
|
|
|
if len(pieces) == 2:
|
|
|
|
return pieces[1], False, None, None
|
2022-10-25 01:53:51 +00:00
|
|
|
|
|
|
|
if len(pieces) == 4:
|
2022-09-24 02:41:03 +00:00
|
|
|
return pieces[1], True, pieces[2], pieces[3]
|
|
|
|
|
|
|
|
return None, False, None, None
|
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
async def update_pitch_value(self):
|
2022-10-26 04:17:57 +00:00
|
|
|
"""Update game state database for closing arguments"""
|
2022-09-24 02:41:03 +00:00
|
|
|
# Determine arguments
|
2022-10-25 01:53:51 +00:00
|
|
|
pitch_value, has_batter, batter_id, batter_guess = self.__stop_args__()
|
2022-09-24 02:41:03 +00:00
|
|
|
if not pitch_value:
|
2022-10-25 01:00:39 +00:00
|
|
|
return await self.message.channel.send(
|
|
|
|
f"Invalid command <@{ str(self.message.author.id) }>!"
|
|
|
|
)
|
2022-09-24 02:41:03 +00:00
|
|
|
|
|
|
|
if has_batter:
|
|
|
|
player_id = batter_id[3:]
|
2022-10-26 04:17:57 +00:00
|
|
|
Guess.create(
|
2022-10-25 01:00:39 +00:00
|
|
|
game_id=self.game.game_id,
|
|
|
|
player_id=player_id,
|
|
|
|
player_name=self.discord.get_user(int(player_id).name),
|
|
|
|
guess=int(batter_guess),
|
2022-10-26 04:17:57 +00:00
|
|
|
).save()
|
2022-09-24 02:41:03 +00:00
|
|
|
|
|
|
|
# Save the pitch value
|
2022-10-31 03:24:51 +00:00
|
|
|
Game.update(
|
2022-10-26 04:17:57 +00:00
|
|
|
{
|
|
|
|
Game.pitch_value: pitch_value,
|
|
|
|
Game.date_ended: datetime.datetime.now(),
|
|
|
|
}
|
2022-10-31 03:24:51 +00:00
|
|
|
).where(Game.game_id == self.game.game_id).execute()
|
2022-10-26 04:17:57 +00:00
|
|
|
|
2022-10-27 05:18:12 +00:00
|
|
|
return int(pitch_value)
|
2022-10-26 04:17:57 +00:00
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
# @check_is_running
|
2022-10-26 04:17:57 +00:00
|
|
|
async def stop(self):
|
|
|
|
"""
|
|
|
|
Stop command - Stops the game if it is currently running,
|
|
|
|
saves the pitch value, and displays differences
|
|
|
|
"""
|
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
# How many valid guesses got placed?
|
|
|
|
guess_count = (
|
2022-11-11 03:03:38 +00:00
|
|
|
Guess.select()
|
|
|
|
.join(Game)
|
2022-11-09 17:48:31 +00:00
|
|
|
.where((Guess.game.game_id == self.game.game_id) & (Guess.guess > 0))
|
|
|
|
.count()
|
|
|
|
)
|
2022-09-24 02:41:03 +00:00
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
# Discard the game if there weren't enough players
|
|
|
|
if guess_count < 3:
|
2022-10-26 04:17:57 +00:00
|
|
|
self.game = None
|
|
|
|
self.is_running = False
|
2022-10-27 05:18:12 +00:00
|
|
|
await self.message.channel.send(
|
2022-10-26 04:17:57 +00:00
|
|
|
("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"
|
|
|
|
)
|
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
pitch_value = await self.update_pitch_value()
|
2022-11-11 03:03:38 +00:00
|
|
|
guess_processor = ProcessGuess(
|
|
|
|
game=self, pitch_value=pitch_value, message=message
|
|
|
|
)
|
2022-10-27 05:18:12 +00:00
|
|
|
|
2022-11-09 17:48:31 +00:00
|
|
|
(
|
|
|
|
message,
|
|
|
|
closest_player_id,
|
|
|
|
furthest_player_id,
|
|
|
|
) = guess_processor.process_guesses()
|
2022-10-26 04:17:57 +00:00
|
|
|
|
|
|
|
message += (
|
2022-10-27 05:18:12 +00:00
|
|
|
f"\nCongrats <@{closest_player_id}>! You were the closest!\n"
|
2022-10-26 04:17:57 +00:00
|
|
|
+ f"Sorry <@{furthest_player_id}>, you were way off"
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.message.channel.send(message)
|
|
|
|
|
2022-09-24 02:41:03 +00:00
|
|
|
# stop and discard game
|
|
|
|
self.is_running = False
|
|
|
|
self.game = None
|
|
|
|
|
2022-10-27 04:36:41 +00:00
|
|
|
# @check_is_running
|
2022-09-24 02:41:03 +00:00
|
|
|
async def guess(self):
|
2022-10-25 01:53:51 +00:00
|
|
|
"""
|
|
|
|
Guess command - Allows the player to add a guess to the current
|
|
|
|
running game
|
|
|
|
"""
|
|
|
|
|
2022-10-04 00:33:15 +00:00
|
|
|
value = int(self.message.content.split()[1])
|
|
|
|
if value < 1 or value > 1000:
|
2022-10-25 01:00:39 +00:00
|
|
|
return await self.message.channel.send(
|
2022-10-25 01:53:51 +00:00
|
|
|
"Invalid value. It must be between 1 and 1000 inclusive"
|
2022-10-25 01:00:39 +00:00
|
|
|
)
|
2022-10-04 00:33:15 +00:00
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
# Create player if they don't exist
|
|
|
|
player, _ = Player.get_or_create(
|
|
|
|
player_id=self.message.author.id, player_name=self.message.author.name
|
2022-10-27 04:36:41 +00:00
|
|
|
)
|
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
# Create the guess (or allow us to say update successful)
|
|
|
|
_, created = Guess.get_or_create(
|
2022-11-09 17:48:31 +00:00
|
|
|
guess_id=uuid.uuid4(), game_id=self.game.game_id, player_id=player.player_id
|
2022-10-04 00:33:15 +00:00
|
|
|
)
|
|
|
|
|
2022-10-31 03:24:51 +00:00
|
|
|
Guess.update({"guess": value}).where(
|
2022-11-09 17:48:31 +00:00
|
|
|
# (Guess.game_id == self.game.game_id)
|
|
|
|
# &
|
|
|
|
(Guess.player_id == self.message.author.id)
|
2022-10-31 03:24:51 +00:00
|
|
|
).execute()
|
|
|
|
|
2022-10-27 05:18:12 +00:00
|
|
|
if created:
|
|
|
|
return await self.message.add_reaction("\N{THUMBS UP SIGN}")
|
2022-10-25 01:53:51 +00:00
|
|
|
|
2022-10-27 05:18:12 +00:00
|
|
|
return await self.message.channel.send(
|
|
|
|
f"<@{ str(self.message.author.id) }> your guess has been updated"
|
|
|
|
)
|
2022-10-04 00:33:15 +00:00
|
|
|
|
2022-09-24 02:41:03 +00:00
|
|
|
async def points(self):
|
2022-10-25 01:53:51 +00:00
|
|
|
"""
|
|
|
|
Points command - returns a table ordered from highest to lowest
|
|
|
|
of the players and their points
|
|
|
|
"""
|
2022-10-25 00:13:34 +00:00
|
|
|
# TODO
|
2022-10-25 01:53:51 +00:00
|
|
|
# value = self.message.content.split()
|
|
|
|
# try:
|
|
|
|
# if len(value) > 1:
|
|
|
|
# timestamp = dateparser.parse(value[1])
|
|
|
|
# except:
|
|
|
|
# return await self.message.channel.send("Invalid timestamp. Try again")
|
2022-10-04 00:41:02 +00:00
|
|
|
|
|
|
|
return await self.message.channel.send("Sorry, not implemented yet")
|
2022-09-24 02:41:03 +00:00
|
|
|
|
|
|
|
async def help(self):
|
2022-10-25 01:53:51 +00:00
|
|
|
"""help command"""
|
2022-09-24 02:41:03 +00:00
|
|
|
# TODO: Add help message
|
|
|
|
help_message = "help"
|
|
|
|
|
|
|
|
recipient = await self.discord.fetch_user(self.message.author.id)
|
|
|
|
await recipient.send(help_message)
|