X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=plomrogue-server.py;h=568b21cf4db9b00c9caf4beeb55c798a90f37aae;hb=82e96719b4cf7105f814f72d07e0d861000c5d39;hp=d9593f854f166196ee4f039c3692e24bff827fc7;hpb=0891d236075c217eb7e32a38c8da354a6419a1c3;p=plomrogue diff --git a/plomrogue-server.py b/plomrogue-server.py index d9593f8..568b21c 100755 --- a/plomrogue-server.py +++ b/plomrogue-server.py @@ -21,6 +21,7 @@ def setup_server_io(io_db): io_db["path_record"] = "record" io_db["path_save"] = "save" io_db["path_worldconf"] = "confserver/world" + io_db["tmp_suffix"] = "_tmp" io_db["teststring"] = str(os.getpid()) + " " + str(time.time()) os.makedirs(io_dir, exist_ok=True) io_db["file_out"] = open(io_db["path_out"], "w") @@ -43,9 +44,9 @@ def cleanup_server_io(io_db): helper("file_worldstate", "path_worldstate") -def detect_atomic_leftover(path): - """Raise explained SystemExit if file is found at path + "_tmp".""" - path_tmp = path + "_tmp" +def detect_atomic_leftover(path, tmp_suffix): + """Raise explained SystemExit if file is found at path + tmp_suffix.""" + path_tmp = path + tmp_suffix msg = "Found file '" + path_tmp + "' that may be a leftover from an " \ "aborted previous attempt to write '" + path + "'. Aborting until " \ "the matter is resolved by removing it from its current path." @@ -83,6 +84,16 @@ def record(cmd, path_recordfile): file.close() +def obey_lines_in_file(path, name): + """Call obey() on each line of path's file, use name in input prefix.""" + file = open(io_db["path_worldconf"], "r") + line_n = 1 + for line in file.readlines(): + obey(line.rstrip(), io_db, name + "file line " + str(line_n)) + line_n = line_n + 1 + file.close() + + io_db = {} try: parser = argparse.ArgumentParser() @@ -90,8 +101,8 @@ try: action='store') setup_server_io(io_db) # print("DUMMY: Run game.") - detect_atomic_leftover(io_db["path_save"]) - detect_atomic_leftover(io_db["path_record"]) + detect_atomic_leftover(io_db["path_save"], io_db["tmp_suffix"]) + detect_atomic_leftover(io_db["path_record"], io_db["tmp_suffix"]) opts, unknown = parser.parse_known_args() if None != opts.replay: if opts.replay < 1: @@ -102,17 +113,12 @@ try: raise SystemExit("No record file found to replay.") else: if os.access(io_db["path_save"], os.F_OK): - print(open(io_db["path_save"], "r").read()) + obey_lines_in_file(io_db["path_save"], "save") else: if not os.access(io_db["path_worldconf"], os.F_OK): msg = "No world config file from which to start a new world." raise SystemExit(msg) - file = open(io_db["path_worldconf"]) - line_n = 1 - for line in file.readlines(): - obey(line.rstrip(), io_db, "worldconfig line " + str(line_n)) - line_n = line_n + 1 - file.close() + obey_lines_in_file(io_db["path_worldconf"], "world config ") obey("MAKE_WORLD " + str(int(time.time())), io_db, "in file") # print("DUMMY: Run io_loop().") except SystemExit as exit: