X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomrogue-server.py;h=0e42ceeccbaf3d0665a29cecf1e94aae1048a848;hb=d88ddfea1633a4d5220afa8b5ceefdf924ee06ce;hp=6562e9f94ab48e8c437325ff934d7ab150268428;hpb=b83325a35eeb77b6d9c83644c54b5ff9c50c5694;p=plomrogue diff --git a/plomrogue-server.py b/plomrogue-server.py index 6562e9f..0e42cee 100755 --- a/plomrogue-server.py +++ b/plomrogue-server.py @@ -6,6 +6,13 @@ import shutil import time +def strong_write(file, string): + """Apply write(string), flush() and os.fsync() to file.""" + file.write(string) + file.flush() + os.fsync(file) + + def setup_server_io(): """Fill IO files DB with proper file( path)s. Write process IO test string. @@ -25,8 +32,7 @@ def setup_server_io(): io_db["teststring"] = str(os.getpid()) + " " + str(time.time()) os.makedirs(io_db["path_server"], exist_ok=True) io_db["file_out"] = open(io_db["path_out"], "w") - io_db["file_out"].write(io_db["teststring"] + "\n") - io_db["file_out"].flush() + strong_write(io_db["file_out"], io_db["teststring"] + "\n") if os.access(io_db["path_in"], os.F_OK): os.remove(io_db["path_in"]) io_db["file_in"] = open(io_db["path_in"], "w") @@ -72,7 +78,7 @@ def obey(command, prefix, replay=False, do_record=False): if len(tokens) > 0 and tokens[0] in commands_db \ and len(tokens) == commands_db[tokens[0]][0] + 1: if commands_db[tokens[0]][1]: - commands_db[tokens[0]][2]() + commands_db[tokens[0]][2](*tokens[1:]) elif replay: print("Due to replay mode, reading command as 'go on in record'.") line = io_db["file_record"].readline() @@ -101,9 +107,7 @@ def atomic_write(path, text, do_append=False): if os.access(path, os.F_OK): shutil.copyfile(path, path_tmp) file = open(path_tmp, mode) - file.write(text) - file.flush() - os.fsync(file.fileno()) + strong_write(file, text) file.close() if os.access(path, os.F_OK): os.remove(path) @@ -270,7 +274,7 @@ def try_worldstate_update(): string = string + line + "\n" # TODO: no proper user-subjective map atomic_write(io_db["path_worldstate"], string) - atomic_write(io_db["path_out"], "WORLD_UPDATED\n", do_append=True) + strong_write(io_db["file_out"], "WORLD_UPDATED\n") io_db["worldstate_updateable"] = False @@ -414,8 +418,7 @@ def id_setter(id, category, id_store=False, start_at_1=False): def command_ping(): """Send PONG line to server output file.""" - io_db["file_out"].write("PONG\n") - io_db["file_out"].flush() + strong_write(io_db["file_out"], "PONG\n") def command_quit(): @@ -423,6 +426,11 @@ def command_quit(): raise SystemExit("received QUIT command") +def command_thingshere(y, x): + # DUMMY + print("Ignoring not-yet implemented THINGS_HERE command.") + + def command_seedmap(seed_string): """Set world_db["SEED_MAP"] to int(seed_string), then (re-)make map.""" setter(None, "SEED_MAP", 0, 4294967295)(seed_string) @@ -474,7 +482,7 @@ def command_makeworld(seed_string): } # generate fov map? # TODO: Generate things (player first, with updated memory) - atomic_write(io_db["path_out"], "NEW_WORLD\n", do_append=True) + strong_write(io_db["file_out"], "NEW_WORLD\n") def command_maplength(maplength_string): @@ -751,6 +759,7 @@ to be called on it. commands_db = { "QUIT": (0, True, command_quit), "PING": (0, True, command_ping), + "THINGS_HERE": (2, True, command_thingshere), "MAKE_WORLD": (1, False, command_makeworld), "SEED_MAP": (1, False, command_seedmap), "SEED_RANDOMNESS": (1, False, setter(None, "SEED_RANDOMNESS", @@ -791,7 +800,7 @@ commands_db = { """World state database. With sane default values.""" world_db = { - "TURN": 1, + "TURN": 0, "SEED_MAP": 0, "SEED_RANDOMNESS": 0, "PLAYER_TYPE": 0,