Skip to content

Commit

Permalink
#106 fix for old constructor call
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 7, 2024
1 parent e5d75d6 commit 2aa55d1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 16 deletions.
8 changes: 4 additions & 4 deletions shvatka/common/data_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
name_id="level_100",
game_id=10,
number_in_game=0,
scenario=scn.LevelScenario(
scenario=scn.LevelScenario.legacy_factory(
id="level_100",
keys={"SH1"},
time_hints=scn.HintsList(
Expand Down Expand Up @@ -102,7 +102,7 @@
name_id="level_101",
game_id=10,
number_in_game=1,
scenario=scn.LevelScenario(
scenario=scn.LevelScenario.legacy_factory(
id="level_101",
keys={"SH2"},
time_hints=scn.HintsList(
Expand Down Expand Up @@ -165,7 +165,7 @@
name_id="level_102",
game_id=10,
number_in_game=0,
scenario=scn.LevelScenario(
scenario=scn.LevelScenario.legacy_factory(
id="level_102",
keys={"SH3"},
time_hints=scn.HintsList(
Expand Down Expand Up @@ -228,7 +228,7 @@
name_id="level_103",
game_id=10,
number_in_game=0,
scenario=scn.LevelScenario(
scenario=scn.LevelScenario.legacy_factory(
id="level_103",
keys={"SH4"},
time_hints=scn.HintsList(
Expand Down
2 changes: 1 addition & 1 deletion shvatka/core/models/dto/action/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .interface import WinCondition, Action, State, Decision, DecisionType, StateHolder
from .interface import Condition, Action, State, Decision, DecisionType, StateHolder
from .decisions import NotImplementedActionDecision, Decisions
from .keys import (
SHKey,
Expand Down
2 changes: 1 addition & 1 deletion shvatka/core/models/dto/action/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Protocol


class WinCondition(Protocol):
class Condition(Protocol):
def check(self, action: Action, state_holder: StateHolder) -> Decision:
raise NotImplementedError

Expand Down
6 changes: 3 additions & 3 deletions shvatka/core/models/dto/action/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from shvatka.core.models import enums
from . import StateHolder
from .decisions import NotImplementedActionDecision
from .interface import Action, State, Decision, WinCondition, DecisionType
from .interface import Action, State, Decision, Condition, DecisionType

SHKey: typing.TypeAlias = str

Expand Down Expand Up @@ -66,7 +66,7 @@ def key_text(self) -> str:


@dataclass
class KeyWinCondition(WinCondition):
class KeyWinCondition(Condition):
keys: set[SHKey]

def check(self, action: Action, state_holder: StateHolder) -> Decision:
Expand Down Expand Up @@ -110,7 +110,7 @@ def key_text(self) -> str:


@dataclass
class KeyBonusCondition(WinCondition):
class KeyBonusCondition(Condition):
keys: set[BonusKey]

def check(self, action: Action, state_holder: StateHolder) -> Decision:
Expand Down
29 changes: 23 additions & 6 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 .hint_part import AnyHint
from .time_hint import TimeHint, EnumeratedTimeHint
from shvatka.core.models.dto.action import (
WinCondition,
Condition,
Action,
Decision,
StateHolder,
Expand Down Expand Up @@ -133,13 +133,13 @@ def __repr__(self):
return repr(self.hints)


class Conditions(Sequence[WinCondition]):
def __init__(self, conditions: Sequence[WinCondition]):
class Conditions(Sequence[Condition]):
def __init__(self, conditions: Sequence[Condition]):
self.validate(conditions)
self.conditions = conditions

@staticmethod
def validate(conditions: Sequence[WinCondition]) -> None:
def validate(conditions: Sequence[Condition]) -> None:
keys: set[str] = set()
win_conditions = []
for c in conditions:
Expand Down Expand Up @@ -174,11 +174,11 @@ def get_bonus_keys(self) -> set[BonusKey]:
return result

@overload
def __getitem__(self, index: int) -> WinCondition:
def __getitem__(self, index: int) -> Condition:
return self.conditions[index]

@overload
def __getitem__(self, index: slice) -> Sequence[WinCondition]:
def __getitem__(self, index: slice) -> Sequence[Condition]:
return self.conditions[index]

def __getitem__(self, index):
Expand Down Expand Up @@ -258,3 +258,20 @@ def hints_count(self) -> int:

def get_hints_for_timedelta(self, delta: timedelta) -> list[TimeHint]:
return self.time_hints.get_hints_for_timedelta(delta)

@classmethod
def legacy_factory(
cls,
id: str, # noqa: A002
time_hints: HintsList,
keys: set[SHKey],
bonus_keys: set[BonusKey] | None = None,
) -> "LevelScenario":
conditions: list[Condition] = [KeyWinCondition(keys)]
if bonus_keys:
conditions.append(KeyBonusCondition(bonus_keys))
return cls(
id=id,
time_hints=time_hints,
conditions=Conditions(conditions),
)
4 changes: 3 additions & 1 deletion shvatka/tgbot/dialogs/level_scn/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ async def save_level(c: CallbackQuery, button: Button, manager: DialogManager):
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_scn = scn.LevelScenario.legacy_factory(
id=id_, keys=keys, time_hints=time_hints, bonus_keys=bonus_keys
)
level = await upsert_level(author=author, scenario=level_scn, dao=dao.level)
await manager.done(result={"level": retort.dump(level)})
await c.answer(text="Уровень успешно сохранён")
Empty file added tests/unit/domain/level_test.py
Empty file.

0 comments on commit 2aa55d1

Please sign in to comment.