Wargroove: Fix Communication Thread (#5125)

This commit is contained in:
Fly Hyping
2025-06-27 19:42:35 -04:00
committed by GitHub
parent 52389731eb
commit da52598c08

View File

@@ -496,70 +496,74 @@ class WargrooveContext(CommonContext):
async def game_watcher(ctx: WargrooveContext): async def game_watcher(ctx: WargrooveContext):
while not ctx.exit_event.is_set(): while not ctx.exit_event.is_set():
if ctx.syncing == True: try:
sync_msg = [{'cmd': 'Sync'}] if ctx.syncing == True:
if ctx.locations_checked: sync_msg = [{'cmd': 'Sync'}]
sync_msg.append({"cmd": "LocationChecks", "locations": list(ctx.locations_checked)}) if ctx.locations_checked:
await ctx.send_msgs(sync_msg) sync_msg.append({"cmd": "LocationChecks", "locations": list(ctx.locations_checked)})
ctx.syncing = False await ctx.send_msgs(sync_msg)
sending = [] ctx.syncing = False
victory = False sending = []
for root, dirs, files in os.walk(ctx.game_communication_path): victory = False
for file in files: for root, dirs, files in os.walk(ctx.game_communication_path):
if file == "deathLinkSend" and ctx.has_death_link: for file in files:
with open(os.path.join(ctx.game_communication_path, file), 'r') as f: if file == "deathLinkSend" and ctx.has_death_link:
failed_mission = f.read()
if ctx.slot is not None:
await ctx.send_death(f"{ctx.player_names[ctx.slot]} failed {failed_mission}")
os.remove(os.path.join(ctx.game_communication_path, file))
if file.find("send") > -1:
st = file.split("send", -1)[1]
sending = sending+[(int(st))]
os.remove(os.path.join(ctx.game_communication_path, file))
if file.find("victory") > -1:
victory = True
os.remove(os.path.join(ctx.game_communication_path, file))
if file == "unitSacrifice" or file == "unitSacrificeAI":
if ctx.has_sacrifice_summon:
stored_units_key = ctx.player_stored_units_key
if file == "unitSacrificeAI":
stored_units_key = ctx.ai_stored_units_key
with open(os.path.join(ctx.game_communication_path, file), 'r') as f: with open(os.path.join(ctx.game_communication_path, file), 'r') as f:
unit_class = f.read() failed_mission = f.read()
message = [{"cmd": 'Set', "key": stored_units_key, if ctx.slot is not None:
"default": [], await ctx.send_death(f"{ctx.player_names[ctx.slot]} failed {failed_mission}")
"want_reply": True, os.remove(os.path.join(ctx.game_communication_path, file))
"operations": [{"operation": "add", "value": [unit_class[:64]]}]}] if file.find("send") > -1:
await ctx.send_msgs(message) st = file.split("send", -1)[1]
os.remove(os.path.join(ctx.game_communication_path, file)) sending = sending+[(int(st))]
if file == "unitSummonRequestAI" or file == "unitSummonRequest": os.remove(os.path.join(ctx.game_communication_path, file))
if ctx.has_sacrifice_summon: if file.find("victory") > -1:
stored_units_key = ctx.player_stored_units_key victory = True
if file == "unitSummonRequestAI": os.remove(os.path.join(ctx.game_communication_path, file))
stored_units_key = ctx.ai_stored_units_key if file == "unitSacrifice" or file == "unitSacrificeAI":
with open(os.path.join(ctx.game_communication_path, "unitSummonResponse"), 'w') as f: if ctx.has_sacrifice_summon:
if stored_units_key in ctx.stored_data: stored_units_key = ctx.player_stored_units_key
stored_units = ctx.stored_data[stored_units_key] if file == "unitSacrificeAI":
if stored_units is None: stored_units_key = ctx.ai_stored_units_key
stored_units = [] with open(os.path.join(ctx.game_communication_path, file), 'r') as f:
wg1_stored_units = [unit for unit in stored_units if unit in ctx.unit_classes] unit_class = f.read()
if len(wg1_stored_units) != 0: message = [{"cmd": 'Set', "key": stored_units_key,
summoned_unit = random.choice(wg1_stored_units) "default": [],
message = [{"cmd": 'Set', "key": stored_units_key, "want_reply": True,
"default": [], "operations": [{"operation": "add", "value": [unit_class[:64]]}]}]
"want_reply": True, await ctx.send_msgs(message)
"operations": [{"operation": "remove", "value": summoned_unit[:64]}]}] os.remove(os.path.join(ctx.game_communication_path, file))
await ctx.send_msgs(message) if file == "unitSummonRequestAI" or file == "unitSummonRequest":
f.write(summoned_unit) if ctx.has_sacrifice_summon:
os.remove(os.path.join(ctx.game_communication_path, file)) stored_units_key = ctx.player_stored_units_key
if file == "unitSummonRequestAI":
stored_units_key = ctx.ai_stored_units_key
with open(os.path.join(ctx.game_communication_path, "unitSummonResponse"), 'w') as f:
if stored_units_key in ctx.stored_data:
stored_units = ctx.stored_data[stored_units_key]
if stored_units is None:
stored_units = []
wg1_stored_units = [unit for unit in stored_units if unit in ctx.unit_classes]
if len(wg1_stored_units) != 0:
summoned_unit = random.choice(wg1_stored_units)
message = [{"cmd": 'Set', "key": stored_units_key,
"default": [],
"want_reply": True,
"operations": [{"operation": "remove", "value": summoned_unit[:64]}]}]
await ctx.send_msgs(message)
f.write(summoned_unit)
os.remove(os.path.join(ctx.game_communication_path, file))
ctx.locations_checked = sending ctx.locations_checked = sending
message = [{"cmd": 'LocationChecks', "locations": sending}] message = [{"cmd": 'LocationChecks', "locations": sending}]
await ctx.send_msgs(message) await ctx.send_msgs(message)
if not ctx.finished_game and victory: if not ctx.finished_game and victory:
await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}]) await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}])
ctx.finished_game = True ctx.finished_game = True
await asyncio.sleep(0.1) await asyncio.sleep(0.1)
except Exception as err:
logger.warn("Exception in communication thread, a check may not have been sent: " + str(err))
def print_error_and_close(msg): def print_error_and_close(msg):