Make evadventure get_sides more consistent. Resolve #3199

This commit is contained in:
Griatch 2023-07-14 13:39:04 +02:00
parent 0d8533e61e
commit eed5ea78a4
9 changed files with 20 additions and 52 deletions

View file

@ -2,6 +2,8 @@
## Main ## 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. - Feature: Add `SSL_CERTIFICATE_ISSUERS` setting for customizing Telnet+SSL.
- Contrib: Refactored `dice.roll` contrib function to use `safe_eval`. Can now - 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. optionally be used as `dice.roll("2d10 + 4 > 10")`. Old way works too.

View file

@ -332,8 +332,6 @@ class EvAdventureCombatBaseHandler(DefaultScript):
def get_combat_summary(self, combatant): def get_combat_summary(self, combatant):
allies, enemies = self.get_sides(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) nallies, nenemies = len(allies), len(enemies)
# prepare colors and hurt-levels # prepare colors and hurt-levels

View file

@ -176,11 +176,11 @@ class EvadventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
npcs = [comb for comb in self.combatants if comb not in pcs] npcs = [comb for comb in self.combatants if comb not in pcs]
if combatant in pcs: if combatant in pcs:
# combatant is a PC, so NPCs are all enemies # combatant is a PC, so NPCs are all enemies
allies = [comb for comb in pcs if comb != combatant] allies = pcs
enemies = npcs enemies = npcs
else: else:
# combatant is an NPC, so PCs are all enemies # combatant is an NPC, so PCs are all enemies
allies = [comb for comb in npcs if comb != combatant] allies = npcs
enemies = pcs enemies = pcs
return allies, enemies 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. - 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. - **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 ### Choose an Ability

View file

@ -152,11 +152,11 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler):
npcs = [comb for comb in combatants if comb not in pcs] npcs = [comb for comb in combatants if comb not in pcs]
if combatant in pcs: if combatant in pcs:
# combatant is a PC, so NPCs are all enemies # combatant is a PC, so NPCs are all enemies
allies = [comb for comb in pcs if comb != combatant] allies = pcs
enemies = npcs enemies = npcs
else: else:
# combatant is an NPC, so PCs are all enemies # combatant is an NPC, so PCs are all enemies
allies = [comb for comb in npcs if comb != combatant] allies = npcs
enemies = pcs enemies = pcs
return allies, enemies return allies, enemies
@ -357,7 +357,6 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler):
""" """
allies, enemies = self.get_sides(self.obj) allies, enemies = self.get_sides(self.obj)
allies.append(self.obj)
location = self.obj.location location = self.obj.location
@ -382,7 +381,7 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler):
We must make sure to check if combat is over. 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. - **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. 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.

View file

@ -356,8 +356,6 @@ class EvAdventureCombatBaseHandler(DefaultScript):
""" """
allies, enemies = self.get_sides(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) nallies, nenemies = len(allies), len(enemies)
# prepare colors and hurt-levels # prepare colors and hurt-levels

View file

@ -250,11 +250,11 @@ class EvAdventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
npcs = [comb for comb in self.combatants if comb not in pcs] npcs = [comb for comb in self.combatants if comb not in pcs]
if combatant in pcs: if combatant in pcs:
# combatant is a PC, so NPCs are all enemies # combatant is a PC, so NPCs are all enemies
allies = [comb for comb in pcs if comb != combatant] allies = pcs
enemies = npcs enemies = npcs
else: else:
# combatant is an NPC, so PCs are all enemies # combatant is an NPC, so PCs are all enemies
allies = [comb for comb in npcs if comb != combatant] allies = npcs
enemies = pcs enemies = pcs
return allies, enemies return allies, enemies
@ -345,7 +345,7 @@ class EvAdventureTurnbasedCombatHandler(EvAdventureCombatBaseHandler):
surviving_combatant = None surviving_combatant = None
allies, enemies = (), () allies, enemies = (), ()
else: 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())) surviving_combatant = random.choice(list(self.combatants.keys()))
allies, enemies = self.get_sides(surviving_combatant) allies, enemies = self.get_sides(surviving_combatant)
@ -537,19 +537,6 @@ def node_choose_allied_target(caller, raw_string, **kwargs):
combathandler = _get_combathandler(caller) combathandler = _get_combathandler(caller)
allies, _ = combathandler.get_sides(caller) allies, _ = combathandler.get_sides(caller)
# can choose yourself
options = [
{
"desc": "Yourself",
"goto": (
_step_wizard,
{
**kwargs,
**{"action_dict": {**action_dict, **{"target": caller}}},
},
),
}
]
options.extend( options.extend(
[ [
{ {
@ -579,19 +566,6 @@ def node_choose_allied_recipient(caller, raw_string, **kwargs):
combathandler = _get_combathandler(caller) combathandler = _get_combathandler(caller)
allies, _ = combathandler.get_sides(caller) allies, _ = combathandler.get_sides(caller)
# can choose yourself
options = [
{
"desc": "Yourself",
"goto": (
_step_wizard,
{
**kwargs,
**{"action_dict": {**action_dict, **{"recipient": caller}}},
},
),
}
]
options.extend( options.extend(
[ [
{ {

View file

@ -106,11 +106,11 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler):
npcs = [comb for comb in combatants if comb not in pcs] npcs = [comb for comb in combatants if comb not in pcs]
if combatant in pcs: if combatant in pcs:
# combatant is a PC, so NPCs are all enemies # combatant is a PC, so NPCs are all enemies
allies = [comb for comb in pcs if comb != combatant] allies = pcs
enemies = npcs enemies = npcs
else: else:
# combatant is an NPC, so PCs are all enemies # combatant is an NPC, so PCs are all enemies
allies = [comb for comb in npcs if comb != combatant] allies = npcs
enemies = pcs enemies = pcs
return allies, enemies return allies, enemies
@ -216,7 +216,6 @@ class EvAdventureCombatTwitchHandler(EvAdventureCombatBaseHandler):
""" """
allies, enemies = self.get_sides(self.obj) allies, enemies = self.get_sides(self.obj)
allies.append(self.obj)
location = self.obj.location location = self.obj.location

View file

@ -93,9 +93,6 @@ class EquipmentHandler:
Args: Args:
obj (EvAdventureObject): The object to add. obj (EvAdventureObject): The object to add.
Raise:
EquipmentError: If there's not enough room.
""" """
if not inherits_from(obj, EvAdventureObject): if not inherits_from(obj, EvAdventureObject):
raise EquipmentError(f"{obj.key} is not something that can be equipped.") raise EquipmentError(f"{obj.key} is not something that can be equipped.")
@ -103,6 +100,7 @@ class EquipmentHandler:
size = obj.size size = obj.size
max_slots = self.max_slots max_slots = self.max_slots
current_slot_usage = self.count_slots() current_slot_usage = self.count_slots()
if current_slot_usage + size > max_slots: if current_slot_usage + size > max_slots:
slots_left = max_slots - current_slot_usage slots_left = max_slots - current_slot_usage
raise EquipmentError( raise EquipmentError(

View file

@ -81,7 +81,7 @@ class TestEvAdventureCombatBaseHandler(_CombatTestBase):
def test_get_combat_summary(self): def test_get_combat_summary(self):
"""Test combat summary""" """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 # as seen from one side
result = str(self.combathandler.get_combat_summary(self.combatant)) result = str(self.combathandler.get_combat_summary(self.combatant))
@ -92,7 +92,7 @@ class TestEvAdventureCombatBaseHandler(_CombatTestBase):
) )
# as seen from other side # 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)) result = str(self.combathandler.get_combat_summary(self.target))
self.assertEqual( self.assertEqual(
@ -383,11 +383,11 @@ class EvAdventureTurnbasedCombatHandlerTest(_CombatTestBase):
# allies to combatant # allies to combatant
allies, enemies = self.combathandler.get_sides(self.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 to monster
allies, enemies = self.combathandler.get_sides(self.target) 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): def test_queue_and_execute_action(self):
"""Queue actions and execute""" """Queue actions and execute"""
@ -551,7 +551,7 @@ class TestEvAdventureTwitchCombatHandler(EvenniaCommandTestMixin, _CombatTestBas
def test_get_sides(self): def test_get_sides(self):
sides = self.combatant_combathandler.get_sides(self.combatant) 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): def test_give_advantage(self):
self.combatant_combathandler.give_advantage(self.combatant, self.target) self.combatant_combathandler.give_advantage(self.combatant, self.target)
@ -612,7 +612,7 @@ class TestEvAdventureTwitchCombatHandler(EvenniaCommandTestMixin, _CombatTestBas
# only one side wiped out # only one side wiped out
self.combatant.hp = 10 self.combatant.hp = 10
self.target.hp = -1 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_combathandler.check_stop_combat()
self.combatant.msg.assert_called_with( self.combatant.msg.assert_called_with(
text=("The combat is over.", {}), from_obj=self.combatant text=("The combat is over.", {}), from_obj=self.combatant