Skip to content

Commit db78935

Browse files
committed
condition
1 parent 3245ad9 commit db78935

67 files changed

Lines changed: 614 additions & 478 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/tuxemon/test_event_condition_manager.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: GPL-3.0
22
# Copyright (c) 2014-2026 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
3+
from dataclasses import dataclass
34
from unittest.mock import MagicMock, patch
45

56
import pytest
@@ -18,8 +19,12 @@ def mock_plugin_manager():
1819
@pytest.fixture
1920
def condition_manager(mock_plugin_manager):
2021

22+
@dataclass
2123
class DummyCondition(EventCondition):
2224
name = "char_at"
25+
a: str = ""
26+
b: int = 0
27+
c: str = ""
2328

2429
def test(self, session, condition_data):
2530
return True
@@ -35,6 +40,7 @@ def test_get_condition_found(condition_manager):
3540
mock_cond_data = MagicMock(spec=SpatialCondition)
3641
mock_cond_data.type = "char_at"
3742
mock_cond_data.operator = "is"
43+
mock_cond_data.parameters = []
3844
condition = condition_manager.get_condition(mock_cond_data)
3945
assert condition is not None
4046
assert condition.is_expected is True

tuxemon/event/conditions/afk_state.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass
6+
from typing import ClassVar
67

78
from tuxemon.db import SpatialCondition
89
from tuxemon.event.eventcondition import EventCondition
@@ -24,9 +25,10 @@ class AFKStateCondition(EventCondition):
2425
is afk_state warn:kick
2526
"""
2627

27-
name = "afk_state"
28+
name: ClassVar[str] = "afk_state"
29+
level: str
2830

2931
def test(self, session: Session, condition: SpatialCondition) -> bool:
3032
afk_manager = session.client.afk_manager
31-
levels = condition.parameters[0].split(":")
33+
levels = self.level.split(":")
3234
return any(afk_manager.is_threshold_met(level) for level in levels)

tuxemon/event/conditions/battle_outcome.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from dataclasses import dataclass
7+
from typing import ClassVar
78

89
from tuxemon.db import SpatialCondition
910
from tuxemon.event.eventcondition import EventCondition
@@ -33,22 +34,24 @@ class BattleOutcomeCondition(EventCondition):
3334
Checks if the 'player' has won against 'npc_maple'.
3435
"""
3536

36-
name = "battle_outcome"
37+
name: ClassVar[str] = "battle_outcome"
38+
fighter: str
39+
outcome: str
40+
opponent: str
3741

3842
def test(self, session: Session, condition: SpatialCondition) -> bool:
39-
fighter, outcome, opponent = condition.parameters[:3]
4043

41-
if outcome not in {"won", "lost", "draw"}:
42-
logger.error(f"Invalid outcome '{outcome}'")
44+
if self.outcome not in {"won", "lost", "draw"}:
45+
logger.error(f"Invalid outcome '{self.outcome}'")
4346
return False
4447

45-
character = session.get_npc(fighter)
48+
character = session.get_npc(self.fighter)
4649
if character is None:
47-
logger.error(f"Character '{fighter}' not found")
50+
logger.error(f"Character '{self.fighter}' not found")
4851
return False
4952

5053
if not character.battle_handler:
5154
return False
5255
return character.battle_handler.has_fought_and_outcome(
53-
outcome=outcome, opponent=opponent
56+
outcome=self.outcome, opponent=self.opponent
5457
)

tuxemon/event/conditions/battle_outcome_count.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from dataclasses import dataclass
7+
from typing import ClassVar
78

89
from tuxemon.db import SpatialCondition
910
from tuxemon.event.eventcondition import EventCondition
@@ -47,35 +48,31 @@ class BattleOutcomeCountCondition(EventCondition):
4748
Checks if the 'player' has won at least 2 times against 'npc_maple'.
4849
"""
4950

50-
name = "battle_outcome_count"
51+
name: ClassVar[str] = "battle_outcome_count"
52+
fighter: str
53+
outcome: str
54+
opponent: str
55+
required_count: int
5156

5257
def test(self, session: Session, condition: SpatialCondition) -> bool:
53-
try:
54-
fighter, outcome, opponent, count_str = condition.parameters[:4]
55-
required_count = int(count_str)
56-
except (ValueError, IndexError) as e:
57-
logger.error(
58-
f"Invalid parameters for battle_outcome_count: {condition.parameters}"
59-
)
60-
return False
6158

62-
if outcome not in {"won", "lost", "draw"}:
63-
logger.error(f"Invalid outcome '{outcome}'")
59+
if self.outcome not in {"won", "lost", "draw"}:
60+
logger.error(f"Invalid outcome '{self.outcome}'")
6461
return False
6562

66-
character = session.get_npc(fighter)
63+
character = session.get_npc(self.fighter)
6764
if character is None or not character.battle_handler:
6865
logger.error(
69-
f"Character '{fighter}' not found or has no battle handler"
66+
f"Character '{self.fighter}' not found or has no battle handler"
7067
)
7168
return False
7269

7370
actual_count = sum(
7471
1
7572
for battle in character.battle_handler.get_battles()
76-
if battle.fighter == fighter
77-
and battle.opponent == opponent
78-
and battle.outcome.value == outcome
73+
if battle.fighter == self.fighter
74+
and battle.opponent == self.opponent
75+
and battle.outcome.value == self.outcome
7976
)
8077

81-
return actual_count >= required_count
78+
return actual_count >= self.required_count

tuxemon/event/conditions/bill_exists.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from dataclasses import dataclass
7+
from typing import ClassVar
78

89
from tuxemon.db import SpatialCondition
910
from tuxemon.event.eventcondition import EventCondition
@@ -29,15 +30,16 @@ class BillExistsCondition(EventCondition):
2930
eg. "is bill_exists player,my_bill"
3031
"""
3132

32-
name = "bill_exists"
33+
name: ClassVar[str] = "bill_exists"
34+
character: str
35+
bill_slug: str
3336

3437
def test(self, session: Session, condition: SpatialCondition) -> bool:
35-
character_name, bill_slug = condition.parameters[:2]
36-
character = session.get_npc(character_name)
38+
character = session.get_npc(self.character)
3739
if character is None:
38-
logger.error(f"Character '{character_name}' not found")
40+
logger.error(f"Character '{self.character}' not found")
3941
return False
4042

4143
money_manager = character.money_controller.money_manager
42-
bill = money_manager.get_bill(bill_slug)
44+
bill = money_manager.get_bill(self.bill_slug)
4345
return bill is not None

tuxemon/event/conditions/bill_is.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import logging
66
from dataclasses import dataclass
7+
from typing import ClassVar
78

89
from tuxemon.db import SpatialCondition
910
from tuxemon.event.eventcondition import EventCondition
@@ -35,29 +36,32 @@ class BillIsCondition(EventCondition):
3536
eg. "is bill_is player,bill_slug,equals,name_variable" (name_variable:75)
3637
"""
3738

38-
name = "bill_is"
39+
name: ClassVar[str] = "bill_is"
40+
character: str
41+
bill_slug: str
42+
operator: str
43+
amount: str | int
3944

4045
def test(self, session: Session, condition: SpatialCondition) -> bool:
4146
player = session.player
42-
character_name, _bill, operator, _amount = condition.parameters[:4]
43-
character = session.get_npc(character_name)
47+
character = session.get_npc(self.character)
4448
if character is None:
45-
logger.error(f"Character '{character_name}' not found")
49+
logger.error(f"Character '{self.character}' not found")
4650
return False
4751

4852
money_manager = character.money_controller.money_manager
49-
bill = money_manager.get_bill(_bill)
53+
bill = money_manager.get_bill(self.bill_slug)
5054
if bill is None:
5155
return False
5256

53-
if not _amount.isdigit():
57+
if isinstance(self.amount, str):
5458
amount = 0
55-
if player.game_variables.has(_amount):
56-
amount = int(player.game_variables.get(_amount, 0))
59+
if player.game_variables.has(self.amount):
60+
amount = int(player.game_variables.get(self.amount, 0))
5761
else:
58-
amount = int(_amount)
62+
amount = self.amount
5963

6064
if bill.amount == 0:
6165
return False
6266
else:
63-
return compare(operator, bill.amount, amount)
67+
return compare(self.operator, bill.amount, amount)

tuxemon/event/conditions/button_combo.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright (c) 2014-2026 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
33
from __future__ import annotations
44

5+
from typing import ClassVar
6+
57
from tuxemon.db import SpatialCondition
68
from tuxemon.event.eventcondition import EventCondition
79
from tuxemon.platform.const.intentions import constants
@@ -21,10 +23,11 @@ class ButtonComboCondition(EventCondition):
2123
buttons: A sequence of button/intention keys (E.g. "up:down:interact").
2224
"""
2325

24-
name = "button_combo"
26+
name: ClassVar[str] = "button_combo"
27+
buttons: str
2528

2629
def test(self, session: Session, condition: SpatialCondition) -> bool:
27-
_buttons = condition.parameters[0].split(":")
30+
_buttons = self.buttons.split(":")
2831
ids: list[int] = []
2932
for _button in _buttons:
3033
try:

tuxemon/event/conditions/button_count.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright (c) 2014-2026 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
33
from __future__ import annotations
44

5+
from typing import ClassVar
6+
57
from tuxemon.db import SpatialCondition
68
from tuxemon.event.eventcondition import EventCondition
79
from tuxemon.platform.const.intentions import constants
@@ -26,16 +28,18 @@ class ButtonCountCondition(EventCondition):
2628
amount: The number of times the button was pressed.
2729
"""
2830

29-
name = "button_count"
31+
name: ClassVar[str] = "button_count"
32+
button_id: str
33+
operator: str
34+
amount: int
3035

3136
def test(self, session: Session, condition: SpatialCondition) -> bool:
32-
button_id, operator, amount = condition.parameters[:3]
3337
try:
34-
button = constants[button_id.upper()]
38+
button = constants[self.button_id.upper()]
3539
except KeyError:
3640
raise ValueError("Constant not found")
3741
counter = (
3842
session.client.input_manager.input_history.count_button_clicks()
3943
)
4044
output = counter.get(button, 0)
41-
return compare(operator, output, int(amount))
45+
return compare(self.operator, output, self.amount)

tuxemon/event/conditions/button_held.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# Copyright (c) 2014-2026 William Edwards <shadowapex@gmail.com>, Benjamin Bean <superman2k5@gmail.com>
33
from __future__ import annotations
44

5+
from typing import ClassVar
6+
57
from tuxemon.db import SpatialCondition
68
from tuxemon.event.eventcondition import EventCondition
79
from tuxemon.platform.const.intentions import constants
@@ -22,14 +24,15 @@ class ButtonHeldCondition(EventCondition):
2224
frames: The number of frames the button must be held.
2325
"""
2426

25-
name = "button_held"
27+
name: ClassVar[str] = "button_held"
28+
button_id: str
29+
time_ms: int
2630

2731
def test(self, session: Session, condition: SpatialCondition) -> bool:
28-
button_id, time_ms = condition.parameters[:2]
2932
try:
30-
button = constants[button_id.upper()]
33+
button = constants[self.button_id.upper()]
3134
except KeyError:
3235
raise ValueError("Constant not found")
3336
return session.client.input_manager.input_history.is_button_held(
34-
button, int(time_ms)
37+
button, self.time_ms
3538
)

tuxemon/event/conditions/button_pressed.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from __future__ import annotations
44

55
from dataclasses import dataclass
6+
from typing import ClassVar
67

78
from tuxemon.db import SpatialCondition
89
from tuxemon.event.eventcondition import EventCondition
@@ -24,14 +25,13 @@ class ButtonPressedCondition(EventCondition):
2425
button: A button/intention key (E.g. "INTERACT").
2526
"""
2627

27-
name = "button_pressed"
28+
name: ClassVar[str] = "button_pressed"
29+
button: str
2830

2931
def test(self, session: Session, condition: SpatialCondition) -> bool:
30-
button = str(condition.parameters[0])
31-
3232
try:
33-
button_id = constants[button]
33+
button_id = constants[self.button]
3434
except KeyError:
35-
raise ValueError(f"Cannot support key type: {button}")
35+
raise ValueError(f"Cannot support key type: {self.button}")
3636

3737
return session.client.input_cache.was_button_pressed(button_id)

0 commit comments

Comments
 (0)