Implement common utility methods.

Reimplment 'balance' and 'withdraw' using the new utility methods. Move
'deposit' into its own function but don't touch it yet.

git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@18863 282e977c-c81d-0410-88c4-b93c2d0d6712
master
partmedia 2013-08-03 00:26:50 +00:00
parent 0528e6f203
commit c0f1d72783
1 changed files with 245 additions and 233 deletions

View File

@ -67,6 +67,32 @@ thanks_message = [
'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):
coinName = ""
# Take the arguments and piece together the full coin name.
for argument in argv:
coinName += argument + ' '
# 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 the exchange rate for the given type of coin.
def getExchangeRate(coinName):
# Try to find exchange rate, set to None if we can't.
if coinName.upper() in CoinTypes:
return int(CoinTypes.get(coinName.upper()))
else:
return None
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Called when the deposit box (ATM) is opened. # Called when the deposit box (ATM) is opened.
# TODO: Fix the ATM. # TODO: Fix the ATM.
@ -243,83 +269,32 @@ def cmd_zero_balance():
whoami.Say(message) whoami.Say(message)
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Withdraw money from the player's account. # Find out how much money the player has in his/her account.
def cmd_withdraw(argv): def cmd_balance(argv):
argc = len(argv) balance = bank.getbalance(activatorname)
# Withdraw a certain number of imperial notes. if len(argv) >= 2:
if argc == 2: coinName = getCoinNameFromArgs(argv[1:])
message = "Sorry, I don't know how to do that yet." exchange_rate = getExchangeRate(coinName)
# Withdraw a certain number of a certain coin.
elif argc >= 3:
amount = int(argv[1])
coinName = ""
# Take the arguments and piece together the full coin name.
for argument in argv[2:]:
coinName += argument + ' '
# 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]
# Try to find exchange rate, set to None if we can't.
if coinName.upper() in CoinTypes:
exchange_rate = int(CoinTypes.get(coinName.upper()))
else:
exchange_rate = None
# Warn the player if no such coin type exists.
if exchange_rate is None: if exchange_rate is None:
message = "I'm sorry, I don't know what type of money that is. " \ whoami.Say("Hmm... I've never seen that kind of money.")
"Valid types of money are silver, gold, platinum, jade, amberium, Imperial Note, 10 Imperial Note, and 100 Imperial Note." return
# Don't let the player withdraw negative money.
elif amount <= 0:
message = "Hey, you can't withdraw a negative amount!"
# Make sure the player has sufficient funds.
elif bank.withdraw(activatorname, amount * exchange_rate):
message = "%d %s withdrawn from your account. %s" \
% (amount, coinName, random.choice(thanks_message))
# Drop the money and have the player pick it up. if balance != 0:
withdrawal = activator.Map.CreateObject(ArchType.get( \ balance /= exchange_rate * 1.0;
coinName.upper()), x, y) message = "You have %2f %s in the bank." % (balance, coinName)
CFItemBroker.Item(withdrawal).add(amount)
activator.Take(withdrawal)
else: else:
message = "I'm sorry, you don't have enough money." message = "Sorry, you have no balance."
else:
message = "Usage:\n" \
"\twithdraw <amount in imperials>\n" \
"\twithdraw <amount> <coin type>"
whoami.Say(message) whoami.Say(message);
else:
# No need to reimplement this command; just recurse.
cmd_balance(["balance", "silver"])
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Script execution begins here. # Deposit money or checks.
def cmd_deposit(text):
# 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':
depositBoxClose()
else:
depositBoxOpen()
else:
text = Crossfire.WhatIsMessage().split()
message = ""
if text[0] == 'help' or text[0] == 'yes':
cmd_help()
elif text[0] == 'zero-balance':
cmd_zero_balance()
elif text[0] == 'profits':
cmd_show_profits()
elif text[0] == 'deposit':
if len(text) == 2: if len(text) == 2:
if text[1] == 'check': if text[1] == 'check':
whoami.Say('x') whoami.Say('x')
@ -497,6 +472,74 @@ else:
# message = 'Usage "deposit <amount> <cointype>\n or deposit <issuer>\'s check"' # message = 'Usage "deposit <amount> <cointype>\n or deposit <issuer>\'s check"'
pass pass
whoami.Say(message)
# ----------------------------------------------------------------------------
# Withdraw money from the player's account.
def cmd_withdraw(argv):
argc = len(argv)
# Withdraw a certain number of imperial notes.
if argc == 2:
message = "Sorry, I don't know how to do that yet."
# Withdraw a certain number of a certain coin.
elif argc >= 3:
coinName = getCoinNameFromArgs(argv[2:])
exchange_rate = getExchangeRate(coinName)
amount = int(argv[1])
if exchange_rate is None:
whoami.Say("Hmm... I've never seen that kind of money.")
return
# Don't let the player withdraw negative money.
if amount <= 0:
whoami.Say("Hey, you can't withdraw a negative amount!")
return
# Make sure the player has sufficient funds.
if bank.withdraw(activatorname, amount * exchange_rate):
message = "%d %s withdrawn from your account. %s" \
% (amount, coinName, random.choice(thanks_message))
# Drop the money and have the player pick it up.
withdrawal = activator.Map.CreateObject(ArchType.get( \
coinName.upper()), x, y)
CFItemBroker.Item(withdrawal).add(amount)
activator.Take(withdrawal)
else:
message = "I'm sorry, you don't have enough money."
else:
message = "Usage:\n" \
"\twithdraw <amount in imperials>\n" \
"\twithdraw <amount> <coin type>"
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':
depositBoxClose()
else:
depositBoxOpen()
else:
text = Crossfire.WhatIsMessage().split()
message = ""
if text[0] == 'help' or text[0] == 'yes':
cmd_help()
elif text[0] == 'zero-balance':
cmd_zero_balance()
elif text[0] == 'profits':
cmd_show_profits()
elif text[0] == 'deposit':
cmd_deposit(text)
elif text[0] == 'withdraw': elif text[0] == 'withdraw':
cmd_withdraw(text) cmd_withdraw(text)
elif text[0] == 'exchange': elif text[0] == 'exchange':
@ -537,38 +580,7 @@ else:
message = \ message = \
'Usage "exchange <amount>" (imperials to platinum coins)' 'Usage "exchange <amount>" (imperials to platinum coins)'
elif text[0] == 'balance': elif text[0] == 'balance':
cmd_balance(text)
if len(text) >= 2:
Type = ''
for i in text[1:]:
Type += i + ' '
Type = Type[:len(Type) - 1]
if Type[len(Type) - 1] == 's':
Type = Type[:len(Type) - 1]
exchange_rate = CoinTypes.get(Type.upper())
balance = bank.getbalance(activatorname) / exchange_rate
message = 'Amount in bank: '
if int(balance) == 1:
message += '1 ' + Type + '.'
elif int(balance) > 1:
message += str(int(balance)) + ' ' + Type
if Type.find('Note') == -1:
message += ' Coins.'
else:
message = 'Your balance is less than 1 ' + Type + '.'
else:
balance = bank.getbalance(activatorname)
if balance == 1:
message = 'Amount in bank: 1 silver coin'
elif balance:
message = \
'Amount in bank: %d pounds sterling or %d silver coins.' \
% (balance * 0.029394968, balance)
else:
message = 'Sorry, you have no balance.'
elif text[0] == 'checks': elif text[0] == 'checks':
balance = bank.getbalance(activatorname) balance = bank.getbalance(activatorname)