diff --git a/worldbuild-manager.py b/worldbuild-manager.py index f655bec..357ca25 100755 --- a/worldbuild-manager.py +++ b/worldbuild-manager.py @@ -69,6 +69,61 @@ def save_state(state): except IOError: print("WARNING: Failed to write to file") +def set_state(state, name, status): + if verbose: + print(name + " set status to " + status) + + if status == "started" or status == "done" or status == "rebuild" or status == "skip" or status == "pending": + if name == "n-pole" or name == "s-pole": + if not name in state: + state[name] = {} + state[name]["status"] = status + else: + match = re.match(r"([ew])(\d{3})([ns])(\d{2})", name) + if match == None: + print("WARNING: Recived status " + status + " from invalid tile '" + name + "'") + else: + ew = match.group(1) + ew_val = int(match.group(2)) + ns = match.group(3) + ns_val = int(match.group(4)) + + ew_val_major = int(ew_val / 10) * 10 + if ew == "w": + if ew_val_major != ew_val: + ew_val_major += 10 + + ns_val_major = int(ns_val / 10) * 10 + if ns == "s": + if ns_val_major != ns_val: + ns_val_major += 10 + + name_major = ew + norm(ew_val_major, 3) + ns + norm(ns_val_major, 2) + if not name_major in state: + state[name_major] = {} + if not name in state[name_major]: + state[name_major][name] = {} + state[name_major][name]["status"] = status + state[name_major]["done"] = 0 + state[name_major]["started"] = 0 + state[name_major]["rebuild"] = 0 + state[name_major]["skip"] = 0 + state[name_major]["pending"] = 0 + for tile in state[name_major]: + # Filter status of name_major + if tile != "done" and tile != "started" and tile != "pending" and tile != "status" and tile != "rebuild" and tile != "skip": + state[name_major][state[name_major][tile]["status"]] += 1 + if state[name_major]["done"] == 100: + state[name_major]["status"] = "done" + elif state[name_major]["started"] > 0: + state[name_major]["status"] = "started" + elif state[name_major]["rebuild"] > 0: + state[name_major]["status"] = "rebuild" + elif state[name_major]["skip"] > 0: + state[name_major]["status"] = "skip" + else: + state[name_major]["status"] = "pending" + if os.path.isfile(sfile): try: with open(sfile) as json_data: @@ -110,59 +165,7 @@ try: print("WARNING: Recived invalid package") if action == "set": - if verbose: - print(name + " set status to " + status) - - if status == "started" or status == "done" or status == "rebuild" or status == "skip" or status == "pending": - if name == "n-pole" or name == "s-pole": - if not name in state: - state[name] = {} - state[name]["status"] = status - else: - match = re.match(r"([ew])(\d{3})([ns])(\d{2})", name) - if match == None: - print("WARNING: Recived status " + status + " from invalid tile '" + name + "'") - else: - ew = match.group(1) - ew_val = int(match.group(2)) - ns = match.group(3) - ns_val = int(match.group(4)) - - ew_val_major = int(ew_val / 10) * 10 - if ew == "w": - if ew_val_major != ew_val: - ew_val_major += 10 - - ns_val_major = int(ns_val / 10) * 10 - if ns == "s": - if ns_val_major != ns_val: - ns_val_major += 10 - - name_major = ew + norm(ew_val_major, 3) + ns + norm(ns_val_major, 2) - if not name_major in state: - state[name_major] = {} - if not name in state[name_major]: - state[name_major][name] = {} - state[name_major][name]["status"] = status - state[name_major]["done"] = 0 - state[name_major]["started"] = 0 - state[name_major]["rebuild"] = 0 - state[name_major]["skip"] = 0 - state[name_major]["pending"] = 0 - for tile in state[name_major]: - # Filter status of name_major - if tile != "done" and tile != "started" and tile != "pending" and tile != "status" and tile != "rebuild" and tile != "skip": - state[name_major][state[name_major][tile]["status"]] += 1 - if state[name_major]["done"] == 100: - state[name_major]["status"] = "done" - elif state[name_major]["started"] > 0: - state[name_major]["status"] = "started" - elif state[name_major]["rebuild"] > 0: - state[name_major]["status"] = "rebuild" - elif state[name_major]["skip"] > 0: - state[name_major]["status"] = "skip" - else: - state[name_major]["status"] = "pending" + state = set_state(state, name, status) save_state(state) else: @@ -173,10 +176,10 @@ try: # Build poles first if not "n-pole" in state or ("n-pole" in state and state["n-pole"]["status"] == get): tile = "n-pole" - state["n-pole"]["status"] = "started" + state = set_state(state, "n-pole", "started") elif not "s-pole" in state or ("s-pole" in state and state["s-pole"]["status"] == get): tile = "s-pole" - state["s-pole"]["status"] = "started" + state = set_state(state, "n-pole", "started") else: ii = -8 @@ -214,7 +217,7 @@ try: name_minor = ew + norm(j, 3) + ns + norm(iii, 2) if not name in state or (not name_minor in state[name] or (name_minor in state[name] and (state[name][name_minor]["status"] == get or state[name][name_minor]["status"] == "pending"))): tile = name_minor - state[name][name_minor]["status"] = "started" + state = set_state(status, name, "started") break iii += ns_step j += ew_step