forked from orphu/mcdungeon
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shop.py
158 lines (140 loc) · 4.95 KB
/
shop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import ConfigParser
import os
import sys
import random
from copy import copy
import items
import loottable
_shops = []
class ShopInfo(object):
def __init__(self, name, profession = 0, trades = [], free_sample = 'air'):
self.name = str(name)
self.profession = int(profession)
self.free_sample = free_sample
self.trades = []
for t in trades:
self.AddTrade(t)
def prepareShop(self):
trades = []
for t in self.trades:
if random.randint(1, 100) <= t.chance:
t.prepareTrade()
trades.append(t)
self.trades = trades
def addTrade(self, trade):
self.trades.append(trade)
def __str__ (self):
return 'Name: %s, Prof: %d, Trades: %s'%(
self.name,
self.profession,
self.trades)
class TradeInfo(object):
def __init__(self, chance, max_uses, output, input, input2 = None, limited = False):
self.chance = int(chance)
self.max_uses = int(max_uses)
self.output = output
self.input = input
self.input2 = input2
self.limited = limited
def prepareTrade(self):
self.outputLoot = self.stringToLoot(self.output)
self.inputLoot = self.stringToLoot(self.input)
self.input2Loot = self.stringToLoot(self.input2)
def stringToLoot(self,inloot):
if inloot == None:
return None
loot = inloot.lower().split(',')
i = random.choice(loot[0].split('/'))
item = items.byName(i)
if item == None:
sys.exit('%s not found'% i)
if len(loot) < 2:
count = 1
else:
count = int(loot[1])
if len(loot) < 3:
enchantments = []
if item.name.startswith('magic_'):
ench_level = 0
if len(item.ench) > 0:
for e in item.ench.split(','):
k = int(e.split('-')[0])
v = int(e.split('-')[-1])
enchantments.append(dict({'id': k, 'lvl': v}))
else:
enchantments = list(loottable.enchant(item.name, int(loot[2])))
return loottable.Loot(0,
count,
item.value,
item.data,
enchantments,
item.p_effect,
item.customname,
item.flag,
item.flagparam,
item.lore,
item.file)
def __str__ (self):
return 'Chance: %s, Max U: %d,\nOutput: %s\nInput: %s\nInput2: %s'%(
self.chance,
self.max_uses,
self.output,
self.input,
self.input2)
def LoadShop(filename):
global _shops
temp = os.path.join(sys.path[0], 'shops', filename)
try:
fh = open(temp)
fh.close
filename = temp
except:
filename = os.path.join('shops', filename)
parser = ConfigParser.SafeConfigParser()
#print 'Reading config from', filename, '...'
try:
parser.readfp(open(filename))
except Exception, e:
print "Failed to read config file!"
sys.exit(e.message)
name = parser.get('shop', 'name')
profession = parser.get('shop', 'profession_id')
free_sample = parser.get('shop', 'free_sample')
shop = ShopInfo(name,profession,free_sample=free_sample)
maxtrade = 1
while (parser.has_section('trade%d' % maxtrade)):
chance = parser.get('trade%d' % maxtrade, 'chance')
max_uses = parser.get('trade%d' % maxtrade, 'max_uses')
input = parser.get('trade%d' % maxtrade, 'input')
if parser.has_option('trade%d' % maxtrade, 'input2'):
input2 = parser.get('trade%d' % maxtrade, 'input2')
else:
input2 = None
limited = False
if parser.has_option('trade%d' % maxtrade, 'limited'):
limited = parser.get('trade%d' % maxtrade, 'limited')
output = parser.get('trade%d' % maxtrade, 'output')
shop.addTrade(TradeInfo(chance,max_uses,output,input,input2,limited=limited))
maxtrade += 1
_shops.append(shop)
def Load(dir_shops):
# Make a list of all the cfg files in the books directory
if os.path.isdir(os.path.join(sys.path[0], dir_shops)):
shop_path = os.path.join(sys.path[0], dir_shops)
elif os.path.isdir(dir_shops):
shop_path = dir_shops
else:
shop_path = ''
shoplist = []
if shop_path != '':
for file in os.listdir(shop_path):
if str(file.lower()).endswith(".cfg"):
shoplist.append(file)
for shop in shoplist:
LoadShop(shop)
print 'Loaded', len(shoplist), 'shops.'
# A Random shop with random trades
def rollShop():
shop = copy(random.choice(_shops))
shop.prepareShop()
return shop