-
Notifications
You must be signed in to change notification settings - Fork 35
/
command.py
542 lines (480 loc) · 20.8 KB
/
command.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
import json
from sessions import session, save_session
from get_game_config import get_game_config, get_level_from_xp, get_name_from_item_id, get_attribute_from_mission_id, get_xp_from_level, get_attribute_from_item_id, get_item_from_subcat_functional
from constants import Constant
from engine import apply_cost, apply_collect, apply_collect_xp, timestamp_now
def get_strategy_type(id):
if id == 8:
return "Defensive"
if id == 9:
return "Mid Defensive"
if id == 7:
return "Mid Aggressive"
if id == 10:
return "Aggressive"
return "Unknown Strategy"
def command(USERID, data):
timestamp = data["ts"]
first_number = data["first_number"]
accessToken = data["accessToken"]
tries = data["tries"]
publishActions = data["publishActions"]
commands = data["commands"]
for i, comm in enumerate(commands):
cmd = comm["cmd"]
args = comm["args"]
do_command(USERID, cmd, args)
save_session(USERID) # Save session
def do_command(USERID, cmd, args):
save = session(USERID)
print (" [+] COMMAND: ", cmd, "(", args, ") -> ", sep='', end='')
if cmd == Constant.CMD_GAME_STATUS:
print(" ".join(args))
elif cmd == Constant.CMD_BUY:
id = args[0]
x = args[1]
y = args[2]
frame = args[3] # TODO ??
town_id = args[4]
bool_dont_modify_resources = bool(args[5]) # 1 if the game "buys" for you, so does not substract whatever the item cost is.
price_multiplier = args[6]
type = args[7]
print("Add", str(get_name_from_item_id(id)), "at", f"({x},{y})")
collected_at_timestamp = timestamp_now()
level = 0 # TODO
orientation = 0
map = save["maps"][town_id]
if not bool_dont_modify_resources:
apply_cost(save["playerInfo"], map, id, price_multiplier)
xp = int(get_attribute_from_item_id(id, "xp"))
map["xp"] = map["xp"] + xp
map["items"] += [[id, x, y, orientation, collected_at_timestamp, level]]
elif cmd == Constant.CMD_COMPLETE_TUTORIAL:
tutorial_step = args[0]
print("Tutorial step", tutorial_step, "reached.")
if tutorial_step >= 31: # 31 is Dragon choosing. After that, you have some freedom. There's at least until step 45.
print("Tutorial COMPLETED!")
save["playerInfo"]["completed_tutorial"] = 1
save["privateState"]["dragonNestActive"] = 1
elif cmd == Constant.CMD_MOVE:
ix = args[0]
iy = args[1]
id = args[2]
newx = args[3]
newy = args[4]
frame = args[5]
town_id = args[6]
reason = args[7] # "Unitat", "moveTo", "colisio", "MouseUsed"
print("Move", str(get_name_from_item_id(id)), "from", f"({ix},{iy})", "to", f"({newx},{newy})")
map = save["maps"][town_id]
for item in map["items"]:
if item[0] == id and item[1] == ix and item[2] == iy:
item[1] = newx
item[2] = newy
break
elif cmd == Constant.CMD_COLLECT:
x = args[0]
y = args[1]
town_id = args[2]
id = args[3]
num_units_contained_when_harvested = args[4]#TODO does this affect multiplier?
resource_multiplier = args[5]
cash_to_substract = args[6]
print("Collect", str(get_name_from_item_id(id)))
map = save["maps"][town_id]
apply_collect(save["playerInfo"], map, id, resource_multiplier)
save["playerInfo"]["cash"] = max(save["playerInfo"]["cash"] - cash_to_substract, 0)
elif cmd == Constant.CMD_SELL:
x = args[0]
y = args[1]
id = args[2]
town_id = args[3]
bool_dont_modify_resources = args[4]
reason = args[5]
print("Remove", str(get_name_from_item_id(id)), "from", f"({x},{y}). Reason: {reason}")
map = save["maps"][town_id]
for item in map["items"]:
if item[0] == id and item[1] == x and item[2] == y:
map["items"].remove(item)
break
if not bool_dont_modify_resources:
price_multiplier = -0.05
if get_attribute_from_item_id(id, "cost_type") != "c":
apply_cost(save["playerInfo"], save["maps"][town_id], id, price_multiplier)
if reason == 'KILL':
pass # TODO : add to graveyard
elif cmd == Constant.CMD_KILL:
x = args[0]
y = args[1]
id = args[2]
town_id = args[3]
type = args[4]
print("Kill", str(get_name_from_item_id(id)), "from", f"({x},{y}).")
map = save["maps"][town_id]
for item in map["items"]:
if item[0] == id and item[1] == x and item[2] == y:
apply_collect_xp(map, id)
map["items"].remove(item)
break
elif cmd == Constant.CMD_COMPLETE_MISSION:
mission_id = args[0]
skipped_with_cash = bool(args[1])
print("Complete mission", mission_id, ":", str(get_attribute_from_mission_id(mission_id, "title")))
if skipped_with_cash:
cash_to_substract = 0 # TODO
save["playerInfo"]["cash"] = max(save["playerInfo"]["cash"] - cash_to_substract, 0)
save["privateState"]["completedMissions"] += [mission_id]
elif cmd == Constant.CMD_REWARD_MISSION:
town_id = args[0]
mission_id = args[1]
print("Reward mission", mission_id, ":", str(get_attribute_from_mission_id(mission_id, "title")))
reward = int(get_attribute_from_mission_id(mission_id, "reward")) # gold
save["maps"][town_id]["coins"] += reward
save["privateState"]["rewardedMissions"] += [mission_id]
elif cmd == Constant.CMD_PUSH_UNIT:
unit_x = args[0]
unit_y = args[1]
unit_id = args[2]
b_x = args[3]
b_y = args[4]
town_id = args[5]
print("Push", str(get_name_from_item_id(unit_id)), "to", f"({b_x},{b_y}).")
map = save["maps"][town_id]
# Unit into building
for item in map["items"]:
if item[1] == b_x and item[2] == b_y:
if len(item) < 7:
item += [[]]
item[6] += [unit_id]
break
# Remove unit
for item in map["items"]:
if item[0] == unit_id and item[1] == unit_x and item[2] == unit_y:
map["items"].remove(item)
break
elif cmd == Constant.CMD_POP_UNIT:
b_x = args[0]
b_y = args[1]
town_id = args[2]
unit_id = args[3]
place_popped_unit = len(args) > 4
if place_popped_unit:
unit_x = args[4]
unit_y = args[5]
unit_frame = args[6] # unknown use
print("Pop", str(get_name_from_item_id(unit_id)), "from", f"({b_x},{b_y}).")
map = save["maps"][town_id]
# Remove unit from building
for item in map["items"]:
if item[1] == b_x and item[2] == b_y:
if len(item) < 7:
break
item[6].remove(unit_id)
break
if place_popped_unit:
# Spawn unit outside
collected_at_timestamp = timestamp_now()
level = 0 # TODO
orientation = 0
map["items"] += [[unit_id, unit_x, unit_y, orientation, collected_at_timestamp, level]]
elif cmd == Constant.CMD_RT_LEVEL_UP:
new_level = args[0]
print("Level Up!:", new_level)
map = save["maps"][0] # TODO : xp must be general, since theres no given town_id
map["level"] = args[0]
current_xp = map["xp"]
min_expected_xp = get_xp_from_level(max(0, new_level - 1))
map["xp"] = max(min_expected_xp, current_xp) # try to fix problems with not counting XP... by keeping up with client-side level counting
elif cmd == Constant.CMD_RT_PUBLISH_SCORE:
new_xp = args[0]
print("xp set to", new_xp)
map = save["maps"][0] # TODO : xp must be general, since theres no given town_id
map["xp"] = new_xp
map["level"] = get_level_from_xp(new_xp)
elif cmd == Constant.CMD_EXPAND:
land_id = args[0]
resource = args[1]
town_id = int(args[2])
print("Expansion", land_id, "purchased")
map = save["maps"][town_id]
if land_id in map["expansions"]:
return
# Substract resources
expansion_prices = get_game_config()["expansion_prices"]
exp = expansion_prices[len(map["expansions"]) - 1]
if resource == "gold":
to_substract = exp["coins"]
save["maps"][town_id]["coins"] = max(save["maps"][town_id]["coins"] - to_substract, 0)
elif resource == "cash":
to_substract = exp["cash"]
save["playerInfo"]["cash"] = max(save["playerInfo"]["cash"] - to_substract, 0)
# Add expansion
map["expansions"].append(land_id)
elif cmd == Constant.CMD_NAME_MAP:
town_id =int(args[0])
new_name = args[1]
print(f"Map name changed to '{new_name}'.")
save["playerInfo"]["map_names"][town_id] = new_name
elif cmd == Constant.CMD_EXCHANGE_CASH:
town_id = args[0]
print("Exchange cash -> coins.")
save["playerInfo"]["cash"] = max(save["playerInfo"]["cash"] - 5, 0)#maybe make function for editing resources
save["maps"][town_id]["coins"] += 2500
elif cmd == Constant.CMD_STORE_ITEM:
x = args[0]
y = args[1]
town_id = int(args[2])
item_id = args[3]
print("Store", str(get_name_from_item_id(item_id)), "from", f"({x},{y})")
map = save["maps"][town_id]
for item in map["items"]:
if item[0] == item_id and item[1] == x and item[2] == y:
map["items"].remove(item)
break
length = len(save["privateState"]["gifts"])
if length <= item_id:
for i in range(item_id - length + 1):
save["privateState"]["gifts"].append(0)
save["privateState"]["gifts"][item_id] += 1
elif cmd == Constant.CMD_PLACE_GIFT:
item_id = args[0]
x = args[1]
y = args[2]
town_id = args[3]#unsure, both 3 and 4 seem to stay 0
args[4]#unknown yet
print("Add", str(get_name_from_item_id(item_id)), "at", f"({x},{y})")
items = save["maps"][town_id]["items"]
orientation = 0#TODO
collected_at_timestamp = timestamp_now()
level = 0
items += [[item_id, x, y, orientation, collected_at_timestamp, level]]#maybe make function for adding items
save["privateState"]["gifts"][item_id] -= 1
if save["privateState"]["gifts"][item_id] == 0: #removes excess zeros at end if necessary
while(save["privateState"]["gifts"][-1] == 0):
save["privateState"]["gifts"].pop()
elif cmd == Constant.CMD_SELL_GIFT:
item_id = args[0]
town_id = args[1]
print("Gift", str(get_name_from_item_id(item_id)), "sold on town:",town_id)
gifts = save["privateState"]["gifts"]
gifts[item_id] -= 1
if gifts[item_id] == 0: #removes excess zeros at end if necessary
while(len(gifts) != 0 and gifts[-1] == 0):
gifts.pop()
price_multiplier = -0.05
if get_attribute_from_item_id(item_id, "cost_type") != "c":
apply_cost(save["playerInfo"], save["maps"][town_id], item_id, price_multiplier)
elif cmd == Constant.CMD_ACTIVATE_DRAGON:
currency = args[0]
print("Dragon nest activated.")
if currency == 'c':
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - 50), 0)
elif currency == 'g':
map = save["maps"]
map[0]["coins"] = max(int(map[0]["coins"] - 100000), 0)
save["privateState"]["dragonNestActive"] = 1
save["privateState"]["timeStampTakeCare"] = -1 # remove timer if any
elif cmd == Constant.CMD_DESACTIVATE_DRAGON:
print("Dragon nest deactivated.")
pState = save["privateState"]
pState["dragonNestActive"] = 0
# reset step and dragon numbers
pState["stepNumber"] = 0
pState["dragonNumber"] = 0
pState["timeStampTakeCare"] = -1 # remove timer if any
elif cmd == Constant.CMD_NEXT_DRAGON_STEP:
unknown = args[0]
print("Dragon step increased.")
pState = save["privateState"]
pState["stepNumber"] += 1
pState["timeStampTakeCare"] = timestamp_now()
elif cmd == Constant.CMD_NEXT_DRAGON:
print("Dragon step reset and dragonNumber increased.")
pState = save["privateState"]
pState["stepNumber"] = 0
pState["dragonNumber"] += 1
pState["timeStampTakeCare"] = -1 # remove timer
elif cmd == Constant.CMD_DRAGON_BUY_STEP_CASH:
price = args[0]
print("Buy dragon step with cash.")
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - price), 0)
save["privateState"]["timeStampTakeCare"] = -1 # remove timer
elif cmd == Constant.CMD_RIDER_BUY_STEP_CASH:
price = args[0]
print("Buy rider step with cash.")
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - price), 0)
save["privateState"]["riderTimeStamp"] = -1 # remove timer
elif cmd == Constant.CMD_NEXT_RIDER_STEP:
print("Rider step increased.")
pState = save["privateState"]
pState["riderStepNumber"] += 1
pState["riderTimeStamp"] = timestamp_now()
elif cmd == Constant.CMD_SELECT_RIDER:
number = int(args[0])
pState = save["privateState"]
if number == 1 or number == 2 or number == 3:
pState["riderNumber"] = number
print("Rider", number, "Selected.")
else:
pState["riderNumber"] = 0
pState["riderStepNumber"] = 0
pState["riderTimeStamp"] = -1 # remove timer
print("Rider reset.")
elif cmd == Constant.CMD_ORIENT:
x = args[0]
y = args[1]
new_orientation = args[2]
town_id = args[3]
print("Item at", f"({x},{y})", "changed to orientation", new_orientation)
map = save["maps"][town_id]
for item in map["items"]:
if item[1] == x and item[2] == y:
item[3] = new_orientation
break
elif cmd == Constant.CMD_MONSTER_BUY_STEP_CASH:
price = args[0]
print("Buy monster step with cash.")
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - price), 0)
save["privateState"]["timeStampTakeCareMonster"] = -1 # remove timer
elif cmd == Constant.CMD_ACTIVATE_MONSTER:
currency = args[0]
print("Monster nest activated.")
if currency == 'c':
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - 50), 0)
elif currency == 'g':
map = save["maps"]
map[0]["coins"] = max(int(map[0]["coins"] - 100000), 0)
save["privateState"]["monsterNestActive"] = 1
save["privateState"]["timeStampTakeCareMonster"] = -1 # remove timer if any
elif cmd == Constant.CMD_DESACTIVATE_MONSTER: # cmd called too late
print("Monster nest deactivated.")
pState = save["privateState"]
pState["monsterNestActive"] = 0
pState["stepMonsterNumber"] = 0
pState["MonsterNumber"] = 0
pState["timeStampTakeCareMonster"] = -1 # remove timer if any
elif cmd == Constant.CMD_NEXT_MONSTER_STEP:
print("Monster Step increased.")
pState = save["privateState"]
pState["stepMonsterNumber"] += 1
pState["timeStampTakeCareMonster"] = timestamp_now()
elif cmd == Constant.CMD_NEXT_MONSTER:
print("Monster Step reset and Monster Number increased.")
pState = save["privateState"]
pState["stepMonsterNumber"] = 0
pState["monsterNumber"] += 1
pState["timeStampTakeCareMonster"] = -1 # remove timer
elif cmd == Constant.CMD_WIN_BONUS:
coins = args[0]
town_id = args[1]
hero = args[2]
claimId = args[3]
cash = args[4]
print("Claiming Win Bonus")
map = save["maps"][town_id]
if cash != 0:
save["playerInfo"]["cash"] = save["playerInfo"]["cash"] + cash
print("Added " + str(cash) + " Cash to players balance")
if coins != 0:
map["coins"] = map["coins"] + coins
print("Added " + str(coins) + " Gold to players balance")
if hero != 0:
length = len(save["privateState"]["gifts"])
if length <= hero:
for i in range(hero - length + 1):
save["privateState"]["gifts"].append(0)
save["privateState"]["gifts"][hero] += 1
print("Added Hero ID=" + str(hero))
pState = save["privateState"]
pState["bonusNextId"] = claimId + 1
pState["timestampLastBonus"] = timestamp_now()
elif cmd == Constant.CMD_ADMIN_ADD_ANIMAL:
subcatFunc = str(args[0])
toBeAdded = int(args[1])
print("Added", toBeAdded, get_item_from_subcat_functional(subcatFunc)["name"])
# TODO
oAnimals: dict = save["privateState"]["arrayAnimals"]
oAnimals[subcatFunc] = toBeAdded + (oAnimals[subcatFunc] if subcatFunc in oAnimals else 0)
elif cmd == Constant.CMD_GRAVEYARD_BUY_POTIONS:
# no args
print("Graveyard buy potion")
# info from config
graveyard_potions = get_game_config()["globals"]["GRAVEYARD_POTIONS"]
amount = graveyard_potions["amount"]
price_cash = graveyard_potions["price"]["c"]
# pay
save["playerInfo"]["cash"] = max(int(save["playerInfo"]["cash"] - price_cash), 0)
# add potion
save["privateState"]["potion"] += amount
elif cmd == Constant.CMD_RESURRECT_HERO:
unit_id = args[0]
x = args[1]
y = args[2]
town_id = args[3]
bool_used_potion = len(args) > 4 and args[4] == '1'
print("Resurrect", str(get_name_from_item_id(unit_id)), "from graveyard")
# pay
if bool_used_potion:
quantity = 1
save["privateState"]["potion"] = max(int(save["privateState"]["potion"] - quantity), 0)
else:
pass # TODO
# Place unit
collected_at_timestamp = timestamp_now()
level = 0 # TODO
orientation = 0
map["items"] += [[id, x, y, orientation, collected_at_timestamp, level]]
elif cmd == Constant.CMD_BUY_SUPER_OFFER_PACK:
town_id = args[0]
unknown2 = args[1] # this is probably the super offer pack ID?
items = args[2]
cash_used = args[3]
map = save["maps"][town_id]
item_array = items.split(',')
for item in item_array:
item_id = int(item)
length = len(save["privateState"]["gifts"])
if length <= item_id:
for i in range(item_id - length + 1):
save["privateState"]["gifts"].append(0)
save["privateState"]["gifts"][item_id] += 1
save["playerInfo"]["cash"] = max(save["playerInfo"]["cash"] - cash_used, 0)#maybe make function for editing resources
print(f"Used {cash_used} cash to buy super offer pack!")
elif cmd == Constant.CMD_SET_STRATEGY:
strategy_type = args[0]
type_name = get_strategy_type(strategy_type)
save["privateState"]["strategy"] = strategy_type
print(f"Set defense strategy type to {type_name}")
elif cmd == Constant.CMD_START_QUEST:
quest_id = args[0]
town_id = args[1]
print(f"Start quest {quest_id}")
elif cmd == Constant.CMD_END_QUEST:
data = json.loads(args[0])
town_id = data["map"]
gold_gained = data["resources"]["g"]
xp_gained = data["resources"]["x"]
units = data["units"]
win = data["win"] == 1
duration_sec = data["duration"]
voluntary_end = data["voluntary_end"] == 1
quest_id = int(data["quest_id"])
item_rewards = data["item_rewards"] if "item_rewards" in data else None
activators_left = data["activators_left"] if "activators_left" in data else None
difficulty = data["difficulty"]
# Resources
save["maps"][town_id]["coins"] += int(gold_gained)
save["maps"][town_id]["xp"] += int(xp_gained)
# Update quests data
save["privateState"]["unlockedQuestIndex"] = max(quest_id + 1, save["privateState"]["unlockedQuestIndex"], 0)
# save["privateState"]["questsRank"] = TODO
# save["maps"]["questTimes"] [quest_id] = TODO min (... , duration_sec)
# save["maps"]["lastQuestTimes"] [quest_id] = TODO min (... , duration_sec)
print(f"Ended quest {quest_id}.")
elif cmd == Constant.CMD_ADD_COLLECTABLE:
collection_id = args[0]
collectible_id = args[1]
# TODO
else:
print(f"Unhandled command '{cmd}' -> args", args)
return