Skip to content

Commit

Permalink
Merge pull request #3470 from chiizujin/sorted_exits
Browse files Browse the repository at this point in the history
Add exit_order kwarg to get_display_exits to sort exit names
  • Loading branch information
Griatch committed Apr 6, 2024
2 parents f823e36 + 9a825c7 commit 9965ab8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
24 changes: 23 additions & 1 deletion evennia/objects/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1573,12 +1573,34 @@ def get_display_exits(self, looker, **kwargs):
Args:
looker (DefaultObject): Object doing the looking.
**kwargs: Arbitrary data for use when overriding.
Keyword Args:
exit_order (iterable of str): The order in which exits should be listed, with
unspecified exits appearing at the end, alphabetically.
Returns:
str: The exits display data.
Examples:
::
For a room with exits in the order 'portal', 'south', 'north', and 'out':
obj.get_display_name(looker, exit_order=('north', 'south'))
-> "Exits: north, south, out, and portal." (markup not shown here)
"""
def _sort_exit_names(names):
exit_order = kwargs.get("exit_order")
if not exit_order:
return names
sort_index = {name: key for key, name in enumerate(exit_order)}
names = sorted(names)
end_pos = len(names) + 1
names.sort(key=lambda name:sort_index.get(name, end_pos))
return names

exits = self.filter_visible(self.contents_get(content_type="exit"), looker, **kwargs)
exit_names = iter_to_str(exi.get_display_name(looker, **kwargs) for exi in exits)
exit_names = (exi.get_display_name(looker, **kwargs) for exi in exits)
exit_names = iter_to_str(_sort_exit_names(exit_names))

return f"|wExits:|n {exit_names}" if exit_names else ""

Expand Down
16 changes: 16 additions & 0 deletions evennia/objects/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from evennia.typeclasses.tags import (AliasProperty, PermissionProperty,
TagCategoryProperty, TagProperty)
from evennia.utils import create, search
from evennia.utils.ansi import strip_ansi
from evennia.utils.test_resources import BaseEvenniaTest, EvenniaTestCase


Expand Down Expand Up @@ -90,6 +91,21 @@ def test_exit_get_return_exit(self):
all_return_exit = ex1.get_return_exit(return_all=True)
self.assertEqual(len(all_return_exit), 2)

def test_exit_order(self):
DefaultExit.create("south", self.room1, self.room2, account=self.account)
DefaultExit.create("portal", self.room1, self.room2, account=self.account)
DefaultExit.create("north", self.room1, self.room2, account=self.account)
DefaultExit.create("aperture", self.room1, self.room2, account=self.account)

# in creation order
exits = strip_ansi(self.room1.get_display_exits(self.char1))
self.assertEqual(exits, "Exits: out, south, portal, north, and aperture")

# in specified order with unspecified exits alpbabetically on the end
exit_order = ('north', 'south', 'out')
exits = strip_ansi(self.room1.get_display_exits(self.char1, exit_order=exit_order))
self.assertEqual(exits, "Exits: north, south, out, aperture, and portal")

def test_urls(self):
"Make sure objects are returning URLs"
self.assertTrue(self.char1.get_absolute_url())
Expand Down

0 comments on commit 9965ab8

Please sign in to comment.