Refactored the eat/drink commands

This commit is contained in:
Howard Abrams 2025-07-13 08:24:30 -07:00
parent 14c8385589
commit 192dd83000
5 changed files with 45 additions and 27 deletions

View file

@ -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)
# -------------------------------------------------------------
#

View file

@ -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):
"""

View file

@ -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 = [
{

View file

@ -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.

View file

@ -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."
)