BaseballBot/game/end_game.py

90 lines
2.7 KiB
Python
Raw Normal View History

2022-11-11 04:42:20 +00:00
#!/usr/bin/env python3
# Copyright 2022 - c0de <c0de@c0de.dev>
# Licensed under the MIT License (https://opensource.org/licenses/MIT)
# pylint: disable=missing-module-docstring
import datetime
from database.models import GameModel as Game, GuessModel as Guess
2022-11-12 02:06:14 +00:00
from game.base import BaseGameManager
2022-11-11 04:42:20 +00:00
from game.process_guess import ProcessGuess
2022-11-11 04:44:24 +00:00
2022-11-11 04:42:20 +00:00
class EndGameManager(BaseGameManager):
2023-03-02 23:52:40 +00:00
"""Commands that run at the end of a play"""
2022-11-11 04:42:20 +00:00
def __init__(self):
super().__init__()
2022-11-11 18:52:54 +00:00
self.commands.append(("resolve", self.stop))
2022-11-11 04:42:20 +00:00
async def update_pitch_value(self):
"""Update game state database for closing arguments"""
2023-03-02 23:52:40 +00:00
pitch_value = self.message.content.split()[1]
2022-11-11 04:42:20 +00:00
if not pitch_value:
return await self.message.channel.send(
f"Invalid command <@{ str(self.message.author.id) }>!"
)
# Save the pitch value
Game.update(
{
Game.pitch_value: pitch_value,
Game.date_ended: datetime.datetime.now(),
}
).where(Game.game_id == self.game.game_id).execute()
return int(pitch_value)
async def stop(self):
"""
Stop command - Stops the game if it is currently running,
saves the pitch value, and displays differences
"""
if not self.is_running:
return await self.message.channel.send("There is no game running")
# How many valid guesses got placed?
guess_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
2023-03-02 23:52:40 +00:00
if guess_count < 2:
2022-11-11 04:42:20 +00:00
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"
2023-03-02 23:52:40 +00:00
+ "__PLAYER | GUESS | DIFFERENCE | POINTS GAINED | TOTAL POINTS__\n"
)
2022-11-11 04:42:20 +00:00
pitch_value = await self.update_pitch_value()
guess_processor = ProcessGuess(
game=self, pitch_value=pitch_value, message=message
2022-11-11 04:42:20 +00:00
)
(
message,
2022-11-11 04:42:20 +00:00
closest_player_id,
furthest_player_id,
) = guess_processor.process_guesses()
message += (
f"\nCongrats <@{closest_player_id}>! You were the closest!\n"
+ f"Sorry <@{furthest_player_id}>, you were way off"
)
2022-11-11 04:42:20 +00:00
await self.message.channel.send(message)
# stop and discard game
self.is_running = False
self.game = None