diff --git a/commands/command.py b/commands/command.py index d49bb94..927a63d 100644 --- a/commands/command.py +++ b/commands/command.py @@ -79,6 +79,33 @@ class Command(BaseCommand): self.tokenized_words = TokenWords(self.args.strip()) return self.tokenized_words + def search_list_by_func(self, lst, func_name, lambda_filter=None): + """ + Search items in 'lst' that have a function, 'func_name'. + + Further refine the return list by the 'lambda_filter'. + """ + + items = [item for item in lst + if item.has_method(func_name)] + if lambda_filter: + return list(filter(lambda_filter, items)) + return items + + def search_room_by_func(self, func_name, lambda_filter=None): + """ + Find items in the caller's room with a function, 'func_name'. + """ + return self.search_list_by_func(self.caller.location.contents, + func_name, lambda_filter) + + def search_contents_by_func(self, func_name, lambda_filter=None): + """ + Find items in the caller's inventory with a function, 'func_name'. + """ + return self.search_list_by_func(self.caller.contents, func_name, + lambda_filter) + # ------------------------------------------------------------- # diff --git a/commands/everyone.py b/commands/everyone.py index e4539d9..546fc36 100755 --- a/commands/everyone.py +++ b/commands/everyone.py @@ -454,10 +454,6 @@ class CmdDrink(Command): key = "drink" aliases = ["sip", "quaff"] - def not_empty(self, item): - """Return true is the cup has some drink left in it.""" - return (item.db.amount or 0) > 0 - def drink_item(self, name): """Find item in inventory, name, and call 'do_drink' on it.""" notfound = f"You don't have {name} in your inventory." @@ -471,8 +467,9 @@ class CmdDrink(Command): def drink_anything(self): """Drink anything in your inventory, but only if you have one thing.""" - containers = [item for item in self.caller.contents - if item.has_method('do_drink') and self.not_empty(item)] + def not_empty(item): + return (item.db.amount or 0) > 0 + containers = self.search_contents_by_func('do_drink', not_empty) if len(containers) == 1: containers[0].do_drink(self.caller) elif len(containers) > 1: @@ -508,10 +505,6 @@ class CmdEat(Command): key = "eat" aliases = ["consume", "bite"] - def not_gone(self, item): - """Return true is the cup has some eat left in it.""" - return (item.db.amount or 0) > 0 - def eat_item(self, name): """Find item in inventory, name, and call 'do_eat' on it.""" notfound = f"You don't have {name} in your inventory." @@ -525,15 +518,16 @@ class CmdEat(Command): def eat_anything(self): """Eat something in your inventory, but only if you have one thing.""" - items = [item for item in self.caller.contents - if item.has_method('do_eat') and self.not_gone(item)] - if len(items) == 1: - items[0].do_eat(self.caller) - elif len(items) > 1: - self.caller.msg("You have too many things to eat. " - "Which one do you want?") - else: + def not_gone(item): + return (item.db.amount or 0) > 0 + items = self.search_contents_by_func('do_eat', not_gone) + if len(items) == 0: self.caller.msg("You have nothing to eat.") + else: + item = items[0] + if len(items) > 1: + self.caller.msg(f"Eating the {item.name}.") + item.do_eat(self.caller) def func(self): """ diff --git a/typeclasses/consumables.py b/typeclasses/consumables.py index f3624f8..80e5c72 100755 --- a/typeclasses/consumables.py +++ b/typeclasses/consumables.py @@ -143,7 +143,6 @@ class Trolley(Producer): A special producer that has a plate of specific dishes for that day. I need this special because I'm not sure how to populate the database with complex arrays of data. - """ scones = [ { diff --git a/typeclasses/objects.py b/typeclasses/objects.py index 3dab749..7943034 100755 --- a/typeclasses/objects.py +++ b/typeclasses/objects.py @@ -255,13 +255,11 @@ class Object(ObjectParent, ContribRPObject): """ Return a list of characters in the current location. """ - return [ char - for char in self.location.contents - if char != self and - (char.is_typeclass("typeclasses.characters.Character") - or (char.is_typeclass("typeclasses.puppets.Puppet") and - puppets)) - ] + return [char + for char in self.location.contents + if char != self and + (char.is_typeclass("typeclasses.characters.Character") + or (char.is_typeclass("typeclasses.puppets.Puppet") and puppets))] def delay_sequence(self, sequence_str, time_delay=1, *args): """Run a sequence of messages or commands with a delay. diff --git a/typeclasses/rooms_dark.py b/typeclasses/rooms_dark.py index 5fc905b..c0b9b03 100755 --- a/typeclasses/rooms_dark.py +++ b/typeclasses/rooms_dark.py @@ -53,7 +53,7 @@ FOUND_LIGHTSOURCE = ( "Your fingers bump against a splinter of wood." " It smells of resin and seems dry enough to burn! " "You pick it up, holding it firmly. Now you just need to" - " |glight|n it using the flint and steel you carry with you." + " |glight|n the |gsplinter|n using the flint and steel you carry with you." )