Skip to content

Commit 4767dea

Browse files
committed
authentication + location connections
1 parent 652ec8e commit 4767dea

6 files changed

Lines changed: 313 additions & 255 deletions

File tree

worlds/wsr/LocationList.py

Lines changed: 236 additions & 228 deletions
Large diffs are not rendered by default.

worlds/wsr/WSRClient.py

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ class CommandID(IntEnum):
5757
# PC client is disconnecting
5858
DISCONNECT = auto()
5959

60+
# PC client is sending slot information to wii
61+
AUTHENTICATE = auto()
62+
6063
# PC client wants to display a message
6164
PRINT = auto()
6265

@@ -159,7 +162,6 @@ async def disconnect(self):
159162

160163
self.established = False
161164

162-
163165
def handle_response(self, data):
164166
"""
165167
Handle incoming UDP response
@@ -374,6 +376,29 @@ def _cmd_check_locations(self, msg = "dummy") -> None:
374376

375377
Utils.async_start(self.ctx.check_locations())
376378

379+
def _cmd_set_name(self, msg = "") -> None:
380+
"""
381+
Set your slot name to authenticate the AP server with the game
382+
383+
@param self: WSRCommandProcessor
384+
@param msg: slot name
385+
"""
386+
if not isinstance(self.ctx, WSRContext):
387+
logger.info("Please connect to the wii first")
388+
389+
asyncio.create_task(self.ctx.write_slot_name(msg))
390+
self.slot = msg
391+
392+
def _cmd_print_missing_locations(self, msg = "") -> None:
393+
"""
394+
Docstring for _print_missing_locations
395+
396+
:param self: Description
397+
:param msg: Description
398+
"""
399+
for location in self.ctx.missing_locations:
400+
logger.info(f"Missing Location: {location}")
401+
377402
# =============================================================================#
378403
# Game context #
379404
# =============================================================================#
@@ -387,6 +412,7 @@ class WSRContext(CommonContext):
387412

388413
command_processor = WSRCommandProcessor
389414
game: str = "Wii Sports Resort"
415+
items_handling: int = 0b001
390416

391417
def __init__(self, server_address: Optional[str], password: Optional[str]) -> None:
392418
"""
@@ -472,11 +498,13 @@ async def server_auth(self, password_requested: bool = False) -> None:
472498
await super().server_auth(password_requested)
473499

474500
if not self.auth:
475-
if not self.awaiting_rom:
476-
self.awaiting_rom = True
477-
self.log("Waiting to connect to the game for player information.")
478-
else:
479-
await self.send_connect()
501+
if self.awaiting_rom:
502+
return
503+
self.awaiting_rom = True
504+
logger.info("Awaiting Slot Name...")
505+
return
506+
507+
await self.send_connect()
480508

481509

482510
def on_package(self, cmd: str, args: dict[str, Any]) -> None:
@@ -503,7 +531,7 @@ def on_package(self, cmd: str, args: dict[str, Any]) -> None:
503531
pass
504532

505533
elif cmd == "PrintJSON":
506-
self.wii_client.send_print_cmd(args)
534+
Utils.async_start(self.wii_client.send_print_cmd(args))
507535

508536

509537
async def give_items(self) -> None:
@@ -530,27 +558,31 @@ async def check_locations(self) -> None:
530558
logger.info("Please connect to the wii first")
531559
return
532560

533-
response = await self.wii_client.send_packet(bytes(), packet_type_id=CommandID.LOCATION)
561+
try:
562+
await self.wii_client.send_packet(bytes(), packet_type_id=CommandID.LOCATION)
563+
except Exception:
564+
return
534565

535566
p_data = self.wii_client.most_recent_packet_data
536567

537-
if(int.from_bytes((p_data), byteorder="big") == 4294967295):
568+
if(int.from_bytes(p_data, byteorder="big") == 0xFFFF):
538569
return
539570

540-
locations_checked = []
541-
location = None
571+
new_locations_found = []
542572

543573
for i in range(0, len(p_data), 4):
544574
location = int.from_bytes(p_data[i:i+4], byteorder="big")
545-
for l in self.missing_locations:
546-
logger.info(l)
547-
if location in self.missing_locations:
548-
self.locations_checked.add(WSRItem.get_apid(location))
549-
locations_checked.append(location)
550-
551-
if locations_checked:
552-
logger.info(f"Sending new check: {p_data}")
553-
await self.send_msgs([{"cmd": "LocationsChecks", "locations": locations_checked}])
575+
576+
logger.info(f"Location: {location}")
577+
578+
if location in self.missing_locations and location not in self.locations_checked:
579+
self.locations_checked.add(location)
580+
new_locations_found.append(location)
581+
582+
logger.info(f"Found new location: {location}")
583+
584+
if new_locations_found:
585+
await self.send_msgs([{"cmd": "LocationsChecks", "locations": new_locations_found}])
554586

555587

556588

@@ -580,6 +612,19 @@ async def _give_item(self, item_name: str) -> bool:
580612

581613
self.log("failed to send item")
582614
return False
615+
616+
async def write_slot_name(self, slot_name: str):
617+
"""
618+
Send the slot name to the wii
619+
"""
620+
if not self.wii_client:
621+
return
622+
623+
packet = slot_name.encode("utf-16-be")
624+
packet += bytearray(2)
625+
626+
if await self.wii_client.send_packet(packet, packet_type_id=CommandID.AUTHENTICATE):
627+
self.auth = slot_name
583628

584629

585630
async def can_receive_items(self) -> bool:
@@ -608,10 +653,12 @@ async def sync_loop(ctx: WSRContext) -> None:
608653

609654
try:
610655
if ctx.is_hooked():
611-
await ctx.give_items()
612-
await ctx.check_locations()
613656
if ctx.awaiting_rom:
614657
await ctx.server_auth()
658+
659+
await ctx.give_items()
660+
await ctx.check_locations()
661+
615662

616663
await asyncio.sleep(0.1)
617664
else:

worlds/wsr/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from BaseClasses import Item, ItemClassification, Tutorial, Location, MultiWorld
22
from .items import item_table, create_item, create_itempool
33
from .regions import create_region, create_regions
4-
from .locations import location_table, is_location_valid, get_locations_names, get_total_locations
4+
from .locations import is_location_valid, get_locations_names, get_total_locations
55
from .options import WSROptions, StampGoal, ChampionGoal, ProStatusGoal, Traps, StartingItems, SportsUnlockState, \
66
IncludeHardStamps, IncludeLongStamps, ExcludedStamps
77
from .types import CategoryIndex, SportIndex, WSRItemData, WSRItem
8+
from .LocationList import location_table
89
from worlds.Files import APPlayerContainer, AutoPatchRegister
910
from worlds.AutoWorld import World, WebWorld, CollectionState
1011
from worlds.generic.Rules import add_rule

worlds/wsr/items.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def create_itempool(world: "WSRWorld") -> List[Item]:
5959

6060
itempool += create_multiple_items(world, name, item_frequencies.get(name, 1), item_type)
6161

62-
itempool += create_junk_items(world, get_total_locations(world) - len(itempool))
62+
itempool += create_junk_items(world, len(world.multiworld.get_unfilled_locations(world.player)) - len(itempool))
6363
return itempool
6464

6565

@@ -326,7 +326,7 @@ def create_junk_items(world: "WSRWorld", count: int) -> List[Item]:
326326
"100 Skill Points (Air Sports - Skydiving)": 3,
327327
"Extra Max Heart (Swordplay Showdown)": 3,
328328
"Extra Max Heart (Cycling)": 3,
329-
"Random Cosmetic": 37
329+
"Random Cosmetic": 42
330330
}
331331

332332

worlds/wsr/locations.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Dict, TYPE_CHECKING
22
from .types import LocData
3+
from .LocationList import location_table as lt
34

45
if TYPE_CHECKING:
56
from . import WSRWorld
@@ -21,7 +22,7 @@ def is_location_valid(world: "WSRWorld", location: str) -> bool:
2122
return True
2223

2324
def get_locations_names() -> Dict[str, int]:
24-
names = {name: data.id for name, data in location_table.items()}
25+
names = {name: data.id for name, data in lt.items()}
2526
return names
2627

2728

worlds/wsr/regions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from BaseClasses import Region, Entrance, ItemClassification, Location, LocationProgressType
22
from .types import CategoryIndex, SportIndex, WSRLocation, WSRItem
3-
from .locations import location_table, HARD_STAMPS_LOCATIONS, LONG_STAMPS_LOCATIONS
3+
from .locations import HARD_STAMPS_LOCATIONS, LONG_STAMPS_LOCATIONS
4+
from .LocationList import location_table
45
from typing import TYPE_CHECKING, List, Dict, Optional
56
from .options import SportsUnlockState
67

0 commit comments

Comments
 (0)