Various style and functional cleanups
git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@19798 282e977c-c81d-0410-88c4-b93c2d0d6712master
parent
e504a2c8f9
commit
6ab5d4865e
|
@ -1,8 +1,9 @@
|
||||||
# banksay.py -- implements 'say' event for bank tellers
|
"""
|
||||||
# Created by: Joris Bontje <jbontje@suespammers.org>
|
Created by: Joris Bontje <jbontje@suespammers.org>
|
||||||
#
|
|
||||||
# Updated to use new path functions in CFPython and broken and
|
This module implements banking in Crossfire. It provides the 'say' event for
|
||||||
# modified a bit by -Todd Mitchell
|
bank tellers, as well as a deposit box for quickly depositing money.
|
||||||
|
"""
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ CoinTypes = {
|
||||||
'IMPERIAL NOTE': 10000,
|
'IMPERIAL NOTE': 10000,
|
||||||
'10 IMPERIAL NOTE': 100000,
|
'10 IMPERIAL NOTE': 100000,
|
||||||
'100 IMPERIAL NOTE': 1000000,
|
'100 IMPERIAL NOTE': 1000000,
|
||||||
}
|
}
|
||||||
|
|
||||||
# Associate coin names with their corresponding archetypes.
|
# Associate coin names with their corresponding archetypes.
|
||||||
ArchType = {
|
ArchType = {
|
||||||
|
@ -46,7 +47,7 @@ ArchType = {
|
||||||
'IMPERIAL NOTE': 'imperial',
|
'IMPERIAL NOTE': 'imperial',
|
||||||
'10 IMPERIAL NOTE': 'imperial10',
|
'10 IMPERIAL NOTE': 'imperial10',
|
||||||
'100 IMPERIAL NOTE': 'imperial100',
|
'100 IMPERIAL NOTE': 'imperial100',
|
||||||
}
|
}
|
||||||
|
|
||||||
# Define several 'thank-you' messages which are chosen at random.
|
# Define several 'thank-you' messages which are chosen at random.
|
||||||
thanks_message = [
|
thanks_message = [
|
||||||
|
@ -61,39 +62,23 @@ thanks_message = [
|
||||||
'Thank you. "Service" is our middle name.',
|
'Thank you. "Service" is our middle name.',
|
||||||
'Thank you. "Service" is our middle name.',
|
'Thank you. "Service" is our middle name.',
|
||||||
'Thank you. Hows about a big slobbery kiss?',
|
'Thank you. Hows about a big slobbery kiss?',
|
||||||
]
|
]
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Piece together several arguments to form a coin name.
|
|
||||||
def getCoinNameFromArgs(argv):
|
def getCoinNameFromArgs(argv):
|
||||||
coinName = ""
|
"""Piece together several arguments to form a coin name."""
|
||||||
|
coinName = str.join(" ", argv)
|
||||||
# Take the arguments and piece together the full coin name.
|
# Remove the trailing 's' from the coin name.
|
||||||
for argument in argv:
|
if coinName[-1] == 's':
|
||||||
coinName += argument + ' '
|
coinName = coinName[:-1]
|
||||||
|
|
||||||
# Remove the trailing space and 's' from the coin name.
|
|
||||||
coinName = coinName[:len(coinName) - 1]
|
|
||||||
|
|
||||||
if coinName[len(coinName) - 1] == 's':
|
|
||||||
coinName = coinName[:len(coinName) - 1]
|
|
||||||
|
|
||||||
return coinName
|
return coinName
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Return the exchange rate for the given type of coin.
|
|
||||||
def getExchangeRate(coinName):
|
def getExchangeRate(coinName):
|
||||||
# Try to find exchange rate, set to None if we can't.
|
"""Return the exchange rate for the given type of coin."""
|
||||||
if coinName.upper() in CoinTypes:
|
if coinName.upper() in CoinTypes:
|
||||||
return int(CoinTypes.get(coinName.upper()))
|
return CoinTypes[coinName.upper()]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Return a string representing the given amount in silver.
|
|
||||||
def strAmount(amount):
|
|
||||||
return Crossfire.CostStringFromValue(amount)
|
|
||||||
|
|
||||||
def get_inventory(obj):
|
def get_inventory(obj):
|
||||||
"""An iterator for a given object's inventory."""
|
"""An iterator for a given object's inventory."""
|
||||||
current_item = obj.Inventory
|
current_item = obj.Inventory
|
||||||
|
@ -102,6 +87,14 @@ def get_inventory(obj):
|
||||||
yield current_item
|
yield current_item
|
||||||
current_item = next_item
|
current_item = next_item
|
||||||
|
|
||||||
|
def deposit_with_fee(player, amount):
|
||||||
|
"""Deposit the given amount for the player, with fees subtracted."""
|
||||||
|
deposit_value = int(amount / fees)
|
||||||
|
CFBank.deposit(player, deposit_value)
|
||||||
|
bank.deposit(Skuds, amount - deposit_value)
|
||||||
|
whoami.Say("A fee of %s has been charged on this transaction." \
|
||||||
|
% Crossfire.CostStringFromValue(amount - deposit_value))
|
||||||
|
|
||||||
def deposit_box_close():
|
def deposit_box_close():
|
||||||
"""Find the total value of items in the deposit box and deposit."""
|
"""Find the total value of items in the deposit box and deposit."""
|
||||||
total_value = 0
|
total_value = 0
|
||||||
|
@ -109,39 +102,29 @@ def deposit_box_close():
|
||||||
if obj.Name != 'Apply' and obj.Name != 'Close':
|
if obj.Name != 'Apply' and obj.Name != 'Close':
|
||||||
total_value += obj.Value * obj.Quantity
|
total_value += obj.Value * obj.Quantity
|
||||||
obj.Teleport(activator.Map, 15, 3)
|
obj.Teleport(activator.Map, 15, 3)
|
||||||
|
deposit_with_fee(activator, total_value)
|
||||||
|
|
||||||
# Calculate the amount of money actually deposited (due to fees).
|
|
||||||
deposit_value = int(total_value / fees)
|
|
||||||
CFBank.deposit(activator, deposit_value)
|
|
||||||
whoami.Say("A fee of %d silver has been charged on this transaction." \
|
|
||||||
% (total_value - deposit_value))
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Print a help message for the player.
|
|
||||||
def cmd_help():
|
def cmd_help():
|
||||||
message = "The Bank of Skud can help you keep your money safe. In addition, you will be able to access your money from any bank in the world! A small service charge of %d percent will be placed on all deposits, though. What would you like to do?" % service_charge
|
"""Print a help message for the player."""
|
||||||
|
whoami.Say("The Bank of Skud can help you keep your money safe. In addition, you will be able to access your money from any bank in the world! A small service charge of %d percent will be placed on all deposits, though. What would you like to do?" % service_charge)
|
||||||
if activator.DungeonMaster:
|
|
||||||
message += "\n\nAs the DM, you can also clear the bank's profits by using 'reset-profits'."
|
|
||||||
|
|
||||||
Crossfire.AddReply("balance", "I want to check my balance.")
|
Crossfire.AddReply("balance", "I want to check my balance.")
|
||||||
Crossfire.AddReply("deposit", "I'd like to deposit some money.")
|
Crossfire.AddReply("deposit", "I'd like to deposit some money.")
|
||||||
Crossfire.AddReply("withdraw", "I'd like to withdraw some money.")
|
Crossfire.AddReply("withdraw", "I'd like to withdraw some money.")
|
||||||
Crossfire.AddReply("profits", "How much money has the Bank of Skud made?")
|
Crossfire.AddReply("profits", "How much money has the Bank of Skud made?")
|
||||||
|
if activator.DungeonMaster:
|
||||||
whoami.Say(message)
|
Crossfire.AddReply("reset-profits", "Reset bank profits!")
|
||||||
|
|
||||||
def cmd_show_profits():
|
def cmd_show_profits():
|
||||||
"""Say the total bank profits."""
|
"""Say the total bank profits."""
|
||||||
message = "To date, the Imperial Bank of Skud has made %s in profit." \
|
whoami.Say("To date, the Imperial Bank of Skud has made %s in profit." \
|
||||||
% strAmount(bank.getbalance(Skuds))
|
% Crossfire.CostStringFromValue(bank.getbalance(Skuds)))
|
||||||
whoami.Say(message)
|
|
||||||
|
|
||||||
def cmd_reset_profit():
|
def cmd_reset_profit():
|
||||||
"""Reset the total bank profits."""
|
"""Reset the total bank profits."""
|
||||||
if activator.DungeonMaster:
|
if activator.DungeonMaster:
|
||||||
bank.withdraw(Skuds, bank.getbalance(Skuds))
|
bank.withdraw(Skuds, bank.getbalance(Skuds))
|
||||||
whoami.Say("Profits reset!")
|
cmd_show_profits()
|
||||||
else:
|
else:
|
||||||
whoami.Say("Only the dungeon master can reset our profits!")
|
whoami.Say("Only the dungeon master can reset our profits!")
|
||||||
|
|
||||||
|
@ -156,65 +139,48 @@ def cmd_balance(argv):
|
||||||
whoami.Say("Hmm... I've never seen that kind of money.")
|
whoami.Say("Hmm... I've never seen that kind of money.")
|
||||||
return
|
return
|
||||||
if balance != 0:
|
if balance != 0:
|
||||||
balance /= exchange_rate * 1.0;
|
balance /= exchange_rate * 1.0
|
||||||
whoami.Say("You have %.3f %s in the bank." % (balance, coinName))
|
whoami.Say("You have %.3f %s in the bank." % (balance, coinName))
|
||||||
else:
|
else:
|
||||||
whoami.Say("Sorry, you have no balance.")
|
whoami.Say("Sorry, you have no balance.")
|
||||||
else:
|
else:
|
||||||
whoami.Say("You have " + strAmount(balance) + " in the bank.")
|
whoami.Say("You have %s in the bank." % \
|
||||||
|
Crossfire.CostStringFromValue(balance))
|
||||||
|
|
||||||
# TODO: Look over checking code to make sure everything's okay.
|
|
||||||
def cmd_deposit(text):
|
def cmd_deposit(text):
|
||||||
"""Deposit a certain amount of money."""
|
"""Deposit a certain amount of money."""
|
||||||
if len(text) >= 3:
|
if len(text) >= 3:
|
||||||
coinName = getCoinNameFromArgs(text[2:])
|
coinName = getCoinNameFromArgs(text[2:])
|
||||||
exchange_rate = getExchangeRate(coinName)
|
exchange_rate = getExchangeRate(coinName)
|
||||||
amount = int(text[1])
|
amount = int(text[1])
|
||||||
|
|
||||||
if exchange_rate is None:
|
if exchange_rate is None:
|
||||||
whoami.Say("Hmm... I've never seen that kind of money.")
|
whoami.Say("Hmm... I've never seen that kind of money.")
|
||||||
return
|
return
|
||||||
|
if amount < 0:
|
||||||
# Don't let the player deposit negative money.
|
|
||||||
if amount <= 0:
|
|
||||||
whoami.Say("Regulations prohibit negative deposits.")
|
whoami.Say("Regulations prohibit negative deposits.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Make sure the player has enough cash on hand.
|
# Make sure the player has enough cash on hand.
|
||||||
actualAmount = amount * exchange_rate
|
actualAmount = amount * exchange_rate
|
||||||
if activator.PayAmount(actualAmount):
|
if activator.PayAmount(actualAmount):
|
||||||
CFBank.deposit(activator, int(actualAmount / fees))
|
deposit_with_fee(activator, actualAmount)
|
||||||
bank.deposit(Skuds, actualAmount - int(actualAmount / fees))
|
|
||||||
|
|
||||||
message = "%d %s received, %s deposited to your account. %s" \
|
|
||||||
% (amount, coinName, strAmount(int(actualAmount / fees)),
|
|
||||||
random.choice(thanks_message))
|
|
||||||
else:
|
else:
|
||||||
message = "But you don't have that much in your inventory!"
|
whoami.Say("But you don't have that much in your inventory!")
|
||||||
else:
|
else:
|
||||||
message = "What would you like to deposit?"
|
whoami.Say("What would you like to deposit?")
|
||||||
Crossfire.AddReply("deposit <amount> <coin type>", "Some money.")
|
Crossfire.AddReply("deposit <amount> <coin type>", "Some money.")
|
||||||
|
|
||||||
whoami.Say(message)
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Withdraw money from the player's account.
|
|
||||||
def cmd_withdraw(argv):
|
def cmd_withdraw(argv):
|
||||||
argc = len(argv)
|
"""Withdraw money from the player's account."""
|
||||||
|
if len(argv) >= 3:
|
||||||
# Withdraw a certain number of a certain coin.
|
|
||||||
if argc >= 3:
|
|
||||||
coinName = getCoinNameFromArgs(argv[2:])
|
coinName = getCoinNameFromArgs(argv[2:])
|
||||||
exchange_rate = getExchangeRate(coinName)
|
exchange_rate = getExchangeRate(coinName)
|
||||||
amount = int(argv[1])
|
amount = int(argv[1])
|
||||||
|
|
||||||
if exchange_rate is None:
|
if exchange_rate is None:
|
||||||
whoami.Say("Hmm... I've never seen that kind of money.")
|
whoami.Say("Hmm... I've never seen that kind of money.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Don't let the player withdraw negative money.
|
|
||||||
if amount <= 0:
|
if amount <= 0:
|
||||||
whoami.Say("Hey, you can't withdraw a negative amount!")
|
whoami.Say("Sorry, you can't withdraw that amount.")
|
||||||
return
|
return
|
||||||
|
|
||||||
# Make sure the player has sufficient funds.
|
# Make sure the player has sufficient funds.
|
||||||
|
@ -223,71 +189,20 @@ def cmd_withdraw(argv):
|
||||||
% (amount, coinName, random.choice(thanks_message))
|
% (amount, coinName, random.choice(thanks_message))
|
||||||
|
|
||||||
# Drop the money and have the player pick it up.
|
# Drop the money and have the player pick it up.
|
||||||
withdrawal = activator.Map.CreateObject(ArchType.get( \
|
withdrawal = activator.Map.CreateObject(
|
||||||
coinName.upper()), x, y)
|
ArchType.get(coinName.upper()), x, y)
|
||||||
CFItemBroker.Item(withdrawal).add(amount)
|
CFItemBroker.Item(withdrawal).add(amount)
|
||||||
activator.Take(withdrawal)
|
activator.Take(withdrawal)
|
||||||
else:
|
else:
|
||||||
message = "I'm sorry, you don't have enough money."
|
message = "I'm sorry, you don't have enough money."
|
||||||
else:
|
else:
|
||||||
message = "How much money would you like to withdraw?"
|
message = "How much money would you like to withdraw?"
|
||||||
Crossfire.AddReply("withdraw <amount> <coin name>", "")
|
Crossfire.AddReply("withdraw <amount> <coin name>", "This much!")
|
||||||
|
|
||||||
whoami.Say(message)
|
whoami.Say(message)
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
def main_employee():
|
||||||
# Exchange money.
|
|
||||||
# TODO: Clean up code here.
|
|
||||||
def cmd_exchange(text):
|
|
||||||
if len(text) == 2:
|
|
||||||
amount = int(text[1])
|
|
||||||
if amount <= 0:
|
|
||||||
message = \
|
|
||||||
'Usage "exchange <amount>" (imperials to platinum coins)'
|
|
||||||
elif amount > 10000:
|
|
||||||
message = \
|
|
||||||
'Sorry, we do not exchange more than 10000 imperials all at once.'
|
|
||||||
else:
|
|
||||||
inv = activator.CheckInventory('imperial')
|
|
||||||
if inv:
|
|
||||||
pay = CFItemBroker.Item(inv).subtract(amount)
|
|
||||||
if pay:
|
|
||||||
exchange_rate = 10000
|
|
||||||
|
|
||||||
# Drop the coins on the floor, then try
|
|
||||||
# to pick them up. This effectively
|
|
||||||
# prevents the player from carrying too
|
|
||||||
# many coins.
|
|
||||||
|
|
||||||
id = activator.Map.CreateObject('platinum coin'
|
|
||||||
, x, y)
|
|
||||||
CFItemBroker.Item(id).add(int(amount
|
|
||||||
* (exchange_rate / 50)))
|
|
||||||
activator.Take(id)
|
|
||||||
|
|
||||||
message = random.choice(thanks_message)
|
|
||||||
else:
|
|
||||||
message = 'Sorry, you do not have %d imperials' \
|
|
||||||
% amount
|
|
||||||
else:
|
|
||||||
message = 'Sorry, you do not have any imperials'
|
|
||||||
else:
|
|
||||||
message = \
|
|
||||||
'Usage "exchange <amount>" (imperials to platinum coins)'
|
|
||||||
|
|
||||||
whoami.Say(message)
|
|
||||||
|
|
||||||
# ----------------------------------------------------------------------------
|
|
||||||
# Script execution begins here.
|
|
||||||
|
|
||||||
# Find out if the script is being run by a deposit box or an employee.
|
|
||||||
if whoami.Name.find('Deposit Box') > -1:
|
|
||||||
ScriptParm = Crossfire.ScriptParameters()
|
|
||||||
if ScriptParm == 'Close':
|
|
||||||
deposit_box_close()
|
|
||||||
else:
|
|
||||||
text = Crossfire.WhatIsMessage().split()
|
text = Crossfire.WhatIsMessage().split()
|
||||||
|
|
||||||
if text[0] == "learn":
|
if text[0] == "learn":
|
||||||
cmd_help()
|
cmd_help()
|
||||||
elif text[0] == "profits":
|
elif text[0] == "profits":
|
||||||
|
@ -304,6 +219,17 @@ else:
|
||||||
whoami.Say("Hello, what can I help you with today?")
|
whoami.Say("Hello, what can I help you with today?")
|
||||||
Crossfire.AddReply("learn", "I want to learn how to use the bank.")
|
Crossfire.AddReply("learn", "I want to learn how to use the bank.")
|
||||||
|
|
||||||
# Close bank database (required) and return.
|
# Find out if the script is being run by a deposit box or an employee.
|
||||||
bank.close()
|
if whoami.Name.find('Deposit Box') > -1:
|
||||||
|
ScriptParm = Crossfire.ScriptParameters()
|
||||||
|
if ScriptParm == 'Close':
|
||||||
|
deposit_box_close()
|
||||||
|
else:
|
||||||
Crossfire.SetReturnValue(1)
|
Crossfire.SetReturnValue(1)
|
||||||
|
try:
|
||||||
|
main_employee()
|
||||||
|
except ValueError:
|
||||||
|
whoami.Say("I don't know how much money that is.")
|
||||||
|
|
||||||
|
# Close bank database (required) and return.
|
||||||
|
bank.close()
|
||||||
|
|
Loading…
Reference in New Issue