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, \
14 from client.query_mapcell import query_mapcell
23 def read_worldstate():
25 if not os.access(io["path_worldstate"], os.F_OK):
26 msg = "No world state file found at " + io["path_worldstate"] + "."
29 worldstate_file = open(io["path_worldstate"], "r")
30 turn_string = worldstate_file.readline()
31 if int(turn_string) != world_data["turn"]:
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
39 # TODO: Hardcore order of necessary fields, ensure dependency order.
41 world_data["turn"] = int(turn_string)
42 for entry in io["worldstate_read_order"]:
45 world_data[entry[0]] = int(worldstate_file.readline())
47 world_data[entry[0]][entry[2]] = \
48 int(worldstate_file.readline())
49 elif entry[1] == "lines":
50 world_data[entry[0]] = []
52 line = worldstate_file.readline().replace("\n", "")
55 world_data[entry[0]] += [line]
56 elif entry[1] == "map":
57 world_data[entry[0]] = ""
58 for i in range(world_data["map_size"]):
59 line = worldstate_file.readline().replace("\n", "")
60 world_data[entry[0]] += line
61 if not world_data["look_mode"]:
62 world_data["map_center"] = world_data["avatar_position"][:]
63 worldstate_file.close()
64 read_worldstate.last_checked_mtime = -1
67 def read_message_queue():
69 while (len(message_queue["messages"]) > 1
70 or (len(message_queue["messages"]) == 1
71 and not message_queue["open_end"])):
72 message = message_queue["messages"].pop(0)
73 if message == "THINGS_HERE START":
74 read_message_queue.parse_thingshere = True
75 world_data["look"] = []
76 elif message == "THINGS_HERE END":
77 read_message_queue.parse_thingshere = False
78 if world_data["look"] == []:
79 world_data["look"] = ["(none known)"]
81 elif read_message_queue.parse_thingshere:
82 world_data["look"] += [message]
83 elif message[0:4] == "LOG ":
84 world_data["log"] += [message[4:]]
86 elif message == "WORLD_UPDATED":
88 read_message_queue.parse_thingshere = False
91 def cursed_main(stdscr):
96 if len(new_data_from_server) > 0:
97 ping_test.sent = False
98 elif ping_test.wait_start + half_wait_time < time.time():
99 if not ping_test.sent:
100 io["file_out"].write("PING\n")
101 io["file_out"].flush()
102 ping_test.sent = True
103 ping_test.wait_start = time.time()
105 raise SystemExit("Server not answering anymore.")
106 ping_test.wait_start = 0
108 def read_into_message_queue():
109 if new_data_from_server == "":
112 if new_data_from_server[-1] is not "\n":
114 new_messages = new_data_from_server.splitlines()
115 if message_queue["open_end"]:
116 message_queue["messages"][-1] += new_messages[0]
118 message_queue["messages"] += new_messages
120 message_queue["open_end"] = True
123 curses.curs_set(False)
124 signal.signal(signal.SIGWINCH,
125 lambda ignore_1, ignore_2: set_windows())
129 stdscr.timeout(int(delay))
135 redraw_windows = False
136 char = stdscr.getch()
140 if len(commands[char]) == 1 or not world_data["look_mode"]:
144 redraw_windows = True
145 new_data_from_server = io["file_in"].read()
147 read_into_message_queue()
153 if (not os.access(io["path_out"], os.F_OK)):
154 msg = "No server input file found at " + io["path_out"] + "."
155 raise SystemExit(msg)
156 io["file_out"] = open(io["path_out"], "a")
157 io["file_in"] = open(io["path_in"], "r")
158 curses.wrapper(cursed_main)
159 except SystemExit as exit:
160 print("ABORTING: " + exit.args[0])
162 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
166 io["file_out"].close()
168 io["file_in"].close()