#!/usr/bin/python # -*- coding: UTF-8 -*- # Not actually part of the project; just easier to maintain everything here import sys,os,traceback import re try: from minitest import minitest term=minitest.Terminal() except: pass try: args=sys.argv[1:] command=sys.argv[1] except: print("No command specified, just outputing the help") command="help" #Custom parsing ENCAISSEMENT_PARSE="""d_vars.encaissement=to_number(tab[i+1]); d_vars.encaissement_dices="d20"; if (tab[i+2]=="4") d_vars.encaissement_dices="d10"; i+=3; break;""" FLAT_DICES_PARSE="""d_vars.flat_dices[d_vars.nb_flat_dices]=to_p_number(tab[i+1]); d_vars.nb_flat_dices++; break;""" ## Custom adders: lambda_flat_dices=lambda a: "@flat_dices[@flat_dices]=type({tab+1});\n@nb_flat_dices++;" ## Custom rerolls: categories=['varname','Pretty name','nb_args','type','code','id','default','switch','parser'] hashedCategories={} for c in range(len(categories)): hashedCategories[categories[c]]=c def findCat(element,category): return element[hashedCategories[category]] list=[] #list.append(['varname','Pretty name','nb_args','type','code','id','default','switch','parser']) list.append(['perfection','Perfection',1,'int','P',0,0,None,None]) list.append(['defense_i_0','Defence impénétrable simple',2,'int','I',1,0,None,None]) list.append(['defense_i_1','Defence impénétrable expert',2,'int','I',2,0,None,None]) list.append(['defense_i_2','Defence impénétrable meta',2,'int','I',4,0,None,None]) list.append(['rempart_p','Rempart parfait',1,'int','R',0,0,None,None]) list.append(['technique_m','Perfection',1,'int','M',0,0,None,None]) list.append(['coup_d','Coup_déchirant',1,'int','D',0,0,None,None]) list.append(['fauchage','Fauchage',1,'int','F',0,0,None,None]) list.append(['exploiter_p_0','Exploiter_les_points_faibles simple',2,'int','E',1,0,None,None]) list.append(['exploiter_p_1','Exploiter_les_points_faibles expert',2,'int','E',2,0,None,None]) list.append(['exploiter_p_2','Exploiter_les_points_faibles meta',2,'int','E',4,0,None,None]) list.append(['tir_p_0','Tir_précis expert',2,'int','T',2,0,None,None]) list.append(['tir_p_1','Tir_précis meta',2,'int','T',4,0,None,None]) list.append(['tir_i','Tir_implacable',1,'int','i',0,0,None,None]) list.append(['charge','Charge',1,'int','C',0,0,None,None]) list.append(['charge_i','Charge_indomptable',1,'int','N',0,0,None,None]) list.append(['nb_2add','Nombre à ajouter',1,'int','+',0,0,None,None]) list.append(['nb_2sub','Nombre a soustraire',1,'int','-',0,0,None,None]) list.append(['relances','Relances',1,'int','r',0,0,None,None]) list.append(['seuil','Seuil',1,'int','s',0,0,None,None]) list.append(['action','Type d\'Action',1,'str','a',0,'""',None,None]) list.append(['flat_dices','Des fixes',1,'int','d',0,"[]",None,FLAT_DICES_PARSE]) list.append(['on_hit_c','Dégats a l\'impact critique',1,'int','H',0,0,None,None]) list.append(['attribute','Caractéristique',1,'int','A',0,0,None,None]) list.append(['encaissement','Encaissement',1,'int','S',0,0,None,ENCAISSEMENT_PARSE]) # It's actually 2 args but no switch list.append(['replace','Des de substitution',1,'int','L',0,-1,None,None]) list.append(['add_to_all','Valeur d\'ajout aux des',1,'int','l',0,0,None,None]) list.append(['max_dices','Nombre de des max',1,'int','m',0,-1,None,None]) list.append(['player_name','Nom du joueur',1,'int',':',0,'""',None,None]) list.append(['nb_dices','Nombre de des',1,'int','',0,0,None,None]) list.append(['nb_flat_dices','Nombre de des fixes',1,'int','',0,0,None,None]) list.append(['encaissement_dices','Perfection',1,'int','',0,'""',None,None]) list.append(['results','Resultats',1,'int','',0,"[]",None,None]) list.append(['cleave','Fauchage',1,'int','',0,"[]",None,None]) list.append(['encaissement_result','Resultat d\'encaissement',1,'int','',0,0,None,None]) list.append(['technique_result','Dégats technique martiale',1,'int','',0,0,None,None]) list.append(['coup_d_results','Coup déchirant results',1,'int','',0,"[]",None,None]) class megaListWrapper(): def __init__(self,table=list): self.table=table self.len=len(table) self.indexes={} for i in range(len(table)): self.indexes[findCat(table[i],"varname")]=i # Theese are array because you can't acess them directly self.singles=[] self.code=[] self.doubles={} for i in table: if findCat(i,'nb_args')==1: self.singles.append(i) elif findCat(i,'nb_args')==2: n=findCat(i,'code') if n not in self.doubles: self.doubles[n]=[] self.doubles[n].append(i) if findCat(i,'code')!='': self.code.append(i) def __getitem__(self,argument): #Actually return the full line as a list wrapper return listWrapper(self.table[self.indexes[argument]]) def __iter__(self): for index,key in self.indexes.items(): yield self[index] def itersingles(self): "Only iterate over the simple argument ones" for index in self.singles: yield self[findCat(index,'varname')] def iterdoubles(self): "Only iterate over the double arguments ones return a table with all" for table in self.doubles.values(): yield [self[findCat(l,'varname')] for l in table] def iterdefined(self): "Only iterate over the ones with defined code" pass def __contains__(self,val): return val in self.indexes def __len__(self): return self.len def pprint(self): for e in self: print(e[0],":",e) class listWrapper(): def __init__(self,list,index=hashedCategories): self.list=list self.indexes=index def pprint(self): print(self.list) def asList(self): return self.list def __len__(self): return len(self.list) def __repr__(self): return str(self.list) def __getitem__(self,nb): return self.list[nb] def __getattr__(self,index): return self.list[self.indexes[index]] def __iter__(self): for e in self.list: yield e ## Commands def initialise(args): "Initialise the variables for the switch" text="var d_vars={" for line in list: text+='"{}":{},'.format(line[0],line[6]) text=text[:-1]+'};' return text def pprint(args): "Print the whole list of values" for i in list: print(i) def pErrors(name,errors): "Print sucess or error of given test" if errors: print("{:<15}: {} errors occured".format(name,errors)) else: print("{:<15}: All test sucessful".format(name)) return errors def reroll(args): "Generate the reroll inline message" reroll="var msg_relance=\"Relancer ce jet";\'' return reroll def parser(args): "Generate the argument parser" tab=" " EOL="\n" res=tab+"if (len_args>0){"+EOL res+=tab*2+"d_vars.nb_dices=to_p_number(tab[0]);"+EOL res+=tab+"}"+EOL res+=tab+"for (i = 1; i < len_args;) {"+EOL res+=tab*2+"switch(tab[i]) {"+EOL def typewise(type): if type=="int": return "to_number(tab[i+1]);" return "tab[i+1];" doubles={} #Should probably be handled by the wrapper with a custom loop for elt in wrapper.itersingles(): if elt.parser: res+=tab*3+'case "{}":'.format(elt.code)+EOL for l in elt.parser.splitlines(): res+=tab*4+l+EOL elif elt.code!="": res+=tab*3+'case "{}":'.format(elt.code)+EOL res+=tab*4+'d_vars.{}+={}'.format(elt.varname,typewise(elt.type))+EOL res+=tab*4+'i+=2;'+EOL res+=tab*4+'break;'+EOL for grp in wrapper.iterdoubles(): res+=tab*3+'case "{}":'.format(grp[0].code)+EOL res+=tab*4+'switch (tab[i+1]) {'+EOL for elt in grp: if elt.parser: for l in elt.parser.splitlines(): res+=tab*5+l+EOL elif elt.code!="": res+=tab*5+'case "{}":'.format(elt.id)+EOL res+=tab*6+'d_vars.{}+={}'.format(elt.varname,typewise(elt.type))+EOL res+=tab*6+'break;'+EOL if not elt.parser: res+=tab*4+"}"+EOL res+=tab*4+'i+=3;'+EOL res+=tab*4+'break;'+EOL res+=tab*3+"default:"+EOL res+=tab*4+'logit("Argument "+tab[i]+" unknown.");'+EOL res+=tab*4+"i++;"+EOL res+=tab*4+"break;"+EOL res+=tab*2+"}"+EOL res+=tab*1+"}"+EOL print(res) class testInit(minitest.simpleTestUnit): """Check the initialisation message""" def __init__(self): super(testInit, self).__init__("initialisation variables") def _testInit(self): self.currentTest("generating test values") try: test_values=args[1] except: self.addWarning("No arguments given, using defaults",nonDestructive=True) test_values={"nb_dices":0,"perfection":0,"defense_i_0":0,"defense_i_1":0, "defense_i_2":0,"rempart_p":0,"technique_m":0,"coup_d":0,"coup_d_results":"[]", "fauchage":0,"exploiter_p_0":0,"exploiter_p_1":0,"exploiter_p_2":0,"tir_p_0":0, "tir_p_1":0,"tir_i":0,"charge":0,"charge_i":0,"nb_2add":0,"nb_2sub":0,"relances":0, "seuil":0,"nb_flat_dices":0,"action":'""',"flat_dices":"[]","results":"[]","technique_result":0, "cleave":"[]","on_hit_c":0,"attribute":0,"encaissement":0,"encaissement_dices":'""', "encaissement_result":0,"replace":-1,"add_to_all":0,"max_dices":-1,"player_name":'""'} a=len(test_values) b=len(list) try: dict_list={} for l in list: dict_list[l[0]]=l[6] self.addSuccess() except Exception as ex: self.addFailure(str(ex)) self.currentTest("testing list") if a {} ({})".format(sys.exc_info()[1],sys.exc_info()[0])) traceback.print_tb(sys.exc_info()[2])