-
Notifications
You must be signed in to change notification settings - Fork 2
/
import_rdb.py
executable file
·286 lines (213 loc) · 9.44 KB
/
import_rdb.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
import os, json
import django
from django import db
from multiprocessing import Pool
from tinkerplants.utils import *
os.environ['DJANGO_SETTINGS_MODULE'] = 'aobase.settings'
django.setup()
from tinkertools.models import *
# Clear out the old data entirely
StatValue.objects.all().delete()
Criterion.objects.all().delete()
Spell.objects.all().delete()
SpellData.objects.all().delete()
AttackDefense.objects.all().delete()
AnimationMesh.objects.all().delete()
Item.objects.all().delete()
Action.objects.all().delete()
def import_items(chunk):
import_rdb(chunk, False)
def import_nanos(chunk):
import_rdb(chunk, True)
def import_rdb(data, is_nano):
for item in data:
newItem, created = Item.objects.get_or_create(aoid=item['AOID'])
# print(newItem.aoid)
newItem.name = item.get('Name')
newItem.description = item.get('Description')
newItem.is_nano = is_nano
newItem.save()
for sv in item.get('StatValues'):
stat = sv.get('Stat')
value = sv.get('RawValue')
if stat == 76:
newItem.itemClass = value
elif stat == 54:
newItem.ql = value
statValue, create = StatValue.objects.get_or_create(stat=stat, value=value)
newItem.stats.add(statValue)
if newItem.ql is None:
newItem.ql = 1
if newItem.itemClass is None:
newItem.itemClass = 0
atkdef = item.get('AttackDefenseData')
if atkdef is not None:
newAtkDef = AttackDefense.objects.create()
for atk in atkdef.get('Attack'):
sv, create = StatValue.objects.get_or_create(stat=atk.get('Stat'), value=atk.get('RawValue'))
newAtkDef.attack.add(sv)
for atk in atkdef.get('Defense'):
sv, create = StatValue.objects.get_or_create(stat=atk.get('Stat'), value=atk.get('RawValue'))
newAtkDef.defense.add(sv)
newItem.atkdef = newAtkDef
actionData = item.get('ActionData')
if actionData is not None:
actions = actionData.get('Actions')
if actions is not None:
for action in actions:
newAction = Action()
newAction.action = action['Action']
newAction.save()
crit_idx = 0
for criterion in action.get('Criteria'):
ac = ActionCriterion()
crit, create = Criterion.objects.get_or_create(value1=criterion['Value1'], value2=criterion['Value2'], operator=criterion['Operator'])
ac.action = newAction
ac.criterion = crit
ac.order = crit_idx
ac.save()
newAction.criteria.add(crit)
crit_idx += 1
newAction.item = newItem
newAction.save()
for spellData in item.get('SpellData'):
newSpellData = SpellData()
newSpellData.event = spellData['Event']
newSpellData.save()
for spell in spellData['Items']:
newSpell = Spell.objects.create()
newSpell.spellID = spell['SpellID']
spell.pop('SpellID', None)
newSpell.target = spell['Target']
spell.pop('Target', None)
newSpell.tickCount = spell['TickCount']
spell.pop('TickCount', None)
newSpell.tickInterval = spell['TickInterval']
spell.pop('TickInterval', None)
newSpell.spellFormat = spell['SpellFormat']
spell.pop('SpellFormat', None)
newSpell.save()
for criterion in spell['Criteria']:
crit, create = Criterion.objects.get_or_create(value1=criterion['Value1'], value2=criterion['Value2'], operator=criterion['Operator'])
newSpell.criteria.add(crit)
newSpell.spellParams = spell
newSpell.save()
newSpellData.spells.add(newSpell)
newSpellData.save()
newItem.spellData.add(newSpellData)
shopHashData = item.get('ShopHashData')
if shopHashData is not None:
for shopItem in shopHashData['Items']:
shopHash = ShopHash()
shopHash.hash = shopItem['Hash']['Text']
shopHash.minLevel = shopItem['MinLevel']
shopHash.maxLevel = shopItem['MaxLevel']
shopHash.baseAmount = shopItem['BaseAmount']
shopHash.regenAmount = shopItem['RegenAmount']
shopHash.regenInterval = shopItem['RegenInterval']
shopHash.spawnChance = shopItem['SpawnChance']
shopHash.save()
newItem.shopHash.add(shopHash)
animesh = item.get('AnimationMesh')
if animesh is not None:
newAniMesh = AnimationMesh()
animation = animesh.get('Animation')
newAniMesh.save()
if animation is not None:
sv, create = StatValue.objects.get_or_create(stat=animation['Stat'], value=animation['RawValue'])
newAniMesh.animation = sv
mesh = animesh.get('Mesh')
if mesh is not None:
sv, create = StatValue.objects.get_or_create(stat=mesh['Stat'], value=mesh['RawValue'])
newAniMesh.mesh = sv
newAniMesh.save()
newItem.animationMesh = newAniMesh
newItem.save()
def get_singletons(data):
statVals = []
criteria = []
idx = 0
for item in data:
if idx % 1000 == 0:
print(idx)
for sv in item.get('StatValues'):
stat = sv.get('Stat')
value = sv.get('RawValue')
# sv = StatValue(stat=stat, value=value)
statVals.append((stat, value))
atkdef = item.get('AttackDefenseData')
if atkdef is not None:
for atk in atkdef.get('Attack'):
# sv = StatValue(stat=atk.get('Stat'), value=atk.get('RawValue'))
statVals.append((atk.get('Stat'), atk.get('RawValue')))
for atk in atkdef.get('Defense'):
# sv = StatValue(stat=atk.get('Stat'), value=atk.get('RawValue'))
statVals.append((atk.get('Stat'), atk.get('RawValue')))
actionData = item.get('ActionData')
if actionData is not None:
actions = actionData.get('Actions')
if actions is not None:
for action in actions:
for criterion in action.get('Criteria'):
# crit = Criterion(value1=criterion['Value1'], value2=criterion['Value2'], operator=criterion['Operator'])
criteria.append((criterion['Value1'], criterion['Value2'], criterion['Operator']))
for spellData in item.get('SpellData'):
for spell in spellData['Items']:
for criterion in spell['Criteria']:
# crit = Criterion(value1=criterion['Value1'], value2=criterion['Value2'], operator=criterion['Operator'])
criteria.append((criterion['Value1'], criterion['Value2'], criterion['Operator']))
animesh = item.get('AnimationMesh')
if animesh is not None:
animation = animesh.get('Animation')
if animation is not None:
# sv = StatValue(stat=animation['Stat'], value=animation['RawValue'])
statVals.append((animation['Stat'], animation['RawValue']))
mesh = animesh.get('Mesh')
if mesh is not None:
# sv = StatValue(stat=mesh['Stat'], value=mesh['RawValue'])
statVals.append((mesh['Stat'], mesh['RawValue']))
idx += 1
return statVals, criteria
def make_statvalues(data):
data = list(set(data))
statVals = []
for item in data:
sv = StatValue(stat=item[0], value=item[1])
statVals.append(sv)
return statVals
def make_criterion(data):
data = list(set(data))
criteria = []
for item in data:
crit = Criterion(value1=item[0], value2=item[1], operator=item[2])
criteria.append(crit)
return criteria
chunkSize = 100
if __name__ == "__main__":
print('Getting Item Singletons...')
with open('items.json', 'r') as fd:
itemData = json.loads(fd.read())
itemSV, itemCrit = get_singletons(itemData)
print('Getting Nano Singletons...')
with open('nanos.json', 'r') as fd:
nanoData = json.loads(fd.read())
nanoSV, nanoCrit = get_singletons(nanoData)
statVals = itemSV + nanoSV
criteria = itemCrit + nanoCrit
print('Creating StatValue and Criterion')
statVals = make_statvalues(statVals)
criteria = make_criterion(criteria)
print('Sending StatValue and Criterion')
StatValue.objects.bulk_create(statVals)
Criterion.objects.bulk_create(criteria)
itemChunks = [itemData[i:i + chunkSize] for i in range(0, len(itemData), chunkSize)]
print('Importing items...')
db.connections.close_all()
with Pool(15) as pool:
pool.map(import_items, itemChunks)
nanoChunks = [nanoData[i:i + chunkSize] for i in range(0, len(nanoData), chunkSize)]
print('Importing nanos...')
db.connections.close_all()
with Pool(15) as pool:
pool.map(import_nanos, nanoChunks)
# breakpoint()