Factorio: send goal completion

This commit is contained in:
Fabian Dill 2021-04-29 04:34:47 +02:00
parent 60f64cc46b
commit 97030590c2
2 changed files with 25 additions and 9 deletions

View File

@ -13,7 +13,7 @@ from MultiServer import mark_raw
import Utils import Utils
import random import random
from NetUtils import RawJSONtoTextParser, NetworkItem from NetUtils import RawJSONtoTextParser, NetworkItem, ClientStatus
from worlds.factorio.Technologies import lookup_id_to_name 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: with open(bridge_file) as f:
data = json.load(f) data = json.load(f)
research_data = data["research_done"] research_data = data["research_done"]
research_data = {int(tech_name.split("-")[1]) for tech_name in research_data} 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: if ctx.locations_checked != research_data:
bridge_logger.info(f"New researches done: " bridge_logger.info(f"New researches done: "
f"{[lookup_id_to_name[rid] for rid in research_data - ctx.locations_checked]}") f"{[lookup_id_to_name[rid] for rid in research_data - ctx.locations_checked]}")

View File

@ -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. -- 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) function on_force_created(event)
--event.force appears to be LuaForce.name, not LuaForce
game.forces[event.force].research_queue_enabled = true game.forces[event.force].research_queue_enabled = true
local data = {} local data = {}
if FREE_SAMPLES ~= 0 then if FREE_SAMPLES ~= 0 then
@ -14,13 +15,14 @@ function on_force_created(event)
["stone-furnace"] = 19 ["stone-furnace"] = 19
} }
end end
data["victory"] = 0
global.forcedata[event.force] = data global.forcedata[event.force] = data
end end
script.on_event(defines.events.on_force_created, on_force_created) 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. -- Destroy force data. This doesn't appear to be currently possible with the Factorio API, but here for completeness.
function on_force_destroyed(event) function on_force_destroyed(event)
global.forcedata[event.force] = nil global.forcedata[event.force.name] = nil
end end
-- Initialize player data, either from them joining the game or them already being part of the game when the mod was -- 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 end
script.on_event(defines.events.on_player_removed, on_player_removed) 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) -- Updates a player, attempting to send them any pending samples (if relevant)
function update_player(index) function update_player(index)
if FREE_SAMPLES == 0 then -- This is effectively a noop if FREE_SAMPLES == 0 then -- This is effectively a noop
@ -134,15 +142,15 @@ end)
-- for testing -- for testing
script.on_event(defines.events.on_tick, function(event) script.on_event(defines.events.on_tick, function(event)
if event.tick%600 == 0 then if event.tick%600 == 300 then
dumpTech(game.forces["player"]) dumpInfo(game.forces["player"])
end end
end) end)
-- hook into researches done -- hook into researches done
script.on_event(defines.events.on_research_finished, function(event) script.on_event(defines.events.on_research_finished, function(event)
local technology = event.research local technology = event.research
dumpTech(technology.force) dumpInfo(technology.force)
if FREE_SAMPLES == 0 then if FREE_SAMPLES == 0 then
return -- Nothing else to do return -- Nothing else to do
end end
@ -171,9 +179,12 @@ script.on_event(defines.events.on_research_finished, function(event)
end end
end) end)
function dumpTech(force) function dumpInfo(force)
local research_done = {} 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 for tech_name, tech in pairs(force.technologies) do
if tech.researched and string.find(tech_name, "ap%-") == 1 then if tech.researched and string.find(tech_name, "ap%-") == 1 then
@ -190,7 +201,7 @@ end
-- add / commands -- add / commands
commands.add_command("ap-sync", "Run manual Research Sync with Archipelago.", function(call) 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.") game.print("Wrote bridge file.")
end) end)