Crossfire Server, Trunk
banksay.py
Go to the documentation of this file.
1 """
2 Created by: Joris Bontje <jbontje@suespammers.org>
3 
4 This module implements banking in Crossfire. It provides the 'say' event for
5 bank tellers.
6 """
7 
8 import random
9 
10 import Crossfire
11 import CFBank
12 import CFItemBroker
13 
14 activator = Crossfire.WhoIsActivator()
15 whoami = Crossfire.WhoAmI()
16 x = activator.X
17 y = activator.Y
18 
19 # Associate coin names with their corresponding values in silver.
20 CoinTypes = {
21  'SILVER': 1,
22  'GOLD': 10,
23  'PLATINUM': 50,
24  'JADE': 5000,
25  'AMBERIUM': 500000,
26  'IMPERIAL NOTE': 10000,
27  '10 IMPERIAL NOTE': 100000,
28  '100 IMPERIAL NOTE': 1000000,
29 }
30 
31 # Associate coin names with their corresponding archetypes.
32 ArchType = {
33  'SILVER': 'silvercoin',
34  'GOLD': 'goldcoin',
35  'PLATINUM': 'platinacoin',
36  'JADE': 'jadecoin',
37  'AMBERIUM': 'ambercoin',
38  'IMPERIAL NOTE': 'imperial',
39  '10 IMPERIAL NOTE': 'imperial10',
40  '100 IMPERIAL NOTE': 'imperial100',
41 }
42 
43 # Define several 'thank-you' messages which are chosen at random.
44 thanks_message = [
45  'Thank you for banking the Imperial Way.',
46  'Thank you for banking the Imperial Way.',
47  'Thank you, please come again.',
48  'Thank you, please come again.',
49  'Thank you for your patronage.',
50  'Thank you for your patronage.',
51  'Thank you, have a nice day.',
52  'Thank you, have a nice day.',
53  'Thank you. "Service" is our middle name.',
54  'Thank you. "Service" is our middle name.',
55  'Thank you. Hows about a big slobbery kiss?',
56 ]
57 
59  """Piece together several arguments to form a coin name."""
60  coinName = str.join(" ", argv)
61  # Remove the trailing 's' from the coin name.
62  if coinName[-1] == 's':
63  coinName = coinName[:-1]
64  return coinName
65 
66 def getExchangeRate(coinName):
67  """Return the exchange rate for the given type of coin."""
68  if coinName.upper() in CoinTypes:
69  return CoinTypes[coinName.upper()]
70  else:
71  return None
72 
73 def do_deposit(player, amount):
74  """Deposit the given amount for the player."""
75  with CFBank.open() as bank:
76  bank.deposit(player.Name, amount)
77  whoami.Say("%s credited to your account." \
78  % Crossfire.CostStringFromValue(amount))
79 
80 def cmd_help():
81  """Print a help message for the player."""
82  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! What would you like to do?")
83 
84  Crossfire.AddReply("coins", "I'd like to know more about existing coins.")
85  Crossfire.AddReply("balance", "I want to check my balance.")
86  Crossfire.AddReply("deposit", "I'd like to deposit some money.")
87  Crossfire.AddReply("withdraw", "I'd like to withdraw some money.")
88  Crossfire.AddReply("convert", "I'd like to compute money conversions.")
89 
90 def cmd_balance(argv):
91  """Find out how much money the player has in his/her account."""
92  balance = 0
93  with CFBank.open() as bank:
94  balance = bank.getbalance(activator.Name)
95  if len(argv) >= 2:
96  # Give balance using the desired coin type.
97  coinName = getCoinNameFromArgs(argv[1:])
98  exchange_rate = getExchangeRate(coinName)
99  if exchange_rate is None:
100  whoami.Say("Hmm... I've never seen that kind of money.")
101  return
102  if balance != 0:
103  balance /= exchange_rate * 1.0
104  whoami.Say("You have %.3f %s in the bank." % (balance, coinName))
105  else:
106  whoami.Say("Sorry, you have no balance.")
107  else:
108  whoami.Say("You have %s in the bank." % \
109  Crossfire.CostStringFromValue(balance))
110 
111 def cmd_deposit(text):
112  """Deposit a certain amount of money."""
113  if len(text) >= 3:
114  coinName = getCoinNameFromArgs(text[2:])
115  exchange_rate = getExchangeRate(coinName)
116  amount = int(text[1])
117  if exchange_rate is None:
118  whoami.Say("Hmm... I've never seen that kind of money.")
119  return
120  if amount < 0:
121  whoami.Say("Regulations prohibit negative deposits.")
122  return
123 
124  # Make sure the player has enough cash on hand.
125  actualAmount = amount * exchange_rate
126  if activator.PayAmount(actualAmount):
127  do_deposit(activator, actualAmount)
128  else:
129  whoami.Say("But you don't have that much in your inventory!")
130  else:
131  whoami.Say("What would you like to deposit?")
132  Crossfire.AddReply("deposit <amount> <coin type>", "Some money.")
133 
134 def cmd_withdraw(argv):
135  """Withdraw money from the player's account."""
136  if len(argv) >= 3:
137  coinName = getCoinNameFromArgs(argv[2:])
138  exchange_rate = getExchangeRate(coinName)
139  amount = int(argv[1])
140  if exchange_rate is None:
141  whoami.Say("Hmm... I've never seen that kind of money.")
142  return
143  if amount <= 0:
144  whoami.Say("Sorry, you can't withdraw that amount.")
145  return
146 
147  # Make sure the player has sufficient funds.
148  with CFBank.open() as bank:
149  if bank.withdraw(activator.Name, amount * exchange_rate):
150  message = "%d %s withdrawn from your account. %s" \
151  % (amount, coinName, random.choice(thanks_message))
152 
153  # Drop the money and have the player pick it up.
154  withdrawal = activator.Map.CreateObject(
155  ArchType.get(coinName.upper()), x, y)
156  CFItemBroker.Item(withdrawal).add(amount)
157  activator.Take(withdrawal)
158  else:
159  message = "I'm sorry, you don't have enough money."
160  else:
161  message = "How much money would you like to withdraw?"
162  Crossfire.AddReply("withdraw <amount> <coin name>", "This much!")
163 
164  whoami.Say(message)
165 
166 def cmd_convert(argc):
167 
168  def attempt(argc):
169  if len(argc) < 4 or argc[3] != "to":
170  return False
171 
172  fromCoin = getCoinNameFromArgs(argc[2:3])
173  toCoin = getCoinNameFromArgs(argc[4:5])
174  if not toCoin or not fromCoin:
175  return False
176 
177  fromValue = getExchangeRate(fromCoin)
178  toValue = getExchangeRate(toCoin)
179 
180  fromAmount = int(argc[1])
181  amount = int(fromAmount * fromValue / toValue)
182  whoami.Say("{} {} coin{} would be {} {} coin{}".format(fromAmount, fromCoin, "s" if fromAmount > 1 else "", amount, toCoin, "s" if amount > 1 else ""))
183  return True
184 
185  if not attempt(argc):
186  whoami.Say("Sorry, I don't understand what you want to convert.\nTry something like \"4 platinum to silver\" please.")
187 
188 def cmd_coins(_):
189  whoami.Say("""The smallest coin available is the silver coin.
190 The gold coin is worth 10 silver coins, while the platinum coin is worth 50.
191 A jade coin is worth 5000 silver, and an amberium 500000.
192 
193 There also exist imperial notes, worth 10000 silver coins.
194 """)
195 
197  text = Crossfire.WhatIsMessage().split()
198  if text[0] == "learn":
199  cmd_help()
200  elif text[0] == "balance":
201  cmd_balance(text)
202  elif text[0] == "deposit":
203  cmd_deposit(text)
204  elif text[0] == "withdraw":
205  cmd_withdraw(text)
206  elif text[0] == "convert":
207  cmd_convert(text)
208  elif text[0] == "coins":
209  cmd_coins(text)
210  else:
211  whoami.Say("Hello, what can I help you with today?")
212  Crossfire.AddReply("learn", "I want to learn how to use the bank.")
213 
214 Crossfire.SetReturnValue(1)
215 try:
216  main_employee()
217 except ValueError:
218  whoami.Say("I don't know how much money that is.")
CFBank.open
def open()
Definition: CFBank.py:70
banksay.do_deposit
def do_deposit(player, amount)
Definition: banksay.py:73
banksay.cmd_help
def cmd_help()
Definition: banksay.py:80
banksay.cmd_deposit
def cmd_deposit(text)
Definition: banksay.py:111
banksay.cmd_balance
def cmd_balance(argv)
Definition: banksay.py:90
banksay.getExchangeRate
def getExchangeRate(coinName)
Definition: banksay.py:66
banksay.cmd_convert
def cmd_convert(argc)
Definition: banksay.py:166
banksay.cmd_withdraw
def cmd_withdraw(argv)
Definition: banksay.py:134
banksay.cmd_coins
def cmd_coins(_)
Definition: banksay.py:188
CFItemBroker.Item
Definition: CFItemBroker.py:15
banksay.getCoinNameFromArgs
def getCoinNameFromArgs(argv)
Definition: banksay.py:58
make_face_from_files.int
int
Definition: make_face_from_files.py:26
banksay.main_employee
def main_employee()
Definition: banksay.py:196
split
static std::vector< std::string > split(const std::string &field, const std::string &by)
Definition: mapper.cpp:2603