10 """Fill IO files DB with proper file( path)s. Write process IO test string.
12 Ensure IO files directory at server/. Remove any old input file if found.
13 Set up new input file for reading, and new output file for writing. Start
14 output file with process hash line of format PID + " " + floated UNIX time
15 (io_db["teststring"]). Raise SystemExit if file is found at path of either
16 record or save file plus io_db["tmp_suffix"].
18 def detect_atomic_leftover(path, tmp_suffix):
19 path_tmp = path + tmp_suffix
20 msg = "Found file '" + path_tmp + "' that may be a leftover from an " \
21 "aborted previous attempt to write '" + path + "'. Aborting " \
22 "until matter is resolved by removing it from its current path."
23 if os.access(path_tmp, os.F_OK):
25 io_db["teststring"] = str(os.getpid()) + " " + str(time.time())
26 os.makedirs(io_db["path_server"], exist_ok=True)
27 io_db["file_out"] = open(io_db["path_out"], "w")
28 io_db["file_out"].write(io_db["teststring"] + "\n")
29 io_db["file_out"].flush()
30 if os.access(io_db["path_in"], os.F_OK):
31 os.remove(io_db["path_in"])
32 io_db["file_in"] = open(io_db["path_in"], "w")
33 io_db["file_in"].close()
34 io_db["file_in"] = open(io_db["path_in"], "r")
35 detect_atomic_leftover(io_db["path_save"], io_db["tmp_suffix"])
36 detect_atomic_leftover(io_db["path_record"], io_db["tmp_suffix"])
39 def cleanup_server_io():
40 """Close and (if io_db["kicked_by_rival"] false) remove files in io_db."""
41 def helper(file_key, path_key):
43 io_db[file_key].close()
44 if not io_db["kicked_by_rival"] \
45 and os.access(io_db[path_key], os.F_OK):
46 os.remove(io_db[path_key])
47 helper("file_out", "path_out")
48 helper("file_in", "path_in")
49 helper("file_worldstate", "path_worldstate")
50 if "file_record" in io_db:
51 io_db["file_record"].close()
54 def obey(command, prefix, replay=False, do_record=False):
55 """Call function from commands_db mapped to command's first token.
57 The command string is tokenized by shlex.split(comments=True). If replay is
58 set, a non-meta command from the commands_db merely triggers obey() on the
59 next command from the records file. If not, and do do_record is set,
60 non-meta commands are recorded via record(), and save_world() is called.
61 The prefix string is inserted into the server's input message between its
62 beginning 'input ' & ':'. All activity is preceded by a server_test() call.
65 print("input " + prefix + ": " + command)
67 tokens = shlex.split(command, comments=True)
68 except ValueError as err:
69 print("Can't tokenize command string: " + str(err) + ".")
71 if len(tokens) > 0 and tokens[0] in commands_db \
72 and len(tokens) == commands_db[tokens[0]][0] + 1:
73 if commands_db[tokens[0]][1]:
74 commands_db[tokens[0]][2]()
76 print("Due to replay mode, reading command as 'go on in record'.")
77 line = io_db["file_record"].readline()
79 obey(line.rstrip(), io_db["file_record"].prefix
80 + str(io_db["file_record"].line_n))
81 io_db["file_record"].line_n = io_db["file_record"].line_n + 1
83 print("Reached end of record file.")
85 commands_db[tokens[0]][2](*tokens[1:])
89 elif 0 != len(tokens):
90 print("Invalid command/argument, or bad number of tokens.")
93 def atomic_write(path, text, do_append=False):
94 """Atomic write of text to file at path, appended if do_append is set."""
95 path_tmp = path + io_db["tmp_suffix"]
99 if os.access(path, os.F_OK):
100 shutil.copyfile(path, path_tmp)
101 file = open(path_tmp, mode)
104 os.fsync(file.fileno())
106 if os.access(path, os.F_OK):
108 os.rename(path_tmp, path)
112 """Append command string plus newline to record file. (Atomic.)"""
113 # This misses some optimizations from the original record(), namely only
114 # finishing the atomic write with expensive flush() and fsync() every 15
115 # seconds unless explicitely forced. Implement as needed.
116 atomic_write(io_db["path_record"], command + "\n", do_append=True)
120 # Dummy for saving all commands to reconstruct current world state.
121 # Misses same optimizations as record() from the original record().
123 for id in world_db["ThingActions"]:
124 ta = world_db["ThingActions"][id]
125 ta_string = ta_string + "TA_ID " + str(id) + "\n" + \
126 "TA_EFFORT " + str(ta["TA_EFFORT"]) + "\n" + \
127 "TA_NAME '" + ta["TA_NAME"] + "'\n"
129 for id in world_db["ThingTypes"]:
130 tt = world_db["ThingTypes"][id]
131 tt_string = tt_string + "TT_ID " + str(id) + "\n" + \
133 str(tt["TT_CONSUMABLE"]) + "\n" + \
135 str(tt["TT_CORPSE_ID"]) + "\n" + \
136 "TT_PROLIFERATE " + \
137 str(tt["TT_PROLIFERATE"]) + "\n" + \
138 "TT_START_NUMBER " + \
139 str(tt["TT_START_NUMBER"]) + "\n" + \
140 "TT_NAME '" + tt["TT_NAME"] + "'\n" + \
141 "TT_SYMBOL '" + tt["TT_SYMBOL"] + "'\n"
142 atomic_write(io_db["path_save"],
143 "WORLD_ACTIVE " + str(world_db["WORLD_ACTIVE"]) + "\n" +
144 "MAP_LENGTH " + str(world_db["MAP_LENGTH"]) + "\n" +
145 "PLAYER_TYPE " + str(world_db["PLAYER_TYPE"]) + "\n" +
146 "TURN " + str(world_db["TURN"]) + "\n" +
147 "SEED_RANDOMNESS " + str(world_db["SEED_RANDOMNESS"]) + "\n" +
148 "SEED_MAP " + str(world_db["SEED_MAP"]) + "\n" +
149 ta_string + tt_string)
150 # TODO: If all this ever does is just writing down what's in world_db, some
151 # loop over its entries should be all that's needed.
154 def obey_lines_in_file(path, name, do_record=False):
155 """Call obey() on each line of path's file, use name in input prefix."""
156 file = open(path, "r")
158 for line in file.readlines():
159 obey(line.rstrip(), name + "file line " + str(line_n),
165 def parse_command_line_arguments():
166 """Return settings values read from command line arguments."""
167 parser = argparse.ArgumentParser()
168 parser.add_argument('-s', nargs='?', type=int, dest='replay', const=1,
170 opts, unknown = parser.parse_known_args()
175 """Ensure valid server out file belonging to current process.
177 This is done by comparing io_db["teststring"] to what's found at the start
178 of the current file at io_db["path_out"]. On failure, set
179 io_db["kicked_by_rival"] and raise SystemExit.
181 if not os.access(io_db["path_out"], os.F_OK):
182 raise SystemExit("Server output file has disappeared.")
183 file = open(io_db["path_out"], "r")
184 test = file.readline().rstrip("\n")
186 if test != io_db["teststring"]:
187 io_db["kicked_by_rival"] = True
188 msg = "Server test string in server output file does not match. This" \
189 " indicates that the current server process has been " \
190 "superseded by another one."
191 raise SystemExit(msg)
195 """Return next newline-delimited command from server in file.
197 Keep building return string until a newline is encountered. Pause between
198 unsuccessful reads, and after too much waiting, run server_test().
205 add = io_db["file_in"].readline()
207 command = command + add
208 if len(command) > 0 and "\n" == command[-1]:
209 command = command[:-1]
212 time.sleep(wait_on_fail)
213 if now + max_wait < time.time():
220 """Replay game from record file.
222 Use opts.replay as breakpoint turn to which to replay automatically before
223 switching to manual input by non-meta commands in server input file
224 triggering further reads of record file. Ensure opts.replay is at least 1.
228 print("Replay mode. Auto-replaying up to turn " + str(opts.replay) +
229 " (if so late a turn is to be found).")
230 if not os.access(io_db["path_record"], os.F_OK):
231 raise SystemExit("No record file found to replay.")
232 io_db["file_record"] = open(io_db["path_record"], "r")
233 io_db["file_record"].prefix = "record file line "
234 io_db["file_record"].line_n = 1
235 while world_db["TURN"] < opts.replay:
236 line = io_db["file_record"].readline()
239 obey(line.rstrip(), io_db["file_record"].prefix
240 + str(io_db["file_record"].line_n))
241 io_db["file_record"].line_n = io_db["file_record"].line_n + 1
243 obey(read_command(), "in file", replay=True)
247 """Play game by server input file commands. Before, load save file found.
249 If no save file is found, a new world is generated from the commands in the
250 world config plus a 'MAKE WORLD [current Unix timestamp]'. Record this
251 command and all that follow via the server input file.
253 if os.access(io_db["path_save"], os.F_OK):
254 obey_lines_in_file(io_db["path_save"], "save")
256 if not os.access(io_db["path_worldconf"], os.F_OK):
257 msg = "No world config file from which to start a new world."
258 raise SystemExit(msg)
259 obey_lines_in_file(io_db["path_worldconf"], "world config ",
261 obey("MAKE_WORLD " + str(int(time.time())), "in file", do_record=True)
263 obey(read_command(), "in file", do_record=True)
268 print("I'd (re-)make the map now, if only I knew how.")
271 def set_world_inactive():
272 """Set world_db["WORLD_ACTIVE"] to 0 and remove worldstate file."""
274 if os.access(io_db["path_worldstate"], os.F_OK):
275 os.remove(io_db["path_worldstate"])
276 world_db["WORLD_ACTIVE"] = 0
279 def integer_test(val_string, min, max):
280 """Return val_string if possible integer >= min and <= max, else False."""
282 val = int(val_string)
283 if val < min or val > max:
287 print("Ignoring: Please use integer >= " + str(min) + " and <= " +
292 def worlddb_value_setter(key, min, max):
293 """Generate: Set world_db[key] to int(val_string) if >= min and <= max."""
294 def func(val_string):
295 val = integer_test(val_string, min, max)
302 """Send PONG line to server output file."""
303 io_db["file_out"].write("PONG\n")
304 io_db["file_out"].flush()
308 """Abort server process."""
309 raise SystemExit("received QUIT command")
312 def command_seedmap(seed_string):
313 """Set world_db["SEED_MAP"] to int(seed_string), then (re-)make map."""
314 worlddb_value_setter("SEED_MAP", 0, 4294967295)(seed_string)
318 def command_makeworld(seed_string):
320 worlddb_value_setter("SEED_MAP", 0, 4294967295)(seed_string)
321 worlddb_value_setter("SEED_RANDOMNESS", 0, 4294967295)(seed_string)
322 # TODO: Test for existence of player thing and 'wait' thing action?
325 def command_maplength(maplength_string):
328 # TODO: remove things, map
329 worlddb_value_setter("MAP_LENGTH", 1, 256)(maplength_string)
332 def command_worldactive(worldactive_string):
334 val = integer_test(worldactive_string, 0, 1)
336 if 0 != world_db["WORLD_ACTIVE"] and 0 == val:
338 elif 0 == world_db["WORLD_ACTIVE"]:
340 player_exists = False
342 # TODO: perform tests:
343 # Is there thing action of name 'wait'?
344 # Is there a player thing?
346 if wait_exists and player_exists and map_exists:
347 # TODO: rebuild al things' FOVs, map memories
348 world_db["WORLD_ACTIVE"] = 1
351 def command_ttid(id_string):
352 """Set ID of ThingType to manipulate. ID unused? Create new ThingType.
354 The ID of the ThingType to manipulate is stored as command_ttid.id. If
355 the integer of the input value is valid (>= -32768 and <= 32767), but <0 or
356 >255, a new ID is calculated: the lowest unused ID >=0 and <= 255.
358 id = integer_test(id_string, -32768, 32767)
360 if id in world_db["ThingTypes"]:
363 if id < 0 or id > 255:
367 if id not in world_db["ThingTypes"]:
371 "No unused ID available to add to ID list.")
374 world_db["ThingTypes"][id] = {
378 "TT_START_NUMBER": 0,
384 def test_for_id_maker(object, category):
385 """Return decorator testing for object having "id" attribute."""
388 if hasattr(object, "id"):
391 print("Ignoring: No " + category +
392 " defined to manipulate yet.")
397 test_ThingType_id = test_for_id_maker(command_ttid, "ThingType")
400 def ThingType_value_setter(key, min, max):
401 """Build: Set selected ThingType's [key] to int(val_string) >=min/<=max."""
404 val = integer_test(val_string, min, max)
406 world_db["ThingTypes"][command_ttid.id][key] = val
411 def command_ttname(name):
412 """Set to name TT_NAME of selected ThingType."""
413 world_db["ThingTypes"][command_ttid.id]["TT_NAME"] = name
417 def command_ttsymbol(char):
418 """Set to char TT_SYMBOL of selected ThingType. """
420 world_db["ThingTypes"][command_ttid.id]["TT_SYMBOL"] = char
422 print("Ignoring: Argument must be single character.")
426 def command_ttcorpseid(str_int):
427 """Set to int(str_int) TT_CORPSE_ID of selected ThingType."""
428 val = integer_test(str_int, 0, 255)
430 if val in world_db["ThingTypes"]:
431 world_db["ThingTypes"][command_ttid.id]["TT_CORPSE_ID"] = val
433 print("Corpse ID belongs to no known object type.")
436 def command_taid(id_string):
437 """Set ID of ThingAction to manipulate. ID unused? Create new ThingAction.
439 The ID of the ThingAction to manipulate is stored as command_taid.id. If
440 the integer of the input value is valid (>= 0 and <= 255), but 0, a new ID
441 is calculated: The lowest unused ID >0 and <= 255.
443 id = integer_test(id_string, 0, 255)
445 if id in world_db["ThingActions"]:
451 if id not in world_db["ThingActions"]:
455 "No unused ID available to add to ID list.")
458 world_db["ThingActions"][id] = {
464 test_ThingAction_id = test_for_id_maker(command_taid, "ThingAction")
468 def command_taeffort(str_int):
469 """Set to int(str_int) TA_EFFORT of selected ThingAction."""
470 val = integer_test(str_int, 0, 255)
472 world_db["ThingActions"][command_taid.id]["TA_EFFORT"] = val
476 def command_taname(name):
477 """Set to name TA_NAME of selected ThingAction.
479 The name must match a valid thing action function. If after the name
480 setting no ThingAction with name "wait" remains, call set_world_inactive().
482 if name == "wait" or name == "move" or name == "use" or name == "drop" \
483 or name == "pick_up":
484 world_db["ThingActions"][command_taid.id]["TA_NAME"] = name
485 if 1 == world_db["WORLD_ACTIVE"]:
487 for id in world_db["ThingActions"]:
488 if "wait" == world_db["ThingActions"][id]["TA_NAME"]:
494 print("Ignoring: Invalid action name.")
495 # In contrast to the original,naming won't map a function to a ThingAction.
498 """Commands database.
500 Map command start tokens to ([0]) number of expected command arguments, ([1])
501 the command's meta-ness (i.e. is it to be written to the record file, is it to
502 be ignored in replay mode if read from server input file), and ([2]) a function
506 "QUIT": (0, True, command_quit),
507 "PING": (0, True, command_ping),
508 "MAKE_WORLD": (1, False, command_makeworld),
509 "SEED_MAP": (1, False, command_seedmap),
510 "SEED_RANDOMNESS": (1, False, worlddb_value_setter("SEED_RANDOMNESS",
512 "TURN": (1, False, worlddb_value_setter("TURN", 0, 65535)),
513 "PLAYER_TYPE": (1, False, worlddb_value_setter("PLAYER_TYPE", 0, 255)),
514 "MAP_LENGTH": (1, False, command_maplength),
515 "WORLD_ACTIVE": (1, False, command_worldactive),
516 "TA_ID": (1, False, command_taid),
517 "TA_EFFORT": (1, False, command_taeffort),
518 "TA_NAME": (1, False, command_taname),
519 "TT_ID": (1, False, command_ttid),
520 "TT_NAME": (1, False, command_ttname),
521 "TT_SYMBOL": (1, False, command_ttsymbol),
522 "TT_CORPSE_ID": (1, False, command_ttcorpseid),
523 "TT_CONSUMABLE": (1, False, ThingType_value_setter("TT_CONSUMABLE",
525 "TT_START_NUMBER": (1, False, ThingType_value_setter("TT_START_NUMBER",
527 "TT_PROLIFERATE": (1, False, ThingType_value_setter("TT_PROLIFERATE",
532 """World state database. With sane default values."""
536 "SEED_RANDOMNESS": 0,
546 """File IO database."""
549 "path_record": "record",
550 "path_worldconf": "confserver/world",
551 "path_server": "server/",
552 "path_in": "server/in",
553 "path_out": "server/out",
554 "path_worldstate": "server/worldstate",
555 "tmp_suffix": "_tmp",
556 "kicked_by_rival": False
561 opts = parse_command_line_arguments()
563 # print("DUMMY: Run game.")
564 if None != opts.replay:
568 except SystemExit as exit:
569 print("ABORTING: " + exit.args[0])
571 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
575 # print("DUMMY: (Clean up C heap.)")