-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.py
296 lines (237 loc) · 8.04 KB
/
config.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
from data.models import *
from lib.static import Colors, set_color
import json
# random world seed ...
WORLD_SEED = 1024
# init attributes
ATTRIBUTES = {'level': 1, 'experience': 1, 'health': 10, 'mana': 10, 'strength': 10, 'dexterity': 10, 'zonex': 0, 'zoney': 0}
class ReadJson(object):
def __init__(self, path):
try:
self.__json_data = open(path)
self.data = json.load(self.__json_data)
except Exception, e:
print e
class User(object):
def __init__(self, name, entity, char, inventory):
self._username = name
self.entity = entity
self._items = []
self._info = ''
self.char = char
self.maxhealth = ATTRIBUTES['health'] + char.level * 2
self.maxmana = ATTRIBUTES['mana'] + char.level
self.inventory = inventory
@property
def username(self):
return self._username
@username.setter
def username(self, value):
self._username = value
@property
def items(self):
query = CharItem.select(CharItem, Items).join(Items).where(CharItem.char == self.char)
item_list = []
for char_item in query:
item_list.append(char_item)
return item_list
#return [config.items[char_item.item.name] for char_item in CharItem.get(CharItem.char == self.char)]
def get_items_by_categories(self, categories = None):
query = CharItem.select(CharItem, Items).join(Items).where((CharItem.char == self.char) & (Items.category << categories if categories else True) & (CharItem.equipped == False)).group_by(Items.name)
item_list = []
for char_item in query:
item_list.append(char_item)
return item_list
@items.setter
def items(self, value):
item = Items.get(Items.name == value.name)
CharItem.create(char=self.char, item=item, condition=item.condition, equipped=False)
def equip_item(self, value):
item = Items.get(Items.name == value.name)
char_item = CharItem.select().join(Items).where((CharItem.char == self.char) & (Items.category == value.category) & (CharItem.equipped == True))
citem = CharItem.get((CharItem.char == self.char) & (CharItem.item == item))
if char_item.count() == 0:
citem.equipped = True
citem.save()
else:
char_item = char_item.get()
char_item.equipped = False
char_item.save()
citem.equipped = True
citem.save()
def unequip_item(self, value):
item = Items.get(Items.name == value.name)
char_item = CharItem.select().join(Items).where((CharItem.char == self.char) & (Items.category == value.category) & (CharItem.equipped == True)).get()
char_item.equipped = False
char_item.save()
def delete_charitem(self, value):
value.delete_instance()
def remove_items(self, items):
for i in items:
self._items.remove(i)
def items_amount(self):
return CharItem.select(CharItem, Items).join(Items).where((CharItem.char == self.char) & (CharItem.equipped == False)).group_by(Items.name).count()
def item_amount(self, item):
i = Items.get(Items.name == item.item.name)
query = CharItem.select().where((CharItem.char == self.char) & (CharItem.item == i))
return query.count()
def get_equipped_item_by_category(self, category):
try:
char_item = CharItem.select().join(Items).where((CharItem.char == self.char) & (Items.category == category) & (CharItem.equipped == True)).get()
return char_item
except:
return None
@property
def info(self):
return self._info
@info.setter
def info(self, value):
self._info = value
@property
def level(self):
return self.char.level
@level.setter
def level(self, value):
self.char.level = value
self.char.save()
@property
def experience(self):
return self.char.experience
@experience.setter
def experience(self, value):
self.char.experience = value
self.char.save()
@property
def health(self):
return self.char.health
@health.setter
def health(self, value):
self.char.health = value
self.char.save()
@property
def mana(self):
return self.char.mana
@mana.setter
def mana(self, value):
self.char.mana = value
self.char.save()
@property
def strength(self):
return self.char.strength
@strength.setter
def strength(self, value):
self.char.strength = value
self.char.save()
@property
def dexterity(self):
return self.char.dexterity
@dexterity.setter
def dexterity(self, value):
self.char.dexterity = value
self.char.save()
@property
def zonex(self):
return self.char.zonex
@zonex.setter
def zonex(self, value):
self.char.zonex = value
self.char.save()
@property
def zoney(self):
return self.char.zoney
@zoney.setter
def zoney(self, value):
self.char.zoney = value
self.char.save()
class Item(object):
def __init__(self, name, attributes):
self._name = name
self._readname = attributes['name']
self._level = attributes['level']
self._attributes = attributes['attributes']
self._rate = attributes['rate']
self._enemies = attributes['enemies']
self._condition = attributes['condition']
self._category = attributes['category']
# FIXME: link to useable classes
@property
def name(self):
return self._name
@property
def readname(self):
return self._readname
@property
def level(self):
return self._level
@property
def attributes(self):
return self._attributes
@property
def rate(self):
return self._rate
@property
def enemies(self):
return self._enemies
@property
def condition(self):
return self._condition
@property
def category(self):
return self._category
class Enemy(object):
def __init__(self, name, attributes):
self._name = name
self._health = attributes['health']
self._level = attributes['level']
self._sign = attributes['sign']
self._colored_sign = attributes['sign']
self._etype = attributes['type']
if self._etype in (3, ):
self._colored_sign = set_color(self._sign, Colors.REDBOLD)
@property
def name(self):
return self._name
@property
def health(self):
return self._health
@health.setter
def health(self, value):
self._health = value
@property
def level(self):
return self._level
@property
def sign(self):
return self._sign
@property
def colored_sign(self):
return self._colored_sign
@property
def etype(self):
return self._etype
class Config(object):
def __init__(self):
self.enemies = {}
self.items = {}
# create db tables
try:
db.connect()
db.create_tables([Users, Char, Items, CharItem])
except Exception, e:
print e
# init enemies
enemies = ReadJson('data/enemies.json').data
for name, attributes in enemies.items():
self.enemies[name] = Enemy(name, attributes)
# init items
items = ReadJson('data/items.json').data
for name, attributes in items.items():
self.items[name] = Item(name, attributes)
if not Items.select().count():
for k, v in items.items():
attributes = v['attributes']
Items.create(name = k, readname = v['name'], level = v['level'], health = attributes[0], mana = attributes[1], strength = attributes[2], dexterity = attributes[3], rate = v['rate'], enemies = v['enemies'], condition = v['condition'], category = v['category'])
@property
def enemies(self):
return self.enemies
config = Config()