Refactored to stop passing game around.
[realism.git] / engine / shop.py
blob3ed2757a5ccc20447821a9fe307055c5c290e209
1 from items import *
2 from game import game
3 party = game.party
5 class shop(object):
6 def __init__(self, contents):
7 self.contents = contents
8 self.redo_stocked_contents()
10 def redo_stocked_contents(self):
11 self.stocked_contents = [item for item in self.contents if item.count > 0]
13 def getContents(self):
14 return self.stocked_contents
16 def buy(self, stocked_item, num = 1):
17 # Translate stocked_item back into the index for self.contents.
18 item = 0
19 for real_item in self.contents:
20 if real_item == self.stocked_contents[stocked_item]:
21 break
22 item += 1
24 price = self.contents[item].value
25 total = num * price
26 if num > self.contents[item].count:
27 return "The store doesn't have that many.\n"
28 elif party.gold < total:
29 return "Not enough money.\n"
30 party.gold -= total
31 player_item = self.contents[item].split(num)
32 party.got_item(player_item)
33 if self.contents[item].count <= 0:
34 self.redo_stocked_contents()
36 def sell(self, item, num = 1):
37 if party.items[item].count < num:
38 return "You don't have that many.\n"
39 shop_price = party.items[item].value
40 price = shop_price / 2
41 party.gold += num * price
42 for shop_item in self.contents:
43 if shop_item == party.items[item]:
44 shop_item.count += num
45 self.redo_stocked_contents()
46 break
47 party.lost_item(item, num)
49 def make_shop(level=1, weapons = False, armor = False, magic = False, other = False, random = True, contents = None):
50 if random:
51 if contents == None:
52 contents = []
53 if weapons:
54 contents.append(sword.make(1))
55 if armor:
56 contents.append(shield.make(1))
57 if magic:
58 contents.append(fire_stone.make(1))
59 contents.append(healing_stone.make(1))
60 if other:
61 contents.append(potion.make(1))
63 return shop(contents)