From 103f01b6a204f30b9f669c184ab88051540246e6 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 1 Jun 2017 13:25:59 +0200 Subject: [PATCH] inventory reports, work on #50 --- sharedPlugins/inventoryImports/config.js | 20 ++++++++++ sharedPlugins/inventoryImports/index.js | 39 +++++++++++++++++++ sharedPlugins/inventoryImports/isJson.js | 9 +++++ .../inventoryImports/lua commands.lua | 25 ++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 sharedPlugins/inventoryImports/config.js create mode 100644 sharedPlugins/inventoryImports/index.js create mode 100644 sharedPlugins/inventoryImports/isJson.js create mode 100644 sharedPlugins/inventoryImports/lua commands.lua diff --git a/sharedPlugins/inventoryImports/config.js b/sharedPlugins/inventoryImports/config.js new file mode 100644 index 00000000..4f49d047 --- /dev/null +++ b/sharedPlugins/inventoryImports/config.js @@ -0,0 +1,20 @@ +/* + +This clusterio plugin is tasked to read your inventory and logistic request slots +it will then attempt to fulfill those requests using items provided by the master +server. There *should* be an in game GUI to toggle this. + +*/ + +module.exports = { + // Name of package. For display somewhere I guess. + name: "inventoryImports", + version: "1.0.0", + // Binary entrypoint for plugin. Don't let it crash. Stdout is sent to game as server chat (run /c commands from here for interface) + // Make sure its cross platform somehow. + binary: "node", + arguments: ["index.js"], + description: "Clusterio plugin to fill character logistic slots with master imports", + // We'll send everything in this file to your stdin. Beware. + scriptOutputFileSubscription: "inventoryImports.txt", +} \ No newline at end of file diff --git a/sharedPlugins/inventoryImports/index.js b/sharedPlugins/inventoryImports/index.js new file mode 100644 index 00000000..b4db6e89 --- /dev/null +++ b/sharedPlugins/inventoryImports/index.js @@ -0,0 +1,39 @@ +const needle = require("needle"); +const isJson = require("./isJson.js"); +const pluginConfig = require("./config.js"); + +console.log("/silent-command game.print('UPSdisplay enabled')\n"); + +setInterval(function(){ + console.log("/silent-command game.write_file('UPSdisplay.txt', game.tick, true, 0)\n"); +},1000); +var config = {}; +process.stdin.setEncoding('utf8'); +let historicalTicks = []; +process.stdin.on('readable', () => { + var chunk = process.stdin.read(); + if (chunk !== null && isNaN(chunk)){ + let inventory = ""; + // eval because my lua is writing a JS object instead of traditional JSON + // this was done because I hate escaping strings and there are more quotes + // in JSON than JS objects. + eval("inventory = " + chunk); + console.log(inventory); + } else if(isJson(chunk) && JSON.parse(chunk).factorioPort){ + config = JSON.parse(chunk); + } +}); +process.stdin.on('end', () => { + process.stdout.write('end'); +}); + +// poll inventories +setInterval(function(){ + // TODO: Make it work for players other than myself, don't error when inventory does not exist, check logistic request slots + console.log("/silent-command "+'local a=game.players["Danielv123"].get_inventory(defines.inventory.player_main).get_contents()local b=game.players["Danielv123"].get_quickbar().get_contents()local c={}local d="{inventory:{"for e,f in pairs(a)do d=d.."['+"'"+'"..e.."'+"'"+']:"..f..","end;for e,f in pairs(b)do d=d.."['+"'"+'"..e.."'+"'"+']:"..f..","end;d=d.."},requestSlots:{"for e,f in pairs(c)do d=d.."['+"'"+'"..e.."'+"'"+']:"..f..","end;d=d.."}}"game.write_file("'+pluginConfig.scriptOutputFileSubscription+'",d)'); + /* + needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {slaveID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) { + // success? + }); + */ +}, 10000); diff --git a/sharedPlugins/inventoryImports/isJson.js b/sharedPlugins/inventoryImports/isJson.js new file mode 100644 index 00000000..6bd89302 --- /dev/null +++ b/sharedPlugins/inventoryImports/isJson.js @@ -0,0 +1,9 @@ +module.exports = function(x){ + var isNotJson = false + try{ + z = JSON.parse(x); + } catch (e) { + isNotJson = true; + } + return !isNotJson; +} \ No newline at end of file diff --git a/sharedPlugins/inventoryImports/lua commands.lua b/sharedPlugins/inventoryImports/lua commands.lua new file mode 100644 index 00000000..1c8c1065 --- /dev/null +++ b/sharedPlugins/inventoryImports/lua commands.lua @@ -0,0 +1,25 @@ +-- Lua cheatsheet + +/c game.write_file("test.txt",serpent.block(game.players["Danielv123"].get_quickbar().get_contents())) +/c game.write_file("test.txt",serpent.block(game.players["Danielv123"].get_inventory(defines.inventory.player_main).get_contents())) + +-- print JSON of entire inventory and request slots to file +-- TODO: Get requester slot data as well +/c +local inventory = game.players["Danielv123"].get_inventory(defines.inventory.player_main).get_contents() +local quickbar = game.players["Danielv123"].get_quickbar().get_contents() +local requestSlots = {} +local o = "{inventory:{" +for k,v in pairs(inventory) do + o = o.."['"..k.."']:"..v.."," +end +for k,v in pairs(quickbar) do + o = o.."['"..k.."']:"..v.."," +end +o = o.."},requestSlots:{" +for k,v in pairs(requestSlots) do +o = o.."['"..k.."']:"..v.."," +end +o = o.."}}" +game.write_file("test.txt", o) +