From de419d94ed449d0515a896a935dfd686172bfe62 Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 4 Aug 2022 21:08:14 +0200 Subject: [PATCH] Evadventure commands, some tests --- .../contrib/tutorials/evadventure/commands.py | 8 +++-- .../tutorials/evadventure/equipment.py | 33 +++++++++++++------ .../contrib/tutorials/evadventure/objects.py | 9 ++++- .../evadventure/tests/test_commands.py | 30 +++++++++++++++++ 4 files changed, 67 insertions(+), 13 deletions(-) create mode 100644 evennia/contrib/tutorials/evadventure/tests/test_commands.py diff --git a/evennia/contrib/tutorials/evadventure/commands.py b/evennia/contrib/tutorials/evadventure/commands.py index c4fcc82157..3e7f8a28a6 100644 --- a/evennia/contrib/tutorials/evadventure/commands.py +++ b/evennia/contrib/tutorials/evadventure/commands.py @@ -112,7 +112,11 @@ class CmdInventory(EvAdventureCommand): aliases = ("i", "inv") def func(self): - self.caller.msg(self.caller.equipment.display_loadout()) + loadout = self.caller.equipment.display_loadout() + backpack = self.caller.equipment.display_backpack() + slot_usage = self.caller.equipment.display_slot_usage() + + self.caller.msg(f"{loadout}\n{backpack}\nYou use {slot_usage} equipment slots.") class CmdWieldOrWear(EvAdventureCommand): @@ -296,7 +300,7 @@ def node_receive(caller, raw_string, **kwargs): {get_obj_stats(item)} -[Your inventory usage: {caller.equipment.get_slot_usage_string()}] +[Your inventory usage: {caller.equipment.display_slot_usage()}] |wDo you want to accept the given item? Y/[N] """ options = ({"key": "_default", "goto": (_accept_or_reject_gift, kwargs)},) diff --git a/evennia/contrib/tutorials/evadventure/equipment.py b/evennia/contrib/tutorials/evadventure/equipment.py index 04dec2c36a..a0b04e5a11 100644 --- a/evennia/contrib/tutorials/evadventure/equipment.py +++ b/evennia/contrib/tutorials/evadventure/equipment.py @@ -83,16 +83,6 @@ class EquipmentHandler: """ return getattr(self.obj, Ability.CON.value, 1) + 10 - def get_slot_usage_string(self): - """ - Get a slot usage/max string for display. - - Returns: - str: The usage string. - - """ - return f"|b{self.count_slots()}/{self.max_slots}|n" - def validate_slot_usage(self, obj): """ Check if obj can fit in equipment, based on its size. @@ -216,6 +206,29 @@ class EquipmentHandler: return f"{weapon_str}{shield_str}\n{armor_str}{helmet_str}" + def display_backpack(self): + """ + Get a visual representation of the backpack's contents. + + """ + backpack = self.slots[WieldLocation.BACKPACK] + if not backpack: + return "Backpack is empty." + out = [] + for item in backpack: + out.append(f"{item.key} [|b{item.size}|n] slot(s)") + return "\n".join(out) + + def display_slot_usage(self): + """ + Get a slot usage/max string for display. + + Returns: + str: The usage string. + + """ + return f"|b{self.count_slots()}/{self.max_slots}|n" + def move(self, obj): """ Moves item to the place it things it should be in - this makes use of the object's wield diff --git a/evennia/contrib/tutorials/evadventure/objects.py b/evennia/contrib/tutorials/evadventure/objects.py index afd08d181e..2807671c61 100644 --- a/evennia/contrib/tutorials/evadventure/objects.py +++ b/evennia/contrib/tutorials/evadventure/objects.py @@ -18,11 +18,12 @@ rune sword (weapon+quest). """ -from evennia import AttributeProperty, TagProperty +from evennia import AttributeProperty from evennia.objects.objects import DefaultObject from evennia.utils.utils import make_iter from .enums import Ability, ObjType, WieldLocation +from .utils import get_obj_stats class EvAdventureObject(DefaultObject): @@ -44,6 +45,12 @@ class EvAdventureObject(DefaultObject): for obj_type in make_iter(self.obj_type): self.tags.add(obj_type, category="obj_type") + def get_display_header(self, looker, **kwargs): + return "" # this is handled by get_obj_stats + + def get_display_desc(self, looker, **kwargs): + return get_obj_stats(self, owner=looker) + def has_obj_type(self, objtype): """ Check if object is of a particular type. diff --git a/evennia/contrib/tutorials/evadventure/tests/test_commands.py b/evennia/contrib/tutorials/evadventure/tests/test_commands.py new file mode 100644 index 0000000000..807487c022 --- /dev/null +++ b/evennia/contrib/tutorials/evadventure/tests/test_commands.py @@ -0,0 +1,30 @@ +""" +Test the EvAdventure commands. + +""" + +from unittest.mock import MagicMock, patch + +from evennia.utils.test_resources import BaseEvenniaCommandTest + +from .. import commands +from .mixins import EvAdventureMixin + + +class TestEvAdventureCommands(EvAdventureMixin, BaseEvenniaCommandTest): + def setUp(self): + super().setUp() + # needed for the .call mechanism + self.char1 = self.character + + def test_inventory(self): + self.call( + commands.CmdInventory(), + "inventory", + """ +You are fighting with your bare fists and have no shield. +You wear no armor and no helmet. +Backpack is empty. +You use 0/11 equipment slots. +""".strip(), + )