moss-n-puddles/typeclasses/consumables.py
Howard Abrams 245a1bb389 Refactored code
Cleaned up flake8 warnings
2025-03-05 21:54:41 -08:00

221 lines
7.4 KiB
Python
Executable file

#!/usr/bin/env python
from evennia import Command, CmdSet, TICKER_HANDLER
from evennia.prototypes.spawner import spawn
from commands.consumables import (
CmdSetEat, CmdSetTrolley, CmdSetMakeConsumable
)
from typeclasses.objects import Object
from utils.word_list import routput
from datetime import date
from re import match
import random
class Consumable(Object):
"""
Create with a command:
@create/drop berries;berry : typeclasses.consumables.Consumable
@desc berries = Bright red, almost violet, berry.
# How many berries are there?
@set berries/amount = 10
# How many berries do you eat at a time:
@set berries/eat_amount = 3
@set berries/eat_msg = "Sweet and slightly tart. [Delicious|Tangy|Mmmm]."
@set berries/finish_msg = "Those were [delicious|great]."
"""
amount = 4
eat_amount = 1
def at_object_creation(self):
self.cmdset.add_default(CmdSetEat)
self.db.amount = self.amount
self.db.eat_amount = self.eat_amount
def do_eat(self, eater):
tea_type = self.db.tea_type or "tea"
tea_details = self.db.tea_details
self.db.amount = self.db.amount - self.db.eat_amount
if self.db.eat_msgs:
msg = random.choice(self.db.eat_msgs)
elif self.db.eat_msg:
msg = self.db.eat_msg
else:
msg = "You take a bite."
eater.msg(routput(msg))
if self.db.amount <= 0:
if self.db.finish_msgs:
msg = random.choice(self.db.finish_msgs)
elif self.db.finish_msg:
msg = self.db.finish_msg
else:
msg = "You finished it."
eater.msg(routput(msg))
self.delete()
class Producer(Object):
"""
An object that produces a Consumable object.
Use it to create a 'berry bush', for instance:
@create/drop berry bush;bush : typeclasses.consumables.Producer
@desc bush = A bush laden with bright red brambleberries.
# Then describe what to produce:
@set bush/make_name = "berries"
@set bush/make_aliases = ["berry"]
@set bush/make_verb = "pick some"
# This one is optional as it defaults to Consumable:
@set bush/make_class = "typeclasses.consumables.Consumable"
@set bush/make_desc = "Bright red berry with flecks of [purple|violet|orange]."
# How many berries are there when you pick them?
@set bush/make_amount = 10
# How many berries do you eat at a time:
@set bush/make_eat_amount = 3
# Either a single msg:
@set bush/make_eat_msg = "Sweet and slightly tart. [Delicious|Tangy|Mmmm]."
# Or many:
@set bush/make_eat_msgs = [ "Sweet and slightly tart.", "[Delicious|Tangy|Mmmm].", "Ooo...that one was sour.", "That was a bit ripe, but still good.", "So sweet with a hint of [orange|maple]." ]
@set bush/make_finish_msg = "Those were [delicious|great]."
"""
def at_object_creation(self):
self.cmdset.add_default(CmdSetMakeConsumable)
def do_make(self, picker, args):
consumable = spawn({
"typeclass": self.db.make_class or "typeclasses.consumables.Consumable",
"key": self.db.make_name,
"aliases": self.db.make_aliases,
"desc": routput(self.db.make_desc),
})[0]
consumable.db.amount = self.db.make_amount or 3
consumable.db.eat_amount = self.db.make_eat_amount or 1
if self.db.make_eat_msg:
consumable.db.eat_msg = self.db.make_eat_msg
if self.db.make_eat_msgs:
consumable.db.eat_msgs = self.db.make_eat_msgs
if self.db.make_finish_msg:
consumable.db.finish_msg = self.db.make_finish_msg
if self.db.make_finish_msgs:
consumable.db.finish_msgs = self.db.make_finish_msgs
consumable.location = picker
picker.msg(f"You {self.db.make_verb or 'get'} {self.db.make_name}.")
# ----------------------------------------------------------------------
TEACUP_DESCS = [
"A rustic, clay teacup carefully crafted on a wheel. Beautiful [dark red|olive green|navy blue] glaze.",
"A fine example of Royal Albert teacup, sporting a design of violet azaleas. Perfect for a black tea.",
"A Wings of Grace style teacup with blue butterflies. Perfect for some Earl Grey.",
"A dark brown Yixing clay teacup. Perfect for an Oolong tea.",
"A light jade green handle-less teacup. A good choice for a green tea.",
]
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 = [
{
"name": "blueberry scone",
"taste": "blueberries",
},
{
"name": "chocolate chip scone",
"taste": "chocolate",
},
{
"name": "lemon poppy seed scone",
"taste": "lemon",
},
{
"name": "cinnamon scone",
"taste": "spice",
},
{
"name": "cranberry orange scone",
"tastes": ["tartness of the cranberries", "orange"],
},
{
"name": "pumpkin scone",
"taste": "spices",
},
{
"name": "cheddar cheese and chive scone",
"expression": "cheesy"
},
{
"name": "cherry almond scone",
"expression": "nutty",
},
{
"name": "apple cinnamon scone",
"tastes": ["apple", "cinnamon"],
},
]
teacup_prototype = {
"typeclass": "typeclasses.drinkables.TeaCup",
"key": "teacup",
"desc": "A nice teacup.",
}
def at_object_creation(self):
self.cmdset.add_default(CmdSetTrolley)
TICKER_HANDLER.add(interval=60 * 60 * 24,
callback=self.do_bake)
def produce_teacup(self, caller):
if caller.has("teacup"):
caller.msg("You already have a teacup.")
else:
cuptype = self.teacup_prototype
cuptype['desc'] = routput(random.choice(TEACUP_DESCS))
cup = spawn(cuptype)[0]
cup.location = caller
caller.msg("You pick up a teacup.")
def do_bake(self):
"""
Set the scone that we bake today.
"""
todays_scone = self.scones[date.today().day % len(self.scones)]
name = todays_scone.get("name")
self.db.make_name = name
self.db.make_aliases = ["scone", "scones"]
self.db.make_desc = routput(f"A [tasty|yummy|delicious]-looking {name}")
self.db.make_amount = 3
self.db.make_eat_amount = 1
msgs = [
"[Delicious|Mmmm|So good|Really good].",
]
if todays_scone.get('taste'):
msgs += [f"You [can|] [really|] [notice|taste] the {todays_scone.get('taste')}."]
if todays_scone.get('tastes'):
for taste in todays_scone.get('tastes'):
msgs += [f"You [can|] [really|] [notice|taste] the {taste}."]
if todays_scone.get('expression'):
msgs += [f"[Really|So] {todays_scone.get('expression')}."]
self.db.make_eat_msgs = msgs
self.db.make_finish_msg = "That was [delicious|great|really good]."
self.location.msg_contents(routput("[New|Aromatic|Fresh|Freshly baked] scones [magically|suddenly|mystically|enchantingly|] appear [on a plate|on plates|] on the trolley."))