home · contact · privacy
Client: Make worldstate file data reading order configurable.
[plomrogue] / roguelike-client
1 #!/usr/bin/python3
2
3 import curses
4 import os
5 import signal
6 import time
7
8
9 from client.config.world_data import world_data
10 from client.config.io import io
11 from client.config.commands import commands
12 from client.window_management import redraw_windows, set_windows, draw_screen, \
13                                      stdscr
14 from client.query_mapcell import query_mapcell
15
16
17 message_queue = {
18     "open_end": False,
19     "messages": []
20 }
21
22
23 def read_worldstate():
24     global redraw_windows
25     if not os.access(io["path_worldstate"], os.F_OK):
26         msg = "No world state file found at " + io["path_worldstate"] + "."
27         raise SystemExit(msg)
28     read_anew = False
29     worldstate_file = open(io["path_worldstate"], "r")
30     turn_string = worldstate_file.readline()
31     if int(turn_string) != world_data["turn"]:
32         read_anew = True
33     if not read_anew: # In rare cases, world may change, but not turn number.
34         mtime = os.stat(io["path_worldstate"])
35         if mtime != read_worldstate.last_checked_mtime:
36             read_worldstate.last_checked_mtime = mtime
37             read_anew = True
38     if read_anew:
39         redraw_windows = True
40         world_data["turn"] = int(turn_string)
41         for entry in io["worldstate_read_order"]:
42             if entry[1] == "int":
43                 if 2 == len(entry):
44                     world_data[entry[0]] = int(worldstate_file.readline())
45                 elif 3 == len(entry):
46                     world_data[entry[0]][entry[2]] = \
47                             int(worldstate_file.readline())
48             elif entry[1] == "lines":
49                 world_data[entry[0]] = []
50                 while True:
51                     line = worldstate_file.readline().replace("\n", "")
52                     if line == '%':
53                         break
54                     world_data[entry[0]] += [line]
55             elif entry[1] == "map":
56                 world_data[entry[0]] = ""
57                 for i in range(world_data["map_size"]):
58                     line = worldstate_file.readline().replace("\n", "")
59                     world_data[entry[0]] += line
60         if not world_data["look_mode"]:
61             world_data["map_center"] = world_data["avatar_position"][:]
62     worldstate_file.close()
63 read_worldstate.last_checked_mtime = -1
64
65
66 def read_message_queue():
67     global redraw_windows
68     while (len(message_queue["messages"]) > 1
69         or (len(message_queue["messages"]) == 1
70             and not message_queue["open_end"])):
71         message = message_queue["messages"].pop(0)
72         if message == "THINGS_HERE START":
73             read_message_queue.parse_thingshere = True
74             world_data["look"] = []
75         elif message == "THINGS_HERE END":
76             read_message_queue.parse_thingshere = False
77             if world_data["look"] == []:
78                 world_data["look"] = ["(none known)"]
79             redraw_windows = True
80         elif read_message_queue.parse_thingshere:
81             world_data["look"] += [message]
82         elif message[0:4] == "LOG ":
83             world_data["log"] += [message[4:]]
84             redraw_windows = True
85         elif message == "WORLD_UPDATED":
86             query_mapcell()
87 read_message_queue.parse_thingshere = False
88
89
90 def cursed_main(stdscr):
91     global redraw_windows
92
93     def ping_test():
94         half_wait_time = 5
95         if len(new_data_from_server) > 0:
96             ping_test.sent = False
97         elif ping_test.wait_start + half_wait_time < time.time():
98             if not ping_test.sent:
99                 io["file_out"].write("PING\n")
100                 io["file_out"].flush()
101                 ping_test.sent = True
102                 ping_test.wait_start = time.time()
103             elif ping_test.sent:
104                 raise SystemExit("Server not answering anymore.")
105     ping_test.wait_start = 0
106
107     def read_into_message_queue():
108         if new_data_from_server == "":
109             return
110         new_open_end = False
111         if new_data_from_server[-1] is not "\n":
112             new_open_end = True
113         new_messages = new_data_from_server.splitlines()
114         if message_queue["open_end"]:
115             message_queue["messages"][-1] += new_messages[0]
116             del new_messages[0]
117         message_queue["messages"] += new_messages
118         if new_open_end:
119             message_queue["open_end"] = True
120
121     curses.noecho()
122     curses.curs_set(False)
123     signal.signal(signal.SIGWINCH,
124         lambda ignore_1, ignore_2: set_windows())
125     set_windows()
126     delay = 1
127     while True:
128         stdscr.timeout(int(delay))
129         if delay < 1000:
130             delay = delay * 1.1
131         if redraw_windows:
132             delay = 1
133             draw_screen()
134             redraw_windows = False
135         char = stdscr.getch()
136         if char >= 0:
137             char = chr(char)
138             if char in commands:
139                 if len(commands[char]) == 1 or not world_data["look_mode"]:
140                     commands[char][0]()
141                 else:
142                     commands[char][1]()
143                 redraw_windows = True
144         new_data_from_server = io["file_in"].read()
145         ping_test()
146         read_into_message_queue()
147         read_worldstate()
148         read_message_queue()
149
150
151 try:
152     if (not os.access(io["path_out"], os.F_OK)):
153         msg = "No server input file found at " + io["path_out"] + "."
154         raise SystemExit(msg)
155     io["file_out"] = open(io["path_out"], "a")
156     io["file_in"] = open(io["path_in"], "r")
157     curses.wrapper(cursed_main)
158 except SystemExit as exit:
159     print("ABORTING: " + exit.args[0])
160 except:
161     print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
162     raise
163 finally:
164     if "file_out" in io:
165         io["file_out"].close()
166     if "file_in" in io:
167         io["file_in"].close()