require('__base__/script/freeplay/control.lua')
    function outputStats(event)
        local outputData = {}

        outputData["game_won"] = "false"
        if game.finished or game.finished_but_continuing then
            outputData["game_won"] = "true";
        end

        for _,p in pairs(game.players) do
            outputData[p.name] = math.floor(p.online_time * 100 / game.tick)
        end
        helpers.write_file("08-2026_stats.txt", helpers.table_to_json(outputData), false)
    end


    function researchFinished(event)
        helpers.write_file("08-2026_tech.txt", event.research.name .. "=" .. math.floor(event.tick / 60) .. "\n", true)
    end

    script.on_event(defines.events.on_pre_scenario_finished, outputStats)
    script.on_event(defines.events.on_player_left_game, outputStats)
    script.on_event(defines.events.on_player_joined_game, outputStats)
    script.on_event(defines.events.on_research_finished, researchFinished)
