import sys def get_name_list(file): list = {} f = open(file) for line in f: items = line.split("|") if len(items) < 2: continue id = line.split("|")[0] name = line.split("|")[1].split("\n")[0] name = name.replace(" ","") list[id] = name f.close() return list def get_troop_info(file): list = {} f = open(file) current_troop = "" for line in f: items = line.split() if len(items) <= 0: continue if items[0].find("trp") == 0: current_troop = items[0] id = len(list) func = int(items[-3]) next_1 = int(items[-2]) next_2 = int(items[-1]) list[id] = {"name":current_troop,"func":func,"next":[next_1,next_2],"in_tree":False,"used":False} elif len(items) == 5: list[len(list)-1]["Lv"] = int(items[4]) for id in list.keys(): next_ids = list[id]["next"] for next_id in next_ids: if next_id > 0: list[next_id]["in_tree"] = True list[id]["in_tree"] = True return list def get_rankup_list(info_list): p_list = {} n_list = {} for id,info in info_list.items(): next_ids = info["next"] for next_id in next_ids: if next_id > 0: if id not in p_list: p_list[id] = [] if next_id not in n_list: n_list[next_id] = [] if next_id not in p_list[id]: p_list[id].append(next_id) if id not in n_list[next_id]: n_list[next_id].append(id) return p_list, n_list def get_head(id,p_list,n_list): head = id while head in n_list: head = n_list[head][0] return head def get_next_tree(tree,id,p_list,n_list,used_list): if id not in p_list: return #print id, p_list[id] for next_id in p_list[id]: add_to_tree(id,next_id,tree) get_next_tree(tree,next_id,p_list,n_list,used_list) if id not in used_list: used_list.append(id) def add_to_tree(id,next_id,tree): #print id,next_id,tree next_line = [] for line in tree: if line == next_line: continue if id not in line: continue if line[-1] == id: line.append(next_id) else: idx = line.index(id) t_idx = tree.index(line) if tree[-1][-1] == next_id or tree[-1][-1] == id: continue new_line = idx*[0] new_line.append(id) new_line.append(next_id) tree.append(new_line) next_line = tree[-1] #print id,next_id,tree def print_tree(tree,fd,info_list,name_list): len_list = [0] has_next = True max_len = 0 while has_next: has_next = False current_idx = len(len_list)-1 for line in tree: if len(line) > current_idx: has_next = True else: continue id = line[current_idx] if id <= 0: continue name_len = 0 id_name = info_list[id]["name"] if id_name in name_list: name_len = len(name_list[id_name])*2/3 else: name_len = len(info_list[id]["name"]) if name_len > max_len: max_len = name_len len_list.append(len_list[-1]+max_len+8) #print max_len #print len_list for line in tree: txt = "" txt_len = 0 for i in range(len(line)): id = line[i] if id <= 0: txt += (len_list[i+1]-len(txt)) * " " txt_len += len_list[i+1]-txt_len else: id_name = info_list[id]["name"] name = id_name if name in name_list: name = name_list[name] txt_len += len(name)*2/3 else: txt_len += len(name) lv = info_list[id]["Lv"] t_lv = str(lv) if lv < 10: t_lv = " "+t_lv txt += name+"Lv"+t_lv txt_len += 4 txt += (len_list[i+1]-txt_len-4) * " " txt_len += len_list[i+1]-txt_len-4 #print i,txt_len,len_list[i+1] if id != line[-1]: txt += " -> " txt_len += 4 txt += "\n" #print txt fd.write(txt) #sys.exit(0) def output_to_file2(p_list, n_list, info_list, name_list, file): f = open(file, "w") used_list = [] for id in sorted(p_list.keys()): if id in used_list: continue tree = [] head = get_head(id,p_list,n_list) tree.append([head]) #get_prev_tree(tree,id,p_list,n_list,used_list) get_next_tree(tree,head,p_list,n_list,used_list) #for line in tree: print line #print used_list print_tree(tree,f,info_list,name_list) f.write(80*"-"+"\n") f.close() if __name__ == "__main__": name_list = get_name_list("languages/cns/troops.csv") info_list = get_troop_info("troops.txt") p_list, n_list = get_rankup_list(info_list) #output_to_file(info_list, name_list, "output.txt") output_to_file2(p_list, n_list, info_list, name_list, "troops_list.txt") print "Done." raw_input()