From eed5ea78a44360b4211aa81aedea060177d239a8 Mon Sep 17 00:00:00 2001 From: Griatch Date: Fri, 14 Jul 2023 13:39:04 +0200 Subject: [PATCH] Make evadventure get_sides more consistent. Resolve #3199 --- CHANGELOG.md | 2 ++ .../Part3/Beginner-Tutorial-Combat-Base.md | 2 -- .../Beginner-Tutorial-Combat-Turnbased.md | 6 ++-- .../Part3/Beginner-Tutorial-Combat-Twitch.md | 7 ++-- .../tutorials/evadventure/combat_base.py | 2 -- .../tutorials/evadventure/combat_turnbased.py | 32 ++----------------- .../tutorials/evadventure/combat_twitch.py | 5 ++- .../tutorials/evadventure/equipment.py | 4 +-- .../evadventure/tests/test_combat.py | 12 +++---- 9 files changed, 20 insertions(+), 52 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 35e6efad4f..ace124344a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Main +- Fix: Clean up `get_sides` function in evadventure tutorial to return also + the calling combatant with its `allies` return, to make it easier to reason around. - Feature: Add `SSL_CERTIFICATE_ISSUERS` setting for customizing Telnet+SSL. - Contrib: Refactored `dice.roll` contrib function to use `safe_eval`. Can now optionally be used as `dice.roll("2d10 + 4 > 10")`. Old way works too. diff --git a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Base.md b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Base.md index 45f34b2787..12d15ff8c9 100644 --- a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Base.md +++ b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Base.md @@ -332,8 +332,6 @@ class EvAdventureCombatBaseHandler(DefaultScript): def get_combat_summary(self, combatant): allies, enemies = self.get_sides(combatant) - # we must include outselves at the top of the list (we are not returned from get_sides) - allies.insert(0, combatant) nallies, nenemies = len(allies), len(enemies) # prepare colors and hurt-levels diff --git a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Turnbased.md b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Turnbased.md index 92d37996c6..b4af5ab22c 100644 --- a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Turnbased.md +++ b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Turnbased.md @@ -176,11 +176,11 @@ class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler): npcs = [comb for comb in self.combatants if comb not in pcs] if combatant in pcs: # combatant is a PC, so NPCs are all enemies - allies = [comb for comb in pcs if comb != combatant] + allies = pcs enemies = npcs else: # combatant is an NPC, so PCs are all enemies - allies = [comb for comb in npcs if comb != combatant] + allies = npcs enemies = pcs return allies, enemies ``` @@ -897,7 +897,7 @@ def node_choose_allied_recipient(caller, raw_string, **kwargs): - Finally we merge this with the existing `kwargs` dict. The result is a new dict that now has the updated `"action_dict"` key pointing to an action-dict where `target` is set. - **Line 23**: We extend the `options` list with the default wizard options (`back`, `abort`). Since we made a helper function for this, this is only one line. -Creating the three other needed nodes `node_choose_enemy_recipient`, `node_choose_allied_target` and `node_choose_allied_recipient` are following the same pattern; they just use either the `allies` or `enemies` return from `combathandler.get_sides()` (for the `allies`, don't forget to add `caller` so you can target yourself!). It then sets either the `target` or `recipient` field in the `action_dict`. We leave these up to the reader to implement. +Creating the three other needed nodes `node_choose_enemy_recipient`, `node_choose_allied_target` and `node_choose_allied_recipient` are following the same pattern; they just use either the `allies` or `enemies` return from `combathandler.get_sides(). It then sets either the `target` or `recipient` field in the `action_dict`. We leave these up to the reader to implement. ### Choose an Ability diff --git a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Twitch.md b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Twitch.md index 515bedaa27..a082eb8d85 100644 --- a/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Twitch.md +++ b/docs/source/Howtos/Beginner-Tutorial/Part3/Beginner-Tutorial-Combat-Twitch.md @@ -152,11 +152,11 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler): npcs = [comb for comb in combatants if comb not in pcs] if combatant in pcs: # combatant is a PC, so NPCs are all enemies - allies = [comb for comb in pcs if comb != combatant] + allies = pcs enemies = npcs else: # combatant is an NPC, so PCs are all enemies - allies = [comb for comb in npcs if comb != combatant] + allies = npcs enemies = pcs return allies, enemies @@ -357,7 +357,6 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler): """ allies, enemies = self.get_sides(self.obj) - allies.append(self.obj) location = self.obj.location @@ -382,7 +381,7 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler): We must make sure to check if combat is over. -- **Line 12**: With our `.get_sides()` method we can easily get the two sides of the conflict. Note that `combatant` is not included among the allies, so we need to add it back in on the following line. +- **Line 12**: With our `.get_sides()` method we can easily get the two sides of the conflict. - **Lines 18, 19**: We get everyone still alive _and still in the same room_. The latter condition is important in case we move away from the battle - you can't hit your enemy from another room. In the `stop_method` we'll need to do a bunch of cleanup. We'll hold off on implementing this until we have the Commands written out. Read on. diff --git a/evennia/contrib/tutorials/evadventure/combat_base.py b/evennia/contrib/tutorials/evadventure/combat_base.py index 1c26013237..3182026e1a 100644 --- a/evennia/contrib/tutorials/evadventure/combat_base.py +++ b/evennia/contrib/tutorials/evadventure/combat_base.py @@ -356,8 +356,6 @@ class EvAdventureCombatBaseHandler(DefaultScript): """ allies, enemies = self.get_sides(combatant) - # we must include outselves at the top of the list (we are not returned from get_sides) - allies.insert(0, combatant) nallies, nenemies = len(allies), len(enemies) # prepare colors and hurt-levels diff --git a/evennia/contrib/tutorials/evadventure/combat_turnbased.py b/evennia/contrib/tutorials/evadventure/combat_turnbased.py index e3c646a728..14b7f0bb26 100644 --- a/evennia/contrib/tutorials/evadventure/combat_turnbased.py +++ b/evennia/contrib/tutorials/evadventure/combat_turnbased.py @@ -250,11 +250,11 @@ class EvAdventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler): npcs = [comb for comb in self.combatants if comb not in pcs] if combatant in pcs: # combatant is a PC, so NPCs are all enemies - allies = [comb for comb in pcs if comb != combatant] + allies = pcs enemies = npcs else: # combatant is an NPC, so PCs are all enemies - allies = [comb for comb in npcs if comb != combatant] + allies = npcs enemies = pcs return allies, enemies @@ -345,7 +345,7 @@ class EvAdventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler): surviving_combatant = None allies, enemies = (), () else: - # grab a random survivor and check of they have any living enemies. + # grab a random survivor and check if they have any living enemies. surviving_combatant = random.choice(list(self.combatants.keys())) allies, enemies = self.get_sides(surviving_combatant) @@ -537,19 +537,6 @@ def node_choose_allied_target(caller, raw_string, **kwargs): combathandler = _get_combathandler(caller) allies, _ = combathandler.get_sides(caller) - # can choose yourself - options = [ - { - "desc": "Yourself", - "goto": ( - _step_wizard, - { - **kwargs, - **{"action_dict": {**action_dict, **{"target": caller}}}, - }, - ), - } - ] options.extend( [ { @@ -579,19 +566,6 @@ def node_choose_allied_recipient(caller, raw_string, **kwargs): combathandler = _get_combathandler(caller) allies, _ = combathandler.get_sides(caller) - # can choose yourself - options = [ - { - "desc": "Yourself", - "goto": ( - _step_wizard, - { - **kwargs, - **{"action_dict": {**action_dict, **{"recipient": caller}}}, - }, - ), - } - ] options.extend( [ { diff --git a/evennia/contrib/tutorials/evadventure/combat_twitch.py b/evennia/contrib/tutorials/evadventure/combat_twitch.py index a23fa0871e..809926f0d1 100644 --- a/evennia/contrib/tutorials/evadventure/combat_twitch.py +++ b/evennia/contrib/tutorials/evadventure/combat_twitch.py @@ -106,11 +106,11 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler): npcs = [comb for comb in combatants if comb not in pcs] if combatant in pcs: # combatant is a PC, so NPCs are all enemies - allies = [comb for comb in pcs if comb != combatant] + allies = pcs enemies = npcs else: # combatant is an NPC, so PCs are all enemies - allies = [comb for comb in npcs if comb != combatant] + allies = npcs enemies = pcs return allies, enemies @@ -216,7 +216,6 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler): """ allies, enemies = self.get_sides(self.obj) - allies.append(self.obj) location = self.obj.location diff --git a/evennia/contrib/tutorials/evadventure/equipment.py b/evennia/contrib/tutorials/evadventure/equipment.py index f8dc99e344..16f08a9993 100644 --- a/evennia/contrib/tutorials/evadventure/equipment.py +++ b/evennia/contrib/tutorials/evadventure/equipment.py @@ -93,9 +93,6 @@ class EquipmentHandler: Args: obj (EvAdventureObject): The object to add. - Raise: - EquipmentError: If there's not enough room. - """ if not inherits_from(obj, EvAdventureObject): raise EquipmentError(f"{obj.key} is not something that can be equipped.") @@ -103,6 +100,7 @@ class EquipmentHandler: size = obj.size max_slots = self.max_slots current_slot_usage = self.count_slots() + if current_slot_usage + size > max_slots: slots_left = max_slots - current_slot_usage raise EquipmentError( diff --git a/evennia/contrib/tutorials/evadventure/tests/test_combat.py b/evennia/contrib/tutorials/evadventure/tests/test_combat.py index ac6042d8a8..76c3ed0a46 100644 --- a/evennia/contrib/tutorials/evadventure/tests/test_combat.py +++ b/evennia/contrib/tutorials/evadventure/tests/test_combat.py @@ -81,7 +81,7 @@ class TestEvAdventureCombatBaseHandler(_CombatTestBase): def test_get_combat_summary(self): """Test combat summary""" - self.combathandler.get_sides = Mock(return_value=([], [self.target])) + self.combathandler.get_sides = Mock(return_value=([self.combatant], [self.target])) # as seen from one side result = str(self.combathandler.get_combat_summary(self.combatant)) @@ -92,7 +92,7 @@ class TestEvAdventureCombatBaseHandler(_CombatTestBase): ) # as seen from other side - self.combathandler.get_sides = Mock(return_value=([], [self.combatant])) + self.combathandler.get_sides = Mock(return_value=([self.target], [self.combatant])) result = str(self.combathandler.get_combat_summary(self.target)) self.assertEqual( @@ -383,11 +383,11 @@ class EvAdventureTurnbasedCombatHandlerTest(_CombatTestBase): # allies to combatant allies, enemies = self.combathandler.get_sides(self.combatant) - self.assertEqual((allies, enemies), ([combatant2], [self.target, target2])) + self.assertEqual((allies, enemies), ([self.combatant, combatant2], [self.target, target2])) # allies to monster allies, enemies = self.combathandler.get_sides(self.target) - self.assertEqual((allies, enemies), ([target2], [self.combatant, combatant2])) + self.assertEqual((allies, enemies), ([self.target, target2], [self.combatant, combatant2])) def test_queue_and_execute_action(self): """Queue actions and execute""" @@ -551,7 +551,7 @@ class TestEvAdventureTwitchCombatHandler(EvenniaCommandTestMixin, _CombatTestBas def test_get_sides(self): sides = self.combatant_combathandler.get_sides(self.combatant) - self.assertEqual(sides, ([], [self.target])) + self.assertEqual(sides, ([self.combatant], [self.target])) def test_give_advantage(self): self.combatant_combathandler.give_advantage(self.combatant, self.target) @@ -612,7 +612,7 @@ class TestEvAdventureTwitchCombatHandler(EvenniaCommandTestMixin, _CombatTestBas # only one side wiped out self.combatant.hp = 10 self.target.hp = -1 - self.combatant_combathandler.get_sides = Mock(return_value=([], [])) + self.combatant_combathandler.get_sides = Mock(return_value=([self.combatant], [])) self.combatant_combathandler.check_stop_combat() self.combatant.msg.assert_called_with( text=("The combat is over.", {}), from_obj=self.combatant