From: Christian Heller Date: Fri, 19 Feb 2016 11:34:36 +0000 (+0100) Subject: Refactor worldstate file writing, make it more extensible. X-Git-Tag: tce~199 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/error?a=commitdiff_plain;h=6f76684b256fdbd680224bdc47d32ef332d1a852;p=plomrogue Refactor worldstate file writing, make it more extensible. --- diff --git a/server/config/io.py b/server/config/io.py index ec6a8ae..4631638 100644 --- a/server/config/io.py +++ b/server/config/io.py @@ -3,6 +3,10 @@ # see the file NOTICE in the root directory of the PlomRogue source package. +from server.worldstate_write_helpers import write_inventory, write_fov_map, \ + write_mem_map + + """File IO database.""" io_db = { "path_save": "save", @@ -16,5 +20,16 @@ io_db = { "worldstate_updateable": False, "wait_on_read_fail": 0.03333, "max_wait_on_read_fail": 5, - "save_wait": 15 + "save_wait": 15, + "worldstate_write_order": [ + ["TURN", "world_int"], + ["T_LIFEPOINTS", "player_int"], + ["T_SATIATION", "player_int"], + [write_inventory, "func"], + ["T_POSY", "player_int"], + ["T_POSX", "player_int"], + ["MAP_LENGTH", "world_int"], + [write_fov_map, "func"], + [write_mem_map, "func"] + ] } diff --git a/server/io.py b/server/io.py index 65d1c35..0357b2e 100644 --- a/server/io.py +++ b/server/io.py @@ -273,56 +273,15 @@ def obey_lines_in_file(path, name, do_record=False): def try_worldstate_update(): """Write worldstate file if io_db["worldstate_updateable"] is set.""" - from server.config.commands import commands_db if io_db["worldstate_updateable"]: - - def write_map(string, map): - for i in range(length): - line = map[i * length:(i * length) + length].decode() - string = string + line + "\n" - return string - - inventory = "" - if [] == world_db["Things"][0]["T_CARRIES"]: - inventory = "(none)\n" - else: - for id in world_db["Things"][0]["T_CARRIES"]: - type_id = world_db["Things"][id]["T_TYPE"] - name = world_db["ThingTypes"][type_id]["TT_NAME"] - inventory = inventory + name + "\n" - string = str(world_db["TURN"]) + "\n" + \ - str(world_db["Things"][0]["T_LIFEPOINTS"]) + "\n" + \ - str(world_db["Things"][0]["T_SATIATION"]) + "\n" + \ - inventory + "%\n" + \ - str(world_db["Things"][0]["T_POSY"]) + "\n" + \ - str(world_db["Things"][0]["T_POSX"]) + "\n" + \ - str(world_db["MAP_LENGTH"]) + "\n" - length = world_db["MAP_LENGTH"] - fov = bytearray(b' ' * (length ** 2)) - ord_v = ord("v") - for pos in [pos for pos in range(length ** 2) - if ord_v == world_db["Things"][0]["fovmap"][pos]]: - fov[pos] = world_db["MAP"][pos] - length = world_db["MAP_LENGTH"] - for id in [id for tid in reversed(sorted(list(world_db["ThingTypes"]))) - for id in world_db["Things"] - if not world_db["Things"][id]["carried"] - if world_db["Things"][id]["T_TYPE"] == tid - if world_db["Things"][0]["fovmap"][ - world_db["Things"][id]["T_POSY"] * length - + world_db["Things"][id]["T_POSX"]] == ord_v]: - type = world_db["Things"][id]["T_TYPE"] - c = ord(world_db["ThingTypes"][type]["TT_SYMBOL"]) - fov[world_db["Things"][id]["T_POSY"] * length - + world_db["Things"][id]["T_POSX"]] = c - string = write_map(string, fov) - mem = world_db["Things"][0]["T_MEMMAP"][:] - for mt in [mt for tid in reversed(sorted(list(world_db["ThingTypes"]))) - for mt in world_db["Things"][0]["T_MEMTHING"] - if mt[0] == tid]: - c = world_db["ThingTypes"][mt[0]]["TT_SYMBOL"] - mem[(mt[1] * length) + mt[2]] = ord(c) - string = write_map(string, mem) + string = "" + for entry in io_db["worldstate_write_order"]: + if entry[1] == "world_int": + string += str(world_db[entry[0]]) + "\n" + elif entry[1] == "player_int": + string += str(world_db["Things"][0][entry[0]]) + "\n" + elif entry[1] == "func": + string += entry[0]() atomic_write(io_db["path_worldstate"], string, delete=False) strong_write(io_db["file_out"], "WORLD_UPDATED\n") io_db["worldstate_updateable"] = False diff --git a/server/worldstate_write_helpers.py b/server/worldstate_write_helpers.py new file mode 100644 index 0000000..8ec4b35 --- /dev/null +++ b/server/worldstate_write_helpers.py @@ -0,0 +1,56 @@ +# This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3 +# or any later version. For details on its copyright, license, and warranties, +# see the file NOTICE in the root directory of the PlomRogue source package. + + +from server.config.world_data import world_db + + +def write_inventory(): + inventory = "" + if [] == world_db["Things"][0]["T_CARRIES"]: + inventory = "(none)\n" + else: + for id in world_db["Things"][0]["T_CARRIES"]: + type_id = world_db["Things"][id]["T_TYPE"] + name = world_db["ThingTypes"][type_id]["TT_NAME"] + inventory = inventory + name + "\n" + inventory += "%\n" + return inventory + +def write_map(map, length): + string = "" + for i in range(length): + line = map[i * length:(i * length) + length].decode() + string = string + line + "\n" + return string + +def write_fov_map(): + length = world_db["MAP_LENGTH"] + fov = bytearray(b' ' * (length ** 2)) + ord_v = ord("v") + for pos in [pos for pos in range(length ** 2) + if ord_v == world_db["Things"][0]["fovmap"][pos]]: + fov[pos] = world_db["MAP"][pos] + for id in [id for tid in reversed(sorted(list(world_db["ThingTypes"]))) + for id in world_db["Things"] + if not world_db["Things"][id]["carried"] + if world_db["Things"][id]["T_TYPE"] == tid + if world_db["Things"][0]["fovmap"][ + world_db["Things"][id]["T_POSY"] * length + + world_db["Things"][id]["T_POSX"]] == ord_v]: + type = world_db["Things"][id]["T_TYPE"] + c = ord(world_db["ThingTypes"][type]["TT_SYMBOL"]) + fov[world_db["Things"][id]["T_POSY"] * length + + world_db["Things"][id]["T_POSX"]] = c + return write_map(fov, length) + +def write_mem_map(): + length = world_db["MAP_LENGTH"] + mem = world_db["Things"][0]["T_MEMMAP"][:] + for mt in [mt for tid in reversed(sorted(list(world_db["ThingTypes"]))) + for mt in world_db["Things"][0]["T_MEMTHING"] + if mt[0] == tid]: + c = world_db["ThingTypes"][mt[0]]["TT_SYMBOL"] + mem[(mt[1] * length) + mt[2]] = ord(c) + return write_map(mem, length)