Crossfire Server, Trunk
powerbroker.py
Go to the documentation of this file.
1 """
2 powerbroker.py -- glowing crystals that buy and sell mana
3 
4 Usage: add an event_apply and event_say to a non-pickable glowing crystal.
5 """
6 
7 import math
8 
9 import Crossfire
10 import CFItemBroker
11 
12 N0 = 10000 # initial stock
13 P0 = 10 # price per mana in silver at initial stock, N0
14 me = Crossfire.WhoAmI()
15 
16 def charge(payer, quantity):
17  return payer.PayAmount(quantity) == 1
18 
19 def pay(payee, quantity):
20  # we can't guarentee that the player has any particular type of coin already
21  # so create the object first, then add 1 less than the total.
22  if quantity >= 50:
23  item = payee.CreateObject('platinum coin')
24  CFItemBroker.Item(item).add(int(quantity/50))
25  if quantity % 50 > 0:
26  item = payee.CreateObject('gold coin')
27  CFItemBroker.Item(item).add(int((quantity % 50)/10))
28  if quantity % 10 > 0:
29  item = payee.CreateObject('silver coin')
30  CFItemBroker.Item(item).add(int(quantity % 10))
31 
32 def on_apply():
33  Crossfire.SetReturnValue(0)
34 
35 def profit():
36  return 0.1
37 
38 def mana_stock():
39  stock = Crossfire.GetPrivateDictionary().get('total_mana')
40  if stock is None:
41  stock = N0
42  return stock
43 
44 def change_stock(amount):
45  Crossfire.GetPrivateDictionary()['total_mana'] = mana_stock() + amount
46 
47 def base_price(sp):
48  # 1 gold per spell point at nominal stock
49  return sp * P0 * (1 + 1/N0*(N0 - mana_stock()))
50 
51 def sell_price(sp):
52  return math.floor(base_price(sp)*(1 - profit()))
53 
54 def buy_price(sp):
55  return max(1, math.ceil(base_price(sp)*(1 + profit())))
56 
57 def buy_mana():
58  message = Crossfire.WhatIsMessage().lower()
59  try:
60  amount = int(message)
61  max_buy = min(me.MaxSP, mana_stock())
62  if mana_stock() == 0:
63  me.Say("Sorry, we are out of stock right now. Come back later.")
64  return
65  if amount <= 0:
66  me.Say("If you want to sell mana, transfer it into the power broker.")
67  return
68  if amount > max_buy:
69  me.Say("I can only sell you up to %d mana right now." % max_buy)
70  amount = max_buy
71  price = buy_price(amount)
72  def do_buy_mana():
73  if charge(Crossfire.WhoIsActivator(), price):
74  me.Say("Bought %d mana for %s." % (amount, Crossfire.CostStringFromValue(price)))
75  me.SP += amount
76  change_stock(-amount)
77  else:
78  me.Say("You can't afford that!")
79  confirm("%d mana would cost you %s. Buy?" % (amount, Crossfire.CostStringFromValue(price)), do_buy_mana)
80  except ValueError:
81  me.Say("How much mana would you like to buy? %d silver per mana point." % buy_price(1))
82 
83 def sell_mana():
84  message = Crossfire.WhatIsMessage().lower()
85  price = sell_price(me.SP)
86  if price <= 0:
87  me.Say("Sorry, we're not interested in buying mana right now.")
88  return
89 
90  def do_sell_mana():
91  me.Say("Sold %d mana for %s." % (me.SP, Crossfire.CostStringFromValue(price)))
92  pay(Crossfire.WhoIsActivator(), price)
93  change_stock(me.SP)
94  me.SP = 0
95  confirm("Sell %d mana for %s?" % (me.SP, Crossfire.CostStringFromValue(price)), do_sell_mana)
96 
97 def confirm(text, action):
98  me.Say(text)
99  Crossfire.AddReply("yes", "Yes!")
100  Crossfire.AddReply("no", "No, thank you.")
101  Crossfire.GetPrivateDictionary()['confirm_action'] = action
102 
104  action = Crossfire.GetPrivateDictionary().get('confirm_action')
105  if action is not None:
106  reply = Crossfire.WhatIsMessage().lower()
107  if reply == "yes":
108  action()
109  del Crossfire.GetPrivateDictionary()['confirm_action']
110  elif reply == "no":
111  me.Say("Thank you, come again!")
112  del Crossfire.GetPrivateDictionary()['confirm_action']
113  else:
114  me.Say("Please answer either 'yes' or 'no'.")
115  return True
116  return False
117 
118 def status():
119  me.Say("%d mana in stock, buy %d/sell %d silver per mana." % (mana_stock(), buy_price(1), sell_price(1)))
120 
121 def stock(message):
122  try:
123  qty = int(message.split()[1])
124  change_stock(qty)
125  me.Say("Added %d mana to stock." % qty)
126  status()
127  except Exception:
128  me.Say("I didn't understand that.")
129 
130 def on_say():
131  if check_confirm():
132  return # don't do anything else
133 
134  message = Crossfire.WhatIsMessage().lower()
135  if message == 'status' and Crossfire.WhoIsActivator().DungeonMaster:
136  status()
137  elif message.split()[0] == 'stock' and Crossfire.WhoIsActivator().DungeonMaster:
138  stock(message)
139  elif me.SP > 0:
140  sell_mana()
141  else:
142  buy_mana()
143 
144 if Crossfire.WhatIsEvent().Subtype == 1:
145  on_apply()
146 elif Crossfire.WhatIsEvent().Subtype == 6:
147  on_say()
powerbroker.charge
def charge(payer, quantity)
Definition: powerbroker.py:16
powerbroker.check_confirm
def check_confirm()
Definition: powerbroker.py:103
powerbroker.base_price
def base_price(sp)
Definition: powerbroker.py:47
powerbroker.sell_mana
def sell_mana()
Definition: powerbroker.py:83
powerbroker.change_stock
def change_stock(amount)
Definition: powerbroker.py:44
powerbroker.pay
def pay(payee, quantity)
Definition: powerbroker.py:19
convert.action
action
Definition: convert.py:25
say.max
dictionary max
Definition: say.py:148
powerbroker.buy_mana
def buy_mana()
Definition: powerbroker.py:57
powerbroker.on_say
def on_say()
Definition: powerbroker.py:130
powerbroker.confirm
def confirm(text, action)
Definition: powerbroker.py:97
powerbroker.stock
def stock(message)
Definition: powerbroker.py:121
powerbroker.sell_price
def sell_price(sp)
Definition: powerbroker.py:51
CFItemBroker.Item
Definition: CFItemBroker.py:15
powerbroker.status
def status()
Definition: powerbroker.py:118
nlohmann::detail::get
auto get(const nlohmann::detail::iteration_proxy_value< IteratorType > &i) -> decltype(i.key())
Definition: json.hpp:4475
make_face_from_files.int
int
Definition: make_face_from_files.py:32
powerbroker.mana_stock
def mana_stock()
Definition: powerbroker.py:38
powerbroker.buy_price
def buy_price(sp)
Definition: powerbroker.py:54
powerbroker.on_apply
def on_apply()
Definition: powerbroker.py:32
powerbroker.profit
def profit()
Definition: powerbroker.py:35