X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=server%2Fio.py;h=01d04365770ace851b97568ea7f0c22cd19dc451;hb=0a790da8aa952ed96d46c913aadebf9fc361fc43;hp=46facb9ea47c475f1b17d27ffed0e60f896e87fd;hpb=08f8592f1a1e5d340b6c2372551f0a3da28a07d8;p=plomrogue diff --git a/server/io.py b/server/io.py index 46facb9..01d0436 100644 --- a/server/io.py +++ b/server/io.py @@ -1,7 +1,11 @@ +# 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. + + import os import time - from server.config.world_data import world_db from server.config.io import io_db @@ -163,21 +167,59 @@ def save_world(): def helper(category, id_string, special_keys={}): string = "" - for id in sorted(world_db[category].keys()): - string = string + id_string + " " + str(id) + "\n" - for key in sorted(world_db[category][id].keys()): + for _id in sorted(world_db[category].keys()): + string = string + id_string + " " + str(_id) + "\n" + for key in sorted(world_db[category][_id].keys()): if not key in special_keys: - x = world_db[category][id][key] + x = world_db[category][_id][key] argument = quote_escape(x) if str == type(x) else str(x) string = string + key + " " + argument + "\n" elif special_keys[key]: - string = string + special_keys[key](id) + string = string + special_keys[key](_id) return string + def helper_things(): + string = "" + memmap = mapsetter("T_MEMMAP") + memdepthmap = mapsetter("T_MEMDEPTHMAP") + for tid in sorted(world_db["Things"].keys()): + string += "T_ID " + str(tid) + "\n" + t = world_db["Things"][tid] + for key in sorted(t.keys()): + if key not in {"T_CARRIES", "carried", "fovmap", "T_MEMMAP", + "T_MEMTHING", "T_MEMDEPTHMAP"}: + argument = t[key] + string += key + " " + (quote_escape(argument) if \ + str == type(argument) else str(argument)) + "\n" + string += memthing(tid) + memmap(tid) + memdepthmap(tid) + return string + + # ALTERNATIVE TO helper_things, but not more efficient despite listcomp + # def helper4(): + # def foo(t, key): + # argument = t[key] + # return key + " " + (quote_escape(argument) if \ + # str == type(argument) else str(argument)) + "\n" + # string = "" + # memmap = mapsetter("T_MEMMAP") + # memdepthmap = mapsetter("T_MEMDEPTHMAP") + # for tid in sorted(world_db["Things"].keys()): + # string += "T_ID " + str(tid) + "\n" + # t = world_db["Things"][tid] + # lines = [foo(t, key) for key in sorted(t.keys()) + # if key not in {"T_CARRIES", "carried", "fovmap", + # "T_MEMMAP", "T_MEMTHING", "T_MEMDEPTHMAP"}] + # string += "".join(lines) + # string += memthing(tid) + memmap(tid) + memdepthmap(tid) + # return string + string = "" + for plugin in world_db["PLUGIN"]: + string = string + "PLUGIN " + plugin + "\n" for key in sorted(world_db.keys()): - if (not isinstance(world_db[key], dict)) and key != "MAP" and \ - key != "WORLD_ACTIVE": + if (not isinstance(world_db[key], dict) and + not isinstance(world_db[key], list)) and key != "MAP" and \ + key != "WORLD_ACTIVE" and key[0].isupper(): string = string + key + " " + str(world_db[key]) + "\n" string = string + mapsetter("MAP")() string = string + helper("ThingActions", "TA_ID") @@ -185,15 +227,11 @@ def save_world(): for id in sorted(world_db["ThingTypes"].keys()): string = string + "TT_ID " + str(id) + "\n" + "TT_CORPSE_ID " + \ str(world_db["ThingTypes"][id]["TT_CORPSE_ID"]) + "\n" - string = string + helper("Things", "T_ID", - {"T_CARRIES": False, "carried": False, - "T_MEMMAP": mapsetter("T_MEMMAP"), - "T_MEMTHING": memthing, "fovmap": False, - "T_MEMDEPTHMAP": mapsetter("T_MEMDEPTHMAP")}) - for id in sorted(world_db["Things"].keys()): - if [] != world_db["Things"][id]["T_CARRIES"]: - string = string + "T_ID " + str(id) + "\n" - for carried in sorted(world_db["Things"][id]["T_CARRIES"]): + string += helper_things() + for tid in sorted(world_db["Things"].keys()): + if [] != world_db["Things"][tid]["T_CARRIES"]: + string = string + "T_ID " + str(tid) + "\n" + for carried in sorted(world_db["Things"][tid]["T_CARRIES"]): string = string + "T_CARRIES " + str(carried) + "\n" string = string + "SEED_RANDOMNESS " + str(rand.seed) + "\n" + \ "WORLD_ACTIVE " + str(world_db["WORLD_ACTIVE"]) @@ -253,7 +291,7 @@ def obey(command, prefix, replay=False, do_record=False): io_db["save_wait"] = time.time() io_db["worldstate_updateable"] = world_db["WORLD_ACTIVE"] elif 0 != len(tokens): - print("Invalid command/argument, or bad number of tokens.") + print("Invalid command/argument, or bad number of tokens: " + command) def obey_lines_in_file(path, name, do_record=False): @@ -269,56 +307,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