#!/usr/bin/python3 # 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. def replay_game(): """Replay game from record file. Use opts.replay as breakpoint turn to which to replay automatically before switching to manual input by non-meta commands in server input file triggering further reads of record file. Ensure opts.replay is at least 1. Run try_worldstate_update() before each interactive obey()/read_command(). """ if opts.replay < 1: opts.replay = 1 print("Replay mode. Auto-replaying up to turn " + str(opts.replay) + " (if so late a turn is to be found).") if not os.access(io_db["path_record"], os.F_OK): raise SystemExit("No record file found to replay.") io_db["file_record"] = open(io_db["path_record"], "r") io_db["file_record"].prefix = "record file line " io_db["file_record"].line_n = 1 while world_db["TURN"] < opts.replay: line = io_db["file_record"].readline() if "" == line: break obey(line.rstrip(), io_db["file_record"].prefix + str(io_db["file_record"].line_n)) io_db["file_record"].line_n = io_db["file_record"].line_n + 1 while True: try_worldstate_update() obey(read_command(), "in file", replay=True) def play_game(): """Play game by server input file commands. Before, load save file found. If no save file is found, a new world is generated from the commands in the world config plus a 'MAKE WORLD [current Unix timestamp]'. Record this command and all that follow via the server input file. Run try_worldstate_update() before each interactive obey()/read_command(). """ import time from server.io import obey_lines_in_file if os.access(io_db["path_save"], os.F_OK): obey_lines_in_file(io_db["path_save"], "save") else: if not os.access(opts.worldconf, os.F_OK): msg = "No world config file from which to start a new world." raise SystemExit(msg) obey_lines_in_file(opts.worldconf, "world config ", do_record=True) obey("MAKE_WORLD " + str(int(time.time())), "in file", do_record=True) while True: try_worldstate_update() obey(read_command(), "in file", do_record=True) from server.io import cleanup_server_io try: from server.utils import opts from server.config.io import io_db if opts.savefile: io_db["path_save"] = opts.savefile io_db["path_record"] = "record_" + opts.savefile from server.io import setup_server_io setup_server_io() if opts.verbose: io_db["verbose"] = True import os from server.config.world_data import world_db from server.io import read_command, try_worldstate_update, obey if None != opts.replay: replay_game() else: play_game() except SystemExit as exit: if len(exit.args) < 2 and exit.args[0] != 0: print("ABORTING: " + str(exit.args[0])) except: print("SOMETHING WENT WRONG IN UNEXPECTED WAYS") raise finally: cleanup_server_io()