From 97030590c2b6946da40edbb6ce8009f21a502d4a Mon Sep 17 00:00:00 2001 From: Fabian Dill Date: Thu, 29 Apr 2021 04:34:47 +0200 Subject: [PATCH] Factorio: send goal completion --- FactorioClient.py | 9 +++++++-- data/factorio/mod_template/control.lua | 25 ++++++++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/FactorioClient.py b/FactorioClient.py index a0eb3621..48f0ed24 100644 --- a/FactorioClient.py +++ b/FactorioClient.py @@ -13,7 +13,7 @@ from MultiServer import mark_raw import Utils import random -from NetUtils import RawJSONtoTextParser, NetworkItem +from NetUtils import RawJSONtoTextParser, NetworkItem, ClientStatus from worlds.factorio.Technologies import lookup_id_to_name @@ -108,8 +108,13 @@ async def game_watcher(ctx: FactorioContext): with open(bridge_file) as f: data = json.load(f) research_data = data["research_done"] - research_data = {int(tech_name.split("-")[1]) for tech_name in research_data} + victory = data["victory"] + + if not ctx.finished_game and victory: + await ctx.send_msgs([{"cmd": "StatusUpdate", "status": ClientStatus.CLIENT_GOAL}]) + ctx.finished_game = True + if ctx.locations_checked != research_data: bridge_logger.info(f"New researches done: " f"{[lookup_id_to_name[rid] for rid in research_data - ctx.locations_checked]}") diff --git a/data/factorio/mod_template/control.lua b/data/factorio/mod_template/control.lua index 0bdd2685..832decbc 100644 --- a/data/factorio/mod_template/control.lua +++ b/data/factorio/mod_template/control.lua @@ -6,6 +6,7 @@ FREE_SAMPLES = {{ free_samples }} -- Initialize force data, either from it being created or already being part of the game when the mod was added. function on_force_created(event) + --event.force appears to be LuaForce.name, not LuaForce game.forces[event.force].research_queue_enabled = true local data = {} if FREE_SAMPLES ~= 0 then @@ -14,13 +15,14 @@ function on_force_created(event) ["stone-furnace"] = 19 } end + data["victory"] = 0 global.forcedata[event.force] = data end script.on_event(defines.events.on_force_created, on_force_created) -- Destroy force data. This doesn't appear to be currently possible with the Factorio API, but here for completeness. function on_force_destroyed(event) - global.forcedata[event.force] = nil + global.forcedata[event.force.name] = nil end -- Initialize player data, either from them joining the game or them already being part of the game when the mod was @@ -43,6 +45,12 @@ function on_player_removed(event) end script.on_event(defines.events.on_player_removed, on_player_removed) +function on_rocket_launched(event) + global.forcedata[event.rocket.force.name]['victory'] = 1 + dumpInfo(event.rocket.force) +end +script.on_event(defines.events.on_rocket_launched, on_rocket_launched) + -- Updates a player, attempting to send them any pending samples (if relevant) function update_player(index) if FREE_SAMPLES == 0 then -- This is effectively a noop @@ -134,15 +142,15 @@ end) -- for testing script.on_event(defines.events.on_tick, function(event) - if event.tick%600 == 0 then - dumpTech(game.forces["player"]) + if event.tick%600 == 300 then + dumpInfo(game.forces["player"]) end end) -- hook into researches done script.on_event(defines.events.on_research_finished, function(event) local technology = event.research - dumpTech(technology.force) + dumpInfo(technology.force) if FREE_SAMPLES == 0 then return -- Nothing else to do end @@ -171,9 +179,12 @@ script.on_event(defines.events.on_research_finished, function(event) end end) -function dumpTech(force) +function dumpInfo(force) local research_done = {} - local data_collection = {["research_done"] = research_done} + local data_collection = { + ["research_done"] = research_done, + ["victory"] = global.forcedata[force.name]["victory"] + } for tech_name, tech in pairs(force.technologies) do if tech.researched and string.find(tech_name, "ap%-") == 1 then @@ -190,7 +201,7 @@ end -- add / commands commands.add_command("ap-sync", "Run manual Research Sync with Archipelago.", function(call) - dumpTech(game.players[call.player_index].force) + dumpInfo(game.players[call.player_index].force) game.print("Wrote bridge file.") end)