From a9435dc6bb7b237f97f5cd4cd6f0f65e7d9c17d7 Mon Sep 17 00:00:00 2001 From: Mysteryem Date: Fri, 17 Jan 2025 03:00:29 +0000 Subject: [PATCH] KH2: Reduce unnecessary packets sent/requested by the client (#4035) --- worlds/kh2/Client.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/worlds/kh2/Client.py b/worlds/kh2/Client.py index d8bdf6a9..3ea47e40 100644 --- a/worlds/kh2/Client.py +++ b/worlds/kh2/Client.py @@ -110,6 +110,7 @@ class KH2Context(CommonContext): 18: TWTNW_Checks, # 255: {}, # starting screen } + self.last_world_int = -1 # 0x2A09C00+0x40 is the sve anchor. +1 is the last saved room # self.sveroom = 0x2A09C00 + 0x41 # 0 not in battle 1 in yellow battle 2 red battle #short @@ -387,13 +388,15 @@ class KH2Context(CommonContext): async def checkWorldLocations(self): try: currentworldint = self.kh2_read_byte(self.Now) - await self.send_msgs([{ - "cmd": "Set", "key": "Slot: " + str(self.slot) + " :CurrentWorld", - "default": 0, "want_reply": True, "operations": [{ - "operation": "replace", - "value": currentworldint - }] - }]) + if self.last_world_int != currentworldint: + self.last_world_int = currentworldint + await self.send_msgs([{ + "cmd": "Set", "key": "Slot: " + str(self.slot) + " :CurrentWorld", + "default": 0, "want_reply": False, "operations": [{ + "operation": "replace", + "value": currentworldint + }] + }]) if currentworldint in self.worldid_to_locations: curworldid = self.worldid_to_locations[currentworldint] for location, data in curworldid.items(): @@ -804,7 +807,7 @@ class KH2Context(CommonContext): logger.info("line 840") -def finishedGame(ctx: KH2Context, message): +def finishedGame(ctx: KH2Context): if ctx.kh2slotdata['FinalXemnas'] == 1: if not ctx.final_xemnas and ctx.kh2_read_byte(ctx.Save + all_world_locations[LocationName.FinalXemnas].addrObtained) \ & 0x1 << all_world_locations[LocationName.FinalXemnas].bitIndex > 0: @@ -836,8 +839,9 @@ def finishedGame(ctx: KH2Context, message): elif ctx.kh2slotdata['Goal'] == 2: # for backwards compat if "hitlist" in ctx.kh2slotdata: + locations = ctx.sending for boss in ctx.kh2slotdata["hitlist"]: - if boss in message[0]["locations"]: + if boss in locations: ctx.hitlist_bounties += 1 if ctx.hitlist_bounties >= ctx.kh2slotdata["BountyRequired"] or ctx.kh2_seed_save_cache["AmountInvo"]["Amount"]["Bounty"] >= ctx.kh2slotdata["BountyRequired"]: if ctx.kh2_read_byte(ctx.Save + 0x36B3) < 1: @@ -878,11 +882,12 @@ async def kh2_watcher(ctx: KH2Context): await asyncio.create_task(ctx.verifyChests()) await asyncio.create_task(ctx.verifyItems()) await asyncio.create_task(ctx.verifyLevel()) - message = [{"cmd": 'LocationChecks', "locations": ctx.sending}] - if finishedGame(ctx, message) and not ctx.kh2_finished_game: + if finishedGame(ctx) and not ctx.kh2_finished_game: await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}]) ctx.kh2_finished_game = True - await ctx.send_msgs(message) + 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.") ctx.kh2 = None