2020)
2121from NetUtils import ClientStatus , NetworkItem
2222
23+ from .items import wsr_items
24+
2325if TYPE_CHECKING :
2426 import kvui
2527
@@ -62,7 +64,7 @@ async def connect(self):
6264 logger .info (f"port: { self .port } " )
6365 self .transport , self .protocol = await loop .create_datagram_endpoint (
6466 lambda : AsyncUDPProtocol (self ),
65- local_addr = ("10.0.0.116" , 51235 ),
67+ local_addr = ("10.0.0.116" , 51235 ), # try empty string for ip/port
6668 remote_addr = (self .wii_ip , self .port )
6769 )
6870 sock = self .transport .get_extra_info ('socket' )
@@ -184,6 +186,19 @@ def close(self):
184186 self .transport .close ()
185187 self .transport = None
186188
189+
190+ async def write_bytes (self , address , data , timeout = 2 ):
191+ command = struct .pack ('>BII' , 0x02 , address , len (data )) + data
192+ checksum = sum (command ) & 0xFF
193+ command += checksum .to_bytes (1 , 'big' )
194+
195+ response = await self ._send_command_queued (command , timeout )
196+
197+ if len (response ) == 1 :
198+ return True
199+ else :
200+ raise Exception (f"Write failed at address { address } " )
201+
187202class WSRCommandProcessor (ClientCommandProcessor ):
188203 """
189204 Command processor for WSR client commands
@@ -278,6 +293,48 @@ async def server_auth(self, password_requested: bool = False) -> None:
278293 return
279294 await self .send_connect ()
280295
296+ async def give_items (self ) -> None :
297+ """
298+ Gives player all outstanding items they have not yet received
299+
300+ @param self: The WSR client context
301+ """
302+ if await self .can_receive_items ():
303+ for item in self .items_received :
304+ await self ._give_item (item )
305+
306+ await self ._give_item ("Bowling (Standard) - Moving" )
307+
308+ async def _give_item (self , item_name : str ) -> bool :
309+ """
310+ Give an item to the player.
311+
312+ @param self: The WSR client context
313+ @param item_name: The name of the item
314+ @return: whether the process was successful
315+ """
316+ if not await self .can_receive_items ():
317+ return False
318+
319+ item_id = wsr_items [item_name ].item_id
320+
321+
322+ if await self .wii_memory_client .write_bytes (0x80532020 , item_id .to_bytes (1 , byteorder = "big" )):
323+ logger .info ("sent item" )
324+ return True
325+
326+ logger .info ("failed to send item" )
327+ return False
328+
329+
330+
331+ async def can_receive_items (self ) -> bool :
332+ return True
333+
334+
335+
336+
337+
281338async def do_sync_task (ctx : WSRContext ) -> None :
282339 """
283340 Manages the connection to the game
@@ -288,9 +345,8 @@ async def do_sync_task(ctx: WSRContext) -> None:
288345 try :
289346 if ctx .is_hooked ():
290347
291- # await ctx.give_items()
348+ await ctx .give_items ()
292349 #await ctx.check_locations()
293- logger .info (f"awaiting rom: { ctx .awaiting_rom } " )
294350 if ctx .awaiting_rom :
295351 await ctx .server_auth ()
296352 await asyncio .sleep (0.1 )
0 commit comments