diff --git a/sharedPlugins/statisticsExporter/Lua/exportStatistics.lua b/sharedPlugins/statisticsExporter/Lua/exportStatistics.lua deleted file mode 100644 index c50196b5..00000000 --- a/sharedPlugins/statisticsExporter/Lua/exportStatistics.lua +++ /dev/null @@ -1,19 +0,0 @@ -local statisticsData = "[" -for _, force in pairs(game.forces) do - statisticsData = statisticsData .. '{"forceName":"'..force.name..'","data":{' - local statistics = {"item_production_statistics", "fluid_production_statistics", "kill_count_statistics", "entity_build_count_statistics"} - for key, stat in pairs(statistics) do - statisticsData = statisticsData..'"'..stat..'":{"input":{' - for name, value in pairs(force[stat].input_counts) do - statisticsData = statisticsData..'"'..name..'":'..value..',' - end - statisticsData = statisticsData..'},"output":{' - for name, value in pairs(force[stat].output_counts) do - statisticsData = statisticsData..'"'..name..'":'..value..',' - end - statisticsData = statisticsData .. '}},' - end - statisticsData = statisticsData..'}},' -end -statisticsData = statisticsData..']' -rcon.print(statisticsData) diff --git a/sharedPlugins/statisticsExporter/index.js b/sharedPlugins/statisticsExporter/index.js index dae7b24f..f6dbebd0 100644 --- a/sharedPlugins/statisticsExporter/index.js +++ b/sharedPlugins/statisticsExporter/index.js @@ -1,9 +1,5 @@ const needle = require("needle"); -const fs = require("fs-extra"); -const path = require("path"); -const clusterTools = require("lib/clusterTools")(); -const pluginConfig = require("./config"); module.exports = class remoteCommands { constructor(mergedConfig, messageInterface, extras){ @@ -36,11 +32,9 @@ module.exports = class remoteCommands { }); }, 15000); } - async getStats(){ - let command = fs.readFileSync(path.join(__dirname, "Lua/exportStatistics.lua"), "utf8")//clusterTools.getLua(path.join(__dirname, "Lua/exportStatistics.lua"), false); - let string = await this.messageInterface(`/silent-command ${command}`); - var data; - eval(`data = ${string}`); - return data; + + async getStats() { + let string = await this.messageInterface('/sc remote.call("statisticsExporter", "export")'); + return JSON.parse(string); } } diff --git a/sharedPlugins/statisticsExporter/lua/export.lua b/sharedPlugins/statisticsExporter/lua/export.lua new file mode 100644 index 00000000..76c2c756 --- /dev/null +++ b/sharedPlugins/statisticsExporter/lua/export.lua @@ -0,0 +1,30 @@ +local statistics = { + "item_production_statistics", + "fluid_production_statistics", + "kill_count_statistics", + "entity_build_count_statistics", +} + +local function export() + local stats = {} + for _, force in pairs(game.forces) do + local data = {} + for _, statName in pairs(statistics) do + data[statName] = { + input = force[statName].input_counts, + output = force[statName].output_counts, + } + end + table.insert(stats, { + forceName = force.name, + data = data, + }) + end + rcon.print(game.table_to_json(stats)) +end + +return { + add_remote_interface = function() + remote.add_interface("statisticsExporter", { export = export }) + end +}