Merge pull request #3633 from count-infinity/3617-Default-object-descriptions

Allow simple customization of default object descriptions
This commit is contained in:
Griatch 2024-12-15 15:32:26 +01:00 committed by GitHub
commit 0004dc5f7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 57 additions and 19 deletions

View file

@ -705,6 +705,7 @@ class CmdCreate(ObjManipCommand):
)
if errors:
self.msg(errors)
if not obj:
continue
@ -714,9 +715,7 @@ class CmdCreate(ObjManipCommand):
)
else:
string = f"You create a new {obj.typename}: {obj.name}."
# set a default desc
if not obj.db.desc:
obj.db.desc = "You see nothing special."
if "drop" in self.switches:
if caller.location:
obj.home = caller.location

View file

@ -43,7 +43,7 @@ class EvAdventureRoomTest(EvenniaTestCase):
/|\
o o o
room_center
You see nothing special.
This is a room.
Exits: north, northeast, east, southeast, south, southwest, west, and northwest"""
result = "\n".join(part.rstrip() for part in strip_ansi(desc).split("\n"))

View file

@ -399,6 +399,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
objects = ObjectManager()
# Used by get_display_desc when self.db.desc is None
default_description = _("You see nothing special.")
# populated by `return_appearance`
appearance_template = """
{header}
@ -1468,10 +1471,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if account:
obj.db.creator_id = account.id
# Set description if there is none, or update it if provided
if description or not obj.db.desc:
desc = description if description else "You see nothing special."
obj.db.desc = desc
# Set description if provided
if description:
obj.db.desc = description
except Exception as e:
errors.append(f"An error occurred while creating this '{key}' object: {e}")
@ -1749,7 +1751,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
str: The desc display string.
"""
return self.db.desc or "You see nothing special."
return self.db.desc or self.default_description
def get_display_exits(self, looker, **kwargs):
"""
@ -3020,6 +3022,9 @@ class DefaultCharacter(DefaultObject):
"edit:pid({account_id}) or perm(Admin)"
)
# Used by get_display_desc when self.db.desc is None
default_description = _("This is a character.")
@classmethod
def get_default_lockstring(
cls, account: "DefaultAccount" = None, caller: "DefaultObject" = None, **kwargs
@ -3133,9 +3138,9 @@ class DefaultCharacter(DefaultObject):
if locks:
obj.locks.add(locks)
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else _("This is a character.")
# Set description if provided
if description:
obj.db.desc = description
except Exception as e:
errors.append(f"An error occurred while creating object '{key} object: {e}")
@ -3346,6 +3351,9 @@ class DefaultRoom(DefaultObject):
# Generally, a room isn't expected to HAVE a location, but maybe in some games?
_content_types = ("room",)
# Used by get_display_desc when self.db.desc is None
default_description = _("This is a room.")
@classmethod
def create(
cls,
@ -3416,9 +3424,9 @@ class DefaultRoom(DefaultObject):
if account:
obj.db.creator_id = account.id
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else _("This is a room.")
# Set description if provided
if description:
obj.db.desc = description
except Exception as e:
errors.append(f"An error occurred while creating this '{key}' object: {e}")
@ -3511,6 +3519,9 @@ class DefaultExit(DefaultObject):
exit_command = ExitCommand
priority = 101
# Used by get_display_desc when self.db.desc is None
default_description = _("This is an exit.")
# Helper classes and methods to implement the Exit. These need not
# be overloaded unless one want to change the foundation for how
# Exits work. See the end of the class for hook methods to overload.
@ -3625,9 +3636,9 @@ class DefaultExit(DefaultObject):
if account:
obj.db.creator_id = account.id
# If no description is set, set a default description
if description or not obj.db.desc:
obj.db.desc = description if description else _("This is an exit.")
# Set description if provided
if description:
obj.db.desc = description
except Exception as e:
errors.append(f"An error occurred while creating this '{key}' object: {e}")

View file

@ -30,6 +30,13 @@ class DefaultObjectTest(BaseEvenniaTest):
self.assertEqual(obj.db.creator_ip, self.ip)
self.assertEqual(obj.db_home, self.room1)
def test_object_default_description(self):
obj, errors = DefaultObject.create("void")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
self.assertIsNone(obj.db.desc)
self.assertEqual(obj.default_description, obj.get_display_desc(obj))
def test_character_create(self):
description = "A furry green monster, reeking of garbage."
home = self.room1.dbref
@ -57,6 +64,13 @@ class DefaultObjectTest(BaseEvenniaTest):
self.assertFalse(errors, errors)
self.assertEqual(obj.name, "SigurXurXorarinsson")
def test_character_default_description(self):
obj, errors = DefaultCharacter.create("dementor")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
self.assertIsNone(obj.db.desc)
self.assertEqual(obj.default_description, obj.get_display_desc(obj))
def test_room_create(self):
description = "A dimly-lit alley behind the local Chinese restaurant."
obj, errors = DefaultRoom.create("alley", self.account, description=description, ip=self.ip)
@ -65,6 +79,13 @@ class DefaultObjectTest(BaseEvenniaTest):
self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip)
def test_room_default_description(self):
obj, errors = DefaultRoom.create("black hole")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
self.assertIsNone(obj.db.desc)
self.assertEqual(obj.default_description, obj.get_display_desc(obj))
def test_exit_create(self):
description = (
"The steaming depths of the dumpster, ripe with refuse in various states of"
@ -78,6 +99,13 @@ class DefaultObjectTest(BaseEvenniaTest):
self.assertEqual(description, obj.db.desc)
self.assertEqual(obj.db.creator_ip, self.ip)
def test_exit_default_description(self):
obj, errors = DefaultExit.create("the nothing")
self.assertTrue(obj, errors)
self.assertFalse(errors, errors)
self.assertIsNone(obj.db.desc)
self.assertEqual(obj.default_description, obj.get_display_desc(obj))
def test_exit_get_return_exit(self):
ex1, _ = DefaultExit.create("north", self.room1, self.room2, account=self.account)
single_return_exit = ex1.get_return_exit()
@ -266,7 +294,7 @@ class TestObjectManager(BaseEvenniaTest):
query = ObjectDB.objects.get_objs_with_key_or_alias("")
self.assertFalse(query)
query = ObjectDB.objects.get_objs_with_key_or_alias("", exact=False)
self.assertEqual(list(query), list(ObjectDB.objects.all().order_by('id')))
self.assertEqual(list(query), list(ObjectDB.objects.all().order_by("id")))
query = ObjectDB.objects.get_objs_with_key_or_alias(
"", exact=False, typeclasses="evennia.objects.objects.DefaultCharacter"