Refactored the eat/drink commands
This commit is contained in:
parent
14c8385589
commit
192dd83000
5 changed files with 45 additions and 27 deletions
|
|
@ -79,6 +79,33 @@ class Command(BaseCommand):
|
||||||
self.tokenized_words = TokenWords(self.args.strip())
|
self.tokenized_words = TokenWords(self.args.strip())
|
||||||
return self.tokenized_words
|
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)
|
||||||
|
|
||||||
|
|
||||||
# -------------------------------------------------------------
|
# -------------------------------------------------------------
|
||||||
#
|
#
|
||||||
|
|
|
||||||
|
|
@ -454,10 +454,6 @@ class CmdDrink(Command):
|
||||||
key = "drink"
|
key = "drink"
|
||||||
aliases = ["sip", "quaff"]
|
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):
|
def drink_item(self, name):
|
||||||
"""Find item in inventory, name, and call 'do_drink' on it."""
|
"""Find item in inventory, name, and call 'do_drink' on it."""
|
||||||
notfound = f"You don't have {name} in your inventory."
|
notfound = f"You don't have {name} in your inventory."
|
||||||
|
|
@ -471,8 +467,9 @@ class CmdDrink(Command):
|
||||||
|
|
||||||
def drink_anything(self):
|
def drink_anything(self):
|
||||||
"""Drink anything in your inventory, but only if you have one thing."""
|
"""Drink anything in your inventory, but only if you have one thing."""
|
||||||
containers = [item for item in self.caller.contents
|
def not_empty(item):
|
||||||
if item.has_method('do_drink') and self.not_empty(item)]
|
return (item.db.amount or 0) > 0
|
||||||
|
containers = self.search_contents_by_func('do_drink', not_empty)
|
||||||
if len(containers) == 1:
|
if len(containers) == 1:
|
||||||
containers[0].do_drink(self.caller)
|
containers[0].do_drink(self.caller)
|
||||||
elif len(containers) > 1:
|
elif len(containers) > 1:
|
||||||
|
|
@ -508,10 +505,6 @@ class CmdEat(Command):
|
||||||
key = "eat"
|
key = "eat"
|
||||||
aliases = ["consume", "bite"]
|
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):
|
def eat_item(self, name):
|
||||||
"""Find item in inventory, name, and call 'do_eat' on it."""
|
"""Find item in inventory, name, and call 'do_eat' on it."""
|
||||||
notfound = f"You don't have {name} in your inventory."
|
notfound = f"You don't have {name} in your inventory."
|
||||||
|
|
@ -525,15 +518,16 @@ class CmdEat(Command):
|
||||||
|
|
||||||
def eat_anything(self):
|
def eat_anything(self):
|
||||||
"""Eat something in your inventory, but only if you have one thing."""
|
"""Eat something in your inventory, but only if you have one thing."""
|
||||||
items = [item for item in self.caller.contents
|
def not_gone(item):
|
||||||
if item.has_method('do_eat') and self.not_gone(item)]
|
return (item.db.amount or 0) > 0
|
||||||
if len(items) == 1:
|
items = self.search_contents_by_func('do_eat', not_gone)
|
||||||
items[0].do_eat(self.caller)
|
if len(items) == 0:
|
||||||
elif len(items) > 1:
|
|
||||||
self.caller.msg("You have too many things to eat. "
|
|
||||||
"Which one do you want?")
|
|
||||||
else:
|
|
||||||
self.caller.msg("You have nothing to eat.")
|
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):
|
def func(self):
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -143,7 +143,6 @@ class Trolley(Producer):
|
||||||
A special producer that has a plate of specific dishes for
|
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
|
that day. I need this special because I'm not sure how to populate
|
||||||
the database with complex arrays of data.
|
the database with complex arrays of data.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
scones = [
|
scones = [
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -259,9 +259,7 @@ class Object(ObjectParent, ContribRPObject):
|
||||||
for char in self.location.contents
|
for char in self.location.contents
|
||||||
if char != self and
|
if char != self and
|
||||||
(char.is_typeclass("typeclasses.characters.Character")
|
(char.is_typeclass("typeclasses.characters.Character")
|
||||||
or (char.is_typeclass("typeclasses.puppets.Puppet") and
|
or (char.is_typeclass("typeclasses.puppets.Puppet") and puppets))]
|
||||||
puppets))
|
|
||||||
]
|
|
||||||
|
|
||||||
def delay_sequence(self, sequence_str, time_delay=1, *args):
|
def delay_sequence(self, sequence_str, time_delay=1, *args):
|
||||||
"""Run a sequence of messages or commands with a delay.
|
"""Run a sequence of messages or commands with a delay.
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ FOUND_LIGHTSOURCE = (
|
||||||
"Your fingers bump against a splinter of wood."
|
"Your fingers bump against a splinter of wood."
|
||||||
" It smells of resin and seems dry enough to burn! "
|
" It smells of resin and seems dry enough to burn! "
|
||||||
"You pick it up, holding it firmly. Now you just need to"
|
"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."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue