BaseballBot/GhostBallBot/game/guess.py

55 lines
1.7 KiB
Python
Raw Normal View History

2022-11-11 03:03:38 +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 04:42:30 +00:00
# pylint: disable=missing-module-docstring
2022-11-11 03:03:38 +00:00
2022-11-11 04:42:30 +00:00
import uuid
2022-11-11 03:03:38 +00:00
2022-11-11 04:42:30 +00:00
from database.models import PlayerModel as Player, GuessModel as Guess
2022-11-12 02:06:14 +00:00
from game.base import BaseGameManager
2022-11-11 04:44:24 +00:00
2022-11-11 04:42:30 +00:00
class GuessManager(BaseGameManager):
"""Commands that run when a player makes a guess"""
2022-11-11 04:42:30 +00:00
def __init__(self):
super().__init__()
2022-11-11 18:52:54 +00:00
self.commands.append(("guess", self.guess))
2022-11-09 17:48:31 +00:00
2022-11-11 04:42:30 +00:00
async def guess(self):
"""
2022-11-11 04:42:30 +00:00
Guess command - Allows the player to add a guess to the current
running game
"""
2022-11-11 04:42:30 +00:00
if not self.is_running:
return await self.message.channel.send("There is no game running")
2022-11-11 04:42:30 +00:00
value = int(self.message.content.split()[1])
if value < 1 or value > 1000:
return await self.message.channel.send(
"Invalid value. It must be between 1 and 1000 inclusive"
)
2022-11-11 04:42:30 +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-11-11 04:42:30 +00:00
# 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
)
2022-11-11 04:42:30 +00:00
Guess.update({"guess": value}).where(
2022-11-11 04:44:24 +00:00
(Guess.game == self.game.game_id) & (Guess.player == self.message.author.id)
2022-11-11 04:42:30 +00:00
).execute()
2022-11-11 04:42:30 +00:00
if created:
return await self.message.add_reaction("\N{THUMBS UP SIGN}")
2022-11-11 04:42:30 +00:00
return await self.message.channel.send(
f"<@{ str(self.message.author.id) }> your guess has been updated"
)