Skip to content

Commit aef9af6

Browse files
committed
items and WSRClient updates
1 parent f73574b commit aef9af6

4 files changed

Lines changed: 233 additions & 168 deletions

File tree

worlds/wsr/WSRClient.py

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
)
2121
from NetUtils import ClientStatus, NetworkItem
2222

23+
from .items import wsr_items
24+
2325
if 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+
187202
class 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+
281338
async 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)

worlds/wsr/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from .locations import location_table, is_location_valid, get_locations_names, get_total_locations
55
from .options import WSROptions, StampGoal, ChampionGoal, ProStatusGoal, Traps, StartingItems, SportsUnlockState, \
66
IncludeHardStamps, IncludeLongStamps, ExcludedStamps
7-
from .types import CategoryIndex, SportIndex, WSRSport, ItemData
7+
from .types import CategoryIndex, SportIndex, WSRSport, WSRItemData
88
from worlds.AutoWorld import World, WebWorld, CollectionState
99
from worlds.generic.Rules import add_rule
1010
from typing import List, Dict, TextIO

0 commit comments

Comments
 (0)