8 def set_window_geometries():
10 def set_window_size():
11 win["size"], win["start"] = [0, 0], [0, 0]
12 win["size"][0] = win["config"][0]
13 if (win["config"][0] == 0):
14 win["size"][0] = screen_size[0] - sep_size
15 elif (win["config"][0] < 0):
16 win["size"][0] = screen_size[0] + win["config"][0] - sep_size
17 win["size"][1] = win["config"][1]
18 if (win["config"][1] == 0):
19 win["size"][1] = screen_size[1]
20 elif (win["config"][1] < 0):
21 win["size"][1] = screen_size[1] + win["config"][1]
24 win_i = windows.index(win)
26 # If win is first window, it goes into the top left corner.
27 win["start"][0] = 0 + sep_size
31 # If not, get win's closest predecessor starting a new stack on the
32 # screen top,fit win's top left to that win_top's top right corner.
34 for i in range(win_i - 1, -1, -1):
36 if (win_top["start"][0] == 0 + sep_size):
38 win["start"][1] = win_top["start"][1] + win_top["size"][1] \
41 # If enough space is found below win's predecessor, fit win's top
42 # left corner to that predecessor's bottom left corner.
43 win_prev = windows[win_i - 1]
44 next_free_y = win_prev["start"][0] + win_prev["size"][0] + sep_size
45 if (win["size"][1] <= win_prev["size"][1] and
46 win["size"][0] <= screen_size[0] - next_free_y):
47 win["start"][1] = win_prev["start"][1]
48 win["start"][0] = next_free_y
50 # If that fails, try to fit win's top left corner to the top right
51 # corner of its closest predecessor win_test 1) below win_top (see
52 # above) 2) and with enough space open to its right between its
53 # right edge and the lower edge of a win_high located directly
54 # above win_test to fit win there (without growing further to the
55 # right than win_high does or surpassing the screen's lower edge).
59 while (win_test != win_top):
60 for i in range(win_i - 2, -1, -1):
62 if win_test["start"][0] > win_high["start"][0]:
64 next_free_y = win_high["start"][0] + win_high["size"][0] \
66 first_free_x = win_test["start"][1] + win_test["size"][1] \
68 last_free_x = win_high["start"][1] + win_high["size"][1]
69 if (win["size"][0] <= screen_size[0] - next_free_y and
70 win["size"][1] <= last_free_x - first_free_x):
71 win["start"][1] = first_free_x
72 win["start"][0] = next_free_y
76 global screen_size, stdscr
78 stdscr = curses.initscr()
79 screen_size = stdscr.getmaxyx()
83 cursed_main.redraw = True
88 def draw_window_border_lines():
91 j = win["start"][int(k == 0)] - sep_size
92 if (j >= 0 and j < screen_size[int(k == 0)]):
93 start = win["start"][k]
94 # start = start if start >= 0 else 0
95 end = win["start"][k] + win["size"][k]
96 end = end if end < screen_size[k] else screen_size[k]
98 [stdscr.addch(j, i, '-') for i in range(start, end)]
100 [stdscr.addch(i, j, '|') for i in range(start, end)]
102 def draw_window_border_corners():
104 up = win["start"][0] - sep_size
105 down = win["start"][0] + win["size"][0]
106 left = win["start"][1] - sep_size
107 right = win["start"][1] + win["size"][1]
108 if (up >= 0 and up < screen_size[0]):
109 if (left >= 0 and left < screen_size[1]):
110 stdscr.addch(up, left, '+')
111 if (right >= 0 and right < screen_size[1]):
112 stdscr.addch(up, right, '+')
113 if (down >= 0 and down < screen_size[0]):
114 if (left >= 0 and left < screen_size[1]):
115 stdscr.addch(down, left, '+')
116 if (right >= 0 and right < screen_size[1]):
117 stdscr.addch(down, right, '+')
119 def draw_window_contents():
123 stop[i] = win["size"][i] + offset[i]
124 stop[i] = stop[i] if stop[i] < size[i] else size[i]
125 for y in range(offset[0], stop[0]):
126 for x in range(offset[1], stop[1]):
127 cell = winmap[y * size[1] + x]
128 y_in_screen = win["start"][0] + (y - offset[0])
129 x_in_screen = win["start"][1] + (x - offset[1])
130 if (y_in_screen < screen_size[0]
131 and x_in_screen < screen_size[1]):
132 stdscr.addch(y_in_screen, x_in_screen, cell)
133 def draw_scroll_hints():
134 def draw_scroll_string(n_lines_outside):
135 hint = ' ' + str(n_lines_outside + 1) + ' more ' + unit + ' '
136 if len(hint) <= win["size"][ni]:
137 non_hint_space = win["size"][ni] - len(hint)
138 hint_offset = int(non_hint_space / 2)
139 for j in range(win["size"][ni] - non_hint_space):
140 pos_2 = win["start"][ni] + hint_offset + j
141 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
142 stdscr.addch(y, x, hint[j], curses.A_REVERSE)
143 def draw_scroll_arrows(ar1, ar2):
144 for j in range(win["size"][ni]):
145 pos_2 = win["start"][ni] + j
146 x, y = (pos_2, pos_1) if ni else (pos_1, pos_2)
147 stdscr.addch(y, x, ar1 if ni else ar2, curses.A_REVERSE)
150 unit = 'rows' if ni else 'columns'
152 pos_1 = win["start"][i]
153 draw_scroll_arrows('^', '<')
154 draw_scroll_string(offset[i])
155 if (size[i] > offset[i] + win["size"][i]):
156 pos_1 = win["start"][i] + win["size"][i] - 1
157 draw_scroll_arrows('v', '>')
158 draw_scroll_string(size[i] - offset[i] - win["size"][i])
160 offset, size, winmap = win["func"]()
165 draw_window_border_lines()
166 draw_window_border_corners()
167 draw_window_contents()
171 def read_worldstate():
172 if not os.access(io["path_worldstate"], os.F_OK):
173 msg = "No world state file found at " + io["path_worldstate"] + "."
174 raise SystemExit(msg)
176 worldstate_file = open(io["path_worldstate"], "r")
177 turn_string = worldstate_file.readline()
178 if int(turn_string) != world_data["turn"]:
180 if not read_anew: # In rare cases, world may change, but not turn number.
181 mtime = os.stat(io["path_worldstate"])
182 if mtime != read_worldstate.last_checked_mtime:
183 read_worldstate.last_checked_mtime = mtime
186 cursed_main.redraw = True
187 world_data["turn"] = int(turn_string)
188 world_data["lifepoints"] = int(worldstate_file.readline())
189 world_data["satiation"] = int(worldstate_file.readline())
190 worldstate_file.close()
191 read_worldstate.last_checked_mtime = -1
194 def read_message_queue():
195 while (len(message_queue["messages"]) > 1
196 or (len(message_queue["messages"]) == 0
197 and not message_queue["open_end"])):
198 message = message_queue["messages"].pop(0)
199 if message[0:4] == "LOG ":
200 world_data["log"] += [message[4:]]
201 cursed_main.redraw = True
204 def cursed_main(stdscr):
208 if len(new_data_from_server) > 0:
209 ping_test.sent = False
210 elif ping_test.wait_start + half_wait_time < time.time():
211 if not ping_test.sent:
212 io["file_out"].write("PING\n")
213 io["file_out"].flush()
214 ping_test.sent = True
215 ping_test.wait_start = time.time()
217 raise SystemExit("Server not answering anymore.")
218 ping_test.wait_start = 0
220 def read_into_message_queue():
221 if new_data_from_server == "":
224 if new_data_from_server[-1] is not "\n":
226 new_messages = new_data_from_server.splitlines()
227 if message_queue["open_end"]:
228 message_queue["messages"][-1] += new_messages[0]
230 message_queue["messages"] += new_messages
232 message_queue["open_end"] = True
235 curses.curs_set(False)
236 # stdscr.keypad(True)
237 signal.signal(signal.SIGWINCH,
238 lambda ignore_1, ignore_2: set_window_geometries())
239 set_window_geometries()
242 stdscr.timeout(delay)
243 delay = delay * 2 if delay < 1000 else delay
244 if cursed_main.redraw:
247 cursed_main.redraw = False
248 char = stdscr.getch()
249 if char >= 0 and chr(char) in commands:
250 commands[chr(char)]()
251 cursed_main.redraw = True
252 new_data_from_server = io["file_in"].read()
254 read_into_message_queue()
260 winmap = ['.', 'o', '.', 'o', 'O', 'o', '.', 'o', '.', 'x', 'y', 'x']
263 return offset, size, winmap
267 winmap = "T: " + str(world_data["turn"]) \
268 + " H: " + str(world_data["lifepoints"]) \
269 + " S: " + str(world_data["satiation"])
270 size = [1, len(winmap)]
272 return offset, size, winmap
276 win_size = next(win["size"] for win in windows if win["func"] == win_log)
280 for line in world_data["log"]:
281 number_of_lines += math.ceil(len(line) / win_size[1])
282 padding_size = win_size[1] - (len(line) % win_size[1])
283 winmap += line + (padding_size * " ")
284 if number_of_lines < win_size[0]:
285 winmap = (" " * win_size[1] * (win_size[0] - number_of_lines)) + winmap
286 number_of_lines = win_size[0]
287 elif number_of_lines > win_size[0]:
288 offset[0] = number_of_lines - win_size[0]
289 winmap_size = [number_of_lines, win_size[1]]
290 return offset, winmap_size, winmap
294 io["file_out"].write("QUIT\n")
295 io["file_out"].flush()
296 raise SystemExit("Received QUIT command, forwarded to server, leaving.")
300 {"config": [1, 33], "func": win_info},
301 {"config": [-7, 33], "func": win_log},
302 {"config": [4, 16], "func": win_foo},
303 {"config": [4, 16], "func": win_foo},
304 {"config": [0, -34], "func": win_foo}
307 "path_out": "server/in",
308 "path_in": "server/out",
309 "path_worldstate": "server/worldstate"
324 sep_size = 1 # Width of inter-window borders and title bars.
330 if (not os.access(io["path_out"], os.F_OK)):
331 msg = "No server input file found at " + io["path_out"] + "."
332 raise SystemExit(msg)
333 io["file_out"] = open(io["path_out"], "a")
334 io["file_in"] = open(io["path_in"], "r")
335 curses.wrapper(cursed_main)
336 except SystemExit as exit:
337 print("ABORTING: " + exit.args[0])
339 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
343 io["file_out"].close()
345 io["file_in"].close()