Add inside_rec lockfunc. Resolves #1618
This commit is contained in:
parent
d0035b83fb
commit
27babf4469
4 changed files with 45 additions and 3 deletions
|
|
@ -547,11 +547,39 @@ def inside(accessing_obj, accessed_obj, *args, **kwargs):
|
|||
Usage:
|
||||
inside()
|
||||
|
||||
Only true if accessing_obj is "inside" accessed_obj
|
||||
True if accessing_obj is 'inside' accessing_obj. Note that this only checks
|
||||
one level down. So if if the lock is on a room, you will pass but not your
|
||||
inventory (since their location is you, not the locked object). If you
|
||||
want also nested objects to pass the lock, use the `insiderecursive`
|
||||
lockfunc.
|
||||
"""
|
||||
return accessing_obj.location == accessed_obj
|
||||
|
||||
|
||||
def inside_rec(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
inside_rec()
|
||||
|
||||
True if accessing_obj is inside the accessed obj, at up to 10 levels
|
||||
of recursion (so if this lock is on a room, then an object inside a box
|
||||
in your inventory will also pass the lock).
|
||||
"""
|
||||
|
||||
def _recursive_inside(obj, accessed_obj, lvl=1):
|
||||
if obj.location:
|
||||
if obj.location == accessed_obj:
|
||||
return True
|
||||
elif lvl >= 10:
|
||||
# avoid infinite recursions
|
||||
return False
|
||||
else:
|
||||
return _recursive_inside(obj.location, accessed_obj, lvl + 1)
|
||||
return False
|
||||
|
||||
return _recursive_inside(accessing_obj, accessed_obj)
|
||||
|
||||
|
||||
def holds(accessing_obj, accessed_obj, *args, **kwargs):
|
||||
"""
|
||||
Usage:
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ except ImportError:
|
|||
|
||||
from evennia import settings_default
|
||||
from evennia.locks import lockfuncs
|
||||
from evennia.utils.create import create_object
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Lock testing
|
||||
|
|
@ -179,6 +180,13 @@ class TestLockfuncs(EvenniaTest):
|
|||
self.assertEqual(False, lockfuncs.inside(self.char1, self.room2))
|
||||
self.assertEqual(True, lockfuncs.holds(self.room1, self.char1))
|
||||
self.assertEqual(False, lockfuncs.holds(self.room2, self.char1))
|
||||
# test recursively
|
||||
self.assertEqual(True, lockfuncs.inside_rec(self.char1, self.room1))
|
||||
self.assertEqual(False, lockfuncs.inside_rec(self.char1, self.room2))
|
||||
inventory_item = create_object(key="InsideTester", location=self.char1)
|
||||
self.assertEqual(True, lockfuncs.inside_rec(inventory_item, self.room1))
|
||||
self.assertEqual(False, lockfuncs.inside_rec(inventory_item, self.room2))
|
||||
inventory_item.delete()
|
||||
|
||||
def test_has_account(self):
|
||||
self.assertEqual(True, lockfuncs.has_account(self.char1, None))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue