rcpd/decode.py

55 lines
1.8 KiB
Python

import shared
import tables
import encode
from functools import reduce
def decode(code,name=''): # This is going to be a huge function
code=shared.delimit(code) # It didn't end up being that huge
code=shared.scramble(code,True)
code,validator=shared.combine(code)
xp=int(code[-1]); seed=int(code[0])
code=code[1:-1]
data=unzip(code)
xp+=data.pop(0)
rank,cap,cob,lsa,rem,clas,armour,trait,spec,talent=data
cob,lsa=bool(cob),bool(lsa)
rank,cap,key,moh,pcc=unadjust_rank(rank,cap)
if seed%2: seed-=1; clas+=10
if clas==6: clas=[8,9,6][armour]; armour=3
check=''
oldclas=clas # Whoops this all needs to be done before globalising talent
gun=tables.gunmaps[clas]
clas-=(clas>8)+min(4,max(0,clas-10)) # WM and mav adjustments
gtalent=tables.clas[clas][talent] # Globalise it lol
# And do it *before* validation because it needs gtalent passed in now
if name:
check=encode.validate_data(name, oldclas,armour,trait,spec,gtalent, rank,cap,xp, key,moh,pcc,cob,lsa,rem)
rank+=1 # This has to come after extracting medals
return clas,gun,armour,trait,spec,talent,gtalent,rank,cap,xp,key,moh,pcc,cob,lsa,rem,seed,validator,check
############################
# SUPPORTING FUNCTIONS BELOW
############################
def unzip(code):
code=int(code)
out=[]
weight=reduce(lambda x,y:x*y,tables.weights)
for n in list(zip(tables.weights,['xp//10','rank','cap','cob','lsa','rem','clas','armour','trait','spec','talent']))[::-1]:
shared.trace(code,weight,n,out)
out.append(code//weight)
code%=weight
weight//=n[0]
out[-1]*=10 # This is XP
return out[::-1]
def unadjust_rank(rank,cap):
key,moh,pcc=False,False,False
if rank>8:
pcc=bool((cap+1)%2)
cap=2+(cap+1)//2
if rank>11: # Actually need to check this on the way out
key=rank>12
moh=bool((rank-11)%2)
rank=11
return rank,cap,key,moh,pcc