Skip to content

Commit aebdd04

Browse files
committed
double packet send + on_package framework
1 parent b367926 commit aebdd04

1 file changed

Lines changed: 35 additions & 2 deletions

File tree

worlds/wsr/WSRClient.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,14 @@ async def write_bytes(self, data, packet_type_id=0, timeout=2):
195195

196196
packet_size = data_len.to_bytes(4, byteorder="big")
197197

198+
response = await self._send_command_queued(packet_size, timeout)
199+
200+
if len(response) != 1:
201+
raise Exception(f"Write failed with data {data}")
202+
198203
packet_type_id = packet_type_id.to_bytes(1, byteorder="big")
199204

200-
command = packet_size + packet_type_id + data
205+
command = packet_type_id + data
201206

202207
logger.info(f"Command: {command} | Data Length: {data_len} | Data: {data.hex()}")
203208

@@ -243,6 +248,7 @@ def __init__(self, server_address: Optional[str], password: Optional[str]) -> No
243248
super().__init__(server_address, password)
244249
self.items_rcvd: list[tuple[NetworkItem, int]] = []
245250
self.sync_task: Optional[asyncio.Task[None]] = None
251+
self.last_rcvd_index = -1
246252
self.awaiting_rom: bool = False
247253
self.wii_memory_client: AsyncWiiMemoryClient = None
248254
self.wii_ip: str = "10.0.0.116"
@@ -302,14 +308,41 @@ async def server_auth(self, password_requested: bool = False) -> None:
302308
return
303309
await self.send_connect()
304310

311+
def on_package(self, cmd: str, args: dict[str, Any]) -> None:
312+
"""
313+
Handle incoming packages from the server.
314+
315+
@param cmd: Command received from server
316+
@param args: Command arguments
317+
"""
318+
if cmd == "Connected":
319+
# this is where death link will be set up
320+
self.slot_data = args.get("slot_data", None)
321+
self.last_rcvd_index = -1
322+
elif cmd == "RoomInfo":
323+
self.seed_name = args["seed_name"]
324+
elif cmd == "Print":
325+
pass
326+
elif cmd == "ReceivedItems":
327+
if args["index"] >= self.last_rcvd_index:
328+
self.last_rcvd_index = args["index"]
329+
for item in args["items"]:
330+
self.items_rcvd.append(item, self.last_rcvd_index)
331+
self.last_rcvd_index += 1
332+
self.items_rcvd.sort(key=lambda v: v[1])
333+
elif cmd == "Retrieved":
334+
pass
335+
elif cmd == "SetReply":
336+
pass
337+
305338
async def give_items(self) -> None:
306339
"""
307340
Gives player all outstanding items they have not yet received
308341
309342
@param self: The WSR client context
310343
"""
311344
if await self.can_receive_items():
312-
for item in self.items_received:
345+
for item in self.items_rcvd:
313346
await self._give_item(item)
314347

315348
await self._give_item("Bowling (Standard) - Moving")

0 commit comments

Comments
 (0)