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: Hardcode order of necessary fields, ensure order dependencies.
45 old_inventory_size = len(world_data["inventory"])
46 world_data["turn"] = int(turn_string)
47 for entry in io["worldstate_read_order"]:
50 world_data[entry[0]] = int(worldstate_file.readline())
52 world_data[entry[0]][entry[2]] = \
53 int(worldstate_file.readline())
54 elif entry[1] == "lines":
55 world_data[entry[0]] = []
57 line = worldstate_file.readline().replace("\n", "")
60 world_data[entry[0]] += [line]
61 elif entry[1] == "map":
62 world_data[entry[0]] = ""
63 for i in range(world_data["map_size"]):
64 line = worldstate_file.readline().replace("\n", "")
65 world_data[entry[0]] += line
66 if not world_data["look_mode"]:
67 world_data["map_center"] = world_data["avatar_position"][:]
68 if world_data["inventory_selection"] > 0 and \
69 len(world_data["inventory"]) < old_inventory_size:
70 world_data["inventory_selection"] -= 1
71 worldstate_file.close()
72 read_worldstate.last_checked_mtime = -1
75 def read_message_queue():
77 while (len(message_queue["messages"]) > 1
78 or (len(message_queue["messages"]) == 1
79 and not message_queue["open_end"])):
80 message = message_queue["messages"].pop(0)
81 if message == "THINGS_HERE START":
82 read_message_queue.parse_thingshere = True
83 world_data["look"] = []
84 elif message == "THINGS_HERE END":
85 read_message_queue.parse_thingshere = False
86 if world_data["look"] == []:
87 world_data["look"] = ["(none known)"]
89 elif read_message_queue.parse_thingshere:
90 world_data["look"] += [message]
91 elif message[0:4] == "LOG ":
92 world_data["log"] += [message[4:]]
94 elif message == "WORLD_UPDATED":
96 elif message[:6] == "PLUGIN":
97 str_plugin = message[7:]
98 if (str_plugin.replace("_", "").isalnum()
99 and os.access("plugins/client/" + str_plugin + ".py",
101 exec(open("plugins/client/" + str_plugin + ".py").read())
103 raise SystemExit("Invalid plugin load path in message: " + message)
104 read_message_queue.parse_thingshere = False
107 def cursed_main(stdscr):
108 global redraw_windows
112 if len(new_data_from_server) > 0:
113 ping_test.sent = False
114 elif ping_test.wait_start + half_wait_time < time.time():
115 if not ping_test.sent:
116 io["file_out"].write("PING\n")
117 io["file_out"].flush()
118 ping_test.sent = True
119 ping_test.wait_start = time.time()
121 raise SystemExit("Server not answering anymore.")
122 ping_test.wait_start = 0
124 def read_into_message_queue():
125 if new_data_from_server == "":
128 if new_data_from_server[-1] is not "\n":
130 new_messages = new_data_from_server.splitlines()
131 if message_queue["open_end"]:
132 message_queue["messages"][-1] += new_messages[0]
134 message_queue["messages"] += new_messages
136 message_queue["open_end"] = True
138 def set_and_redraw_windows(*ignore):
143 curses.curs_set(False)
144 signal.signal(signal.SIGWINCH, set_and_redraw_windows)
148 stdscr.timeout(int(delay))
154 redraw_windows = False
155 char = stdscr.getch()
159 if len(commands[char]) == 1 or not world_data["look_mode"]:
163 redraw_windows = True
164 new_data_from_server = io["file_in"].read()
166 read_into_message_queue()
172 if (not os.access(io["path_out"], os.F_OK)):
173 msg = "No server input file found at " + io["path_out"] + "."
174 raise SystemExit(msg)
175 io["file_out"] = open(io["path_out"], "a")
176 io["file_in"] = open(io["path_in"], "r")
177 curses.wrapper(cursed_main)
178 except SystemExit as exit:
179 print("ABORTING: " + exit.args[0])
181 print("SOMETHING WENT WRONG IN UNEXPECTED WAYS")
185 io["file_out"].close()
187 io["file_in"].close()