Implement and use new silver-to-string function.

Also take this opportunity to fix the bank profit calculations.

git-svn-id: svn://svn.code.sf.net/p/crossfire/code/maps/trunk@18874 282e977c-c81d-0410-88c4-b93c2d0d6712
master
partmedia 2013-08-03 00:28:40 +00:00
parent eba15f3400
commit 84764f325b
1 changed files with 27 additions and 11 deletions

View File

@ -40,6 +40,10 @@ CoinTypes = {
'100 IMPERIAL NOTE': 1000000, '100 IMPERIAL NOTE': 1000000,
} }
# The names of the most common coin types (must be least to greatest value).
# Do not include the least valuable coin (currently "SILVER").
commonCoinNames = ["GOLD", "PLATINUM", "JADE", "AMBERIUM"]
# Associate coin names with their corresponding archetypes. # Associate coin names with their corresponding archetypes.
ArchType = { ArchType = {
'SILVER': 'silvercoin', 'SILVER': 'silvercoin',
@ -93,6 +97,19 @@ def getExchangeRate(coinName):
else: else:
return None return None
# ----------------------------------------------------------------------------
# Return a string representing the given amount in silver.
def strAmount(amount):
# Find the most valuable coin type we can use (amount / value >= 1).
# The [::-1] syntax is a hack to reverse 'commonCoinNames'.
for coinName in commonCoinNames[::-1]:
value = CoinTypes[coinName]
if amount >= value:
return "%.3f %s" % (float(amount) / value, coinName.lower())
# If no suitable coin was found, use the base value (silver).
return "%d %s" % (amount, "silver")
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Called when the deposit box (ATM) is opened. # Called when the deposit box (ATM) is opened.
# TODO: Fix the ATM. # TODO: Fix the ATM.
@ -253,8 +270,8 @@ def cmd_help():
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Show the profits made by the bank. # Show the profits made by the bank.
def cmd_show_profits(): def cmd_show_profits():
message = "To date, the Imperial Bank of Skud has made %s " \ message = "To date, the Imperial Bank of Skud has made %s in profit." \
"platinum in profit." % str(bank.getbalance(Skuds) / 50.0) % strAmount(bank.getbalance(Skuds))
whoami.Say(message) whoami.Say(message)
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
@ -283,14 +300,13 @@ def cmd_balance(argv):
if balance != 0: if balance != 0:
balance /= exchange_rate * 1.0; balance /= exchange_rate * 1.0;
message = "You have %s %s in the bank." % (str(balance), coinName) message = "You have %.3f %s in the bank." % (balance, coinName)
else: else:
message = "Sorry, you have no balance." message = "Sorry, you have no balance."
whoami.Say(message); whoami.Say(message);
else: else:
# No need to reimplement this command; just recurse. whoami.Say("You have " + strAmount(balance) + " in the bank.")
cmd_balance(["balance", "silver"])
# ---------------------------------------------------------------------------- # ----------------------------------------------------------------------------
# Deposit a certain amount of money or the value of a check. # Deposit a certain amount of money or the value of a check.
@ -311,13 +327,13 @@ def cmd_deposit(text):
return return
# Make sure the player has enough cash on hand. # Make sure the player has enough cash on hand.
if activator.PayAmount(amount * exchange_rate): actualAmount = amount * exchange_rate
bank.deposit(activatorname, int(amount * exchange_rate / fees)) if activator.PayAmount(actualAmount):
bank.deposit(Skuds, int(amount * exchange_rate bank.deposit(activatorname, int(actualAmount / fees))
- amount * exchange_rate / fees)) bank.deposit(Skuds, actualAmount - int(actualAmount / fees))
message = "%d %s received, %s %s deposited to your account. %s" \ message = "%d %s received, %s deposited to your account. %s" \
% (amount, coinName, str(amount / fees), coinName, % (amount, coinName, strAmount(int(actualAmount / fees)),
random.choice(thanks_message)) random.choice(thanks_message))
else: else:
message = "But you don't have that much in your inventory!" message = "But you don't have that much in your inventory!"