From 2ac4e42645bf1f497c86920fd41d9ff8f5edd600 Mon Sep 17 00:00:00 2001 From: Zergling_man Date: Sun, 7 Apr 2024 15:34:16 +1000 Subject: [PATCH] Error reporting is better now. --- codegen.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/codegen.py b/codegen.py index ffd3f91..a4de8e5 100644 --- a/codegen.py +++ b/codegen.py @@ -133,30 +133,35 @@ def generate(officer=None,name=None): out=[] if len(pieces)==6: rank=cap=int(pieces.pop(-1)) else: rank,cap=12,12 - for n in zip(pieces,tables.displays_nogun,tables.displays_nogun_str): - ind=lookup_piece(n[0],n[1]) - rank,cap=min(rank,pro[n[2]][ind][0]),min(cap,pro[n[2]][ind][1]) - out.append(ind) - if gun: out.insert(1,lookup_piece(gun,tables.guns,True)) - else: out.insert(1,tables.gunmaps_wrapped[out[0]]) + try: + for n in zip(pieces,tables.displays_nogun,tables.displays_nogun_str): + ind=lookup_piece(n[0],n[1],n[2]) + rank,cap=min(rank,pro[n[2]][ind][0]),min(cap,pro[n[2]][ind][1]) + if 0 in [rank,cap]: print(f"{n[0]} is locked, go unlock it first.") + out.append(ind) + if gun: out.insert(1,lookup_piece(gun,tables.guns,'guns',True)) + else: out.insert(1,tables.gunmaps_wrapped[out[0]]) + except LookupException as ex: print(ex); return rank,cap=min(rank,pro['guns'][out[1]][0]),min(cap,pro['guns'][out[1]][1]) # Derp2, forgot to actually cap on gun + if 0 in [rank,cap]: return # Already warned about locks earlier. cap=(cap-(cap<10)+(cap>10))//3 # Derp - if rank<1 or cap<0: print("You have selected a locked item, go unlock it first."); return - out[-1]=tables.clas[out[0]].index(out[-1]) + try: out[-1]=tables.clas[out[0]].index(out[-1]) + except ValueError: print(f"{tables.classes[out[0]]} can't get talent {tables.talent[out[-1]]}, options are {list(map(lambda x:tables.talent[x],tables.clas[out[0]]))}"); return muxed=shared.muxhero(out[0],out[3],out[4]) meds=[] if muxed in pro['medals']: meds=shared.muxmedals(pro['medals'][muxed]) return encode.encode(name,*out,rank,cap,0,*meds) -def lookup_piece(piece,table,partial=False): +def lookup_piece(piece,table,name,partial=False): if len(piece)<3: p=piece.upper() else: p=piece.capitalize() # Bloody Americans if not partial: try: return table.index(p) - except ValueError: return None - res=list(filter(lambda x:x.startswith(p),table)) - if len(res)!=1: return None # Ambiguous lookups not allowed + except ValueError: raise LookupException(f"Couldn't resolve {piece} from {name}") + res=list(filter(lambda x:p in x,table)) + if len(res)!=1: raise LookupException(f"Couldn't resolve {piece} with partial match from {name} (found {len(res)} possibilities)") # Ambiguous lookups not allowed return table.index(res[0]) +class LookupException(Exception): pass def ranks(): for n,m in zip(tables.displays_str,tables.displays): @@ -167,4 +172,5 @@ def ranks(): load_conf() + if __name__=="__main__": print("Please open an interpreter and use 'import codegen', this script doesn't serve as an entrypoint on its own yet.")