Skip to content

Commit

Permalink
#106 fixed retort rules for conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
bomzheg committed Nov 28, 2024
1 parent f9fed16 commit f4c4683
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion shvatka/common/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@

from shvatka.common.url_factory import UrlFactory
from shvatka.core.models.dto import scn
from shvatka.core.models.dto.scn import HintsList, TimeHint
from shvatka.core.models.dto.action import AnyCondition
from shvatka.core.models.dto.scn import HintsList, TimeHint, Conditions
from shvatka.core.models.schems import schemas
from shvatka.core.utils import exceptions
from shvatka.core.utils.input_validation import validate_level_id, is_multiple_keys_normal
Expand All @@ -39,6 +40,10 @@ def create_telegraph(self, bot_config: BotConfig) -> Telegraph:
REQUIRED_GAME_RECIPES = [
loader(HintsList, lambda x: HintsList.parse(x), Chain.LAST),
ABCProxy(HintsList, list[TimeHint]), # internal class, can be broken in next version adaptix
loader(Conditions, lambda x: Conditions(x), Chain.LAST),
ABCProxy(
Conditions, list[AnyCondition]
), # internal class, can be broken in next version adaptix
dumper(set, lambda x: tuple(x)),
]

Expand Down
5 changes: 5 additions & 0 deletions shvatka/core/models/dto/action/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import enum
import typing

from .interface import Condition, Action, State, Decision, DecisionType, StateHolder
from .decisions import NotImplementedActionDecision, Decisions
from .keys import (
Expand All @@ -12,3 +15,5 @@
WrongKeyDecision,
)
from .state_holder import InMemoryStateHolder

AnyCondition: typing.TypeAlias = KeyWinCondition | KeyBonusCondition
7 changes: 7 additions & 0 deletions shvatka/core/models/dto/action/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
from typing import Protocol


class ConditionType(enum.StrEnum):
WIN_KEY = enum.auto()
BONUS_KEY = enum.auto()


class Condition(Protocol):
type: ConditionType

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

Expand Down
4 changes: 3 additions & 1 deletion 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, Condition, DecisionType
from .interface import Action, State, Decision, Condition, DecisionType, ConditionType

SHKey: typing.TypeAlias = str

Expand Down Expand Up @@ -68,6 +68,7 @@ def key_text(self) -> str:
@dataclass
class KeyWinCondition(Condition):
keys: set[SHKey]
type: Literal[ConditionType.WIN_KEY] = ConditionType.WIN_KEY

def check(self, action: Action, state_holder: StateHolder) -> Decision:
if not isinstance(action, TypedKeyAction):
Expand Down Expand Up @@ -112,6 +113,7 @@ def key_text(self) -> str:
@dataclass
class KeyBonusCondition(Condition):
keys: set[BonusKey]
type: Literal[ConditionType.BONUS_KEY] = ConditionType.BONUS_KEY

def check(self, action: Action, state_holder: StateHolder) -> Decision:
if not isinstance(action, TypedKeyAction):
Expand Down

0 comments on commit f4c4683

Please sign in to comment.