mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Working inventory imports resolves #50
This commit is contained in:
1 parent
f9a2e8519b
commit
c4ead8c699
3 files changed
+180
-95
No files matched your search
@@ -4,25 +4,26 @@ const pluginConfig = require("./config.js");
|
|||||||
|
|
||||||
const functions = require("./interfacingFunctions");
|
const functions = require("./interfacingFunctions");
|
||||||
|
|
||||||
console.log("/silent-command game.print('InventoryImports enabled')\n");
|
console.log("/silent-command game.print('InventoryImports enabled')");
|
||||||
|
|
||||||
var config = {};
|
var config = {};
|
||||||
process.stdin.setEncoding('utf8');
|
process.stdin.setEncoding('utf8');
|
||||||
process.stdin.on('readable', () => {
|
process.stdin.on('readable', () => {
|
||||||
var chunk = process.stdin.read();
|
var chunk = process.stdin.read();
|
||||||
console.log(chunk)
|
// console.log(chunk)
|
||||||
if (chunk !== null && isNaN(chunk) && config && !isJson(chunk)){
|
if (chunk !== null && isNaN(chunk) && config && isJson(chunk) && !JSON.parse(chunk).factorioPort){
|
||||||
// example chunk Stdout: {1:{inventory:{['iron-ore']:100,['steel-plate']:5800,['iron-ore']:50,},
|
// example chunk Stdout: {1:{inventory:{['iron-ore']:100,['steel-plate']:5800,['iron-ore']:50,},
|
||||||
// requestSlots:{['underground-belt']:50, ['fast-transport-belt']:50, ['express-underground-belt']:50, ['underground-belt']:50, }},}
|
// requestSlots:{['underground-belt']:50, ['fast-transport-belt']:50, ['express-underground-belt']:50, ['underground-belt']:50, }},}
|
||||||
// returns JS object, throws if there are syntax errors in input string
|
// returns JS object, throws if there are syntax errors in input string
|
||||||
let inventory = functions.parseJsString(chunk);
|
// let inventory = functions.parseJsString(chunk);
|
||||||
console.log(inventory);
|
let inventory = JSON.parse(chunk)
|
||||||
|
// console.log(inventory);
|
||||||
// process inventory of players and ask master for more stuff
|
// process inventory of players and ask master for more stuff
|
||||||
functions.handleInventory(inventory, config);
|
functions.handleInventory(inventory, config);
|
||||||
} else if(isJson(chunk) && JSON.parse(chunk).factorioPort){
|
} else if(isJson(chunk) && JSON.parse(chunk).factorioPort){
|
||||||
config = JSON.parse(chunk);
|
config = JSON.parse(chunk);
|
||||||
// if its a LUA table message returning with the exports send em off to master
|
// if its a LUA table message returning with the exports send em off to master
|
||||||
} else if(!isJson(chunk) && !!chunk && config){
|
}/* else if(!isJson(chunk) && !!chunk && config){
|
||||||
let thing = functions.parseJsString(chunk.replace(/=/g, ":"));
|
let thing = functions.parseJsString(chunk.replace(/=/g, ":"));
|
||||||
Object.keys(thing).forEach(function(playerName){
|
Object.keys(thing).forEach(function(playerName){
|
||||||
Object.keys(thing[playerName]).forEach(function(itemName){
|
Object.keys(thing[playerName]).forEach(function(itemName){
|
||||||
@@ -34,7 +35,7 @@ process.stdin.on('readable', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}*/
|
||||||
});
|
});
|
||||||
process.stdin.on('end', () => {
|
process.stdin.on('end', () => {
|
||||||
process.stdout.write('end');
|
process.stdout.write('end');
|
||||||
@@ -42,6 +43,6 @@ process.stdin.on('end', () => {
|
|||||||
|
|
||||||
// poll inventories
|
// poll inventories
|
||||||
setInterval(function(){
|
setInterval(function(){
|
||||||
console.log("Polling inventories")
|
// console.log("Polling inventories\n")
|
||||||
console.log(functions.pollInventories(pluginConfig.scriptOutputFileSubscription));
|
console.log(functions.pollInventories(pluginConfig.scriptOutputFileSubscription));
|
||||||
}, 10000);
|
}, 10000);
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
const objectOps = require("../../lib/objectOps.js");
|
const objectOps = require("../../lib/objectOps.js");
|
||||||
const needle = require("needle");
|
const needle = require("needle");
|
||||||
|
const pluginConfig = require("./config")
|
||||||
|
|
||||||
// returns JS object, throws if there are syntax errors in input string
|
// returns JS object, throws if there are syntax errors in input string
|
||||||
function parseJsString(string){
|
function parseJsString(string){
|
||||||
@@ -28,90 +29,104 @@ function parseJsString(string){
|
|||||||
}}}
|
}}}
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
function handleInventory(inventory, config){
|
function handleInventory(json, config){
|
||||||
// we want to seperate players, parse their inventory and compare to request slots
|
/*
|
||||||
// then ask master for the difference
|
json is one of these two samples
|
||||||
// we must then prepare it all and inject it into players inventories with lua before
|
{"players":{"1":{"inventory":{"stone":12,"iron-ore":120,"raw-wood":10,"iron-plate":8,"steel-plate":480},"requestSlots":{}}}}
|
||||||
// the next inventory report arrives to avoid duplicating orders
|
{"exports":{"1":[{"name":"iron-ore","count":0},{"name":"steel-plate","count":0}]}}
|
||||||
if(inventory && typeof inventory != "string" && typeof inventory == "object"){
|
|
||||||
|
we want to seperate players, parse their json and compare to request slots
|
||||||
|
then ask master for the difference
|
||||||
|
we must then prepare it all and inject it into players inventories with lua before
|
||||||
|
the next json report arrives to avoid duplicating orders
|
||||||
|
*/
|
||||||
|
if(json && typeof json != "string" && typeof json == "object"){
|
||||||
let exports = {};
|
let exports = {};
|
||||||
let players = Object.keys(inventory);
|
|
||||||
for(let i = 0; i < players.length; i++) {
|
|
||||||
let pinv = inventory[players[i]];
|
|
||||||
// create a copy of player.requestSlots
|
|
||||||
let toRequest = objectOps.deepclone(pinv.requestSlots);
|
|
||||||
// create a place to store the object we have confirmed for importing
|
|
||||||
let confirmedItems = {};
|
|
||||||
let itemsToConfirm = Object.keys(pinv.inventory).length;
|
|
||||||
|
|
||||||
// subtract items already in inventory
|
// this is an inventory report, check whats missing and request from master
|
||||||
Object.keys(pinv.inventory).forEach(function(key){
|
// {"players":{"1":{"inventory":{"stone":12,"iron-ore":120,"raw-wood":10,"iron-plate":8,"steel-plate":480},"requestSlots":{}}}}
|
||||||
let itemCount = pinv.inventory[key];
|
if(json.players){
|
||||||
if(toRequest[key]){
|
let playerNames = Object.keys(json.players);
|
||||||
toRequest[key] -= itemCount;
|
for(let i = 0; i < playerNames.length; i++) {
|
||||||
}
|
// haha, this turned into bit of a mess didn't it?
|
||||||
if(toRequest[key] > 0){
|
let playerInv = json.players[playerNames[i]];
|
||||||
let request = {
|
// create a copy of player.requestSlots
|
||||||
name:key,
|
let toRequest = objectOps.deepclone(playerInv.requestSlots);
|
||||||
count:toRequest[key],
|
let inventory = playerInv.inventory;
|
||||||
}
|
let itemsToConfirm = Object.keys(toRequest).length;
|
||||||
// request the difference from master
|
// create a place to store the object we have confirmed for importing
|
||||||
needle.post(config.masterIP + ":" + config.masterPort + '/api/remove', request, function (err, response, body) {
|
let confirmedItems = {};
|
||||||
if (response && response.body && typeof response.body == "object") {
|
let collectStacks = function(stack){
|
||||||
// confirmed orders, already removed from master inventory
|
itemsToConfirm--;
|
||||||
//response.body.name: response.body.count
|
if(stack){
|
||||||
let stack = {name:response.body.name,count:response.body.count}
|
if(confirmedItems[stack.name]){
|
||||||
collectStacks(stack);
|
confirmedItems[stack.name] += stack.count;
|
||||||
} else {
|
} else {
|
||||||
collectStacks(false);
|
confirmedItems[stack.name] = stack.count;
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
}
|
if(itemsToConfirm == 0 && Object.keys(confirmedItems).length > 0){
|
||||||
});
|
// make lua table
|
||||||
function collectStacks(stack){
|
// we gotta construct a string because I can't find any nice library for this
|
||||||
itemsToConfirm--;
|
let itemTable = "{";
|
||||||
if(stack){
|
Object.keys(confirmedItems).forEach(function(name){
|
||||||
if(confirmedItems[stack.name]){
|
let count = confirmedItems[name];
|
||||||
confirmedItems[stack.name] += stack.count;
|
itemTable += '["'+name+'"]='+count+',';
|
||||||
} else {
|
});
|
||||||
confirmedItems[stack.name] = stack.count;
|
itemTable += "}";
|
||||||
|
let outputFile = pluginConfig.scriptOutputFileSubscription;
|
||||||
|
// a="'+players[i]+'"
|
||||||
|
// b='+itemTable+'
|
||||||
|
console.log('/silent-command local a="'+playerNames[i]+'"local b='+itemTable+'local c={}if game.players[a]and game.players[a].connected then local d='+"'"+'{"exports":{"'+"'"+'..a..'+"'"+'":['+"'"+'local e=false;for f,g in pairs(b)do local h=game.players[a].insert{name=f,count=g}if e then d=d..'+"'"+','+"'"+'else e=true end;d=d..'+"'"+'{"name":"'+"'"+'..f..'+"'"+'","count":'+"'"+'..g-h..'+"'"+'}'+"'"+'end;d=d..'+"'"+']}}'+"'"+'game.write_file("'+outputFile+'",d)end');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(itemsToConfirm <= 0){
|
// subtract items already in inventory
|
||||||
// make lua table
|
Object.keys(playerInv.requestSlots).forEach(function(key){
|
||||||
// we gotta construct a string because I can't find any nice library for this
|
// if this item in inventory is also in a request slot, subtract invCount from requestCount
|
||||||
let itemTable = "{";
|
if(inventory[key]){
|
||||||
Object.keys(confirmedItems).forEach(function(name){
|
toRequest[key] -= inventory[key];
|
||||||
let count = confirmedItems[name];
|
}
|
||||||
itemTable += '["'+name+'"]='+count+',';
|
// if the difference is more than 0, request item from master
|
||||||
});
|
if(toRequest[key] > 0){
|
||||||
itemTable += "}";
|
let request = {
|
||||||
|
name:key,
|
||||||
console.log('/c local a="'+players[i]+'"local b='+itemTable+'local c={}if game.players[a]and game.players[a].connected then for d,e in pairs(b)do local f=game.players[a].insert{name=d,count=e}if not c[a]then c[a]={}end;if c[a][d]then c[a][d]=c[a][d]+e-f else c[a][d]=e-f end end;game.write_file("'+outputFile+'",serpent.line(c,{["comment"]=false,["compact"]=true}))end');
|
count:toRequest[key],
|
||||||
}
|
}
|
||||||
}
|
//console.log("Requesting: "+JSON.stringify(request));
|
||||||
|
// request the difference from master
|
||||||
// handle exports (why would they be there? I don't know!)
|
needle.post(config.masterIP + ":" + config.masterPort + '/api/remove', request, function (err, response, body) {
|
||||||
if(pinv.exports){
|
if (response && response.body && typeof response.body == "object") {
|
||||||
Object.keys(pinv.exports).forEach(function(name){
|
// confirmed orders, already removed from master inventory
|
||||||
if(exports[name]){
|
//response.body.name: response.body.count
|
||||||
exports[name] += pinv.exports[name];
|
let stack = {name:response.body.name,count:Number(response.body.count)}
|
||||||
|
collectStacks(stack);
|
||||||
|
} else {
|
||||||
|
collectStacks(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
exports[name] = pinv.exports[name];
|
collectStacks(false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// {"exports":{"1":[{"name":"iron-ore","count":0},{"name":"steel-plate","count":0}]}}
|
||||||
// keep handling exports here, now we send em off
|
// this is a report of stuff we didn't have space for, send back to master
|
||||||
Object.keys(exports).forEach(function(name){
|
if(json.exports){
|
||||||
needle.post(config.masterIP + ":" + config.masterPort + '/api/place', {
|
// loop over players (normally only one, but whatever)
|
||||||
name: name,
|
Object.keys(json.exports).forEach(function(playerName){
|
||||||
count: exports[name]
|
// loop over simpleItemStacks to export
|
||||||
}, function (err, resp, body) {
|
let stackArray = json.exports[playerName];
|
||||||
// console.log(body);
|
for(let i = 0; i < stackArray.length; i++){
|
||||||
|
if(stackArray[i].count > 0){
|
||||||
|
console.log("Returning overflow: " + JSON.stringify(stackArray[i]));
|
||||||
|
needle.post(config.masterIP + ":" + config.masterPort + '/api/place', stackArray[i], function (err, resp, body) {
|
||||||
|
// console.log(body);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
} else return false; // inventory is falsey or a string
|
} else return false; // inventory is falsey or a string
|
||||||
}
|
}
|
||||||
function pollInventories(outputFile){
|
function pollInventories(outputFile){
|
||||||
@@ -119,7 +134,8 @@ function pollInventories(outputFile){
|
|||||||
// console.log("Running inventory LUA")
|
// console.log("Running inventory LUA")
|
||||||
//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={}for d=1,game.players["Danielv123"].force.character_logistic_slot_count do c[d]=game.players["Danielv123"].character.get_request_slot(d)end;local e="{inventory:{"for f,g in pairs(a)do e=e.."['+"'"+'"..f.."'+"'"+']:"..g..","end;for f,g in pairs(b)do e=e.."['+"'"+'"..f.."'+"'"+']:"..g..","end;e=e.."},requestSlots:{"for f,g in pairs(c)do e=e.."['+"'"+'"..g["name"].."'+"'"+']:"..g["count"]..", "end;e=e.."}}"game.write_file("'+pluginConfig.scriptOutputFileSubscription+'",e)')
|
//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={}for d=1,game.players["Danielv123"].force.character_logistic_slot_count do c[d]=game.players["Danielv123"].character.get_request_slot(d)end;local e="{inventory:{"for f,g in pairs(a)do e=e.."['+"'"+'"..f.."'+"'"+']:"..g..","end;for f,g in pairs(b)do e=e.."['+"'"+'"..f.."'+"'"+']:"..g..","end;e=e.."},requestSlots:{"for f,g in pairs(c)do e=e.."['+"'"+'"..g["name"].."'+"'"+']:"..g["count"]..", "end;e=e.."}}"game.write_file("'+pluginConfig.scriptOutputFileSubscription+'",e)')
|
||||||
// console.log("/silent-command "+'local a="{"for b,c in pairs(game.players)do local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end;a=a.."}"game.write_file("'+pluginConfig.scriptOutputFileSubscription+'",a)')
|
// console.log("/silent-command "+'local a="{"for b,c in pairs(game.players)do local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end;a=a.."}"game.write_file("'+pluginConfig.scriptOutputFileSubscription+'",a)')
|
||||||
return "/silent-command "+'local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.write_file("'+outputFile+'",a)'
|
//return "/silent-command "+'local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.write_file("'+outputFile+'",a)'
|
||||||
|
return '/silent-command '+'local a='+"'"+'{"players":{'+"'"+'local b=false;for c,d in pairs(game.players)do if d.connected then if b then a=a..'+"'"+','+"'"+'else b=true end;local e=game.players[c].get_inventory(defines.inventory.player_main).get_contents()local f=game.players[c].get_quickbar().get_contents()local g={}for h=1,game.players[c].force.character_logistic_slot_count do g[h]=game.players[c].character.get_request_slot(h)end;a=a..'+"'"+'"'+"'"+'..c..'+"'"+'":{"inventory":{'+"'"+'local i=false;for j,k in pairs(e)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;for j,k in pairs(f)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;a=a..'+"'"+'},"requestSlots":{'+"'"+'i=false;for j,k in pairs(g)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..k['+"'"+'name'+"'"+']..'+"'"+'":'+"'"+'..k['+"'"+'count'+"'"+']end;a=a..'+"'"+'}}'+"'"+'end end;a=a.."}}"game.write_file("'+outputFile+'",a)'
|
||||||
/*
|
/*
|
||||||
needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {slaveID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) {
|
needle.post(config.masterIP+':'+config.masterPort+'/api/editSlaveMeta', {slaveID: config.unique, password: config.clientPassword, meta: {UPS:UPS}}, function(err, resp) {
|
||||||
// success?
|
// success?
|
||||||
|
|||||||
@@ -22,11 +22,18 @@ for k,v in pairs(requests) do
|
|||||||
end
|
end
|
||||||
game.print(o)
|
game.print(o)
|
||||||
|
|
||||||
-- sum up our inventory and request slots
|
-- sum up our inventory and request slots and print it as valid JSON
|
||||||
|
-- turns out the valid part is important
|
||||||
/c
|
/c
|
||||||
local o = "{"
|
local o = '{"players":{'
|
||||||
|
local notFirst = false
|
||||||
for a,b in pairs(game.players) do
|
for a,b in pairs(game.players) do
|
||||||
if b.connected then
|
if b.connected then
|
||||||
|
if notFirst then
|
||||||
|
o = o .. ','
|
||||||
|
else
|
||||||
|
notFirst = true
|
||||||
|
end
|
||||||
local inventory = game.players[a].get_inventory(defines.inventory.player_main).get_contents()
|
local inventory = game.players[a].get_inventory(defines.inventory.player_main).get_contents()
|
||||||
local quickbar = game.players[a].get_quickbar().get_contents()
|
local quickbar = game.players[a].get_quickbar().get_contents()
|
||||||
--[[ Get contents of requester slots into a table ]]
|
--[[ Get contents of requester slots into a table ]]
|
||||||
@@ -35,27 +42,46 @@ for a,b in pairs(game.players) do
|
|||||||
requests[i] = game.players[a].character.get_request_slot(i)
|
requests[i] = game.players[a].character.get_request_slot(i)
|
||||||
end
|
end
|
||||||
|
|
||||||
o = o..a..":{inventory:{"
|
o = o..'"'..a..'":{"inventory":{'
|
||||||
--[[ generate JS table (not JSON) ]]
|
--[[ generate JSON table ]]
|
||||||
|
local addComma = false
|
||||||
for k,v in pairs(inventory) do
|
for k,v in pairs(inventory) do
|
||||||
o = o.."['"..k.."']:"..v..","
|
if addComma then
|
||||||
|
o = o..','
|
||||||
|
else
|
||||||
|
addComma = true
|
||||||
|
end
|
||||||
|
o = o..'"'..k..'":'..v
|
||||||
end
|
end
|
||||||
for k,v in pairs(quickbar) do
|
for k,v in pairs(quickbar) do
|
||||||
o = o.."['"..k.."']:"..v..","
|
if addComma then
|
||||||
|
o = o..','
|
||||||
|
else
|
||||||
|
addComma = true
|
||||||
|
end
|
||||||
|
o = o..'"'..k..'":'..v
|
||||||
end
|
end
|
||||||
|
|
||||||
o = o.."},requestSlots:{"
|
o = o..'},"requestSlots":{'
|
||||||
|
addComma = false
|
||||||
for k,v in pairs(requests) do
|
for k,v in pairs(requests) do
|
||||||
o = o.."['"..v["name"].."']:"..v["count"]..", "
|
if addComma then
|
||||||
|
o = o..','
|
||||||
|
else
|
||||||
|
addComma = true
|
||||||
|
end
|
||||||
|
o = o..'"'..v['name']..'":'..v['count']
|
||||||
end
|
end
|
||||||
o = o.."}},"
|
o = o..'}}' --[[ We need a comma after these two if there is more than 1 player, handled up top by notFirst]]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
o = o.."}"
|
o = o.."}}"
|
||||||
|
game.write_file("t.txt", o)
|
||||||
game.print(o)
|
game.print(o)
|
||||||
game.write_file("test.txt", o)
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
Example output
|
||||||
|
{"players":{"1":{"inventory":{"stone":12,"iron-ore":120,"raw-wood":10,"iron-plate":8,"steel-plate":480},"requestSlots":{}}}}
|
||||||
|
|
||||||
Minification procedure:
|
Minification procedure:
|
||||||
|
|
||||||
@@ -63,12 +89,13 @@ Run through https://mothereff.in/lua-minifier
|
|||||||
Replace ' with '+"'"+'
|
Replace ' with '+"'"+'
|
||||||
Prepend /silent-command
|
Prepend /silent-command
|
||||||
remove game.print(o)
|
remove game.print(o)
|
||||||
replace "test.txt" with "'+outputFile+'"
|
replace "t.txt" with "'+outputFile+'"
|
||||||
enclose in ''
|
enclose in ''
|
||||||
|
|
||||||
]]
|
]]
|
||||||
|
|
||||||
-- minified strings (newest on the top)
|
-- minified strings (newest on the top)
|
||||||
|
local a='+"'"+'{"players":{'+"'"+'local b=false;for c,d in pairs(game.players)do if d.connected then if b then a=a..'+"'"+','+"'"+'else b=true end;local e=game.players[c].get_inventory(defines.inventory.player_main).get_contents()local f=game.players[c].get_quickbar().get_contents()local g={}for h=1,game.players[c].force.character_logistic_slot_count do g[h]=game.players[c].character.get_request_slot(h)end;a=a..'+"'"+'"'+"'"+'..c..'+"'"+'":{"inventory":{'+"'"+'local i=false;for j,k in pairs(e)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;for j,k in pairs(f)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;a=a..'+"'"+'},"requestSlots":{'+"'"+'i=false;for j,k in pairs(g)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..k['+"'"+'name'+"'"+']..'+"'"+'":'+"'"+'..k['+"'"+'count'+"'"+']end;a=a..'+"'"+'}}'+"'"+'end end;a=a.."}}"game.write_file("'+outputFile+'",a)
|
||||||
-- offline playes
|
-- offline playes
|
||||||
local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.print(a)
|
local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.print(a)
|
||||||
-- multiple players
|
-- multiple players
|
||||||
@@ -81,7 +108,7 @@ local inventory = game.players["Danielv123"].get_inventory(defines.inventory.pla
|
|||||||
-- name should be players[i]
|
-- name should be players[i]
|
||||||
|
|
||||||
/c
|
/c
|
||||||
local name = "Danielv123"
|
local name = "1"
|
||||||
local items = {["iron-ore"]=30, ["steel-plate"]=120}
|
local items = {["iron-ore"]=30, ["steel-plate"]=120}
|
||||||
local items_failed = {}
|
local items_failed = {}
|
||||||
if(game.players[name] and game.players[name].connected) then
|
if(game.players[name] and game.players[name].connected) then
|
||||||
@@ -96,17 +123,58 @@ if(game.players[name] and game.players[name].connected) then
|
|||||||
items_failed[name][item] = count - i
|
items_failed[name][item] = count - i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
game.write_file("t.txt", serpent.line(items_failed, {["comment"]=false, ["compact"]=true}))
|
game.write_file("t.txt", serpent.line({["exports"]=items_failed}, {["comment"]=false, ["compact"]=true}))
|
||||||
|
end
|
||||||
|
|
||||||
|
--[[
|
||||||
|
Writes something like
|
||||||
|
|
||||||
|
{exports={["1"]={["iron-ore"]=0,["steel-plate"]=0}}}
|
||||||
|
|
||||||
|
converting to JS would be
|
||||||
|
replace "[" and "]" with ""???
|
||||||
|
replace "=" with ":"
|
||||||
|
]]
|
||||||
|
|
||||||
|
--[[
|
||||||
|
JSON edition
|
||||||
|
|
||||||
|
{"exports":{"1":[{"name":"iron-ore","count":0},{"name":"steel-plate","count":0}]}}
|
||||||
|
|
||||||
|
Should always return valid JSON
|
||||||
|
]]
|
||||||
|
/c
|
||||||
|
local name = "1"
|
||||||
|
local items = {["iron-ore"]=30, ["steel-plate"]=120}
|
||||||
|
if(game.players[name] and game.players[name].connected) then
|
||||||
|
local o = '{"exports":{"'..name..'":['
|
||||||
|
local addComma = false
|
||||||
|
for item,count in pairs(items) do
|
||||||
|
local i = game.players[name].insert{name=item,count=count}
|
||||||
|
if addComma then
|
||||||
|
o = o..','
|
||||||
|
else
|
||||||
|
addComma = true
|
||||||
|
end
|
||||||
|
o = o .. '{"name":"'..item..'","count":'..count - i..'}'
|
||||||
|
end
|
||||||
|
o = o .. ']}}'
|
||||||
|
game.write_file("t.txt", o)
|
||||||
end
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
Minification procedure:
|
Minification procedure:
|
||||||
|
|
||||||
Run through https://mothereff.in/lua-minifier
|
Run through https://mothereff.in/lua-minifier
|
||||||
|
Replace ' with '+"'"+'
|
||||||
Prepend /silent-command
|
Prepend /silent-command
|
||||||
replace "t.txt" with "'+outputFile+'"
|
replace "t.txt" with "'+outputFile+'"
|
||||||
enclose in ''
|
enclose in ''
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
|
||||||
|
-- JSON outputFile
|
||||||
|
local a="1"local b={["iron-ore"]=30,["steel-plate"]=120}local c={}if game.players[a]and game.players[a].connected then local d='+"'"+'{"exports":{"'+"'"+'..a..'+"'"+'":['+"'"+'local e=false;for f,g in pairs(b)do local h=game.players[a].insert{name=f,count=g}if e then d=d..'+"'"+','+"'"+'else e=true end;d=d..'+"'"+'{"name":"'+"'"+'..f..'+"'"+'","count":'+"'"+'..g-h..'+"'"+'}'+"'"+'end;d=d..'+"'"+']}}'+"'"+'game.write_file("'+outputFile+'",d)end
|
||||||
|
-- output to serpented LUA table
|
||||||
local a="Danielv123"local b={["iron-ore"]=30,["steel-plate"]=120}local c={}if game.players[a]and game.players[a].connected then for d,e in pairs(b)do local f=game.players[a].insert{name=d,count=e}if not c[a]then c[a]={}end;if c[a][d]then c[a][d]=c[a][d]+e-f else c[a][d]=e-f end end;game.write_file("'+outputFile+'",serpent.line(c,{["comment"]=false,["compact"]=true}))end
|
local a="Danielv123"local b={["iron-ore"]=30,["steel-plate"]=120}local c={}if game.players[a]and game.players[a].connected then for d,e in pairs(b)do local f=game.players[a].insert{name=d,count=e}if not c[a]then c[a]={}end;if c[a][d]then c[a][d]=c[a][d]+e-f else c[a][d]=e-f end end;game.write_file("'+outputFile+'",serpent.line(c,{["comment"]=false,["compact"]=true}))end
|
||||||
|
|
||||||
Reference in new issue
Block a user