summaryrefslogtreecommitdiff
path: root/player.py
blob: a8b6f6e1bb61733dc8c49df96a67bedec09f4d6e (plain) (blame)
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
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
#################################################################################
#     This file is part of Spheres.
#     Copyright (C) 2019  Jesusalva

#     This program is free software: you can redistribute it and/or modify
#     it under the terms of the GNU General Public License as published by
#     the Free Software Foundation, either version 3 of the License, or
#     (at your option) any later version.

#     This program is distributed in the hope that it will be useful,
#     but WITHOUT ANY WARRANTY; without even the implied warranty of
#     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#     GNU General Public License for more details.

#     You should have received a copy of the GNU General Public License
#     along with this program.  If not, see <http://www.gnu.org/licenses/>.
#################################################################################
# Player Module (parties, inventory, and player data)

from utils import (stdout, dl_search, allunits, date_from_sql, date_from_now,
    now, compress, cli_search, Player, ApTimer)
from consts import (SQL_DELAY, SQL_SAVE_TIME, MAX_INV_SIZE, AP_REGEN_TIME_F,
    ERR_LOGIN, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY,
    ERR_BAD, ERR_OFF, ERR_ERR, CLIENTVERSION, UNSELLABLE, DOUBLE_GP, EXP_UP,
    ERR_DONE, ERR_OK, MAX_PARTIES, NO_PARTY, SUPEREVOMAT, EVO_MAT, AP_REGEN_TIME,
    INT_MAX, EXPTABLE, SQL_CLEAR)
import sql
import json, re, traceback
from copy import copy
from threading import Timer

####################################### Private methods
# Internal function for ApTimer, DO NOT CAST manually
# FIXME: Maybe find a better replacement for this
def ap_updater(token):
    global ApTimer, Player

    # TODO: What if token is no longer valid? (Maybe use queue instead?)
    Player[token]["ap"]+=1
    if (Player[token]["ap"] < Player[token]["max_ap"]):
        try:
            ApTimer[token].cancel()
            del ApTimer[token]
        except:
            pass
        ApTimer[token]=Timer(AP_REGEN_TIME_F, ap_updater, args=[token])
        ApTimer[token].daemon=True
        ApTimer[token].start()

    # Send APREFRESH packet to client
    try:
        usr=cli_search(Player[token]["userid"])
        usr.send_message("APREFRESH:%d" % Player[token]["ap"])
    except:
        traceback.print_exc()
        stdout("Unable to send AP Info for token %s" % token)
    return

# Internal function for SQL, DO NOT CAST manually
def sql_routine():
    global Player, SQLTimer

    #stdout("Now saving SQL data")
    for token in Player:
        stdout("[SQL] Now saving token \"%s\"" % token)
        stdout(str(Player[token]))
        clear(token, SQL_DELAY)

    SQLTimer=Timer(SQL_SAVE_TIME, sql_routine)
    SQLTimer.daemon=True
    SQLTimer.start()
    return

# Attempts to place an item. Return inv index, -1 on error
def inventoryplace(token):
    # Check for a free slot or return next index
    try:
        i=Player[token]["inv"].index(None)
    except:
        i=len(Player[token]["inv"])

    # Return -1 if array exceeds the maximum size
    if (i > MAX_INV_SIZE):
        return -1
    else:
        return i

# Adds/Deletes AP from a user, manages the ApTimer
def update_ap(token, newval):
    global ApTimer, Player

    running=False
    if (Player[token]["ap"] < Player[token]["max_ap"]):
        running=True

    # Update AP
    # TODO: Do we allow over-the-cap AP?
    Player[token]["ap"]+=int(newval)

    # Handle the timer
    if (not running and Player[token]["ap"] < Player[token]["max_ap"]):
        ApTimer[token]=Timer(AP_REGEN_TIME_F, ap_updater, args=[token])
        ApTimer[token].daemon=True
        ApTimer[token].start()
    if (running and Player[token]["ap"] >= Player[token]["max_ap"]):
        # If it is not running/error: We don't care (very rare error)
        try:
            ApTimer[token].cancel()
        except:
            pass

    return

# Function which list all unit id on party
def party_dupcheck(token, pid):
    ar=[]
    for unt in Player[token]["party_%s" % pid]:
        ar.append(unt["unit_id"])
    return ar

# Function which list all unit id on inventory
def inv_dupcheck(token, unique=True):
    ar=[]
    for unt in Player[token]["inv"]:
        if unt is None:
            if unique:
                continue
            else:
                ar.append(0)
        else:
            if unique:
                if unt["unit_id"] in ar:
                    continue
            else:
                ar.append(unt["unit_id"])
    return ar

def clear(token, mask=SQL_CLEAR):
    # This function saves and clears a token
    #########################################
    try:
        stdout("Saving user ID %d" % Player[token]["userid"])
    except:
        stdout("ERROR: Token \"%s\" is not valid." % token)
        return

    #########################################
    try:
        # Save inventory data to SQL
        sql.save_inv(token, mask)

        # Erase inventory if asked
        if mask & SQL_CLEAR:
            Player[token]["inv"]={}

        # Save party data to SQL
        sql.save_party(token)

        # Erase parties if needed
        if mask & SQL_CLEAR:
            Player[token]["party_1"]={}
            Player[token]["party_2"]={}
            Player[token]["party_3"]={}

        # Now save Player data to SQL
        sql.save_player(token, mask)

        # Erase Player Structure if needed
        if mask & SQL_CLEAR:
            del Player[token]

        # Stop the AP regeneration if we're clearing
        if mask & SQL_CLEAR:
            try:
                ApTimer[token].cancel()
                del ApTimer[token]
            except:
                pass
    except:
        stdout("SQL ERROR: FAILED TO CLEAR TOKEN %s" % token)
        if mask & SQL_CLEAR:
            del Player[token]
        if mask & SQL_CLEAR:
            try:
                ApTimer[token].cancel()
                del ApTimer[token]
            except:
                pass
    # TODO: Battle[token]
    return

def exp_to_lvlup(level, unit=False):
    try:
        next_exp=EXPTABLE[level]
        if unit:
            next_exp=int(next_exp/1.5)
    except:
        stdout("Level %d is out of bounds" % (level))
        next_exp=INT_MAX
    #stdout("nmult is %.2f, exp needed: %d" % (nmult, next_exp))
    return next_exp

# Check if player ranked up
# Return: {"code": True/False, "ap": <ap_raised>, "next": <exp_needed>}
def check_rank_up(token):
    rs={"code": False, "ap": 0}
    next_exp=exp_to_lvlup(Player[token]["level"])

    if Player[token]["exp"] >= next_exp:
        # We leveled up! Prepare the bonuses
        rs["code"]=True

        # AP rules: Every 5 levels: +1 AP (always)
        if Player[token]["level"] % 5 == 0:
            rs["ap"]+=1
        # AP rules: Every 10 levels: +1 AP (always)
        if Player[token]["level"] % 10 == 0:
            rs["ap"]+=1
        # AP rules: Every level before the 20th: +1 AP (always_
        if Player[token]["level"] < 20:
            rs["ap"]+=1

        # Apply the level up
        Player[token]["exp"]-=next_exp
        Player[token]["level"]+=1
        Player[token]["max_ap"]+=rs["ap"]
        Player[token]["max_exp"]=exp_to_lvlup(Player[token]["level"])
        update_ap(token, Player[token]["max_ap"]-Player[token]["ap"])
        # Sanitize
        Player[token]["exp"]=min(Player[token]["exp"], exp_to_lvlup(Player[token]["level"]))

    return rs

# Conditional EXP attribution
# Doesn't returns anything
def grant_exp(token, inv_id, exp):
    # We need some data
    un=dl_search(allunits, "unit_id", Player[token]["inv"][inv_id]["unit_id"])
    if un == "ERROR":
        stdout("WARNING, Invalid unit id on GEXP: %d" % (Player[token]["inv"][inv_id]["unit_id"]))
        return

    # Max level, do nothing
    if Player[token]["inv"][inv_id]["level"] >= un["max_level"]:
        return

    Player[token]["inv"][inv_id]["exp"]+=exp
    return

# Same as check_rank_up but for units
# Returns True/False
def check_level_up(token, inv_id):
    global allunits

    # Prepare un, the variable from unit database
    un=dl_search(allunits, "unit_id", Player[token]["inv"][inv_id]["unit_id"])
    if un == "ERROR":
        stdout("WARNING, Invalid unit id on CLU: %d" % (Player[token]["inv"][inv_id]["unit_id"]))
        return -1

    # FIX
    if Player[token]["inv"][inv_id]["level"] > un["max_level"]:
        stdout("WARNING, Overlevelled unit (Lv %d/%d)" % (Player[token]["inv"][inv_id]["level"], un["max_level"]))
        Player[token]["inv"][inv_id]["exp"]=0
        Player[token]["inv"][inv_id]["level"]=copy(un["max_level"])

    # Max level reached (FIXME why it still gets exp?!)
    if Player[token]["inv"][inv_id]["level"] >= un["max_level"]:
        return 0

    # Calculate exp needed to level up
    next_exp=exp_to_lvlup(Player[token]["inv"][inv_id]["level"], True)
    ret=0
    while Player[token]["inv"][inv_id]["exp"] >= next_exp:
        ret += 1
        Player[token]["inv"][inv_id]["exp"]-=next_exp
        Player[token]["inv"][inv_id]["level"]+=1
        # No overflow!
        if Player[token]["inv"][inv_id]["level"] >= un["max_level"]:
            Player[token]["inv"][inv_id]["exp"]=0
            Player[token]["inv"][inv_id]["level"]=copy(un["max_level"])
            break
        next_exp=exp_to_lvlup(Player[token]["inv"][inv_id]["level"], True)
    return ret

# Calculates sell price of an unit
# After 10 stars, the sell price overflows 10,000 and gets messy
# So I capped it at 10,000. Further stars will only raise level weight.
def calc_sell_price(rarity, level):
    return min(10000, rarity**2*100)+(level*rarity)

def daily_login(token):
    # Prepare dates from player last login, and from now
    # FIXME: "lastlogin" is updated in cycles
    # If players pass midnight connected --> BOOM
    # We should fill "lastlogin" at sql.load_player() only, instead of
    # auto-updating it
    dlcode=copy(ERR_LOGIN)
    stdout("Last login: %s" % str(Player[token]["lastlogin"]))

    # dlcode mask: (ERR_LOGIN meaning)
    # 5 - 0 - 0 - 0
    # ID - Monthly - Monthly - Weekly

    d,m,y,w,H,M=date_from_sql(str(Player[token]["lastlogin"]))
    td,tm,ty,tw,tH,tM=date_from_now()

    # We don't really care with this bit of info
    del H, M, tH, tM

    # It's not the same: A reward is due
    if (d!=td or m!=tm or y!=ty):
        dlcode+=1000

    # TODO: Weekly rewards
    if (w != tw) or (w == tw and (d != td or m != tm or y != ty)):
        # A weekly reward is due!
        dlcode+=tw

        if tw == MONDAY:
            Player[token]["crystals"]+=50
        elif tw == TUESDAY:
            Player[token]["crystals"]+=50
        elif tw == WEDNESDAY:
            Player[token]["crystals"]+=50
        elif tw == THURSDAY:
            Player[token]["crystals"]+=50
        elif tw == FRIDAY:
            Player[token]["crystals"]+=50
        elif tw == SATURDAY:
            Player[token]["crystals"]+=50
        elif tw == SUNDAY:
            Player[token]["crystals"]+=50
        else:
            stdout("ERROR ERROR, UNKNOWN DAY OF WEEK %d" % tw)

    # TODO: Monthly rewards
    # TODO: Streak rewards

    # Finally, get rid of this data
    del Player[token]["lastlogin"]
    return dlcode

####################################### Public methods (Player/Client)
def get_data(args, token):
    stdout("Data received")
    stdout("Now loading user ID: ```%s```" % str(args))
    try:
        y=json.loads(args)
        passwd=y["passwd"]
        if not passwd.isalnum():
            raise Exception("Illegal password")
        if len(passwd) not in [12, 16]:
            raise Exception("Illegal password")
        vs=str(y["version"])
    except:
        return ERR_BAD

    # Version check
    if vs != CLIENTVERSION:
        return ERR_OFF

    # We have now loaded to memory from SQL
    # We definitely should use K-Line here
    target_uid=sql.load_player(token, passwd)

    # Maybe something went wrong, or password is invalid
    if (target_uid == ERR_BAD):
        return ERR_BAD
    if (target_uid == ERR_ERR):
        return ERR_ERR

    # Check if user is already logged in
    # If they are, cause a disconnection on old one and update tokens
    try:
        org_usr=cli_search(target_uid["userid"])
        tk="0"
        if org_usr not in ["ERROR"]:
            stdout("Closing duplicate login from %s (token %s)" % (org_usr.address[0], org_usr.token))
            tk=org_usr.token
            org_usr.close(status=1000, reason='Duplicated login')
        else:
            raise Exception("Not logged in")

        # TODO: ApTimer[tk] & Battle[tk]
        Player[token]=copy(Player[tk])
        clear(tk)
        Player[token]["code"]=ERR_LOGIN
        Player[token]["token"]=token
        stdout("Player is already logged in")

        # Delete user id, send payload, create user id again
        uid=copy(Player[token]["userid"])
        del Player[token]["userid"]
        paydata=compress(Player[token])
        Player[token]["userid"]=copy(uid)
        del uid

        # Remove other temporary data
        del Player[token]["code"]
        del Player[token]["token"]
        return paydata
    except:
        #traceback.print_exc()
        pass

    # Create session
    Player[token] = target_uid

    # Complete additional data
    Player[token]["code"]=0
    Player[token]["token"]=token

    # Give you offline AP and refresh to NOW, round down
    delta=(now()-Player[token]["aptime"])/AP_REGEN_TIME
    delta2=(now()-Player[token]["aptime"])%AP_REGEN_TIME
    Player[token]["ap"]=int(min(Player[token]["max_ap"], Player[token]["ap"]+delta))
    Player[token]["aptime"]=now()-delta2

    # This is only to start the timer if needed
    if (Player[token]["ap"] < Player[token]["max_ap"]):
        ApTimer[token]=Timer(AP_REGEN_TIME_F-delta2, ap_updater, args=[token])
        ApTimer[token].daemon=True
        ApTimer[token].start()

    # Daily login rewards
    Player[token]["code"]=daily_login(token)

    # Include max_exp for the client
    Player[token]["max_exp"]=exp_to_lvlup(Player[token]["level"])


    # Remove UID from packet and save to JSON
    uid=copy(Player[token]["userid"])
    del Player[token]["userid"]
    sjson=compress(Player[token])
    Player[token]["userid"]=copy(uid)

    # Clear temporary data, write internal big data fields
    del uid
    del Player[token]["code"]
    del Player[token]["token"]
    Player[token]["inv"]=sql.load_inv(token)
    Player[token]["party_1"]=sql.load_party(token, 1)
    Player[token]["party_2"]=sql.load_party(token, 2)
    Player[token]["party_3"]=sql.load_party(token, 3)
    # TODO: Load currency table
    # TODO: Load event table
    # TODO: Load world table

    # Logged in
    # {responseCode, token, status, gp, crystals, level, ap, max_ap, aptime }
    return sjson

# This returns the player inventory.
def get_inv(args, token):
    sjson=compress(Player[token]["inv"])
    return sjson

# This returns the player AP Data
def ap_data(args, token):
    sjson=compress({"ap": Player[token]["ap"],
                    "max_ap": Player[token]["max_ap"],
                    "aptime": Player[token]["aptime"]})
    return sjson

# Sell units. Receives a single JSON array of inventory IDs.
def sellunits(args, token):
    try:
        y=json.loads(args)
        gp=0
        for tmp in y:
            tmpa=int(tmp)
            # Inventory size check (first check is not needed in a try loop...)
            if tmpa >= len(Player[token]["inv"]):
                raise Exception("Not in inventory")
            if Player[token]["inv"][tmpa] == None:
                stdout("None supplied (%d)" % tmpa)
                raise Exception("Not in inventory")

            # UNSELLABLE flag
            un=dl_search(allunits, "unit_id",Player[token]["inv"][tmpa]["unit_id"])
            if un["flags"] & UNSELLABLE:
                stdout("This unit cannot be sold!")
                raise Exception("This unit cannot be sold!")

            # No duplicates
            if y.count(tmp) != 1:
                stdout("Duplication detected: %d" % (args.count(tmp)))
                raise Exception("Duplicate index detected")

            # Search in party
            stdout("PARTY LOOKUP IN PROGRESS")
            tmpb=dl_search(Player[token]["party_1"], "inv_id", tmpa)
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_2"], "inv_id", tmpa)
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_3"], "inv_id", tmpa)
            if tmpb != "ERROR":
                stdout("Unit is in party (%d)" % tmpb)
                raise Exception("Party members can't be sold")
            del tmpb

            # No point wasting time, we have "un" so sum GP as well
            am=calc_sell_price(un["rare"], Player[token]["inv"][tmpa]["level"])
            if un["flags"] & DOUBLE_GP:
                am*=2
            gp+=am
            del un, am
        del tmpa
    except:
        return ERR_BAD

    # Delete sold units and sum the GP
    for idx in y:
        Player[token]["inv"][idx]=None
    Player[token]["gp"]+=int(gp)
    sjson=compress('{"gp": %d, "profit": %d}' % (Player[token]["gp"], gp))
    return sjson

# Upgrade units. Receives a JSON array: [UNIT-TO-UPGRADE, MAT1, MAT2, MAT3...]
# Based on invindex
def upgrade(args, token):
    # Data validation (party members can't be used as material)
    try:
        y=json.loads(args)
        w=False
        for tmp in y:
            tmpa=int(tmp)
            # Inventory size check (first check is not needed in a try loop...)
            if tmpa >= len(Player[token]["inv"]):
                raise Exception("Not in inventory")
            if Player[token]["inv"][tmpa] == None:
                stdout("None supplied (%d)" % tmpa)
                raise Exception("Not in inventory")
            # First entry can be in the party, but must be uppable
            if not w:
                w=True
                un=dl_search(allunits, "unit_id",Player[token]["inv"][tmpa]["unit_id"])
                stdout("Unit flags:")
                stdout("%s" % un["flags"])
                #if un["flags"] & NO_LVLUP: # FIXME: Makes no sense now
                #    stdout("This unit cannot level up!")
                #    raise Exception("This unit cannot level up!")
                target_ele=un["attribute"]
                del un
                continue
            # Self fusion??
            stdout("begin: starting")
            if y.count(tmp) != 1:
                stdout("Duplication detected: %d" % (args.count(tmp)))
                raise Exception("Duplicate index or self fusion detected")
            # Search in party
            stdout("LOOKUP IN PROGRESS")
            tmpb=dl_search(Player[token]["party_1"], "inv_id", tmpa)
            stdout("dl_search OK")
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_2"], "inv_id", tmpa)
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_3"], "inv_id", tmpa)
            if tmpb != "ERROR":
                stdout("Unit is in party (%d)" % tmpb)
                raise Exception("Party members can't be used as material")
            del tmpb
        del tmpa, w
    except:
        return ERR_BAD

    # Get "target" inv id
    target=y.pop(0)
    stdout("Target is (%d)" % target)

    # Define the experience you'll get by draining the relevant indexes
    xp=0
    for idx in y:
        uxp=0
        ud=Player[token]["inv"][idx]["unit_id"]
        un=dl_search(allunits, "unit_id", ud)
        try:
            r=un["rare"]
            f=un["flags"]
            e=un["attribute"]
            next_exp=exp_to_lvlup(Player[token]["inv"][idx]["level"], True)
        except:
            r=1
            f=0
            e=-1
            stdout("ERROR, INVALID UNIT ID: %d" % ud)

        # Units with EXP_UP are always "max-level"
        if f & EXP_UP:
            lv=10+(r*10)
        else:
            lv=Player[token]["inv"][idx]["level"]

        uxp+=lv*20.0*((r+1)/2.0)
        uxp+=Player[token]["inv"][idx]["exp"]/next_exp*100.0*20.0

        # Flags and same element bonus
        if f & EXP_UP:
            uxp*=1.5
        if e == target_ele:
            uxp*=1.2

        # Give the exp, and remove from inventory
        xp+=int(uxp)
        Player[token]["inv"][idx]=None

    # We now have the experience, so we grant it
    grant_exp(token, target, xp)
    r=check_level_up(token, target)

    sjson=compress(r)
    return sjson

# args is the party ID
def get_party(args, token):
    try:
        pid=int(args)
        if (pid > MAX_PARTIES):
            raise Exception("too many parties")
    except:
        return ERR_BAD

    sjson=compress(Player[token]["party_%d" % pid])
    return sjson

# TODO: Obviously this is also a WIP
# {"party_id": pid, "formation": [p1, p2, p3, p4]}
def set_party(args, token):
    # Standard checks
    try:
        y=json.loads(args)
        tmp=y["party_id"]
        pid=int(tmp)
        # Maximum party number
        if (pid > MAX_PARTIES):
            raise Exception("too many parties")
        # Only integers
        for ele in y["formation"]:
            tmp=int(ele)
            # If the unit is not valid (in inventory), this will EXPLODE
            un=dl_search(allunits, "unit_id", Player[token]["inv"][tmp]["unit_id"])
            if un["flags"] & NO_PARTY:
                raise Exception("This unit cannot be part of a party!")
        del un
    except:
        return ERR_BAD

    # FIXME: We can't have duplicates Oh My :o
    #Player[token]["party_1"]=[]
    stdout("Request to edit party %d" % pid)

    # Check each request before appending
    for i, idx in enumerate(y["formation"]):
        stdout("Now checking (%d, %d)" % (i, idx))

        # Empty the index in analysis
        Player[token]["party_%s" % pid][i]={"unit_id": 0,
                                             "inv_id": -1}

        # Ignored index
        if (idx < 0):
            continue

        # Duplicate checking!
        if (Player[token]["inv"][idx]["unit_id"] in party_dupcheck(token, pid)):
            return ERR_BAD

        # Retrieve inventory data and replace it in player tokens
        Player[token]["party_%s" % pid][i]["unit_id"]=Player[token]["inv"][idx]["unit_id"]
        Player[token]["party_%s" % pid][i]["inv_id"]=idx

    return ERR_DONE

# Evolve units. Receives a JSON array: [UNIT-TO-EVOLVE, MAT1, MAT2]
# Based on invindex
def evolve(args, token):
    # Data validation (party members can't be used as material)
    try:
        y=json.loads(args)
        if len(y) != 3:
            return ERR_BAD
        w=False
        for tmp in y:
            tmpa=int(tmp)
            # Inventory size check (first check is not needed in a try loop...)
            if tmpa >= len(Player[token]["inv"]):
                raise Exception("Not in inventory")
            if Player[token]["inv"][tmpa] == None:
                stdout("None supplied (%d)" % tmpa)
                raise Exception("Not in inventory")
            # First entry can be in the party, but must be MAX LEVEL and level-able
            if not w:
                w=True
                un=dl_search(allunits, "unit_id",Player[token]["inv"][tmpa]["unit_id"])
                stdout("Unit flags:")
                stdout("%s" % un["flags"])
                #if un["flags"] & NO_LVLUP: # FIXME: Makes no sense now
                #    stdout("This unit cannot level up!")
                #    raise Exception("This unit cannot level up!")
                if un["max_level"] != Player[token]["inv"][tmpa]["level"]:
                    stdout("raise: Level not yet maxed")
                    raise Exception("Not yet max level!")
                target_ele=un["attribute"]
                target_id=un["unit_id"]
                target_rare=un["rare"]
                # Check if evolved version exists
                evolved=dl_search(allunits, "unit_id",Player[token]["inv"][tmpa]["unit_id"]+1)
                evolved_id=evolved["unit_id"] # Same as raise Exception if can't evolve
                del un
                continue
            # Self fusion??
            stdout("begin: starting")
            if y.count(tmp) != 1:
                stdout("Duplication detected: %d" % (args.count(tmp)))
                raise Exception("Duplicate index or self fusion detected")
            # Search in party
            stdout("LOOKUP IN PROGRESS")
            tmpb=dl_search(Player[token]["party_1"], "inv_id", tmpa)
            stdout("dl_search OK")
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_2"], "inv_id", tmpa)
            if tmpb == "ERROR":
                tmpb=dl_search(Player[token]["party_3"], "inv_id", tmpa)
            if tmpb != "ERROR":
                stdout("Unit is in party (%d)" % tmpb)
                raise Exception("Party members can't be used as material")
            # TODO: Check if it is suitable material (same rarity etc.)
            r=False
            tmpb=dl_search(allunits, "unit_id",Player[token]["inv"][tmpa]["unit_id"])
            if tmpb["rare"] == target_rare:
                if tmpb["flags"] & SUPEREVOMAT:
                    r=True
                elif tmpb["flags"] & EVO_MAT:
                    if tmpb["attribute"] == target_ele:
                        r=True
                elif tmpb["unit_id"] == target_id:
                    r=True
            if not r:
                stdout("raise: Invalid evolution material")
                raise Exception("Invalid evolve material")
            del tmpb
        del tmpa, w
    except:
        return ERR_BAD

    # Get "target" inv id
    #target=y.pop(0)
    stdout("Target is (%d)" % target_id)

    # Evolve and remove material. Clear level/exp as well.
    Player[token]["inv"][y[0]]["unit_id"]+=1
    Player[token]["inv"][y[0]]["level"]=0
    Player[token]["inv"][y[0]]["exp"]=0
    Player[token]["inv"][y[1]]=None
    Player[token]["inv"][y[2]]=None
    r=ERR_OK

    # TODO: Update party, NOT high priority
    try:
        tmpb=dl_search(Player[token]["party_1"], "inv_id", target_id)
        if tmpb != "ERROR":
            tmpb["unit_id"]+=1

        tmpb=dl_search(Player[token]["party_2"], "inv_id", target_id)
        if tmpb != "ERROR":
            tmpb["unit_id"]+=1

        tmpb=dl_search(Player[token]["party_3"], "inv_id", target_id)
        if tmpb != "ERROR":
            tmpb["unit_id"]+=1

        # Now, do this cause a duplicate? If yes, unsocket it!
        tmpa=0
        for tmpb in Player[token]["party_1"]:
            if tmpb["unit_id"] == evolved_id:
                tmpa+=1
            if tmpa > 1:
                tmpb["unit_id"]=-1
                tmpb["inv_id"]=-1
                stdout("Unsocket")

        tmpa=0
        for tmpb in Player[token]["party_2"]:
            if tmpb["unit_id"] == evolved_id:
                tmpa+=1
            if tmpa > 1:
                tmpb["unit_id"]=-1
                tmpb["inv_id"]=-1
                stdout("Unsocket")

        tmpa=0
        for tmpb in Player[token]["party_3"]:
            if tmpb["unit_id"] == evolved_id:
                tmpa+=1
            if tmpa > 1:
                tmpb["unit_id"]=-1
                tmpb["inv_id"]=-1
                stdout("Unsocket")

    except:
        pass

    sjson=compress(r)
    return sjson


# Creates a new account. Arguments: email
def register(args, token):
    stdout("Request to register an account: %s" % str(args))
    # https://emailregex.com/email-validation-summary/ - RFC allows more emails
    # But we don't want to risk compromising the database, and some are dump
    regex = '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$'
    try:
        y=json.loads(args)
        tmp=y["email"]
        mail=str(tmp)
        tmp=y["MyUID"]
        leni=len(tmp)
        MyUID=str(y["MyUID"])
        # Check if email is valid with above regex
        if not re.search(regex,mail):
            if leni != 32 or mail != "":
                raise Exception("Not a valid email")
            mail="_%s@spheres.tmw2.org" % (MyUID)
    except:
        return ERR_BAD

    # TODO: "duplicate": all emails which difference consists on ponctuation
    #email=mail.replace(".", "").replace("_", "").replace("-", "").replace("+", "")

    stdout("Initial checks succeded")
    # Check if email is already registered
    check=sql.query_email(mail)
    if (check != ""):
        stdout("WARNING: Tried to register email \"%s\" (belongs to account %s)" % (mail, check))
        return ERR_BAD

    # If email is an UID mail, clobber it
    check=sql.query_email("_%s@spheres.tmw2.org" % (MyUID))
    if (check != ""):
        passwd=sql.clobber_email(check, "_%s@spheres.tmw2.org" % (MyUID), mail)
        stdout("INFO: Set a new email for \"%s\" (%s)" % (check, mail))
        return compress({"userid": MyUID, "password": passwd})

    stdout("Now registering account")
    # Register it
    data=sql.add_player(mail)
    if (data["userid"] <= 0):
        return ERR_ERR

    # From data, we have: userid, password
    return compress(data)