Compare commits

..

No commits in common. "04df87f054db3843b473e8cec7598a66e91e2fec" and "f1db0f62fccf89ebe90081abbba7d4d7e8f908dc" have entirely different histories.

8 changed files with 32 additions and 92 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", "-u", "/app/main.py"] CMD ["python", "/app/main.py"]

View File

@ -4,8 +4,6 @@
# 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
@ -26,18 +24,6 @@ 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

View File

@ -1,31 +0,0 @@
#!/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.total_points)
for player in players:
player.total_points = 0
Player.bulk_update(players, Player.total_points)
clear_message = "The score has been cleared!"
recipient = await self.discord.fetch_user(self.message.author.id)
await recipient.send(clear_message)

View File

@ -12,20 +12,42 @@ from game.process_guess import ProcessGuess
class EndGameManager(BaseGameManager): class EndGameManager(BaseGameManager):
"""Commands that run at the end of a play""" """Commands that run at the end of a game"""
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"""
pitch_value = self.message.content.split()[1] # Determine arguments
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(
{ {
@ -54,7 +76,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 < 2: if guess_count < 3:
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(
@ -63,7 +85,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,9 +24,10 @@ 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"
+ "!clear - Clear the session scoreboard\n" + " You can also add a batter's guess with: "
+ "!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\n" + "!reset - Removes all players and total points"
+ "!help - Shows this message" + "!help - Shows this message"
) )

View File

@ -16,11 +16,10 @@ 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, ClearManager NewGameManager, EndGameManager, GuessManager, PointsManager, ResetManager, HelpManager
): ):
""" """
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

@ -9,7 +9,7 @@ from game.base import BaseGameManager
class PointsManager(BaseGameManager): class PointsManager(BaseGameManager):
"""Commands that run when a player wants to view the session leaderboard""" """Commands that run when a player makes a guess"""
def __init__(self): def __init__(self):
super().__init__() super().__init__()

View File

@ -40,7 +40,6 @@ 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(
@ -72,25 +71,14 @@ 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
@ -100,51 +88,32 @@ 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
@ -160,9 +129,6 @@ 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()
@ -170,13 +136,10 @@ 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