Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Nov 8, 2024
1 parent 312619e commit a515a5a
Show file tree
Hide file tree
Showing 26 changed files with 74 additions and 106 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ cover:
# Lint

format:
ruff check . --fix
black .
isort .
ruff check . --fix

check-ruff:
ruff check .
Expand Down
17 changes: 0 additions & 17 deletions app/bot/handlers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,17 +0,0 @@
from .base import EventHandler # noqa
from .config_difficulty import ConfigDifficultyHandler # noqa
from .config_language import ConfigLanguageHandler # noqa
from .config_model import ConfigModelHandler # noqa
from .config_solvers import ConfigSolverHandler # noqa
from .continue_get_id import ContinueGetIdHandler # noqa
from .continue_handler import ContinueHandler # noqa
from .custom import CustomHandler # noqa
from .error import ErrorHandler # noqa
from .fallback import FallbackHandler # noqa
from .get_session import GetSessionsHandler # noqa
from .help import HelpMessageHandler # noqa
from .warmup import WarmupHandler # noqa
from .next_move import NextMoveHandler # noqa
from .process_message import ProcessMessageHandler # noqa
from .start import StartEventHandler # noqa
from .testing import TestingHandler # noqa
6 changes: 0 additions & 6 deletions app/bot/handlers/continue_get_id.py

This file was deleted.

6 changes: 0 additions & 6 deletions app/bot/handlers/continue_handler.py

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from bot.models import AVAILABLE_MODELS, BadMessageError, BotState
from telegram import ReplyKeyboardMarkup
from the_spymaster_solvers_api.structs import Difficulty
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bot.handlers.base import EventHandler
from bot.handlers.common import SUPPORTED_LANGUAGES
from bot.handlers.other.common import SUPPORTED_LANGUAGES
from bot.handlers.other.event_handler import EventHandler
from bot.models import BadMessageError, BotState
from telegram import ReplyKeyboardMarkup
from the_spymaster_util.logger import get_logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bot.handlers.base import EventHandler
from bot.handlers.start import StartEventHandler
from bot.handlers.gameplay.start import StartEventHandler
from bot.handlers.other.event_handler import EventHandler
from bot.models import AVAILABLE_MODELS, BadMessageError
from the_spymaster_solvers_api.structs import ModelIdentifier
from the_spymaster_util.logger import get_logger
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from bot.handlers.base import EventHandler
from bot.handlers.common import title_list
from bot.handlers.start import StartEventHandler
from bot.handlers.gameplay.start import StartEventHandler
from bot.handlers.other.common import title_list
from bot.handlers.other.event_handler import EventHandler
from bot.models import BadMessageError, BotState
from telegram import ReplyKeyboardMarkup
from the_spymaster_solvers_api.structs import Difficulty, Solver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bot.handlers.base import EventHandler
from bot.handlers.common import SUPPORTED_LANGUAGES, title_list
from bot.handlers.other.common import SUPPORTED_LANGUAGES, title_list
from bot.handlers.other.event_handler import EventHandler
from bot.models import BotState, GameConfig, Session
from telegram import ReplyKeyboardMarkup
from the_spymaster_solvers_api.structs import Difficulty
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler


class NextMoveHandler(EventHandler):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from bot.handlers import HelpMessageHandler
from bot.handlers.base import EventHandler
from bot.handlers.common import (
from bot.handlers.other.common import (
get_given_guess_result_message_text,
is_blue_guesser_turn,
)
from bot.handlers.other.event_handler import EventHandler
from bot.handlers.other.help import HelpMessageHandler
from bot.models import COMMAND_TO_INDEX
from codenames.game.board import Board
from the_spymaster_api.structs import GuessRequest, GuessResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from bot.models import GameConfig, Session
from the_spymaster_api.structs import StartGameRequest
from the_spymaster_util.logger import get_logger
Expand Down
15 changes: 0 additions & 15 deletions app/bot/handlers/get_session.py

This file was deleted.

Empty file.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from bot.models import BotState
from the_spymaster_util.logger import get_logger

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import requests
from bot.config import get_config
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from bot.models import AVAILABLE_MODELS
from the_spymaster_solvers_api.structs import LoadModelsRequest, LoadModelsResponse

Expand Down
Empty file.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from the_spymaster_util.logger import get_logger

log = get_logger(__name__)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import sentry_sdk
from beautifultable import BeautifulTable
from bot.handlers.common import (
from bot.handlers.other.common import (
enrich_sentry_context,
get_given_guess_result_message_text,
is_blue_guesser_turn,
Expand All @@ -16,6 +16,7 @@
BadMessageError,
BotState,
GameConfig,
ParsingState,
Session,
)
from codenames.game.card import Card
Expand Down Expand Up @@ -100,6 +101,12 @@ def config(self) -> Optional[GameConfig]:
return None
return self.session.config

@property
def parsing_state(self) -> Optional[ParsingState]:
if not self.session:
return None
return self.session.parsing_state

@classmethod
def generate_callback(cls, bot: "TheSpymasterBot") -> Callable[[Update, CallbackContext], Any]:
def callback(update: Update, context: CallbackContext) -> Any:
Expand Down Expand Up @@ -146,6 +153,13 @@ def update_game_config(self, **kwargs) -> Session:
new_config = old_config.copy(update=kwargs)
return self.update_session(config=new_config)

def update_parsing_state(self, **kwargs) -> Session:
old_parsing_state = self.parsing_state
if not old_parsing_state:
raise NoneValueError("parsing state is not set, cannot update parsing state.")
new_parsing_state = old_parsing_state.copy(update=kwargs)
return self.update_session(parsing_state=new_parsing_state)

def handle(self):
raise NotImplementedError

Expand Down Expand Up @@ -176,7 +190,7 @@ def fast_forward(self, state: GameState):
self.send_game_summary(state=state)
log.update_context(game_id=None)
self.update_session(game_id=None)
from bot.handlers import HelpMessageHandler
from bot.handlers.other.help import HelpMessageHandler

self.trigger(HelpMessageHandler)
return None
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler


class FallbackHandler(EventHandler):
Expand Down
5 changes: 2 additions & 3 deletions app/bot/handlers/help.py → app/bot/handlers/other/help.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from bot.handlers.base import EventHandler
from bot.handlers.other.event_handler import EventHandler
from the_spymaster_util.logger import get_logger

log = get_logger(__name__)
Expand All @@ -11,9 +11,8 @@ def handle(self):
/start - start a new game.
/custom - start a new game with custom configurations.
/help - show this message.
Under development: 👨🏻‍💻
In development: 👨🏻‍💻
/parse - get help with your camera.
/continue - continue an old game.
How to play:
You are the blue guesser. The bot will play all other roles. \
Expand Down
12 changes: 8 additions & 4 deletions app/bot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ class BadMessageError(Exception):


class BotState(IntEnum):
ENTRY = 0
PLAYING = 30
# Config
CONFIG_LANGUAGE = 10
CONFIG_SOLVER = 11
CONFIG_DIFFICULTY = 12
CONFIG_MODEL = 13
# Parsing
PARSE_LANGUAGE = 40
PARSE_MAP = 41
PARSE_BOARD = 42
PARSE_FIXES = 43
# Other
CONTINUE_GET_ID = 20
PLAYING = 30
PARSE_MAP = 40
PARSE_BOARD = 41


class GameConfig(BaseModel): # Move to backend api?
Expand Down
61 changes: 28 additions & 33 deletions app/bot/the_spymaster_bot.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
from typing import Any, Callable, Dict, Optional, Type

from bot.handlers import (
ConfigDifficultyHandler,
ConfigLanguageHandler,
ConfigModelHandler,
ConfigSolverHandler,
ContinueGetIdHandler,
ContinueHandler,
CustomHandler,
ErrorHandler,
EventHandler,
FallbackHandler,
GetSessionsHandler,
HelpMessageHandler,
NextMoveHandler,
ProcessMessageHandler,
StartEventHandler,
TestingHandler,
WarmupHandler,
)
from bot.handlers.parse_handler import ParseBoardHandler, ParseHandler, ParseMapHandler
from bot.handlers.warmup import handle_warmup
from bot.handlers.custom.config_difficulty import ConfigDifficultyHandler
from bot.handlers.custom.config_language import ConfigLanguageHandler
from bot.handlers.custom.config_model import ConfigModelHandler
from bot.handlers.custom.config_solvers import ConfigSolverHandler
from bot.handlers.custom.custom import CustomHandler
from bot.handlers.gameplay.next_move import NextMoveHandler
from bot.handlers.gameplay.process_message import ProcessMessageHandler
from bot.handlers.gameplay.start import StartEventHandler
from bot.handlers.internal.testing import TestingHandler
from bot.handlers.internal.warmup import WarmupHandler, handle_warmup
from bot.handlers.other.error import ErrorHandler
from bot.handlers.other.event_handler import EventHandler
from bot.handlers.other.fallback import FallbackHandler
from bot.handlers.other.help import HelpMessageHandler
from bot.handlers.parse.parse_board_handler import ParseBoardHandler
from bot.handlers.parse.parse_handler import ParseHandler
from bot.handlers.parse.parse_language_handler import ParseLanguageHandler
from bot.handlers.parse.parse_map_handler import ParseMapHandler
from bot.models import BotState
from persistence.dynamo_db_persistence import DynamoDbPersistence
from telegram import Update
Expand Down Expand Up @@ -86,6 +83,7 @@ def _construct_updater(self):
next_move_handler = CommandHandler("next_move", self.generate_callback(NextMoveHandler))
# Parsing
parse_handler = CommandHandler("parse", self.generate_callback(ParseHandler))
parse_language_handler = MessageHandler(Filters.text, self.generate_callback(ParseLanguageHandler))
parse_map_handler = MessageHandler(Filters.photo, self.generate_callback(ParseMapHandler))
parse_board_handler = MessageHandler(Filters.photo, self.generate_callback(ParseBoardHandler))
# Util
Expand All @@ -95,10 +93,6 @@ def _construct_updater(self):
# Internal
load_models_handler = CommandHandler("warmup", self.generate_callback(WarmupHandler))
testing_handler = CommandHandler("test", self.generate_callback(TestingHandler))
# Not supported
continue_game_handler = CommandHandler("continue", self.generate_callback(ContinueHandler))
continue_get_id_handler = MessageHandler(Filters.text, self.generate_callback(ContinueGetIdHandler))
get_sessions_handler = CommandHandler("sessions", self.generate_callback(GetSessionsHandler))

conv_handler = ConversationHandler(
name="main",
Expand All @@ -109,19 +103,20 @@ def _construct_updater(self):
next_move_handler,
load_models_handler,
testing_handler,
get_sessions_handler,
continue_game_handler,
parse_handler,
],
states={
# Custom
BotState.CONFIG_LANGUAGE: [config_language_handler],
BotState.CONFIG_SOLVER: [config_solver_handler, fallback_handler],
BotState.CONFIG_DIFFICULTY: [config_difficulty_handler, fallback_handler],
BotState.CONFIG_MODEL: [config_model_handler, fallback_handler],
BotState.CONTINUE_GET_ID: [continue_get_id_handler],
BotState.CONFIG_SOLVER: [config_solver_handler],
BotState.CONFIG_DIFFICULTY: [config_difficulty_handler],
BotState.CONFIG_MODEL: [config_model_handler],
# Game
BotState.PLAYING: [process_message_handler],
BotState.PARSE_MAP: [parse_map_handler, fallback_handler],
BotState.PARSE_BOARD: [parse_board_handler, fallback_handler],
# Parse
BotState.PARSE_LANGUAGE: [parse_language_handler],
BotState.PARSE_MAP: [parse_map_handler],
BotState.PARSE_BOARD: [parse_board_handler],
},
fallbacks=[fallback_handler],
allow_reentry=True,
Expand Down

0 comments on commit a515a5a

Please sign in to comment.