3 # This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 # or any later version. For details on its copyright, license, and warranties,
5 # see the file NOTICE in the root directory of the PlomRogue source package.
9 """Replay game from record file.
11 Use opts.replay as breakpoint turn to which to replay automatically before
12 switching to manual input by non-meta commands in server input file
13 triggering further reads of record file. Ensure opts.replay is at least 1.
14 Run try_worldstate_update() before each interactive obey()/read_command().
18 print("Replay mode. Auto-replaying up to turn " + str(opts.replay) +
19 " (if so late a turn is to be found).")
20 if not os.access(io_db["path_record"], os.F_OK):
21 raise SystemExit("No record file found to replay.")
22 io_db["file_record"] = open(io_db["path_record"], "r")
23 io_db["file_record"].prefix = "record file line "
24 io_db["file_record"].line_n = 1
25 while world_db["TURN"] < opts.replay:
26 line = io_db["file_record"].readline()
29 obey(line.rstrip(), io_db["file_record"].prefix
30 + str(io_db["file_record"].line_n))
31 io_db["file_record"].line_n = io_db["file_record"].line_n + 1
33 try_worldstate_update()
34 obey(read_command(), "in file", replay=True)
38 """Play game by server input file commands. Before, load save file found.
40 If no save file is found, a new world is generated from the commands in the
41 world config plus a 'MAKE WORLD [current Unix timestamp]'. Record this
42 command and all that follow via the server input file. Run
43 try_worldstate_update() before each interactive obey()/read_command().
46 from server.io import obey_lines_in_file
47 if os.access(io_db["path_save"], os.F_OK):
48 obey_lines_in_file(io_db["path_save"], "save")
50 if not os.access(opts.worldconf, os.F_OK):
51 msg = "No world config file from which to start a new world."
53 obey_lines_in_file(opts.worldconf, "world config ", do_record=True)
54 obey("MAKE_WORLD " + str(int(time.time())), "in file", do_record=True)
56 try_worldstate_update()
57 obey(read_command(), "in file", do_record=True)
60 from server.io import cleanup_server_io
62 from server.utils import opts
63 from server.config.io import io_db
65 io_db["path_save"] = opts.savefile
66 io_db["path_record"] = "record_" + opts.savefile
67 from server.io import setup_server_io
70 io_db["verbose"] = True
72 from server.config.world_data import world_db
73 from server.io import read_command, try_worldstate_update, obey
74 if None != opts.replay:
78 except SystemExit as exit:
79 print("ABORTING: " + str(exit.args[0]))
81 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")