diff options
Diffstat (limited to 'apworld/context.py')
-rw-r--r-- | apworld/context.py | 29 |
1 files changed, 25 insertions, 4 deletions
diff --git a/apworld/context.py b/apworld/context.py index 35ba483..ffe2acd 100644 --- a/apworld/context.py +++ b/apworld/context.py | |||
@@ -10,7 +10,7 @@ import Utils | |||
10 | import settings | 10 | import settings |
11 | from BaseClasses import ItemClassification | 11 | from BaseClasses import ItemClassification |
12 | from CommonClient import CommonContext, server_loop, gui_enabled, logger, get_base_parser, handle_url_arg | 12 | from CommonClient import CommonContext, server_loop, gui_enabled, logger, get_base_parser, handle_url_arg |
13 | from NetUtils import Endpoint, decode, encode | 13 | from NetUtils import Endpoint, decode, encode, ClientStatus |
14 | from Utils import async_start | 14 | from Utils import async_start |
15 | from . import Lingo2World | 15 | from . import Lingo2World |
16 | from .tracker import Tracker | 16 | from .tracker import Tracker |
@@ -36,6 +36,7 @@ class Lingo2Manager: | |||
36 | 36 | ||
37 | keyboard: dict[str, int] | 37 | keyboard: dict[str, int] |
38 | worldports: set[int] | 38 | worldports: set[int] |
39 | goaled: bool | ||
39 | 40 | ||
40 | def __init__(self, game_ctx: "Lingo2GameContext", client_ctx: "Lingo2ClientContext"): | 41 | def __init__(self, game_ctx: "Lingo2GameContext", client_ctx: "Lingo2ClientContext"): |
41 | self.game_ctx = game_ctx | 42 | self.game_ctx = game_ctx |
@@ -53,6 +54,7 @@ class Lingo2Manager: | |||
53 | self.keyboard[k] = 0 | 54 | self.keyboard[k] = 0 |
54 | 55 | ||
55 | self.worldports = set() | 56 | self.worldports = set() |
57 | self.goaled = False | ||
56 | 58 | ||
57 | def update_keyboard(self, new_keyboard: dict[str, int]) -> dict[str, int]: | 59 | def update_keyboard(self, new_keyboard: dict[str, int]) -> dict[str, int]: |
58 | ret: dict[str, int] = {} | 60 | ret: dict[str, int] = {} |
@@ -177,6 +179,9 @@ class Lingo2GameContext: | |||
177 | if len(self.manager.tracker.accessible_worldports) > 0: | 179 | if len(self.manager.tracker.accessible_worldports) > 0: |
178 | msg["worldports"] = list(self.manager.tracker.accessible_worldports) | 180 | msg["worldports"] = list(self.manager.tracker.accessible_worldports) |
179 | 181 | ||
182 | if self.manager.tracker.goal_accessible and not self.manager.goaled: | ||
183 | msg["goal"] = True | ||
184 | |||
180 | async_start(self.send_msgs([msg]), name="accessible locations") | 185 | async_start(self.send_msgs([msg]), name="accessible locations") |
181 | 186 | ||
182 | def send_update_locations(self, locations): | 187 | def send_update_locations(self, locations): |
@@ -226,6 +231,7 @@ class Lingo2ClientContext(CommonContext): | |||
226 | items_handling = 0b111 | 231 | items_handling = 0b111 |
227 | 232 | ||
228 | slot_data: dict[str, Any] | None | 233 | slot_data: dict[str, Any] | None |
234 | victory_data_storage_key: str | ||
229 | 235 | ||
230 | def __init__(self, server_address: str | None = None, password: str | None = None): | 236 | def __init__(self, server_address: str | None = None, password: str | None = None): |
231 | super().__init__(server_address, password) | 237 | super().__init__(server_address, password) |
@@ -253,7 +259,10 @@ class Lingo2ClientContext(CommonContext): | |||
253 | self.manager.tracker.set_checked_locations(self.checked_locations) | 259 | self.manager.tracker.set_checked_locations(self.checked_locations) |
254 | self.manager.game_ctx.send_accessible_locations() | 260 | self.manager.game_ctx.send_accessible_locations() |
255 | 261 | ||
256 | self.set_notify(self.get_datastorage_key("keyboard1"), self.get_datastorage_key("keyboard2")) | 262 | self.victory_data_storage_key = f"_read_client_status_{self.team}_{self.slot}" |
263 | |||
264 | self.set_notify(self.get_datastorage_key("keyboard1"), self.get_datastorage_key("keyboard2"), | ||
265 | self.victory_data_storage_key) | ||
257 | msg_batch = [{ | 266 | msg_batch = [{ |
258 | "cmd": "Set", | 267 | "cmd": "Set", |
259 | "key": self.get_datastorage_key("keyboard1"), | 268 | "key": self.get_datastorage_key("keyboard1"), |
@@ -280,8 +289,9 @@ class Lingo2ClientContext(CommonContext): | |||
280 | 289 | ||
281 | async_start(self.send_msgs(msg_batch), name="default keys") | 290 | async_start(self.send_msgs(msg_batch), name="default keys") |
282 | elif cmd == "RoomUpdate": | 291 | elif cmd == "RoomUpdate": |
283 | self.manager.tracker.set_checked_locations(self.checked_locations) | 292 | if "checked_locations" in args: |
284 | self.manager.game_ctx.send_update_locations(args["checked_locations"]) | 293 | self.manager.tracker.set_checked_locations(self.checked_locations) |
294 | self.manager.game_ctx.send_update_locations(args["checked_locations"]) | ||
285 | elif cmd == "ReceivedItems": | 295 | elif cmd == "ReceivedItems": |
286 | self.manager.tracker.set_collected_items(self.items_received) | 296 | self.manager.tracker.set_collected_items(self.items_received) |
287 | 297 | ||
@@ -359,6 +369,10 @@ class Lingo2ClientContext(CommonContext): | |||
359 | }) | 369 | }) |
360 | 370 | ||
361 | self.manager.game_ctx.send_location_info(locations) | 371 | self.manager.game_ctx.send_location_info(locations) |
372 | elif cmd == "Retrieved": | ||
373 | for k, v in args["keys"].items(): | ||
374 | if k == self.victory_data_storage_key: | ||
375 | self.handle_status_update(v) | ||
362 | elif cmd == "SetReply": | 376 | elif cmd == "SetReply": |
363 | if args["key"] == self.get_datastorage_key("keyboard1"): | 377 | if args["key"] == self.get_datastorage_key("keyboard1"): |
364 | self.handle_keyboard_update(1, args) | 378 | self.handle_keyboard_update(1, args) |
@@ -368,6 +382,8 @@ class Lingo2ClientContext(CommonContext): | |||
368 | updates = self.manager.update_worldports(set(args["value"])) | 382 | updates = self.manager.update_worldports(set(args["value"])) |
369 | if len(updates) > 0: | 383 | if len(updates) > 0: |
370 | self.manager.game_ctx.send_update_worldports(updates) | 384 | self.manager.game_ctx.send_update_worldports(updates) |
385 | elif args["key"] == self.victory_data_storage_key: | ||
386 | self.handle_status_update(args["value"]) | ||
371 | 387 | ||
372 | def get_datastorage_key(self, name: str): | 388 | def get_datastorage_key(self, name: str): |
373 | return f"Lingo2_{self.slot}_{name}" | 389 | return f"Lingo2_{self.slot}_{name}" |
@@ -444,6 +460,11 @@ class Lingo2ClientContext(CommonContext): | |||
444 | }] | 460 | }] |
445 | }]) | 461 | }]) |
446 | 462 | ||
463 | def handle_status_update(self, value: int): | ||
464 | self.manager.goaled = (value == ClientStatus.CLIENT_GOAL) | ||
465 | self.manager.tracker.refresh_state() | ||
466 | self.manager.game_ctx.send_accessible_locations() | ||
467 | |||
447 | 468 | ||
448 | async def pipe_loop(manager: Lingo2Manager): | 469 | async def pipe_loop(manager: Lingo2Manager): |
449 | while not manager.client_ctx.exit_event.is_set(): | 470 | while not manager.client_ctx.exit_event.is_set(): |