diff --git a/worlds/kh2/Client.py b/worlds/kh2/Client.py index 96b406c7..fcd27c4c 100644 --- a/worlds/kh2/Client.py +++ b/worlds/kh2/Client.py @@ -34,7 +34,7 @@ class KH2Context(CommonContext): self.growthlevel = None self.kh2connected = False self.kh2_finished_game = False - self.serverconneced = False + self.serverconnected = False self.item_name_to_data = {name: data for name, data, in item_dictionary_table.items()} self.location_name_to_data = {name: data for name, data, in all_locations.items()} self.kh2_data_package = {} @@ -47,6 +47,8 @@ class KH2Context(CommonContext): self.location_name_to_worlddata = {name: data for name, data, in all_world_locations.items()} self.sending = [] + self.slot_name = None + self.disconnect_from_server = False # list used to keep track of locations+items player has. Used for disoneccting self.kh2_seed_save_cache = { "itemIndex": -1, @@ -185,11 +187,20 @@ class KH2Context(CommonContext): if password_requested and not self.password: await super(KH2Context, self).server_auth(password_requested) await self.get_username() - await self.send_connect() + # if slot name != first time login or previous name + # and seed name is none or saved seed name + if not self.slot_name and not self.kh2seedname: + await self.send_connect() + elif self.slot_name == self.auth and self.kh2seedname: + await self.send_connect() + else: + logger.info(f"You are trying to connect with data still cached in the client. Close client or connect to the correct slot: {self.slot_name}") + self.serverconnected = False + self.disconnect_from_server = True async def connection_closed(self): self.kh2connected = False - self.serverconneced = False + self.serverconnected = False if self.kh2seedname is not None and self.auth is not None: with open(self.kh2_seed_save_path_join, 'w') as f: f.write(json.dumps(self.kh2_seed_save, indent=4)) @@ -197,7 +208,8 @@ class KH2Context(CommonContext): async def disconnect(self, allow_autoreconnect: bool = False): self.kh2connected = False - self.serverconneced = False + self.serverconnected = False + self.locations_checked = [] if self.kh2seedname not in {None} and self.auth not in {None}: with open(self.kh2_seed_save_path_join, 'w') as f: f.write(json.dumps(self.kh2_seed_save, indent=4)) @@ -239,7 +251,15 @@ class KH2Context(CommonContext): def on_package(self, cmd: str, args: dict): if cmd == "RoomInfo": - self.kh2seedname = args['seed_name'] + if not self.kh2seedname: + self.kh2seedname = args['seed_name'] + elif self.kh2seedname != args['seed_name']: + self.disconnect_from_server = True + self.serverconnected = False + self.kh2connected = False + logger.info("Connection to the wrong seed, connect to the correct seed or close the client.") + return + self.kh2_seed_save_path = f"kh2save2{self.kh2seedname}{self.auth}.json" self.kh2_seed_save_path_join = os.path.join(self.game_communication_path, self.kh2_seed_save_path) @@ -338,7 +358,7 @@ class KH2Context(CommonContext): }, }, } - if start_index > self.kh2_seed_save_cache["itemIndex"] and self.serverconneced: + if start_index > self.kh2_seed_save_cache["itemIndex"] and self.serverconnected: self.kh2_seed_save_cache["itemIndex"] = start_index for item in args['items']: asyncio.create_task(self.give_item(item.item, item.location)) @@ -370,12 +390,14 @@ class KH2Context(CommonContext): if not self.kh2: self.kh2 = pymem.Pymem(process_name="KINGDOM HEARTS II FINAL MIX") self.get_addresses() - +# except Exception as e: if self.kh2connected: self.kh2connected = False logger.info("Game is not open.") - self.serverconneced = True + + self.serverconnected = True + self.slot_name = self.auth def data_package_kh2_cache(self, loc_to_id, item_to_id): self.kh2_loc_name_to_id = loc_to_id @@ -930,7 +952,7 @@ def finishedGame(ctx: KH2Context): async def kh2_watcher(ctx: KH2Context): while not ctx.exit_event.is_set(): try: - if ctx.kh2connected and ctx.serverconneced: + if ctx.kh2connected and ctx.serverconnected: ctx.sending = [] await asyncio.create_task(ctx.checkWorldLocations()) await asyncio.create_task(ctx.checkLevels()) @@ -944,13 +966,19 @@ async def kh2_watcher(ctx: KH2Context): if ctx.sending: message = [{"cmd": 'LocationChecks', "locations": ctx.sending}] await ctx.send_msgs(message) - elif not ctx.kh2connected and ctx.serverconneced: - logger.info("Game Connection lost. waiting 15 seconds until trying to reconnect.") + elif not ctx.kh2connected and ctx.serverconnected: + logger.info("Game Connection lost. trying to reconnect.") ctx.kh2 = None - while not ctx.kh2connected and ctx.serverconneced: - await asyncio.sleep(15) - ctx.kh2 = pymem.Pymem(process_name="KINGDOM HEARTS II FINAL MIX") - ctx.get_addresses() + while not ctx.kh2connected and ctx.serverconnected: + try: + ctx.kh2 = pymem.Pymem(process_name="KINGDOM HEARTS II FINAL MIX") + ctx.get_addresses() + logger.info("Game Connection Established.") + except Exception as e: + await asyncio.sleep(5) + if ctx.disconnect_from_server: + ctx.disconnect_from_server = False + await ctx.disconnect() except Exception as e: if ctx.kh2connected: ctx.kh2connected = False