home · contact · privacy
Wrapper script: Give server ten seconds to wind down properly.
[plomrogue] / roguelike-server
1 #!/usr/bin/python3
2
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.
6
7
8 def replay_game():
9     """Replay game from record file.
10
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().
15     """
16     if opts.replay < 1:
17         opts.replay = 1
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()
27         if "" == line:
28             break
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
32     while True:
33         try_worldstate_update()
34         obey(read_command(), "in file", replay=True)
35
36
37 def play_game():
38     """Play game by server input file commands. Before, load save file found.
39
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().
44     """
45     import time
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")
49     else:
50         if not os.access(opts.worldconf, os.F_OK):
51             msg = "No world config file from which to start a new world."
52             raise SystemExit(msg)
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)
55     while True:
56         try_worldstate_update()
57         obey(read_command(), "in file", do_record=True)
58
59
60 from server.io import cleanup_server_io
61 try:
62     from server.utils import opts
63     from server.config.io import io_db
64     if opts.savefile:
65         io_db["path_save"] = opts.savefile
66         io_db["path_record"] = "record_" + opts.savefile
67     from server.io import setup_server_io
68     setup_server_io()
69     if opts.verbose:
70         io_db["verbose"] = True
71     import os
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:
75         replay_game()
76     else:
77         play_game()
78 except SystemExit as exit:
79     if len(exit.args) < 2 and exit.args[0] != 0:
80         print("ABORTING: " + str(exit.args[0]))
81 except:
82     print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
83     raise
84 finally:
85     cleanup_server_io()