Skip to content

Commit

Permalink
#106 few refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 28, 2024
1 parent 8a7bec7 commit f3a701a
Show file tree
Hide file tree
Showing 12 changed files with 51 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from .interface import WinCondition, Action, State, Decision, DecisionType, StateHolder
from .decisions import NotImplementedActionDecision, Decisions
from .keys import (
SHKey,
BonusKey,
KeyDecision,
KeyWinCondition,
TypedKeyAction,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dataclasses import dataclass
from typing import Literal, Sequence, overload

from shvatka.core.models.dto.scn.action.interface import DecisionType, Decision
from shvatka.core.models.dto.action.interface import DecisionType, Decision


@dataclass
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,24 @@
from . import StateHolder
from .decisions import NotImplementedActionDecision
from .interface import Action, State, Decision, WinCondition, DecisionType
from shvatka.core.models.dto.scn import BonusKey

SHKey: typing.TypeAlias = str


@dataclass(frozen=True)
class BonusKey:
text: str
bonus_minutes: float

def __eq__(self, other: object) -> bool:
if not isinstance(other, BonusKey):
return NotImplemented
return self.text == other.text

def __hash__(self) -> int:
return hash(self.text)


@dataclass
class TypedKeyAction(Action):
key: SHKey
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
from dataclasses import dataclass

from . import TypedKeysState
from . import TypedKeysState, SHKey
from .interface import StateHolder, T
from shvatka.core.models.dto import scn


@dataclass
class InMemoryStateHolder(StateHolder):
typed_correct: set[scn.SHKey]
all_typed: set[scn.SHKey]
typed_correct: set[SHKey]
all_typed: set[SHKey]

def get(self, state_class: type[T]) -> T:
if isinstance(state_class, TypedKeysState):
Expand Down
3 changes: 2 additions & 1 deletion shvatka/core/models/dto/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from datetime import timedelta

from .player import Player
from .scn.level import LevelScenario, BonusKey
from .scn.level import LevelScenario
from .action.keys import BonusKey
from .scn.time_hint import TimeHint, EnumeratedTimeHint


Expand Down
2 changes: 1 addition & 1 deletion shvatka/core/models/dto/scn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
PhotoHint,
ContactHint,
)
from .level import LevelScenario, SHKey, BonusKey, HintsList, Conditions
from .level import LevelScenario, HintsList, Conditions
from .parsed_zip import ParsedZip
from .time_hint import TimeHint
24 changes: 8 additions & 16 deletions shvatka/core/models/dto/scn/level.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from shvatka.core.utils import exceptions
from .hint_part import AnyHint
from .time_hint import TimeHint, EnumeratedTimeHint
from .action import (
from shvatka.core.models.dto.action import (
WinCondition,
Action,
Decision,
Expand All @@ -20,25 +20,17 @@
KeyBonusCondition,
NotImplementedActionDecision,
)
from .action.keys import SHKey, KeyWinCondition, TypedKeyAction, WrongKeyDecision
from shvatka.core.models.dto.action.keys import (
SHKey,
KeyWinCondition,
TypedKeyAction,
WrongKeyDecision,
BonusKey,
)

logger = logging.getLogger(__name__)


@dataclass(frozen=True)
class BonusKey:
text: str
bonus_minutes: float

def __eq__(self, other: object) -> bool:
if not isinstance(other, BonusKey):
return NotImplemented
return self.text == other.text

def __hash__(self) -> int:
return hash(self.text)


class HintsList(Sequence[TimeHint]):
def __init__(self, hints: list[TimeHint]):
self.verify(hints)
Expand Down
4 changes: 2 additions & 2 deletions shvatka/core/models/dto/time_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
from datetime import datetime

from shvatka.core.models import dto, enums
from . import scn
from . import action


@dataclass(frozen=True)
class KeyTime:
text: scn.SHKey
text: action.SHKey
type_: enums.KeyType
is_duplicate: bool
at: datetime
Expand Down
2 changes: 1 addition & 1 deletion shvatka/core/services/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from shvatka.core.interfaces.dal.game_play import GamePlayerDao
from shvatka.core.models import dto
from shvatka.core.models.dto.scn import action
from shvatka.core.models.dto import action
from shvatka.core.utils import exceptions
from shvatka.core.utils.input_validation import is_key_valid
from shvatka.core.utils.key_checker_lock import KeyCheckerFactory
Expand Down
4 changes: 2 additions & 2 deletions shvatka/tgbot/dialogs/level_scn/getters.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from adaptix import Retort
from aiogram_dialog import DialogManager

from shvatka.core.models.dto import scn
import shvatka.core.models.dto.action.keys
from shvatka.core.models.dto.scn import TimeHint


Expand All @@ -24,7 +24,7 @@ async def get_bonus_keys(dialog_manager: DialogManager, **_):
"bonus_keys", dialog_manager.start_data.get("bonus_keys", [])
)
return {
"bonus_keys": retort.load(keys_raw, list[scn.BonusKey]),
"bonus_keys": retort.load(keys_raw, list[shvatka.core.models.dto.action.keys.BonusKey]),
}


Expand Down
20 changes: 15 additions & 5 deletions shvatka/tgbot/dialogs/level_scn/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from aiogram_dialog import Data, DialogManager
from aiogram_dialog.widgets.kbd import Button

import shvatka.core.models.dto.action.keys
from shvatka.core.models import dto
from shvatka.core.models.dto import scn
from shvatka.core.services.level import upsert_level, get_by_id
Expand Down Expand Up @@ -69,7 +70,7 @@ async def on_correct_keys(m: Message, dialog_: Any, manager: DialogManager, keys
await manager.done({"keys": keys})


def convert_bonus_keys(text: str) -> list[scn.BonusKey]:
def convert_bonus_keys(text: str) -> list[shvatka.core.models.dto.action.keys.BonusKey]:
result = []
for key_str in text.splitlines():
key, bonus = key_str.split(maxsplit=1)
Expand All @@ -78,7 +79,9 @@ def convert_bonus_keys(text: str) -> list[scn.BonusKey]:
parsed_bonus = float(bonus)
if not (-600 < parsed_bonus < 60):
raise ValueError("bonus out of available range")
parsed_key = scn.BonusKey(text=key, bonus_minutes=parsed_bonus)
parsed_key = shvatka.core.models.dto.action.keys.BonusKey(
text=key, bonus_minutes=parsed_bonus
)
result.append(parsed_key)
return result

Expand All @@ -94,7 +97,10 @@ async def not_correct_bonus_keys(


async def on_correct_bonus_keys(
m: Message, dialog_: Any, manager: DialogManager, keys: list[scn.BonusKey]
m: Message,
dialog_: Any,
manager: DialogManager,
keys: list[shvatka.core.models.dto.action.keys.BonusKey],
):
retort: Retort = manager.middleware_data["retort"]
await manager.done({"bonus_keys": retort.dump(keys)})
Expand Down Expand Up @@ -139,7 +145,9 @@ async def on_start_level_edit(start_data: dict[str, Any], manager: DialogManager
manager.dialog_data["level_id"] = level.name_id
manager.dialog_data["keys"] = list(level.get_keys())
manager.dialog_data["time_hints"] = retort.dump(level.scenario.time_hints)
manager.dialog_data["bonus_keys"] = retort.dump(level.get_bonus_keys(), set[scn.BonusKey])
manager.dialog_data["bonus_keys"] = retort.dump(
level.get_bonus_keys(), set[shvatka.core.models.dto.action.keys.BonusKey]
)


async def on_start_hints_edit(start_data: dict[str, Any], manager: DialogManager):
Expand Down Expand Up @@ -210,7 +218,9 @@ async def save_level(c: CallbackQuery, button: Button, manager: DialogManager):
id_ = data["level_id"]
keys = set(map(normalize_key, data["keys"]))
time_hints = retort.load(data["time_hints"], list[scn.TimeHint])
bonus_keys = retort.load(data.get("bonus_keys", []), set[scn.BonusKey])
bonus_keys = retort.load(
data.get("bonus_keys", []), set[shvatka.core.models.dto.action.keys.BonusKey]
)

level_scn = scn.LevelScenario(id=id_, keys=keys, time_hints=time_hints, bonus_keys=bonus_keys)
level = await upsert_level(author=author, scenario=level_scn, dao=dao.level)
Expand Down

0 comments on commit f3a701a

Please sign in to comment.