Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exit_order kwarg to get_display_exits to sort exit names #3470

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 23 additions & 1 deletion evennia/objects/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -1572,12 +1572,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 @@ -10,6 +10,7 @@
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 @@ -94,6 +95,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