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.
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")
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)
string = ""
for key in world_db:
- if dict != type(world_db[key]):
+ if dict != type(world_db[key]) and key != "MAP":
string = string + key + " " + str(world_db[key]) + "\n"
string = string + helper("ThingActions", "TA_ID")
string = string + helper("ThingTypes", "TT_ID", {"TT_CORPSE_ID": False})
string = string + line + "\n"
# TODO: no proper user-subjective map
atomic_write(io_db["path_worldstate"], string)
- io_db["file_out"].write("WORLD_UPDATED\n")
- io_db["file_out"].flush()
- os.fsync(io_db["file_out"])
+ strong_write(io_db["file_out"], "WORLD_UPDATED\n")
io_db["worldstate_updateable"] = False
def command_ping():
"""Send PONG line to server output file."""
- io_db["file_out"].write("PONG\n")
- io_db["file_out"].flush()
- os.fsync(io_db["file_out"].fileno())
+ strong_write(io_db["file_out"], "PONG\n")
def command_quit():
}
# 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):
- # DUMMY.
+ """Redefine map length. Invalidate map, therefore lose all things on it."""
set_world_inactive()
- # TODO: remove map (is this necessary? no memory management trouble …)
world_db["Things"] = {}
setter(None, "MAP_LENGTH", 1, 256)(maplength_string)
def command_worldactive(worldactive_string):
- # DUMMY.
+ """Toggle world_db["WORLD_ACTIVE"] if possible.
+
+ An active world can always be set inactive. An inactive world can only be
+ set active with a "wait" ThingAction, and a player Thing (of ID 0). On
+ activation, rebuild all Things' FOVs and map memories.
+ """
+ # In original version, map existence was also tested (unnecessarily?).
val = integer_test(worldactive_string, 0, 1)
if val:
if 0 != world_db["WORLD_ACTIVE"]:
if 0 == Thing:
player_exists = True
break
- map_exists = "MAP" in world_db
- if wait_exists and player_exists and map_exists:
+ if wait_exists and player_exists:
# TODO: rebuild all things' FOVs, map memories
world_db["WORLD_ACTIVE"] = 1
"""World state database. With sane default values."""
world_db = {
- "TURN": 1,
+ "TURN": 0,
"SEED_MAP": 0,
"SEED_RANDOMNESS": 0,
"PLAYER_TYPE": 0,