From 473fba5c136f9f5a9b2fe10708b97645f7ea5c42 Mon Sep 17 00:00:00 2001 From: Wendy Wang Date: Thu, 20 Oct 2022 21:30:37 +0200 Subject: [PATCH 1/2] Addition of matching_exit() function on DefaultExit to return the exit(s) that pair with it, optionally formatted as list or singular Exit with as_list parameter. --- evennia/objects/objects.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/evennia/objects/objects.py b/evennia/objects/objects.py index 570398894a..7492cc8831 100644 --- a/evennia/objects/objects.py +++ b/evennia/objects/objects.py @@ -3133,3 +3133,25 @@ class DefaultExit(DefaultObject): """ traversing_object.msg(_("You cannot go there.")) + + def matching_exit(self, as_list=False): + """ + Get the exits that pair with this one in its destination room + (i.e. returns to its location) + + Args: + as_list (bool): Whether to return available results as a + list or single matching exit. + + Returns: + list, exit (Exit), or None: The matching exit(s). + """ + no_match = [] if as_list else None + if not self.destination: + return no_match + matching_exits = [x for x in self.destination.exits if x.destination == self.location] + if not matching_exits: + return no_match + if as_list: + return matching_exits + return matching_exits[0] From adf280432851990714f9d5663c08b7eae0c07d1c Mon Sep 17 00:00:00 2001 From: Wendy Wang Date: Fri, 21 Oct 2022 01:14:41 +0200 Subject: [PATCH 2/2] Updates with Griatch's recs, addition of unit test --- evennia/objects/objects.py | 21 ++++++++------------- evennia/objects/tests.py | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/evennia/objects/objects.py b/evennia/objects/objects.py index 7492cc8831..d811414fdb 100644 --- a/evennia/objects/objects.py +++ b/evennia/objects/objects.py @@ -3134,24 +3134,19 @@ class DefaultExit(DefaultObject): """ traversing_object.msg(_("You cannot go there.")) - def matching_exit(self, as_list=False): + def get_return_exit(self, return_all=False): """ Get the exits that pair with this one in its destination room (i.e. returns to its location) Args: - as_list (bool): Whether to return available results as a - list or single matching exit. + return_all (bool): Whether to return available results as a + list or single matching exit. Returns: - list, exit (Exit), or None: The matching exit(s). + queryset or exit (Exit): The matching exit(s). """ - no_match = [] if as_list else None - if not self.destination: - return no_match - matching_exits = [x for x in self.destination.exits if x.destination == self.location] - if not matching_exits: - return no_match - if as_list: - return matching_exits - return matching_exits[0] + query = ObjectDB.objects.filter(db_location=self.destination, db_destination=self.location) + if return_all: + return query + return query.first() diff --git a/evennia/objects/tests.py b/evennia/objects/tests.py index fb53385205..c2ef53b498 100644 --- a/evennia/objects/tests.py +++ b/evennia/objects/tests.py @@ -69,6 +69,23 @@ class DefaultObjectTest(BaseEvenniaTest): self.assertEqual(description, obj.db.desc) self.assertEqual(obj.db.creator_ip, self.ip) + 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() + all_return_exit = ex1.get_return_exit(return_all=True) + self.assertEqual(single_return_exit, None) + self.assertEqual(len(all_return_exit), 0) + + ex2, _ = DefaultExit.create("south", self.room2, self.room1, account=self.account) + single_return_exit = ex1.get_return_exit() + all_return_exit = ex1.get_return_exit(return_all=True) + self.assertEqual(single_return_exit, ex2) + self.assertEqual(len(all_return_exit), 1) + + ex3, _ = DefaultExit.create("also_south", self.room2, self.room1, account=self.account) + all_return_exit = ex1.get_return_exit(return_all=True) + self.assertEqual(len(all_return_exit), 2) + def test_urls(self): "Make sure objects are returning URLs" self.assertTrue(self.char1.get_absolute_url())