Branch addition #1

Merged
c0de merged 11 commits from zed/BaseballBot:Toof into main 2024-04-23 22:19:23 +00:00
9 changed files with 93 additions and 34 deletions

View File

@ -15,4 +15,4 @@ FROM build AS run
ENV discord_token "" ENV discord_token ""
ENV database_path "/tmp/baseball.db" ENV database_path "/tmp/baseball.db"
CMD ["python", "/app/main.py"] CMD ["python", "-u", "/app/main.py"]

View File

@ -4,6 +4,8 @@
# pylint: disable=missing-module-docstring # pylint: disable=missing-module-docstring
import logging
from database.models import database, GameModel as Game from database.models import database, GameModel as Game
@ -24,6 +26,18 @@ class BaseGameManager:
# Discord client instance # Discord client instance
self.discord = None self.discord = None
logger = logging.getLogger()
console = logging.StreamHandler()
format_str = '%(asctime)s\t%(levelname)s -- %(processName)s %(filename)s:%(lineno)s -- %(message)s'
console.setFormatter(logging.Formatter(format_str))
logger.addHandler(console)
logger.setLevel(logging.DEBUG)
self.logger = logger
def __enter__(self): def __enter__(self):
""" """
Allows use of `with Game() as game` for try/except statements Allows use of `with Game() as game` for try/except statements

30
game/clear.py Normal file
View File

@ -0,0 +1,30 @@
#!/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,too-few-public-methods
from database.models import PlayerModel as Player
from game.base import BaseGameManager
class ClearManager(BaseGameManager):
"""Commands that run when a player clears the session leaderboard"""
def __init__(self):
super().__init__()
self.commands.append(("clear", self.clear))
async def clear(self):
"""Clear command - Clears the session scoreboard"""
players = Player.select(Player.player_id, Player.total_points)
for player in players:
player.total_points = 0
Player.bulk_update(players, fields=[Player.total_points])
clear_message = "The score has been cleared!"
await self.message.channel.send(clear_message)

View File

@ -12,42 +12,20 @@ from game.process_guess import ProcessGuess
class EndGameManager(BaseGameManager): class EndGameManager(BaseGameManager):
"""Commands that run at the end of a game""" """Commands that run at the end of a play"""
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.commands.append(("resolve", self.stop)) self.commands.append(("resolve", self.stop))
def __stop_args__(self):
pieces = self.message.content.split()
if len(pieces) == 2:
return pieces[1], False, None, None
if len(pieces) == 4:
return pieces[1], True, pieces[2], pieces[3]
return None, False, None, None
async def update_pitch_value(self): async def update_pitch_value(self):
"""Update game state database for closing arguments""" """Update game state database for closing arguments"""
# Determine arguments pitch_value = self.message.content.split()[1]
pitch_value, has_batter, batter_id, batter_guess = self.__stop_args__()
if not pitch_value: if not pitch_value:
return await self.message.channel.send( return await self.message.channel.send(
f"Invalid command <@{ str(self.message.author.id) }>!" f"Invalid command <@{ str(self.message.author.id) }>!"
) )
if has_batter:
player_id = batter_id[3:]
player_name = await self.discord.get_user(int(player_id).name)
Guess.create(
game_id=self.game.game_id,
player_id=player_id,
player_name=player_name,
guess=int(batter_guess),
).save()
# Save the pitch value # Save the pitch value
Game.update( Game.update(
{ {
@ -76,7 +54,7 @@ class EndGameManager(BaseGameManager):
) )
# Discard the game if there weren't enough players # Discard the game if there weren't enough players
if guess_count < 3: if guess_count < 2:
self.game = None self.game = None
self.is_running = False self.is_running = False
return await self.message.channel.send( return await self.message.channel.send(
@ -85,7 +63,7 @@ class EndGameManager(BaseGameManager):
message = ( message = (
"Closed this play! Here are the results\n" "Closed this play! Here are the results\n"
+ "PLAYER | GUESS | DIFFERENCE | POINTS GAINED | TOTAL POINTS\n" + "__PLAYER | GUESS | DIFFERENCE | POINTS GAINED | TOTAL POINTS__\n"
) )
pitch_value = await self.update_pitch_value() pitch_value = await self.update_pitch_value()

View File

@ -24,10 +24,9 @@ class HelpManager(BaseGameManager):
+ "!guess - While a game is running, add a guess" + "!guess - While a game is running, add a guess"
+ " (or update an existing one) from 1-1000\n" + " (or update an existing one) from 1-1000\n"
+ "!resolve <value> - 1-1000 to resolve the game\n" + "!resolve <value> - 1-1000 to resolve the game\n"
+ " You can also add a batter's guess with: " + "!clear - Clear the session scoreboard\n"
+ "!resolve <value> <discord id #> <guess>\n"
+ "!points - Shows a table of the most recent players, and their scores\n" + "!points - Shows a table of the most recent players, and their scores\n"
+ "!reset - Removes all players and total points" + "!reset - Removes all players and total points\n"
+ "!help - Shows this message" + "!help - Shows this message"
) )

View File

@ -16,10 +16,11 @@ from game.guess import GuessManager
from game.points import PointsManager from game.points import PointsManager
from game.reset import ResetManager from game.reset import ResetManager
from game.help import HelpManager from game.help import HelpManager
from game.clear import ClearManager
class GameManager( class GameManager(
NewGameManager, EndGameManager, GuessManager, PointsManager, ResetManager, HelpManager NewGameManager, EndGameManager, GuessManager, PointsManager, ResetManager, HelpManager, ClearManager
): ):
""" """
Represents what this bot is able to do on a channel (or DMs) Represents what this bot is able to do on a channel (or DMs)

View File

@ -13,7 +13,7 @@ class NewGameManager(BaseGameManager):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.commands.append(("braveball", self.start)) self.commands.append(("bb", self.start))
async def start(self): async def start(self):
""" """

View File

@ -9,7 +9,7 @@ from game.base import BaseGameManager
class PointsManager(BaseGameManager): class PointsManager(BaseGameManager):
"""Commands that run when a player makes a guess""" """Commands that run when a player wants to view the session leaderboard"""
def __init__(self): def __init__(self):
super().__init__() super().__init__()
@ -27,7 +27,7 @@ class PointsManager(BaseGameManager):
players = Player.select( players = Player.select(
Player.player_name, Player.total_points, Player.last_update Player.player_name, Player.total_points, Player.last_update
).order_by(Player.last_update.desc(), Player.total_points.desc()) ).order_by(Player.total_points.desc())
for player in players: for player in players:
message += ( message += (

View File

@ -40,6 +40,7 @@ class ProcessGuess:
Player.player_id, Player.player_id,
Player.player_name, Player.player_name,
Player.total_points, Player.total_points,
Guess.difference
) )
.join(Player) .join(Player)
.where( .where(
@ -71,14 +72,25 @@ class ProcessGuess:
def get_difference(self, guess=None): def get_difference(self, guess=None):
"""Difference calculation, includes "loop over" effect""" """Difference calculation, includes "loop over" effect"""
self.game_manager.logger.debug("get_difference")
self.game_manager.logger.debug(guess if guess else "")
if not guess: if not guess:
guess = self.guess.guess guess = self.guess.guess
self.game_manager.logger.debug(f"guess: {guess}")
difference = abs(guess - self.pitch_value) difference = abs(guess - self.pitch_value)
self.game_manager.logger.debug(f"Difference:{difference}")
if difference > 500: if difference > 500:
difference = 1000 - difference difference = 1000 - difference
self.game_manager.logger.debug("Diff loop over 500")
self.game_manager.logger.debug(f"{difference}")
self.difference = difference self.difference = difference
return self.difference return self.difference
@ -88,32 +100,51 @@ class ProcessGuess:
they are (within range of 0-500) to the pitch_value they are (within range of 0-500) to the pitch_value
""" """
self.game_manager.logger.debug("> get_difference_score")
self.game_manager.logger.debug(self.difference)
if self.difference == 0: if self.difference == 0:
self.difference_score = 15 self.difference_score = 15
self.game_manager.logger.debug("0 Diff")
elif self.difference > 0 and self.difference < 21: elif self.difference > 0 and self.difference < 21:
self.difference_score = 8 self.difference_score = 8
self.game_manager.logger.debug("0 to 20 diff")
elif self.difference > 20 and self.difference < 51: elif self.difference > 20 and self.difference < 51:
self.difference_score = 5 self.difference_score = 5
self.game_manager.logger.debug("21 to 50 Diff")
elif self.difference > 50 and self.difference < 101: elif self.difference > 50 and self.difference < 101:
self.difference_score = 3 self.difference_score = 3
self.game_manager.logger.debug("51 to 100 Diff")
elif self.difference > 100 and self.difference < 151: elif self.difference > 100 and self.difference < 151:
self.difference_score = 2 self.difference_score = 2
self.game_manager.logger.debug("101 to 150 Diff")
elif self.difference > 150 and self.difference < 201: elif self.difference > 150 and self.difference < 201:
self.difference_score = 1 self.difference_score = 1
self.game_manager.logger.debug("151 to 200 Diff")
elif self.difference > 200 and self.difference < 495: elif self.difference > 200 and self.difference < 495:
self.difference_score = 0 self.difference_score = 0
self.game_manager.logger.debug("Diff too big")
else: else:
self.difference_score = -5 self.difference_score = -5
self.game_manager.logger.debug("Big succ")
return self.difference_score return self.difference_score
def get_winner_loser(self): def get_winner_loser(self):
"""Determine which guesses are closest and furthest from the pitch_value""" """Determine which guesses are closest and furthest from the pitch_value"""
self.game_manager.logger.debug("> get_winner_loser")
guess_values = [record.guess for record in self.get_guesses()] guess_values = [record.guess for record in self.get_guesses()]
self.game_manager.logger.debug(", ".join([str(guess) for guess in guess_values]))
# Closest to the pitch_value # Closest to the pitch_value
winner = min(guess_values, key=lambda guess: self.get_difference(guess)) winner = min(guess_values, key=lambda guess: self.get_difference(guess))
self.game_manager.logger.debug(f"winner: {winner}")
# Furthest from the pitch_value # Furthest from the pitch_value
loser = max(guess_values, key=lambda guess: self.get_difference(guess)) loser = max(guess_values, key=lambda guess: self.get_difference(guess))
self.game_manager.logger.debug(f"loser: {loser}")
return winner, loser return winner, loser
@ -129,6 +160,9 @@ class ProcessGuess:
for guess in self.get_guesses(): for guess in self.get_guesses():
self.guess = guess self.guess = guess
self.game_manager.logger.debug(f"Current guess: {guess}")
difference = self.get_difference() difference = self.get_difference()
difference_score = self.get_difference_score() difference_score = self.get_difference_score()
self.update_difference_value() self.update_difference_value()
@ -136,10 +170,13 @@ class ProcessGuess:
self.message += f"{guess.player.player_name} | {guess.guess} | {difference} | {difference_score} | {(guess.player.total_points + difference_score)}\n" self.message += f"{guess.player.player_name} | {guess.guess} | {difference} | {difference_score} | {(guess.player.total_points + difference_score)}\n"
self.game_manager.logger.debug(f"new total: {(guess.player.total_points + difference_score)}")
if guess.guess == winner: if guess.guess == winner:
closest_player_id = guess.player.player_id closest_player_id = guess.player.player_id
if guess.guess == loser: if guess.guess == loser:
furthest_player_id = guess.player.player_id furthest_player_id = guess.player.player_id
self.game_manager.logger.debug(self.message)
return self.message, closest_player_id, furthest_player_id return self.message, closest_player_id, furthest_player_id