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
40 world_data["turn"] = int(turn_string)
41 for entry in io["worldstate_read_order"]:
44 world_data[entry[0]] = int(worldstate_file.readline())
46 world_data[entry[0]][entry[2]] = \
47 int(worldstate_file.readline())
48 elif entry[1] == "lines":
49 world_data[entry[0]] = []
51 line = worldstate_file.readline().replace("\n", "")
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
66 def read_message_queue():
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)"]
80 elif read_message_queue.parse_thingshere:
81 world_data["look"] += [message]
82 elif message[0:4] == "LOG ":
83 world_data["log"] += [message[4:]]
85 elif message == "WORLD_UPDATED":
87 read_message_queue.parse_thingshere = False
90 def cursed_main(stdscr):
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()
104 raise SystemExit("Server not answering anymore.")
105 ping_test.wait_start = 0
107 def read_into_message_queue():
108 if new_data_from_server == "":
111 if new_data_from_server[-1] is not "\n":
113 new_messages = new_data_from_server.splitlines()
114 if message_queue["open_end"]:
115 message_queue["messages"][-1] += new_messages[0]
117 message_queue["messages"] += new_messages
119 message_queue["open_end"] = True
122 curses.curs_set(False)
123 signal.signal(signal.SIGWINCH,
124 lambda ignore_1, ignore_2: set_windows())
128 stdscr.timeout(int(delay))
134 redraw_windows = False
135 char = stdscr.getch()
139 if len(commands[char]) == 1 or not world_data["look_mode"]:
143 redraw_windows = True
144 new_data_from_server = io["file_in"].read()
146 read_into_message_queue()
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])
161 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
165 io["file_out"].close()
167 io["file_in"].close()