Create a currency system of coins

Each character can have a coin purse with gold coins. Granted, the
purse defaults to a pouch.
This commit is contained in:
Howard Abrams 2025-06-19 23:23:35 -07:00
parent 028d1ff631
commit e654a46137
5 changed files with 105 additions and 8 deletions

View file

@ -23,6 +23,7 @@ from commands.sittables import CmdNoSitStand
from commands.everyone import CmdTake, CmdThink, CmdSay, CmdWhisper, CmdRead, CmdEat, CmdDrink
from commands.wizards import CmdGM, CmdSpell, CmdGMTrigger, CmdMakeCocktail
class CharacterCmdSet(default_cmds.CharacterCmdSet):
"""
The `CharacterCmdSet` contains general in-game commands like `look`,

View file

@ -59,7 +59,6 @@ class CmdThrow(Command):
Usage:
throw [ object ]
"""
key = "throw"
locks = "holds(stick)"
@ -195,7 +194,8 @@ class CmdSetCat(CmdSet):
class CmdOctopus(Command):
"""Have your octopus do some antics.
"""
Have your octopus do some antics.
Usage:

View file

@ -11,11 +11,11 @@ creation commands.
from datetime import datetime, timedelta
from re import match, compile, sub
from evennia.commands.command import InterruptCommand
from evennia.contrib.game_systems.gendersub import GenderCharacter
from evennia.contrib.rpg.rpsystem import ContribRPCharacter
from evennia.contrib.rpg.rpsystem import send_emote
from evennia.contrib.rpg.rpsystem import ContribRPCharacter, send_emote
from evennia.prototypes.spawner import spawn
from evennia.utils import delay, logger
from evennia.utils import delay, logger, int2str
from utils.word_list import routput, choices
from .objects import Object
@ -69,6 +69,7 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
if self.dbref != "#1" and not self.is_typeclass('typeclasses.puppets.Puppet'):
self.create_letter()
self.create_ticket()
self.create_pouch()
def delete_inv(self, typeclass):
for obj in self.contents:
@ -125,6 +126,49 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
text = (capitalize_line(text[0]), text[1])
super().msg(text, from_obj=from_obj, session=session, **kwargs)
def create_pouch(self, name="pouch", desc="leather pouch", giver=None):
"""
Create a leather pouch of coins.
Fill it with ten gold coins, so they can play the games.
"""
pouch = spawn({
"typeclass": "typeclasses.things.CoinPurse",
"key": name,
"desc": f"A {desc} containing coins.",
})[0]
pouch.db.gold_amount = 10
pouch.location = self
if giver:
self.announce_action(f"The {giver.get_display_name(self)} tosses a {desc} to $you().")
self.msg(f"You now have a {name} with {int2str(pouch.db.gold_amount)} coins.")
def get_pouch(self):
"""
Return a pouch object.
Throws InterruptCommand exception if the character has
no pouch, and therefore, no money.
"""
pouches = self.search("", location=self, quiet=True,
typeclass="typeclasses.things.CoinPurse")
if pouches:
return pouches[0]
raise InterruptCommand("No coin purse")
def how_many_coins(self):
"Return the amount of money in the coin purse."
self.get_pouch().how_much()
def adjust_coins(self, amount):
"Increase or decrease the amount of money in the coin purse."
self.get_pouch().adjust_amount(amount)
def has_coins(self, at_least=1):
"Return True if character has 'at_least' that many coins."
return self.get_pouch().has_amount(at_least)
def create_letter(self):
"create a welcome letter in a character's inventory"
letter = spawn({
@ -391,3 +435,7 @@ class Character(Object, GenderCharacter, ContribRPCharacter):
if len(inv_items) > 0:
return inv_items
return None
# Local Variables:
# jinx-local-words: "Marsivan"
# End:

View file

@ -11,10 +11,8 @@ with a location in the game world (like Characters, Rooms, Exits).
from re import split, match
# from evennia.objects.objects import DefaultObject
from evennia.contrib.rpg.rpsystem.rpsystem import ContribRPObject
from evennia.utils import delay, logger
# from evennia.utils import delay, logger, search
from utils.word_list import routput

View file

@ -4,8 +4,9 @@ from random import choice, random, randint
from enum import Enum
from evennia import create_script
from evennia.commands.command import InterruptCommand
from evennia.prototypes.spawner import spawn
from evennia.utils import logger, delay
from evennia.utils import logger, delay, iter_to_str, int2str
from commands.misc import (CmdSetPuddle,
CmdSetStick,
@ -21,6 +22,54 @@ from .objects import Object
import re
class CoinPurse(Object):
"""
A pouch containing the character coins.
Only the player can see how many coins it contains.
"""
def at_object_creation(self):
"""
Set the initial number of coins.
"""
self.db.gold_amount = 10
def return_appearance(self, looker, **kwargs):
"""
Return description based on the 'looker'.
"""
if self.location == looker:
if self.has_amount(1):
return f"Contains {int2str(self.db.gold_amount)} gold coins."
return "It is empty."
return "Probably containing coins."
def how_much(self):
"""
Return the amount of gold coins in the purse.
"""
return self.db.gold_amount or 0
def adjust_amount(self, amount):
"""
Change the amount of gold in the purse.
The 'amount' can be a negative number.
"""
owner = self.location
if self.how_much() + amount < 0:
raise InterruptCommand("Not enough coins")
self.db.gold_amount = self.how_much() + amount
if self.how_much() == 1:
owner.msg("You now have one gold coin.")
else:
owner.msg(f"You now have {int2str(self.how_much())} gold coins.")
def has_amount(self, at_least):
"""Return True is pouch has 'at_least' amount of coins."""
return self.how_much() >= at_least
class Trinket(Object):
"""
For instance:
@ -94,6 +143,7 @@ class CrystalBall(Object):
"Very doubtful",
])
class Ring(Object):
def move_to(self, destination, **kwargs):
"""