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.
13 from client.config.world_data import world_data
14 from client.config.io import io
15 from client.config.commands import commands
16 from client.window_management import redraw_windows, set_windows, draw_screen, \
18 from client.query_mapcell import query_mapcell
27 def read_worldstate():
29 if not os.access(io["path_worldstate"], os.F_OK):
30 msg = "No world state file found at " + io["path_worldstate"] + "."
33 worldstate_file = open(io["path_worldstate"], "r")
34 turn_string = worldstate_file.readline()
35 if int(turn_string) != world_data["turn"]:
37 if not read_anew: # In rare cases, world may change, but not turn number.
38 mtime = os.stat(io["path_worldstate"])
39 if mtime != read_worldstate.last_checked_mtime:
40 read_worldstate.last_checked_mtime = mtime
43 # TODO: Hardcore order of necessary fields, ensure dependency order.
45 world_data["turn"] = int(turn_string)
46 for entry in io["worldstate_read_order"]:
49 world_data[entry[0]] = int(worldstate_file.readline())
51 world_data[entry[0]][entry[2]] = \
52 int(worldstate_file.readline())
53 elif entry[1] == "lines":
54 world_data[entry[0]] = []
56 line = worldstate_file.readline().replace("\n", "")
59 world_data[entry[0]] += [line]
60 elif entry[1] == "map":
61 world_data[entry[0]] = ""
62 for i in range(world_data["map_size"]):
63 line = worldstate_file.readline().replace("\n", "")
64 world_data[entry[0]] += line
65 if not world_data["look_mode"]:
66 world_data["map_center"] = world_data["avatar_position"][:]
67 worldstate_file.close()
68 read_worldstate.last_checked_mtime = -1
71 def read_message_queue():
73 while (len(message_queue["messages"]) > 1
74 or (len(message_queue["messages"]) == 1
75 and not message_queue["open_end"])):
76 message = message_queue["messages"].pop(0)
77 if message == "THINGS_HERE START":
78 read_message_queue.parse_thingshere = True
79 world_data["look"] = []
80 elif message == "THINGS_HERE END":
81 read_message_queue.parse_thingshere = False
82 if world_data["look"] == []:
83 world_data["look"] = ["(none known)"]
85 elif read_message_queue.parse_thingshere:
86 world_data["look"] += [message]
87 elif message[0:4] == "LOG ":
88 world_data["log"] += [message[4:]]
90 elif message == "WORLD_UPDATED":
92 read_message_queue.parse_thingshere = False
95 def cursed_main(stdscr):
100 if len(new_data_from_server) > 0:
101 ping_test.sent = False
102 elif ping_test.wait_start + half_wait_time < time.time():
103 if not ping_test.sent:
104 io["file_out"].write("PING\n")
105 io["file_out"].flush()
106 ping_test.sent = True
107 ping_test.wait_start = time.time()
109 raise SystemExit("Server not answering anymore.")
110 ping_test.wait_start = 0
112 def read_into_message_queue():
113 if new_data_from_server == "":
116 if new_data_from_server[-1] is not "\n":
118 new_messages = new_data_from_server.splitlines()
119 if message_queue["open_end"]:
120 message_queue["messages"][-1] += new_messages[0]
122 message_queue["messages"] += new_messages
124 message_queue["open_end"] = True
127 curses.curs_set(False)
128 signal.signal(signal.SIGWINCH,
129 lambda ignore_1, ignore_2: set_windows())
133 stdscr.timeout(int(delay))
139 redraw_windows = False
140 char = stdscr.getch()
144 if len(commands[char]) == 1 or not world_data["look_mode"]:
148 redraw_windows = True
149 new_data_from_server = io["file_in"].read()
151 read_into_message_queue()
157 if (not os.access(io["path_out"], os.F_OK)):
158 msg = "No server input file found at " + io["path_out"] + "."
159 raise SystemExit(msg)
160 io["file_out"] = open(io["path_out"], "a")
161 io["file_in"] = open(io["path_in"], "r")
162 curses.wrapper(cursed_main)
163 except SystemExit as exit:
164 print("ABORTING: " + exit.args[0])
166 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
170 io["file_out"].close()
172 io["file_in"].close()