Compare commits

..
54 changed files with 552 additions and 2616 deletions

No files matched your search

+3 -2
View File
@@ -65,5 +65,6 @@ sharedPlugins/*/DISABLED
\.cache-require-paths\.json
# IDE
.idea/
\.vs/
bin/
-19
View File
@@ -1,19 +0,0 @@
const needle = require("needle")
const config = require("./../config.json")
const cli = require("cli")
let options = cli.parse({
name: ["n", "Name of the player to delete, case sensitive", "string"],
token: [false, "Factorio auth token", "string", config.masterAuthToken]
})
if(!options.name){
cli.getUsage()
process.exit(1)
}
needle.post(config.masterIP+":"+config.masterPort+"/api/playerManager/deletePlayer", {
name: options.name,
token: options.token
}, (err, resp) => {
console.log(resp.body)
});
-4
View File
@@ -226,10 +226,6 @@ Copy your config.json.dist to config.json and configure it.
Some systems don't let non root processes listen to ports below 1000. Either run with `sudo` or change config.json to use higher port numbers.
According to [this link](https://askubuntu.com/questions/839520/open-port-443-for-a-node-web-app) if you manually installed node.js following the above instructions, you may need to run the following command to fix this issue:
sudo setcap 'cap_net_bind_service=+ep' $(readlink -f $(which node))
### InitializationError: First argument must be an abstract-leveldown compliant store
do:
+9 -12
View File
@@ -317,7 +317,6 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
"clientPort": args["rcon-port"] || process.env.RCONPORT || Math.floor(Math.random() * 65535),
"__comment_clientPassword": "This is the rcon password. Its also used for making an instanceID. Make sure its unique and not blank.",
"clientPassword": args["rcon-password"] || Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 8),
"info": {}
}
console.log("Clusterio | Created instance with settings:")
console.log(instconf);
@@ -365,14 +364,12 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
data = data.toString("utf8").replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(data);
if(data.includes("Starting RCON interface")){
let client = new Rcon({
packetResponseTimeout: 200000,
maxPending: 5
});
let client = new Rcon();
client.connect({
host: 'localhost',
port: Number(process.env.RCONPORT) || instconf.clientPort,
password: instconf.clientPassword
password: instconf.clientPassword,
timeout: 5000
});
client.onDidAuthenticate(() => {
console.log('Clusterio | RCON Authenticated!');
@@ -496,10 +493,8 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
// connect to the server with rcon
if(true || process.platform != "linux"){
// IP, port, password
client = new Rcon({
packetResponseTimeout: 200000, // 200s, should allow for commands up to 1250kB in length
maxPending: 5
});
client = new Rcon();
// check the logfile to see if the RCON interface is running as there is no way to continue without it
// we read the log every 2 seconds and stop looping when we start connecting to factorio
function checkRcon() {
@@ -510,6 +505,7 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
host: 'localhost',
port: args["rcon-port"] || Number(process.env.RCONPORT) || instanceconfig.clientPort,
password: args["rcon-password"] || instanceconfig.clientPassword,
timeout: 5000
});
} else {
setTimeout(function(){
@@ -644,7 +640,9 @@ async function instanceManagement(instanceconfig) {
if(stuff[o] && !stuff[o].includes('\u0000\u0000')) {
try{
plugins[i].scriptOutput(stuff[o]);
}catch(e){console.error(e)}
}catch(e){
throw new Error(`Plugin ${pluginConfig.name} has a fileOutputSubscription but plugin.scriptOutput is not a function`)
}
}
}
},
@@ -674,7 +672,6 @@ async function instanceManagement(instanceconfig) {
publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config
mods:modHashes,
instanceName: instance,
info: instanceconfig.info,
}
if(playerCount){
payload.playerCount = playerCount.replace(/(\r\n\t|\n|\r\t)/gm, "");
+2 -10
View File
@@ -1,13 +1,5 @@
const needle = require("needle");
const cli = require("cli");
const config = require("./../config.json");
let options = cli.parse()
cli.setUsage(`node cluster_rcon.js /c game.player.force = "player"`)
if(process.argv.length < 3){
cli.getUsage()
process.exit(1)
}
const config = require("./config.json");
const masterIP = `${config.masterIP}:${config.masterPort}`;
const token = config.masterAuthToken
@@ -38,4 +30,4 @@ let killBiters = `/silent-command local surface=game.surfaces[1] for key, entity
// console.log(resp.body)
// });
// }, 1000*60*5);
})();
})();
+1 -1
View File
@@ -49,7 +49,7 @@
"logItemTransfers": false,
"__comment6.5": "When the master is undersupplied, we have 3 different modes to 'fairly' distribute items. First to ask gets all, dole division using weird formula or experimental neural net tries to make stuff as fair as possible. Feel free to play around.",
"disableFairItemDistribution": false,
"useNeuralNetDoleDivider": true,
"useNeuralNetDoleDivider": false,
"uploadModsToMaster": true,
"msBetweenCommands": 10,
"allowRemoteCommandExecution": true,
-57
View File
@@ -1,57 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const NodeInstance_1 = require("./NodeInstance");
const Point_1 = require("./Point");
const Grid_1 = require("./Grid");
/**
* Manages all nodes within the cluster
*/
class ClusterManager {
constructor() {
this.Grid = new Grid_1.Grid();
}
/**
* Generates a x*y grid of 1x1 nodes
* @param width How wide to make the node grid
* @param height How high to make the node grid
* @param nodeNamePrefix The prefix used for the initial node names. The name will be in the format <prefix>_x_y.
*/
GenerateBasicGridOfNodes(width, height, nodeNamePrefix) {
for (let x = 0; x < width; x++) {
for (let y = 0; y < height; y++) {
let newNode = new NodeInstance_1.NodeInstance(nodeNamePrefix + "-" + (x + 1) + "-" + (y + 1), new Point_1.Point(x, y), 1, 1);
this.Grid.AddNodeToGrid(newNode);
}
}
}
/**
* Assigns all nodes that currently do not have a server assigned to the given server
* @param server Server to assign all unassigned nodes to
*/
AssignAllUnassignedNodesToServer(server) {
this.CheckServerArray(server);
this.Grid.GetNodes()
.filter(node => { node.Server === null; })
.forEach(node => {
server.AddNodeInstance(node);
node.Server = server;
});
}
/**
* Assigns a node to a server
* @param x x co-ordinate of the node
* @param y y co-ordinate of the node
* @param serverName server name
*/
AssignNodeToServer(x, y, server) {
this._nodeArray[x][y].Server = server;
}
CheckServerArray(server) {
let s = this._servers[server.Name];
if (s == null) {
this._servers[server.Name] = server;
}
}
}
exports.ClusterManager = ClusterManager;
//# sourceMappingURL=ClusterManager.js.map
-112
View File
@@ -1,112 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const child_process = require("child_process");
const needle = require("needle");
const Helpers_1 = require("./Helpers");
const fs = require("fs");
const util = require("util");
/**
* Contains all interactions between the cluster model, the
* Clusterio master.js/client.js infrastructure and the operating system.
*/
class ClusterioMasterProxy {
constructor(masterIP, masterPort, masterAuthToken, serviceHostType) {
this._masterIP = `${masterIP}:${masterPort}`;
this._needleOptionsWithTokenAuthHeader = {
headers: {
'x-access-token': masterAuthToken,
},
};
}
async RunRconCommand(instanceId, command, logDescription) {
console.log("Running command: " + logDescription);
let request = { instanceID: instanceId, command };
let response = await needle("post", this._masterIP + "/api/runCommand", request, this._needleOptionsWithTokenAuthHeader);
}
async GetSlaves() {
let response = await needle("get", this._masterIP + "/api/slaves", this._needleOptionsWithTokenAuthHeader);
let body = response.body;
let slaves = new Array();
Object.entries(body).forEach((keyValuePair, index, array) => {
let slave = keyValuePair[1];
slave.id = keyValuePair[0];
slaves.push(slave);
});
return slaves;
}
async CreateNodeInstancesOnLocalServer(nodes) {
await Helpers_1.asyncForEach(nodes, async (node) => {
await this.CreateNodeInstanceOnLocalServer(node);
});
await Helpers_1.asyncForEach(nodes, async (node) => {
await this.CreateTeleportZonesForANodeInstance(node);
});
await this.sleep(1000);
await Helpers_1.asyncForEach(nodes, async (node) => {
await this.CreateTeleportRestrictionsForANodeInstance(node);
await this.sleep(100);
});
}
async CreateNodeInstanceOnLocalServer(node) {
console.log(`Creating instance ${node.Name}...`);
await this.CreateNodeInstanceOnLocalServerUsingPm2(node);
console.log("Waiting for node to connect to cluster master...");
while (true) {
await this.sleep(1000);
//TODO: Use /api/getSlaveMeta/
let slaves = await this.GetSlaves();
let slave = slaves.find((slave, index, array) => slave.instanceName === node.Name);
if (slave != null) {
node.ClusterioWorldId = slave.id;
break;
}
}
}
async CreateNodeInstanceOnLocalServerUsingPm2(node) {
let proc = child_process.spawn("pm2", ['start', '--name', node.Name, 'client.js', '--', 'start', node.Name]);
proc.stdout.on('data', (data) => { console.log(`stdout: ${data}`); });
proc.stderr.on('data', data => { console.log(`stderr: ${data}`); });
proc.on('error', error => { console.log(`error: ${error}`); });
}
async CreateNodeInstanceOnLocalServerUsingScreen(node) {
// first call of client.js start for a new node creates map from HotPatch scenario then exits.
let proc = child_process.spawn("screen", ['-dmS', node.Name, '-L', '-Logfile', node.Name + '.log', 'node', 'client.js', 'start', node.Name]);
proc.stdout.on('data', (data) => { console.log(`stdout: ${data}`); });
proc.stderr.on('data', data => { console.log(`stderr: ${data}`); });
proc.on('error', error => { console.log(`error: ${error}`); });
console.log("Waiting for creation of new map...");
// create awaiter for fs.readFile
const readFile = util.promisify(fs.readFile);
while (true) {
await this.sleep(1000);
let fileData = await readFile(node.Name + '.log');
if (fileData.includes('Instance created')) {
break;
}
}
// start node
proc = child_process.spawn("screen", ['-dmS', node.Name, '-L', '-Logfile', node.Name + '.log', 'node', 'client.js', 'start', node.Name]);
proc.stdout.on('data', data => { console.log(`stdout: ${data}`); });
proc.stderr.on('data', data => { console.log(`stderr: ${data}`); });
proc.on('error', error => { console.log(`error: ${error}`); });
}
async CreateTeleportZonesForANodeInstance(nodeInstance) {
await Helpers_1.asyncForEach(nodeInstance.TeleportZones.values(), async (zone) => {
let o = zone.GenerateTeleportZoneInFactorioCoordinates();
let cmd = `CreateZone('${o[0]}',${o[1]},${o[2]},${o[3]},${o[4]},${o[5]},true)`;
await this.RunRconCommand(nodeInstance.ClusterioWorldId, `/c remote.call('trainTeleports','runCode',\"${cmd}\")`, cmd);
});
}
async CreateTeleportRestrictionsForANodeInstance(nodeInstance) {
await Helpers_1.asyncForEach(nodeInstance.TeleportZones.values(), async (zone) => {
let o = zone.GenerateZoneRestriction();
let cmd = `CreateZoneRestriction(${o[0]},'${o[1]}','${o[2]}')`;
await this.RunRconCommand(nodeInstance.ClusterioWorldId, `/c remote.call('trainTeleports','runCode',\"${cmd}\")`, cmd);
});
}
sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
exports.ClusterioMasterProxy = ClusterioMasterProxy;
//# sourceMappingURL=ClusterioMasterProxy.js.map
-8
View File
@@ -1,8 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/** The multiplier used to convert from the grid's coordinate system to factorio's.
* 1000 means a 1x1 grid node will have a factorio map size of 1000x1000. */
exports.GridToFactorioCoordinateSystemMultiplier = 1000;
/** The width of teleport zones when they get created. */
exports.TeleportZoneWidth = 50;
//# sourceMappingURL=Config.js.map
-177
View File
@@ -1,177 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const typescript_collections_1 = require("typescript-collections");
const Point_1 = require("./Point");
const TeleportZone_1 = require("./TeleportZone");
const TeleportZoneDirection_1 = require("./TeleportZoneDirection");
class Grid {
constructor() {
this._nodes = new typescript_collections_1.Dictionary();
this._minX = 0;
this._minY = 0;
this._maxX = 0;
this._maxY = 0;
}
GetNodes() {
return this._nodes.values();
}
/**
* Adds a node to the grid
* @param topLeftCoordinate The top left x,y coordinate of the node
* @param nodeInstance
*/
AddNodeToGrid(nodeInstance) {
let topLeftCoordinate = nodeInstance.TopLeftCoordinate;
let pointsToSave = new Array();
// check all points that make up nodeInstance for existing nodes
for (let x = topLeftCoordinate.X; x < topLeftCoordinate.X + nodeInstance.Width; x++) {
for (let y = topLeftCoordinate.Y; y < topLeftCoordinate.Y + nodeInstance.Height; y++) {
let currentPoint = new Point_1.Point(x, y);
let existingNode = this._nodes.getValue(currentPoint);
if (existingNode != null) {
throw new Error("Cannot add node to grid - collision detected with node '" + existingNode.Name + "'");
}
pointsToSave.push(currentPoint);
}
}
// no existing nodes found to assign node to these points
for (let i = 0; i < pointsToSave.length; i++) {
this._nodes.setValue(pointsToSave[i], nodeInstance);
}
//TODO: Refactor
//#region Set up teleport zones
// check surrounding points for nodes
// check north points
let y = topLeftCoordinate.Y - 1;
for (let x = topLeftCoordinate.X; x < topLeftCoordinate.X + nodeInstance.Width; x++) {
let currentPoint = new Point_1.Point(x, y);
let existingNode = this._nodes.getValue(currentPoint);
// found a node - set up teleport zone
if (existingNode != null) {
// Create/Update teleport node in existing node to new node
let teleportZoneFromExistingToNew = existingNode.TeleportZones.getValue(nodeInstance.Key);
if (teleportZoneFromExistingToNew == null) {
teleportZoneFromExistingToNew = new TeleportZone_1.TeleportZone(existingNode, TeleportZoneDirection_1.TeleportZoneDirection.South, currentPoint, currentPoint, existingNode.TeleportZones.size(), nodeInstance);
existingNode.TeleportZones.setValue(nodeInstance.Key, teleportZoneFromExistingToNew);
}
else {
teleportZoneFromExistingToNew.BottomRightCoordinate = currentPoint;
}
// Create/Update teleport node in new node to existing node
let teleportZoneFromNewToExisting = nodeInstance.TeleportZones.getValue(existingNode.Key);
let pointInNewNode = new Point_1.Point(currentPoint.X, currentPoint.Y + 1);
if (teleportZoneFromNewToExisting == null) {
teleportZoneFromNewToExisting = new TeleportZone_1.TeleportZone(nodeInstance, TeleportZoneDirection_1.TeleportZoneDirection.North, pointInNewNode, pointInNewNode, nodeInstance.TeleportZones.size(), existingNode);
nodeInstance.TeleportZones.setValue(existingNode.Key, teleportZoneFromNewToExisting);
}
else {
teleportZoneFromNewToExisting.BottomRightCoordinate = currentPoint;
}
}
}
// check south points
y = topLeftCoordinate.Y + nodeInstance.Height + 1;
for (let x = topLeftCoordinate.X; x < topLeftCoordinate.X + nodeInstance.Width; x++) {
let currentPoint = new Point_1.Point(x, y);
let existingNode = this._nodes.getValue(currentPoint);
// found a node - set up teleport zone
if (existingNode != null) {
// Create/Update teleport node in existing node to new node
let teleportZoneFromExistingToNew = existingNode.TeleportZones.getValue(nodeInstance.Key);
if (teleportZoneFromExistingToNew == null) {
teleportZoneFromExistingToNew = new TeleportZone_1.TeleportZone(existingNode, TeleportZoneDirection_1.TeleportZoneDirection.North, currentPoint, currentPoint, existingNode.TeleportZones.size(), nodeInstance);
existingNode.TeleportZones.setValue(nodeInstance.Key, teleportZoneFromExistingToNew);
}
else {
teleportZoneFromExistingToNew.BottomRightCoordinate = currentPoint;
}
// Create/Update teleport node in new node to existing node
let teleportZoneFromNewToExisting = nodeInstance.TeleportZones.getValue(existingNode.Key);
let pointInNewNode = new Point_1.Point(currentPoint.X, currentPoint.Y - 1);
if (teleportZoneFromNewToExisting == null) {
teleportZoneFromNewToExisting = new TeleportZone_1.TeleportZone(nodeInstance, TeleportZoneDirection_1.TeleportZoneDirection.South, pointInNewNode, pointInNewNode, nodeInstance.TeleportZones.size(), existingNode);
nodeInstance.TeleportZones.setValue(existingNode.Key, teleportZoneFromNewToExisting);
}
else {
teleportZoneFromNewToExisting.BottomRightCoordinate = currentPoint;
}
}
}
// check west points
let x = topLeftCoordinate.X - 1;
for (let y = topLeftCoordinate.Y; y < topLeftCoordinate.Y + nodeInstance.Height; y++) {
let currentPoint = new Point_1.Point(x, y);
let existingNode = this._nodes.getValue(currentPoint);
// found a node - set up teleport zone
if (existingNode != null) {
// Create/Update teleport node in existing node to new node
let teleportZoneFromExistingToNew = existingNode.TeleportZones.getValue(nodeInstance.Key);
if (teleportZoneFromExistingToNew == null) {
teleportZoneFromExistingToNew = new TeleportZone_1.TeleportZone(existingNode, TeleportZoneDirection_1.TeleportZoneDirection.East, currentPoint, currentPoint, existingNode.TeleportZones.size(), nodeInstance);
existingNode.TeleportZones.setValue(nodeInstance.Key, teleportZoneFromExistingToNew);
}
else {
teleportZoneFromExistingToNew.BottomRightCoordinate = currentPoint;
}
// Create/Update teleport node in new node to existing node
let teleportZoneFromNewToExisting = nodeInstance.TeleportZones.getValue(existingNode.Key);
let pointInNewNode = new Point_1.Point(currentPoint.X + 1, currentPoint.Y);
if (teleportZoneFromNewToExisting == null) {
teleportZoneFromNewToExisting = new TeleportZone_1.TeleportZone(nodeInstance, TeleportZoneDirection_1.TeleportZoneDirection.West, pointInNewNode, pointInNewNode, nodeInstance.TeleportZones.size(), existingNode);
nodeInstance.TeleportZones.setValue(existingNode.Key, teleportZoneFromNewToExisting);
}
else {
teleportZoneFromNewToExisting.BottomRightCoordinate = currentPoint;
}
}
}
// check east points
x = topLeftCoordinate.X + nodeInstance.Width + 1;
for (let y = topLeftCoordinate.Y; y < topLeftCoordinate.Y + nodeInstance.Height; y++) {
let currentPoint = new Point_1.Point(x, y);
let existingNode = this._nodes.getValue(currentPoint);
// found a node - set up teleport zone
if (existingNode != null) {
// Create/Update teleport node in existing node to new node
let teleportZoneFromExistingToNew = existingNode.TeleportZones.getValue(nodeInstance.Key);
if (teleportZoneFromExistingToNew == null) {
teleportZoneFromExistingToNew = new TeleportZone_1.TeleportZone(existingNode, TeleportZoneDirection_1.TeleportZoneDirection.West, currentPoint, currentPoint, existingNode.TeleportZones.size(), nodeInstance);
existingNode.TeleportZones.setValue(nodeInstance.Key, teleportZoneFromExistingToNew);
}
else {
teleportZoneFromExistingToNew.BottomRightCoordinate = currentPoint;
}
// Create/Update teleport node in new node to existing node
let teleportZoneFromNewToExisting = nodeInstance.TeleportZones.getValue(existingNode.Key);
let pointInNewNode = new Point_1.Point(currentPoint.X - 1, currentPoint.Y);
if (teleportZoneFromNewToExisting == null) {
teleportZoneFromNewToExisting = new TeleportZone_1.TeleportZone(nodeInstance, TeleportZoneDirection_1.TeleportZoneDirection.East, pointInNewNode, pointInNewNode, nodeInstance.TeleportZones.size(), existingNode);
nodeInstance.TeleportZones.setValue(existingNode.Key, teleportZoneFromNewToExisting);
}
else {
teleportZoneFromNewToExisting.BottomRightCoordinate = currentPoint;
}
}
}
//#endregion
this._nodes.setValue(topLeftCoordinate, nodeInstance);
this._minX = Math.min(this._minX, topLeftCoordinate.X);
this._minY = Math.min(this._minY, topLeftCoordinate.Y);
this._maxX = Math.max(this._maxX, topLeftCoordinate.X + nodeInstance.Width);
this._maxY = Math.max(this._maxY, topLeftCoordinate.Y + nodeInstance.Height);
}
get Width() {
return this._maxX - this._minX;
}
get Height() {
return this._maxY - this._minY;
}
get TopLeftCoordinate() {
return new Point_1.Point(this._minX, this._minY);
}
get BottomRightCoordinate() {
return new Point_1.Point(this._maxX, this._maxY);
}
}
exports.Grid = Grid;
//# sourceMappingURL=Grid.js.map
-10
View File
@@ -1,10 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//TODO: Make generic
async function asyncForEach(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
exports.asyncForEach = asyncForEach;
//# sourceMappingURL=Helpers.js.map
-3
View File
@@ -1,3 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=IApiSlaves.js.map
-3
View File
@@ -1,3 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=IClusterioMasterProxy.js.map
-27
View File
@@ -1,27 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log("started.");
const ClusterioMasterProxy_1 = require("./ClusterioMasterProxy");
const ClusterManager_1 = require("./ClusterManager");
const argv = require('minimist')(process.argv.slice(2));
let configPath = argv['configPath'];
if (configPath === undefined || configPath === null || configPath === "") {
throw new Error("configPath is null.");
}
;
let config = require(configPath);
let proxy = new ClusterioMasterProxy_1.ClusterioMasterProxy(config.masterIP, config.masterPort, config.masterAuthToken);
let manager = new ClusterManager_1.ClusterManager();
manager.GenerateBasicGridOfNodes(2, 2, 'GridLockTest_');
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// async function test() {
// await asyncForEach([1,2,3], async (v, i, a) => {
// console.log("test" + i);
// await sleep(5000);
// });
// }
// test();
proxy.CreateNodeInstancesOnLocalServer(manager.Grid.GetNodes());
//# sourceMappingURL=ModelController.js.map
-24
View File
@@ -1,24 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log("started.");
const ClusterioMasterProxy_1 = require("./ClusterioMasterProxy");
const ClusterManager_1 = require("./ClusterManager");
const argv = require('minimist')(process.argv.slice(2));
let configPath = argv['configPath'];
if (configPath === undefined || configPath === null || configPath === "") {
throw new Error("configPath is null.");
}
;
let config = require(configPath);
let proxy = new ClusterioMasterProxy_1.ClusterioMasterProxy(config.masterIP, config.masterPort, config.masterAuthToken);
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
// main
(async () => {
let manager = new ClusterManager_1.ClusterManager();
manager.GenerateBasicGridOfNodes(2, 2, 'GridLockTest');
await proxy.CreateNodeInstancesOnLocalServer(manager.Grid.GetNodes());
return;
})();
//# sourceMappingURL=ModelDemo.js.map
-61
View File
@@ -1,61 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
console.log("Started.");
const ClusterioMasterProxy_1 = require("./ClusterioMasterProxy");
const ClusterManager_1 = require("./ClusterManager");
const argv = require('minimist')(process.argv.slice(2));
// arg: configPath
// description: should point to the clusterio config.json file.
let configPath = argv['configPath'];
if (configPath === undefined || configPath === null || configPath === "") {
configPath = "../../config.json";
}
;
let config = require(configPath);
// arg: mode
// description: should be GenerateBasicGridOfNodes for now. Can extend in future.
let mode = argv['mode'];
if (mode === undefined || mode === null || mode === "") {
throw new Error("mode is null. mode must be 'GenerateBasicGridOfNodes' (for now).");
}
;
// arg: serviceHostType
// description: what program to use to host the client.js instances.
let serviceHostType = argv['serviceHostType'];
if (serviceHostType === undefined || serviceHostType === null || serviceHostType === "") {
throw new Error("serviceHostType is null. serviceHostType must be either 'pm2' or 'screen'.");
}
;
if (serviceHostType != "pm2" && serviceHostType != "screen") {
throw new Error("serviceHostType must be either 'pm2' or 'screen'.");
}
// arg: width
// description: width of node grid
let width = argv['width'];
if (width === undefined || width === null || width === "") {
throw new Error("width is null. width must be an int > 0.");
}
;
// arg: height
// description: height of node grid
let height = argv['height'];
if (height === undefined || height === null || height === "") {
throw new Error("height is null. height must be an int > 0.");
}
;
// arg: namePrefix
// description: namePrefix of node grid
let namePrefix = argv['namePrefix'];
if (namePrefix === undefined || namePrefix === null || namePrefix === "") {
throw new Error("namePrefix is null.");
}
;
let proxy = new ClusterioMasterProxy_1.ClusterioMasterProxy(config.masterIP, config.masterPort, config.masterAuthToken, serviceHostType);
// main
(async () => {
let manager = new ClusterManager_1.ClusterManager();
manager.GenerateBasicGridOfNodes(width, height, namePrefix);
await proxy.CreateNodeInstancesOnLocalServer(manager.Grid.GetNodes());
return;
})();
//# sourceMappingURL=ModelRunner.js.map
-29
View File
@@ -1,29 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Point_1 = require("./Point");
const typescript_collections_1 = require("typescript-collections");
/**
* An instance of a Factorio server. Multiple nodes can run on the same server.
*/
class NodeInstance {
constructor(name, topLeftCoordinate, width, height) {
this.TeleportZones = new typescript_collections_1.Dictionary();
this.Name = name;
this.Width = width;
this.Height = height;
this.TopLeftCoordinate = topLeftCoordinate;
}
get BottomRightCoordinate() {
return new Point_1.Point(this.TopLeftCoordinate.X + this.Width, this.TopLeftCoordinate.Y + this.Height);
}
// public AddTeleportZone(name: string, topLeftCoordinate: Point, bottomRightCoordinate: Point) {
// this.TeleportZones.push(new TeleportZone(this, topLeftCoordinate, bottomRightCoordinate, name));
// }
AddTeleportZonePoint() {
}
get Key() {
return this.Name;
}
}
exports.NodeInstance = NodeInstance;
//# sourceMappingURL=NodeInstance.js.map
-57
View File
@@ -1,57 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class Point {
/**
* Constructs a Point. Will not accept NaNs.
* @param x A number. Cannot be NaN.
* @param y A number. Cannot be NaN.
*/
constructor(x, y) {
this.toString = () => {
return "(" + this.X + "," + this.Y + ")";
};
if (Number.isNaN(x)) {
throw new Error("x is NaN. Will not construct Point.");
}
if (Number.isNaN(y)) {
throw new Error("y is NaN. Will not construct Point.");
}
this.X = x;
this.Y = y;
}
/**
* Constructs a Point from a string representation in the format (x,y). Will not accept NaNs.
* @param vectorAsString A string in the format (x,y). Will not accept NaNs.
*/
static FromString(vectorAsString) {
this.CheckChar('(', vectorAsString[0], vectorAsString);
this.CheckChar(')', vectorAsString[vectorAsString.length - 1], vectorAsString);
if (vectorAsString.indexOf(',') === -1) {
throw new Error("vectorAsString is an invalid format. Cannot find ','. fullString: " + vectorAsString);
}
let splitStr = vectorAsString.substr(1, vectorAsString.length - 2).split(',');
if (splitStr.length != 2) {
throw new Error("vectorAsString is an invalid format. Expecting string in format (x,y). fullString: " + vectorAsString);
}
let x = parseInt(splitStr[0]);
let y = parseInt(splitStr[1]);
let v = new Point(x, y);
return v;
}
static CheckChar(expectedChar, actualChar, fullString) {
if (actualChar != expectedChar) {
throw new Error("vectorAsString is an invalid format. Expecting: '" + expectedChar + "'. Actual: '" + actualChar + "'. fullString: " + fullString);
}
}
equals(obj) {
return this.X === obj.X && this.Y === obj.Y;
}
Add(p) {
return new Point(this.X + p.X, this.Y + p.Y);
}
Subtract(p) {
return new Point(this.X - p.X, this.Y - p.Y);
}
}
exports.Point = Point;
//# sourceMappingURL=Point.js.map
-16
View File
@@ -1,16 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* A server. Can contain multiple nodes (Factorio server instances).
*/
class Server {
constructor(name) {
this.Nodes = new Array();
this.Name = name;
}
AddNodeInstance(node) {
this.Nodes.push(node);
}
}
exports.Server = Server;
//# sourceMappingURL=Server.js.map
-8
View File
@@ -1,8 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var ServiceHostType;
(function (ServiceHostType) {
ServiceHostType[ServiceHostType["pm2"] = 0] = "pm2";
ServiceHostType[ServiceHostType["screen"] = 1] = "screen";
})(ServiceHostType = exports.ServiceHostType || (exports.ServiceHostType = {}));
//# sourceMappingURL=ServiceHostType.js.map
-64
View File
@@ -1,64 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Point_1 = require("./Point");
const TeleportZoneDirection_1 = require("./TeleportZoneDirection");
const Config = require("./Config");
class TeleportZone {
constructor(parentNode, direction, topLeftCoordinate, bottomRightCoordinate, index, targetNode) {
this.TopLeftCoordinate = topLeftCoordinate;
this.BottomRightCoordinate = bottomRightCoordinate;
this.ParentNode = parentNode;
this.Direction = direction;
this.Index = index;
this.TargetNode = targetNode;
}
get Name() {
return TeleportZoneDirection_1.TeleportZoneDirection[this.Direction] + " to " + this.TargetNode.Name;
}
// public AddRestriction(restrictingTeleportZone: TeleportZone) {
// this.Restrictions.push(new TeleportZoneRestriction(this, restrictingTeleportZone));
// }
/**
* Converts this teleport zone into Factorio map co-ordinates
* @returns [name, topleft.x, topleft.y, width, height, zoneIndex]
*/
GenerateTeleportZoneInFactorioCoordinates() {
// get where this zone is relative to the node itself
let topLeftRelativeCoordinate = this.TopLeftCoordinate.Subtract(this.ParentNode.TopLeftCoordinate);
// a grid coordinate point is equivalent to a 1x1 node, so bottomright = topleft.
// add (1,1) to translate from area representation to coordinates representing actual points.
let bottomRightRelativeCoordinate = this.BottomRightCoordinate
.Subtract(this.ParentNode.TopLeftCoordinate)
.Add(new Point_1.Point(1, 1));
// Factorio map is e.g. (-500,-500) to (500, 500) for a 1000x1000 node.
// We therefore need to offset our co-ordinates so that (0,0) is the center and not (width/2, height/2)
let m = Config.GridToFactorioCoordinateSystemMultiplier;
let factorioTeleportZoneTopLeft = new Point_1.Point(topLeftRelativeCoordinate.X * m - m / 2, topLeftRelativeCoordinate.Y * m - m / 2);
let factorioTeleportZoneBottomRight = new Point_1.Point(bottomRightRelativeCoordinate.X * m - m / 2, bottomRightRelativeCoordinate.Y * m - m / 2);
let zoneWidth = factorioTeleportZoneBottomRight.X - factorioTeleportZoneTopLeft.X;
let zoneHeight = factorioTeleportZoneBottomRight.Y - factorioTeleportZoneTopLeft.Y;
let tl = factorioTeleportZoneTopLeft;
let br = factorioTeleportZoneBottomRight;
let zw = Config.TeleportZoneWidth;
switch (this.Direction) {
case TeleportZoneDirection_1.TeleportZoneDirection.North:
return [this.Name, tl.X, tl.Y, zoneWidth, zw, this.Index + 1];
case TeleportZoneDirection_1.TeleportZoneDirection.West:
return [this.Name, tl.X, tl.Y, zw, zoneHeight, this.Index + 1];
case TeleportZoneDirection_1.TeleportZoneDirection.South:
return [this.Name, tl.X, br.Y - zw, zoneWidth, zw, this.Index + 1];
case TeleportZoneDirection_1.TeleportZoneDirection.East:
return [this.Name, br.X - zw, tl.Y, zw, zoneHeight, this.Index + 1];
}
}
/**
* Generates a zone restriction from this teleport zone to target zone
* @returns [zoneIndex, targetNodeName, targetZoneName]
*/
GenerateZoneRestriction() {
let targetZone = this.TargetNode.TeleportZones.getValue(this.ParentNode.Key);
return [this.Index + 1, this.TargetNode.Name, targetZone.Name];
}
}
exports.TeleportZone = TeleportZone;
//# sourceMappingURL=TeleportZone.js.map
-10
View File
@@ -1,10 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var TeleportZoneDirection;
(function (TeleportZoneDirection) {
TeleportZoneDirection[TeleportZoneDirection["North"] = 0] = "North";
TeleportZoneDirection[TeleportZoneDirection["East"] = 1] = "East";
TeleportZoneDirection[TeleportZoneDirection["South"] = 2] = "South";
TeleportZoneDirection[TeleportZoneDirection["West"] = 3] = "West";
})(TeleportZoneDirection = exports.TeleportZoneDirection || (exports.TeleportZoneDirection = {}));
//# sourceMappingURL=TeleportZoneDirection.js.map
-10
View File
@@ -1,10 +0,0 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class TeleportZoneRestriction {
constructor(parentTeleportZone, restrictingTeleportZone) {
this.ParentTeleportZone = parentTeleportZone;
this.RestrictingTeleportZone = restrictingTeleportZone;
}
}
exports.TeleportZoneRestriction = TeleportZoneRestriction;
//# sourceMappingURL=TeleportZoneRestriction.js.map
-13
View File
@@ -1,13 +0,0 @@
"use strict";
function __export(m) {
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
}
Object.defineProperty(exports, "__esModule", { value: true });
__export(require("./ClusterManager"));
__export(require("./NodeInstance"));
__export(require("./Server"));
__export(require("./TeleportZone"));
__export(require("./TeleportZoneRestriction"));
__export(require("./Point"));
__export(require("./Grid"));
//# sourceMappingURL=index.js.map
+36 -37
View File
@@ -33,14 +33,12 @@ function sigmoid3(x)
//carry - carry for item X slave Y
//prev_req - Numreq for previous request for item X slave Y
//* doleinfo_last - Object with 2 values for last "tick period": numreq - total number or requests for item, numslave - number of slaves requesting that item
//debt - debt for item X slave Y
//Returns: Array of 3 elements
//0.Number of items to give in that dose
//1.New dole for item X
//2.New carry for item X slave Y
//3.New debt
function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_adj,debt)
function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_adj)
{
numreq=Number(numreq);
instore=Number(instore);
@@ -49,37 +47,32 @@ function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_ad
var b=Math.trunc(prev_req/instore_adj*numreq);
var inp1=[numreq,instore_adj,store_last_tick/numreq,dole,carry];//0..4
var inp2=[prev_req/numreq,numreq/numreq_total_adj,store_last_tick/numreq_total_adj,prev_req/instore_adj,b,numreq_total_adj,store_last_tick,debt];//5..12
var inp2=[prev_req/numreq,numreq/numreq_total_adj,store_last_tick/numreq_total_adj,prev_req/instore_adj,b];//5..9
var inputs=inp1.concat(inp2);
var internal=new Array(20).fill(0);
var outputs=[0,0,0,0];
var internal=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
var outputs=[0,0,0];
//NN: Please don't touch :), code is generated
outputs[0]=inputs[7]*0.8058582544;
outputs[0]=sigmoid2(outputs[0]);
outputs[0]=inputs[2]*3.101320028;
outputs[0]=outputs[0]+inputs[7]*0.5471593142;
outputs[0]=outputs[0]+inputs[3]*2.691268921;
outputs[0]=outputs[0]+outputs[1]*-1.64255929;
outputs[0]=outputs[0]+inputs[6]*0.7746122479;
outputs[0]=sigmoid0(outputs[0]);
outputs[1]=inputs[2]*-0.9946503043;
outputs[1]=outputs[1]+inputs[1]*0.2128598392;
outputs[1]=sigmoid0(outputs[1]);
outputs[2]=sigmoid0(outputs[2]);
outputs[3]=inputs[6]*0.2294657379;
outputs[3]=sigmoid0(outputs[3]);
outputs[2]=sigmoid2(outputs[2]);
debt=debt || 0;
if (outputs[0]>0.99) outputs[0]=1.0;
var res1=Number(outputs[0])*numreq+outputs[3]+debt;
var res_capped=Math.round(res1);
if (res_capped>numreq) res_capped=numreq;
if (res_capped>instore) res_capped=instore;
if (res_capped<0) res_capped=0;
res1=res1 || 0;
res_capped=res_capped || 0;//Safeguard against wrong values
debt=res1-res_capped;
debt_cap=Math.round(numreq*1.5)+5;
if (debt>debt_cap) debt=debt_cap;
carry=sigmoid0(outputs[2]*carry+outputs[0]) || 0;
return [res_capped,dole+outputs[1],carry,debt];
outputs[0]=Number(outputs[0]);
if (outputs[0]<0) outputs[0]=0;
outputs[0]=outputs[0]*numreq;
var res1=Math.round(outputs[0]);
if (res1>numreq) res1=numreq;
if (res1>instore) res1=instore;
if (res1<0) res1=0;
carry=sigmoid0(outputs[2]);
return [res1,sigmoid0(outputs[1]),carry];
}
//TICK - Updates values for item X, should be called once for each item for each round of all slaves requesting that item
@@ -97,16 +90,22 @@ function Tick(instore,dole,store_last_tick,numreq_total_adj)
{
var instore_adj=instore;
if (instore_adj==0) instore_adj=0.1;
var inputs=[instore,dole,store_last_tick/instore_adj,store_last_tick/numreq_total_adj,store_last_tick,numreq_total_adj];
var internal=new Array(10).fill(0);
var inputs=[instore,dole,store_last_tick/instore_adj,store_last_tick/numreq_total_adj];
var internal=[0,0,0,0,0];
var outputs=[0,0];
//NN: Please don't touch :), code is generated
outputs[0]=inputs[3]*0.9364774823;
outputs[0]=outputs[0]*1.0*0.4590721726;
outputs[0]=sigmoid2(outputs[0]);
outputs[1]=outputs[0]*1.828422785;
outputs[1]=sigmoid3(outputs[1]);
internal[2]=sigmoid0(internal[2]);
outputs[0]=internal[2]*0.4049594998;
outputs[0]=outputs[0]+1.0*-5.026565075;
outputs[0]=outputs[0]+inputs[0]*0.9100194573;
outputs[0]=outputs[0]+inputs[1]*-1.063795686;
outputs[0]=sigmoid1(outputs[0]);
outputs[1]=outputs[0]*1.049500823;
outputs[1]=outputs[1]*inputs[0]*3.437163115;
outputs[1]=sigmoid0(outputs[1]);
avg=outputs[1];
if (numreq_total_adj<0.11) avg=1;//When no item's are requested, result is 0.1 ; In that case there is no deficit, so return 1 = 100% demand fulfilled
@@ -116,4 +115,4 @@ outputs[1]=sigmoid3(outputs[1]);
return [sigmoid0(outputs[0]),avg];
}
module.exports = {Dose,Tick}
module.exports = {Dose,Tick}
+63 -88
View File
@@ -235,7 +235,7 @@ const db = {};
(function(){
try{
let x = fs.statSync(path.resolve(config.databaseDirectory, "slaves.json"));
let x = fs.statSync(path.resolve(config.databaseDirectory, "/slaves.json"));
console.log(`loading slaves from path.resolve(config.databaseDirectory, "slaves.json")`);
slaves = JSON.parse(fs.readFileSync(path.resolve(config.databaseDirectory, "slaves.json")));
} catch (e){
@@ -352,18 +352,16 @@ app.post("/api/editSlaveMeta", authenticate.middleware, function(req,res) {
// request.body should be an object
// {instanceID, pass, meta:{x,y,z}}
if(req.body && req.body.instanceID && req.body.meta){
if(slaves[req.body.instanceID]){
if(!slaves[req.body.instanceID].meta) {
if(req.body && req.body.instanceID && req.body.password && req.body.meta){
// check for editing permissions
if(slaves[req.body.instanceID] && slaves[req.body.instanceID].rconPassword == req.body.password){
if(!slaves[req.body.instanceID].meta){
slaves[req.body.instanceID].meta = {};
}
slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true});
let metaPortion = JSON.stringify(req.body.meta);
if(metaPortion.length > 50) {
metaPortion = metaPortion.substring(0,20) + "...";
}
if(metaPortion.length > 50) metaPortion = metaPortion.substring(0,20) + "...";
// console.log("Updating slave "+slaves[req.body.instanceID].instanceName+": " + slaves[req.body.instanceID].mac + " : " + slaves[req.body.instanceID].serverPort+" at " + slaves[req.body.instanceID].publicIP, metaPortion);
res.sendStatus(200);
} else {
res.send("ERROR: Invalid instanceID or password")
}
@@ -379,20 +377,12 @@ POST Get metadata from a single slave. For whenever you find the data returned b
app.post("/api/getSlaveMeta", function (req, res) {
endpointHitCounter.labels(req.route.path).inc();
console.log("body", req.body);
if(req.body && req.body.instanceID){
if (slaves[req.body.instanceID]) {
console.log("returning meta for ", req.body.instanceID);
if(!slaves[req.body.instanceID].meta) {
slaves[req.body.instanceID].meta = {};
}
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
} else {
res.status(404)
res.send('{"status": 404, "info": "Slave not registered"}')
}
if(req.body && req.body.instanceID && req.body.password){
console.log("returning meta for ", req.body.instanceID);
res.send(JSON.stringify(slaves[req.body.instanceID].meta))
} else {
res.status(400);
res.send('{"INVALID_REQUEST":1}');
res.status(400);
res.send('{"INVALID REQUEST":1}');
}
});
// mod management
@@ -403,7 +393,7 @@ POST Check if a mod has been uploaded to the master before. Only checks against
@instance
@alias /api/checkMod
*/
app.post("/api/checkMod", authenticate.middleware, function(req,res) {
app.post("/api/checkMod", function(req,res) {
endpointHitCounter.labels(req.route.path).inc();
let files = fs.readdirSync(masterModFolder);
let found = false;
@@ -444,59 +434,37 @@ app.post("/api/uploadMod", authenticate.middleware, function(req,res) {
}
});
/**
Prepare the slaveCache for both /api/slaves calls
*/
let slaveCache = {
timestamp: Date.now(),
};
function getSlaves() {
if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000) {
let copyOfSlaves = JSON.parse(JSON.stringify(slaves));
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
}
return slaveCache;
}
/**
GET endpoint for getting information about all our slaves
@memberof clusterioMaster
@instance
@alias /api/slaves
*/
let slaveCache = {
timestamp: Date.now(),
};
app.get("/api/slaves", function(req, res) {
endpointHitCounter.labels(req.route.path).inc();
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
let slaveCache = getSlaves();
for(key in slaveCache.cache) {
slaveCache.cache[key].rconPassword = "hidden";
if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000){
let copyOfSlaves = JSON.parse(JSON.stringify(slaves));
// filter out the rcon password because thats kindof not a safe thing to share
for(key in copyOfSlaves){
copyOfSlaves[key].rconPassword = "hidden";
}
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
}
res.send(slaveCache.cache);
});
/**
POST endpoint for getting the rconPasswords of all our slaves
@memberof clusterioMaster
@instance
@alias /api/rconPasswords
*/
app.post("/api/rconPasswords", authenticate.middleware, function(req, res) {
endpointHitCounter.labels(req.route.path).inc();
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
let slaveCache = getSlaves();
let validKeys = ['rconPort', 'rconPassword', 'publicIP'];
for(key in slaveCache.cache) {
slaveCache.cache[key] = {'publicIP': slaveCache.cache[key].publicIP, 'rconPort': slaveCache.cache[key].rconPort, 'rconPassword': slaveCache.cache[key].rconPassword};
}
res.send(slaveCache.cache);
});
var recievedItemStatisticsBySlaveID = {};
var sentItemStatisticsBySlaveID = {};
/*
var recievedItemStatistics = new averagedTimeSeries({
maxEntries: config.itemStats.maxEntries,
entriesPerSecond: config.itemStats.entriesPerSecond,
}, console.log);*/
var recievedItemStatisticsBySlaveID = {};
var sentItemStatisticsBySlaveID = {};
//
/**
POST endpoint for storing items in master's inventory.
@@ -729,38 +697,45 @@ Requires x-access-token header to be set. Find you api token in secret-api-token
@param {object} JSON {instanceID:19412312, broadcast:false, command:"/c game.print('hello')"}
@returns {object} Status {auth: bool, message: "Informative error", data:{}}
*/
app.post("/api/runCommand", authenticate.middleware, function(req,res) {
app.post("/api/runCommand", (req,res) => {
endpointHitCounter.labels(req.route.path).inc();
// validate request.body
let body = req.body;
var token = req.headers['x-access-token'];
if(!token) return res.status(401).send({ auth: false, message: 'No token provided.' });
jwt.verify(token, config.masterAuthSecret, function(err, decoded) {
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' });
// validate request.body
let body = req.body;
if (
body.broadcast
&& body.command
&& typeof body.command == "string"
&& body.command[0] == "/")
{
let instanceResponses = [];
for (let instanceID in wsSlaves) {
// skip loop if the property is from prototype
if (!wsSlaves.hasOwnProperty(instanceID)) continue;
wsSlaves[instanceID].runCommand(body.command);
if (
body.broadcast
&& body.command
&& typeof body.command == "string"
&& body.command[0] == "/")
{
let instanceResponses = [];
for (let instanceID in wsSlaves) {
// skip loop if the property is from prototype
if (!wsSlaves.hasOwnProperty(instanceID)) continue;
wsSlaves[instanceID].runCommand(body.command);
}
res.status(200).send({auth: true, message: "success", response: "Cluster wide messaging initiated"});
} else if (
body.instanceID
&& wsSlaves[body.instanceID]
&& body.command
&& typeof body.command == "string"
&& body.command[0] == "/")
{
// execute command
wsSlaves[body.instanceID].runCommand(body.command, data => {
res.status(200).send({auth: true, message: "success", data});
});
} else {
res.status(400).send({auth: true, message: "Error: invalid request.body"});
}
res.status(200).send({auth: true, message: "success", response: "Cluster wide messaging initiated"});
} else if (
body.instanceID
&& wsSlaves[body.instanceID]
&& body.command
&& typeof body.command == "string"
&& body.command[0] == "/")
{
// execute command
wsSlaves[body.instanceID].runCommand(body.command, data => {
res.status(200).send({auth: true, message: "success", data});
});
} else {
res.status(400).send({auth: true, message: "Error: invalid request.body"});
}
});
});
/**
GET endpoint. Returns factorio's base locale as a JSON object.
@@ -794,6 +769,7 @@ if(args.masterPort || config.masterPort){
server.listen(args.masterPort || config.masterPort || 8080, function () {
console.log("Listening on port %s...", server.address().port);
});
module.exports = server;
}
/* Websockets for remoteMap */
@@ -1024,5 +1000,4 @@ async function getPlugins(){
return plugins;
}
module.exports = app;
})();
-11
View File
@@ -76,17 +76,6 @@ describe('Master server endpoint testing', function() {
});
});
});
it("sends some HTML when accessing /remoteMap",function(done){
this.timeout(6000);
request(app).get("/remoteMap").end(function(err,res){
assert.equal(res.statusCode, 200);
validateHTML(res.text).then(result => {
// there should be 1 error regarding complaining about me using ES6 modules before they are fully supported
assert(result.messages.length === 2, "there are HTML errors on the page, please fix: "+JSON.stringify(result.messages));
done();
});
});
});
});
let persistentMaster = request(app);
describe("#POST /api/place", function(){
BIN
View File
Binary file not shown.
+3 -5
View File
@@ -30,7 +30,6 @@
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.2",
"cache-require-paths": "^0.3.0",
"cli": "^1.0.1",
"cli-interact": "^0.1.9",
"codecov": "^3.0.1",
"compression": "^1.7.2",
@@ -57,20 +56,19 @@
"node-factorio-api": "^0.3.1-alpha",
"node-forge": "^0.7.1",
"prom-client": "^10.2.3",
"rcon-client": "git+https://gitlab.com/Hornwitser/rcon-client.git",
"rcon-client": "^3.0.2",
"request": "^2.83.0",
"right-pad": "^1.0.1",
"rmdir-sync": "^1.0.1",
"sanitizer": "^0.1.3",
"simple-git": "^1.96.0",
"single-line-log": "^1.1.2",
"snyk": "^1.134.2",
"socket.io": "^2.0.4",
"socket.io-client": "^2.0.4",
"socket.io-prometheus": "^0.2.1",
"sync-request": "^4.0.1",
"typescript-collections": "^1.3.2",
"underscore": "^1.8.3"
"underscore": "^1.8.3",
"snyk": "^1.134.2"
},
"devDependencies": {
"documentation": "^5.4.0",
-3
View File
@@ -11,7 +11,4 @@ module.exports = function(app) {
app.get("/nodeDetails", function(req, res) {
res.render("nodeDetails");
});
app.get("/remoteMap", function(req, res) {
res.render("remoteMap");
});
};
+2 -5
View File
@@ -33,7 +33,6 @@ class neuralDole {
this.carry = {}
this.lastRequest = {}
this.stats=[]
this.debt= {}
setInterval(()=>{
for(let name in this.items){
@@ -72,8 +71,7 @@ class neuralDole {
this.dole[object.name],
this.carry[object.name+" "+object.instanceID] || 0,
this.lastRequest[object.name+"_"+object.instanceID+"_"+object.instanceName] || 0,
this.getRequestStats(object.name,5),
this.debt[object.name+" "+object.instanceID] || 0
this.getRequestStats(object.name,5)
)
if ((this.stats[object.name] || []).length>0)
{
@@ -86,7 +84,6 @@ class neuralDole {
//2.New carry for item X slave Y
this.dole[object.name] = magicData[1]
this.carry[object.name+" "+object.instanceID] = magicData[2]
this.debt[object.name+" "+object.instanceID] = magicData[3]
// Remove item from DB and send it
if(this.items.removeItem({count: magicData[0], name: object.name})){
@@ -180,4 +177,4 @@ function doleDivider({
module.exports = {
doleDivider,
neuralDole
}
}
-149
View File
@@ -1,149 +0,0 @@
--[[
HUGE SHOUTOUT TO HORNWITSER FOR WRITING THIS SEXY AS MODLOADER <3
and for not getting mad when i coppyed it :)
]]
local modules = {
require("pdnc"),
require("doomsday"),
require("gui"),
}
script.on_init(function()
for i,mod in ipairs(modules) do
if mod.on_init then
mod.on_init()
end
end
end)
script.on_load(function()
for i,mod in ipairs(modules) do
if mod.on_load then
mod.on_load()
end
end
end)
script.on_configuration_changed(function(data)
for i,mod in ipairs(modules) do
if mod.on_configuration_changed then
mod.on_configuration_changed(data)
end
end
end)
local function register_tick_handlers()
local ticks = {}
for i,mod in ipairs(modules) do
print(mod.on_nth_ticks)
if mod.on_nth_ticks then
mod_ticks = mod.on_nth_ticks
for t,f in pairs(mod_ticks) do
if not ticks[t] then
ticks[t] = {}
end
table.insert(ticks[t], f)
end
end
end
for t,fs in pairs(ticks) do
script.on_nth_tick(t, function(event_data)
for i,f in ipairs(fs) do
f(event_data)
end
end)
end
end
register_tick_handlers()
local function register_events()
local events = {}
for i,mod in ipairs(modules) do
if mod.get_events then
mod_events = mod.get_events()
for e,f in pairs(mod_events) do
if not events[e] then
events[e] = {}
end
table.insert(events[e], f)
end
end
end
for e,fs in pairs(events) do
script.on_event(e, function(event_data)
for i,f in ipairs(fs) do
f(event_data)
end
end)
end
end
register_events()
for i,mod in ipairs(modules) do
if mod.add_remote_interfaces then
mod.add_remote_interfaces()
end
end
for i,mod in ipairs(modules) do
if mod.add_commands then
mod.add_commands()
end
end
--[[
-- This is a example of what to put at the end of your code, ie doomsday.lua
-- replace all the EXAMPLE 's with any name you want, they have to be all the same
-- replace tick with number if ticks and function with the function name to call
-- everywhere there is a blank space is where you put stuff in
local EXAMPLE_init = {}
local script_events = {
--place the here what you would normaly use Event.register for
-- Event.register(defines.events.on_player_created, testfunction)
-- is the same as
-- [defines.events.on_player_created] = testfunction,
-- where testfunction is | local function testfunction() { }
--[Event] = function,
--put stuff here
}
EXAMPLE_init.on_nth_ticks = {
--place the here what you would normaly use
--[tick] = function,
--put stuff here
}
EXAMPLE_init.on_init = function() -- this runs when Event.core_events.init
log("EXAMPLE init")
--put stuff here
global.EXAMPLE_data = global.EXAMPLE_data or script_data -- NO TOUCHY
end
EXAMPLE_init.on_load = function() -- this runs when Event.core_events.load
log("EXAMPLE load")
--put stuff here
script_data = global.EXAMPLE_data or script_data -- NO TOUCHY
end
EXAMPLE_init.get_events = function()
return script_events
end
return EXAMPLE_init
]]
-251
View File
@@ -1,251 +0,0 @@
-- doomsday module. Requires the programmable day-night cycle (pdnc) module to work.
-- made by Zr4g0n
-- this module currently has issues with the event module and 'on nth tick'.
require("pdnc") --is this the best way to do this?
global.doomsday_enabled = true
global.doomsday = global.doomsday or {} -- used to check if this exists.
global.doomsday_start = 8.75 -- in ingame days. Use n.75 to make sure doomsday is at midnight.
global.doomsday_pollution = 20 -- amount to be applied per tick
global.doomsday_surface = 1
global.doomsday_enable_players_online_compensator = false
global.doomsday_current_fuzzy_playercount = 1.5 -- start assuming 1.5 players! :V
global.doomsday_use_early_death = false
global.doomsday_early_death_has_happened = false
global.doomsday_use_different_spawn = false
global.doomsday_different_spawn = {x = -2000, y = -500}
global.doomsday_has_happened = false
function doomsday_status()
stats = {
"Time Left: " .. doomsday_time_left(),
"global.doomsday_start: " .. global.doomsday_start,
"global.doomsday_pollution: " .. global.doomsday_pollution,
"global.doomsday_surface: " .. global.doomsday_surface,
"global.doomsday_enabled: " .. pdnc_bool_to_string(global.doomsday_enabled),
"global.doomsday_enable_players_online_compensator: " .. pdnc_bool_to_string(global.doomsday_enable_players_online_compensator),
"global.doomsday_current_fuzzy_playercount: " .. global.doomsday_current_fuzzy_playercount,
"global.doomsday_use_early_death: " .. pdnc_bool_to_string(global.doomsday_use_early_death),
"global.doomsday_early_death_has_happened: " .. pdnc_bool_to_string(global.doomsday_early_death_has_happened),
"global.doomsday_use_different_spawn: " .. pdnc_bool_to_string(global.doomsday_use_different_spawn),
"global.doomsday_different_spawn: x = " .. global.doomsday_different_spawn.x .. ", y = " .. global.doomsday_different_spawn.y,
"doomsday_has_happened: " .. pdnc_bool_to_string(global.doomsday_has_happened),
}
return stats
end
function doomsday_console_status()
--logs to console, used for debuging
log("Time Left: " .. doomsday_time_left())
log("global.doomsday_start: " .. global.doomsday_start)
log("global.doomsday_pollution: " .. global.doomsday_pollution)
log("global.doomsday_surface: " .. global.doomsday_surface)
log("global.doomsday_enabled: " .. pdnc_bool_to_string(global.doomsday_enabled))
log("global.doomsday_enable_players_online_compensator: " .. pdnc_bool_to_string(global.doomsday_enable_players_online_compensator))
log("global.doomsday_current_fuzzy_playercount: " .. global.doomsday_current_fuzzy_playercount)
log("global.doomsday_use_early_death: " .. pdnc_bool_to_string(global.doomsday_use_early_death))
log("global.doomsday_early_death_has_happened: " .. pdnc_bool_to_string(global.doomsday_early_death_has_happened))
log("global.doomsday_use_different_spawn: " .. pdnc_bool_to_string(global.doomsday_use_different_spawn))
log("global.doomsday_different_spawn: x = " .. global.doomsday_different_spawn.x .. ", y = " .. global.doomsday_different_spawn.y)
log("doomsday_has_happened: " .. pdnc_bool_to_string(global.doomsday_has_happened))
return stats
end
function doomsday_toggle()
global.doomsday_enabled = not global.doomsday_enabled
doomsday_status()
end
function doomsday_setup()
if global.doomsday_use_different_spawn then
doomsday_activate_different_spawn()
end
end
function doomsday_early_death()
local radius = 512
local pollution = 2500
doomsday_pollute(radius*0.66,pollution,12)
doomsday_pollute(radius*1.00,pollution,24)
doomsday_pollute(radius*1.50,pollution,36)
global.doomsday_early_death_has_happened = true
end
function doomsday_activate_different_spawn()
game.forces["player"].set_spawn_position(global.doomsday_different_spawn, game.surfaces[global.doomsday_surface])
end
function doomsday_core()
local current_time = game.tick / game.surfaces[global.doomsday_surface].ticks_per_day
local x = current_time * 6.2831853 --2pi
local returnvalue = 0
if(global.doomsday_enable_players_online_compensator)then
doomsday_players_online_compensator()
end
if global.doomsday_use_early_death
and (current_time > 1)
and not global.doomsday_early_death_has_happened then
doomsday_early_death()
end
local x = current_time * 6.2831853 --2pi
local returnvalue = 0
if (current_time < global.doomsday_start) then
returnvalue = math.pow(pdnc_c_boxy(x), (1 + current_time / 4))
-- days become darker over time towards n^6.125
elseif (current_time < global.doomsday_start + 1) then
returnvalue = doomsday_pollution_zero_hour(current_time)
if not global.doomsday_has_happened then
global.doomsday_has_happened = true
log("Doomsday activated at tick: " .. game.tick)
end
else
global.pdnc_enable_brightness_limit = true
returnvalue = math.pow(pdnc_c_boxy(x), 6.125)--*0.5
end
return pdnc_scaler(returnvalue)
end
function doomsday_pollution_zero_hour(current_time)
local radius = 128 --make global
local pollution = global.doomsday_pollution -- total pollution applied per tick
local nodes = 5 -- the number of nodes to spread
doomsday_pollute(radius*0.66,pollution,nodes*0.66)
doomsday_pollute(radius*1.00,pollution,nodes*1.00)
doomsday_pollute(radius*1.50,pollution,nodes*1.50)
return math.pow(((global.doomsday_start + 1) - current_time), 7)
end
function doomsday_normal_curve(x)
return (1+ ((math.sin(x) + (0.111 * math.sin(3 * x))) * 1.124859392575928))/2
-- magic numbers to make it scale to (-1, 1)
end
function doomsday_pollute(radius,pollution,nodes) -- spawn a ring of pollution based on radius, amount of pollution and number of points in the ring.
local p = global.pdnc_stepsize * pollution -- needed to make it 'step size independant'
p = p / nodes
local position = {x = 0.0, y = 0.0}
if global.doomsday_use_different_spawn then
position = global.doomsday_different_spawn
end
--game.surfaces[global.doomsday_surface].pollute(position, p) --circle + center point
local step = (math.pi * 2) / (nodes - 1)
for i=0, (nodes - 1) do
position = {x = math.sin(step*i)*radius, y = math.cos(step*i)*radius}
game.surfaces[global.doomsday_surface].pollute(position, p)
end
end
function doomsday_time_left()
if (global.doomsday_start > 0)then
local ticks_until_doomsday = game.surfaces[global.doomsday_surface].ticks_per_day * global.doomsday_start
local ticks = ticks_until_doomsday - game.tick
if (ticks >= 0) then
local seconds = math.floor(ticks/ 60)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
return(string.format("-" .. "%d:%02d:%02d:%02d", hours, minutes % 60, seconds % 60, ticks % 60))
else
ticks = ticks * -1
local seconds = math.floor(ticks / 60)
local minutes = math.floor(seconds / 60)
local hours = math.floor(minutes / 60)
local days = math.floor(hours / 24)
return(string.format("%d:%02d:%02d:%02d", hours, minutes % 60, seconds % 60, ticks % 60) .. " ago...")
end
else
return("Nothing to see here, move along! No doomsday here, nope!")
end
end
function doomsday_time_left_with_ticks()
local tick = (((game.surfaces[global.doomsday_surface].ticks_per_day * global.doomsday_start) - game.tick)%60)
return doomsday_time_left() .. ":" .. string.format("%02d", tick)
end
function doomsday_players_online_compensator()
local target = #game.connected_players
local current = global.doomsday_current_fuzzy_playercount
local step_size = 0.02 -- (at 21 ticks per iteration, that's 17.5sec per player change.
if(current < 0)then
game.print("Error; 'current' number of players' is less than 0!")
end
if(target < 0)then
game.print("Error; 'number of players' is less than 0!")
end
if(current == target) then
-- do nothing, this should be most of the time
elseif ( current < target) then
current = current + step_size
elseif ( current > target) then
current = current - step_size
else
-- do nothing?
end
local modifier = 20/(0.1 * current * current + 6) -- Nice, smooth curve that should hopefully give boost equal to the playercount!
game.forces.player.character_running_speed_modifier = modifier
global.doomsday_current_fuzzy_playercount = current
end
--[[
function reduce_brightness(n)
global.pdnc_max_brightness = 1 - ((global.pdnc_current_time / global.pdnc_doomsday_start)*n)
if(global.pdnc_max_brightness < n) then
global.pdnc_max_brightness = n
end
end
]]
-- This is a example of what to put at the end of your code, ie doomsday.lua
-- replaze EXAMPLE with any name you want
-- replace tick with number if ticks and function with the function name to call
-- everywhere there is a blank space is where you put stuff in
local doomsday_init = {}
local script_events = {
--place the here what you would normaly use Event.register for
-- Event.register(defines.events.on_player_created, testfunction)
-- is the same as
-- [defines.events.on_player_created] = testfunction,
-- where testfunction is | local functuin testfunction() { }
--[Event] = function,
--put stuff here
}
doomsday_init.on_nth_ticks = {
--place the here what you would normaly use
--[tick] = function,
--put stuff here
--[60] = doomsday_console_status, -- prints status to console ever second
}
doomsday_init.on_init = function() -- this runs when Event.core_events.init
log("doomsday init")
--put stuff here
doomsday_setup()
global.doomsday_data = global.doomsday_data or script_data -- NO TOUCHY
end
doomsday_init.on_load = function() -- this runs when Event.core_events.load
log("doomsday load")
--put stuff here
script_data = global.doomsday_data or script_data -- NO TOUCHY
end
doomsday_init.get_events = function()
return script_events
end
return doomsday_init
--Event.register(-global.pdnc_stepsize, doomsday_pdnc_program) --intentionally using the PDNC stepsize so the functions sync this can be skipped, since it's called from pdnc!
--Event.register(Event.core_events.init, doomsday_setup)
-266
View File
@@ -1,266 +0,0 @@
local mod_gui = require 'mod-gui'
-- ADDS DOOMSDAY STATS BUTTON
local function gui_update_DOOM(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.doomsday_stats
if not frame then
return
end
frame.clear()
for i,stat in ipairs(doomsday_status()) do
frame.add{
type = "label",
caption = stat
}
end
end
local function toggle_frame_DOOM(player)
local gui = mod_gui.get_frame_flow(player)
local frame = gui.doomsday_stats
if frame then
frame.destroy()
return
end
frame = gui.add{
type = "frame",
name = "doomsday_stats",
direction = "vertical",
caption = "Doomsday stats",
style = mod_gui.frame_style,
}
frame.style.horizontally_stretchable = false
frame.style.vertically_stretchable = false
gui_update_DOOM(player)
end
local function get_sprite_button_DOOM(player)
local button_flow = mod_gui.get_button_flow(player)
button_DOOM = button_flow.doomsday_stats_button
if button_DOOM then
button_DOOM.destroy()
end
if player.admin then
button_DOOM = button_flow.add{
type = "sprite-button",
name = "doomsday_stats_button",
sprite = "item/raw-fish",
style = mod_gui.button_style,
tooltip = "Show debug stats for doomsday",
}
end
end
--ADDS PDNC STATS BUTTON
local function gui_update_PDNC(player)
local gui = mod_gui.get_frame_flow(player)
local frame_PDNC = gui.PDNC_stats
if not frame_PDNC then
return
end
frame_PDNC.clear()
for i,stat in ipairs(pdnc_extended_status()) do
frame_PDNC.add{
type = "label",
caption = stat
}
end
end
local function toggle_frame_PDNC(player)
local gui = mod_gui.get_frame_flow(player)
local frame_PDNC = gui.PDNC_stats
if frame_PDNC then
frame_PDNC.destroy()
return
end
frame_PDNC = gui.add{
type = "frame",
name = "PDNC_stats",
direction = "vertical",
caption = "PDNC stats",
style = mod_gui.frame_style,
}
frame_PDNC.style.horizontally_stretchable = false
frame_PDNC.style.vertically_stretchable = false
gui_update_PDNC(player)
end
local function get_sprite_button_PDNC(player)
local button_flow = mod_gui.get_button_flow(player)
local button_PDNC = button_flow.PDNC_stats_button
if button_PDNC then
button_PDNC.destroy()
end
if player.admin then
button_PDNC = button_flow.add{
type = "sprite-button",
name = "PDNC_stats_button",
sprite = "item/raw-fish",
style = mod_gui.button_style,
tooltip = "Show debug stats for PDNC",
}
end
end
-- ADDS DOOMSDAY TIME LEFT COUNTER
local function gui_update_counter(player)
local gui = player.gui.screen --mod_gui.get_frame_flow(player)
local frame_counter = gui.doomsday_counter
if not frame_counter then
return
end
frame_counter.clear()
frame_counter.add{
type = "label",
caption = doomsday_time_left()
}
frame_counter.location = {player.display_resolution.width/2 - 59, 10}
frame_counter.visible = true
end
local function toggle_frame_counter(player)
--local player = player
local gui = player.gui.screen --mod_gui.get_frame_flow(player)
local frame_counter = gui.doomsday_counter
-- if frame_counter then
-- frame_counter.destroy()
-- return
-- end
frame_counter = gui.add{
type = "frame",
name = "doomsday_counter",
direction = "horizontal",
--caption = "Doomsday counter",
style = mod_gui.frame_style,
}
--frame_counter.style.horizontally_stretchable = false
frame_counter.style.vertically_stretchable = false
frame_counter.visible = false
gui_update_counter(player)
end
local function update_buttons(event)
local player = game.players[event.player_index]
if not (player and player.valid) then
return
end
get_sprite_button_DOOM(player)
get_sprite_button_PDNC(player)
-- Make sure the frames are not visible to players
if not player.admin then
local gui = mod_gui.get_frame_flow(player)
local frame_PDNC = gui.PDNC_stats
if frame_PDNC then
frame_PDNC.destroy()
end
local frame_DOOM = gui.doomsday_stats
if frame_DOOM then
frame_DOOM.destroy()
end
end
end
-- ON GUI CLICK AND OTHERS
local function on_gui_click(event)
local gui = event.element
local player = game.players[event.player_index]
if not (player and player.valid and gui and gui.valid) then
return
end
if gui.name == "doomsday_stats_button" then
toggle_frame_DOOM(player)
end
if gui.name == "PDNC_stats_button" then
toggle_frame_PDNC(player)
end
end
local function on_player_created(event)
local player = game.players[event.player_index]
if not (player and player.valid) then
return
end
update_buttons(event)
toggle_frame_counter(player)
end
local function gui_update_all()
for _, player in pairs(game.players) do
if player and player.valid then
gui_update_DOOM(player)
gui_update_PDNC(player)
gui_update_counter(player)
end
end
end
local function on_gui_tick(event)
gui_update_all()
end
local doomsdaygui_init = {}
local script_events = {
--place the here what you would normaly use Event.register for
-- Event.register(defines.events.on_player_created, testfunction)
-- is the same as
-- [defines.events.on_player_created] = testfunction,
-- where testfunction is | local functuin testfunction() { }
--[Event] = function,
--put stuff here
[defines.events.on_gui_click] = on_gui_click,
[defines.events.on_player_created] = on_player_created,
[defines.events.on_player_promoted] = update_buttons,
[defines.events.on_player_demoted] = update_buttons,
}
doomsdaygui_init.on_nth_ticks = {
--place the here what you would normaly use
--[tick] = function,
--put stuff here
[1] = on_gui_tick,
}
doomsdaygui_init.on_init = function() -- this runs when Event.core_events.init
log("doomsdaygui init")
--put stuff here
global.doomsdaygui_data = global.doomsdaygui_data or script_data -- NO TOUCHY
end
doomsdaygui_init.on_load = function() -- this runs when Event.core_events.load
log("doomsdaygui load")
--put stuff here
script_data = global.doomsdaygui_data or script_data -- NO TOUCHY
end
doomsdaygui_init.get_events = function()
return script_events
end
return doomsdaygui_init
-309
View File
@@ -1,309 +0,0 @@
-- Third itteration of programmable day-night cycle for Factorio
-- Developed by Zr4g0n with help
-- Features needed:
-- Set 'maximum brightness' from rocket launches cluster wide
-- Set 'time until doomsday' to any time, in minutes.
-- Set 'evolution target' to any number [0.0, 1.0]
-- Changing day-night cycle based on days
-- Handle 'next time' elegantly
-- lamps enable darkness: [0.595 - 0.425] scaled to [0.0 - 0.85] range from [0.0 - 1.0] range
global.pdnc = global.pdnc or {}
global.pdnc_enabled = true
global.pdnc_stepsize = 53 -- also used for script.on_nth_tick. Cannot be changed at runtime!
global.pdnc_surface = 1
global.pdnc_current_time = 0
global.pdnc_current_point = {x = 0, y = 1.0}
global.pdnc_last_point = {x = -1, y = 0.0}
global.pdnc_max_brightness = 0.5 -- for clusterio
global.pdnc_debug = false
global.pdnc_enable_brightness_limit = false
global.pdnc_enable_rocket_darkness = false
global.pdnc_rockets_launched = 0
global.pdnc_rockets_launched_step_size = 0.025
global.pdnc_rockets_launched_smooth = 0
global.pdnc_min_per_day = 15.0
function pdnc_setup()
--game.surfaces[global.pdnc_surface].ticks_per_day = pdnc_min_to_ticks(10.0)
doomsday_setup()
end
-- Returns true if the player is an admin
function check_admin(ctx)
local player = game.players[ctx.player_index]
if not player.admin then
player.print("Only admins can use /"..ctx.name)
return false
end
return true
end
function pdnc_toggle_debug(ctx)
if not check_admin(ctx) then return end
global.pdnc_debug = not global.pdnc_debug
pdnc_print_status(ctx)
end
function pdnc_toggle(ctx)
if not check_admin(ctx) then return end
global.pdnc_enabled = not global.pdnc_enabled
pdnc_print_status(ctx)
end
function pdnc_print_status(ctx)
local player = game.players[ctx.player_index]
if(global.pdnc_enabled)then
player.print("PDNC is enabled")
else
player.print("PDNC is disabled")
end
if(global.pdnc_debug)then
player.print("PDNC debug is enabled")
pdnc_extended_status()
else
player.print("PDNC debug is disabled")
end
end
function pdnc_extended_status()
stats = {
"global.pdnc_stepsize: " .. global.pdnc_stepsize,
"global.pdnc_surface: " .. global.pdnc_surface,
"global.pdnc_current_time: " .. global.pdnc_current_time,
"global.pdnc_max_brightness: " .. global.pdnc_max_brightness,
"global.pdnc_enable_brightness_limit: " .. pdnc_bool_to_string(global.pdnc_enable_brightness_limit),
"global.pdnc_enable_rocket_darkness: " .. pdnc_bool_to_string(global.pdnc_enable_rocket_darkness),
"global.pdnc_rockets_launched: " .. global.pdnc_rockets_launched,
"global.pdnc_rockets_launched_step_size: " .. global.pdnc_rockets_launched_step_size,
"global.pdnc_rockets_launched_smooth: " .. global.pdnc_rockets_launched_smooth,
"ticks per day: " .. game.surfaces[global.pdnc_surface].ticks_per_day,
"current tick: " .. game.tick,
}
return stats
end
function pdnc_bool_to_string(b)
if(b)then
return "true"
else
return "false"
end
end
function pdnc_core()
if(global.pdnc_enabled)then
doomsday_core()
local current_surface = game.surfaces[global.pdnc_surface]
pdnc_freeze_check(current_surface)
current_surface.ticks_per_day = pdnc_min_to_ticks(global.pdnc_min_per_day) -- move this somewhere else; doesn't need to run every nth tick!
global.pdnc_current_time = game.tick / current_surface.ticks_per_day
-- get current Y coordinate. X = 0 always.
local current_point = {x = global.pdnc_current_time, y = pdnc_program(global.pdnc_current_time)}
-- get next Y coordinate, X = 'step size'
local next_point = {x = (global.pdnc_current_time + (global.pdnc_stepsize/current_surface.ticks_per_day)), y = pdnc_program(global.pdnc_current_time + (global.pdnc_stepsize/current_surface.ticks_per_day))}
local top_point = pdnc_intersection_top(current_point, next_point)
local bot_point = pdnc_intersection_bot(current_point, next_point)
-- the order is dusk - evening - morning - dawn. They *must* be in that order and they cannot be equal
if(top_point < bot_point) then -- dusk -> evening
pdnc_cleanup_last_tick(current_surface)
current_surface.evening = bot_point - current_point.x
current_surface.dusk = top_point - current_point.x
elseif(top_point > bot_point) then -- morning -> dawn
pdnc_cleanup_last_tick(current_surface)
current_surface.morning = bot_point - current_point.x
current_surface.dawn = top_point - current_point.x
elseif(top_point == bot_point) then
pdnc_debug_message("PDNC: Top and bot point equal")
-- no cleanup is done here
-- if the points are equal, use last value until not equal
-- this should never be reached unless the pdnc_program() is broken.
else
pdnc_debug_message("Top and bot not different nor equal. probably a NaN error")
pdnc_debug_message("bot_point: " .. bot_point)
pdnc_debug_message("top_point: " .. top_point)
-- this should never be reached.
end
else
end
end
function pdnc_cleanup_last_tick(current_surface)
current_surface.daytime = 0
-- must be in this spesific order to
-- preserve the order at all times
-- dusk < evening < morning < dawn.
-- DO NOT CHANGE THE ORDER!
current_surface.dusk = -999999999999
current_surface.dawn = 999999999999
current_surface.evening = -999999999998
current_surface.morning = 999999999998
end
function pdnc_disable_and_reset(ctx)
if not check_admin(ctx) then return end
local current_surface = game.surfaces[global.pdnc_surface]
pdnc_cleanup_last_tick(current_surface)
-- DO NOT CHANGE THIS ORDER!
current_surface.evening = 0.45
current_surface.morning = 0.55
current_surface.dusk = 0.25
current_surface.dawn = 0.75
current_surface.ticks_per_day = 25000
global.pdnc_enabled = false
pdnc_debug_message("PDNC Programable Day-Night Cycle disabled, normal day-night cycle enabled")
end
function pdnc_freeze_check(current_surface)
if(current_surface.freeze_daytime)then
current_surface.freeze_daytime = false
game.print("Can't use freeze_daytime while programmable day-night cycle is active; time has been unfrozen")
end
end
function pdnc_program(x)
return pdnc_scaler(pdnc_c_boxy(x*6.28318530717958647692)) -- that's Tau, aka 2xPi
end
function pdnc_c_boxy(x)
return pdnc_normalize((math.sin(x) + (0.111 * math.sin(3 * x))) * 1.124859392575928)
-- magic numbers to make it scale to (-1, 1)
end
function pdnc_normalize(n)
return (n + 1)/2
end
function pdnc_scaler(r) -- a bit messy, but simplifies a lot elsewhere
if(pdnc_check_valid(r, "pdnc_scaler"))then
local a = 1
local b = 1
if(global.pdnc_enable_brightness_limit) then
a = global.pdnc_max_brightness
end
if(global.pdnc_enable_rocket_darkness) then
b = 1 - pdnc_rocket_launch_darkness()
end
return r * 0.85 * a * b -- 0 -> 0.85 is the 'brightness range' of factorio
end
end
function pdnc_intersection (s1, e1, s2, e2)
local d = (s1.x - e1.x) * (s2.y - e2.y) - (s1.y - e1.y) * (s2.x - e2.x)
local a = s1.x * e1.y - s1.y * e1.x
local b = s2.x * e2.y - s2.y * e2.x
local x = (a * (s2.x - e2.x) - (s1.x - e1.x) * b) / d
--local y = (a * (s2.y - e2.y) - (s1.y - e1.y) * b) / d
return x--, y
end
function pdnc_intersection_top (s2, e2)
local s1, e1 = {x = -999999999, y = 0.85}, {x = 999999999, y = 0.85}
return pdnc_intersection (s1, e1, s2, e2)
end
function pdnc_intersection_bot (s2, e2)
local s1, e1 = {x = -999999999, y = 0.0}, {x = 999999999, y = 0.0}
return pdnc_intersection (s1, e1, s2, e2)
end
function pdnc_set_max_brightness(n)
if(pdnc_check_valid(n, "pdnc_set_max_brightness")) then
global.pdnc_max_brightness = n
pdnc_debug_message("global.pdnc_max_brightness set to " .. global.pdnc_max_brightness)
end
end
function pdnc_min_to_ticks(m)
return 60*60*m
end
function pdnc_rocket_launch_counter()
global.pdnc_rockets_launched = 1 + global.pdnc_rockets_launched
end
function pdnc_rocket_launch_darkness()
if (global.pdnc_rockets_launched_smooth < global.pdnc_rockets_launched)then
global.pdnc_rockets_launched_smooth = global.pdnc_rockets_launched_step_size + global.pdnc_rockets_launched_smooth
end
return (1 - (50/(global.pdnc_rockets_launched_smooth+50)))
end
function pdnc_check_valid(n, s)
if (n == nil) then
pdnc_debug_message(s .. " set to nil! Set to 1.0 instead")
return false
elseif (n < 0) then
pdnc_debug_message(s .. " cannot be " .. n .. " limited to 0.0 instead")
return false
elseif (n > 1) then
pdnc_debug_message(s .. " cannot be " .. n .. " limited to 1.0 instead")
return false
elseif (n ~= n) then
pdnc_debug_message(s .. " cannot be " .. n .. " since it's not a valid number!")
else return true
end
end
function pdnc_debug_message(s)
if(global.pdnc_debug) then
game.print(s)
end
end
local PDNC_init = {}
local script_events = {
--place the here what you would normaly use Event.register for
-- Event.register(defines.events.on_player_created, testfunction)
-- is the same as
-- [defines.events.on_player_created] = testfunction,
-- where testfunction is | local functuin testfunction() { }
--[Event] = function,
--put stuff here
}
PDNC_init.on_nth_ticks = {
--place the here what you would normaly use
--[tick] = function,
--put stuff here
[global.pdnc_stepsize] = pdnc_core,
}
PDNC_init.add_commands = function()
commands.add_command("pdnc", "gives PDNC's status", pdnc_print_status)
commands.add_command("pdnc_toggle", "toggles pdnc", pdnc_toggle)
commands.add_command("pdnc_toggle_debug", "toggles pdnc debug mode", pdnc_toggle_debug)
commands.add_command("pdnc_disable_and_reset", "Disabled pdnc and resets the dnc to the vanilla default", pdnc_disable_and_reset)
end
PDNC_init.on_init = function() -- this runs when Event.core_events.init
log("PDNC init")
--put stuff here
global.PDNC_data = global.PDNC_data or script_data -- NO TOUCHY
end
PDNC_init.on_load = function() -- this runs when Event.core_events.load
log("PDNC load")
--put stuff here
script_data = global.PDNC_data or script_data -- NO TOUCHY
end
PDNC_init.get_events = function()
return script_events
end
return PDNC_init
@@ -2,6 +2,8 @@
--
-- Copyright (c) 2017 Florian Jung
--
-- Modified by Daniel Vestøl
--
-- Permission is hereby granted, free of charge, to any person obtaining a
-- copy of this factorio lua stub and associated
-- documentation files (the "Software"), to deal in the Software without
@@ -22,12 +24,13 @@
-- DEALINGS IN THE SOFTWARE.
function write_file(data)
stringsToWriteNextTick = stringsToWriteNextTick .. data .. '\n'
global.tileQueue = global.tileQueue .. data .. '\n'
-- stringsToWriteNextTick = stringsToWriteNextTick .. data .. '\n'
-- game.write_file("remoteMap.txt", data, true)
end
function dump_cached_writes()
game.write_file("remoteMap.txt", stringsToWriteNextTick, true, 0)
-- game.write_file("remoteMap.txt", stringsToWriteNextTick, true, 0)
end
function complain(text)
@@ -89,7 +92,7 @@ function on_chunk_generated(event)
end
-- writeout_resources(surface, area)
writeout_objects(surface, area)
-- writeout_objects(surface, area)
-- writeout_tiles(surface, area)
end
function on_some_entity_created(event, entp)
@@ -104,7 +107,7 @@ function on_some_entity_created(event, entp)
complain("wtf, on_some_entity_created has nil entity")
return
end
local entityData = ent.name..","..ent.position.x..","..ent.position.y
local entityData = "name="..ent.name..",x="..ent.position.x..",y="..ent.position.y
if ent.supports_direction then
entityData = entityData .. ",rot="..ent.direction
end
@@ -126,7 +129,7 @@ function on_some_entity_deleted(event)
--local area = {left_top={x=math.floor(ent.position.x), y=math.floor(ent.position.y)}, right_bottom={x=math.floor(ent.position.x)+1, y=math.floor(ent.position.y)+1}}
--table.insert(todo_next_tick, function () writeout_objects(surface, area ) end)
write_file("deleted,"..ent.position.x..","..ent.position.y)
write_file("name=deleted,x="..ent.position.x..",y="..ent.position.y)
--complain("on_some_entity_deleted: "..ent.name.." at "..ent.position.x..","..ent.position.y)
end
@@ -143,3 +146,22 @@ script.on_event(defines.events.on_player_mined_entity, on_some_entity_deleted) -
script.on_event(defines.events.on_robot_mined_entity, on_some_entity_deleted) --entity
script.on_event(defines.events.on_resource_depleted, on_some_entity_deleted) --entity
script.on_init(function()
global.tileQueue = ""
end)
remote.remove_interface("remoteMap")
remote.add_interface("remoteMap", {
exportTiles = function()
rcon.print(global.tileQueue)
global.tileQueue = ""
end,
exportChunk = function(xc, yc, ref)
local data = ""
local entities = game.surfaces[1].find_entities({{xc*32,yc*32},{xc*32+32,yc*32+32}})
for _, ent in pairs(entities) do
data = data .. "|name:"..ent.name..",direction:"..ent.direction..",x:"..ent.position.x..",y:"..ent.position.y
end
rcon.print(data)
end
})
+5 -8
View File
@@ -22,15 +22,12 @@ module.exports = class remoteCommands {
words.shift();
words.shift();
words.shift();
let message = words.join(" ").replace(/[";]/g, " ").replace(/[']/g,"").replace("/shout ", "").replace("!shout ","").replace(/\[gps=.*?\]/g, "").replace(/\[train=.*?\]/g, "");
let message = words.join(" ").replace(/[";]/g, " ").replace(/[']/g,"").replace("/shout ", "").replace("!shout", "");
let instance = await this.getInstanceName(data.instanceID);
if(data.data.includes("[gps=")){
//Nothing
}else{
this.messageInterface("/silent-command game.print('[" + instance + "] " + message + "')");
}
}
let instance = await this.getInstanceName(data.instanceID);
this.messageInterface("/silent-command game.print('[" + instance + "] " + message + "')");
}
}
});
}
@@ -1,7 +1,7 @@
-- 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.character_main).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
@@ -34,7 +34,7 @@ for a,b in pairs(game.players) do
else
notFirst = true
end
local inventory = game.players[a].get_inventory(defines.inventory.character_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()
--[[ Get contents of requester slots into a table ]]
local requests = {}
@@ -95,13 +95,13 @@ enclose in ''
]]
-- 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.character_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,true,0)
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,true,0)
-- 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.character_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
local a="{"for b,c in pairs(game.players)do local d=game.players[b].get_inventory(defines.inventory.character_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.print(a)
local a=game.players["Danielv123"].get_inventory(defines.inventory.character_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.print(e)
local inventory = game.players["Danielv123"].get_inventory(defines.inventory.character_main).get_contents()local quickbar = game.players["Danielv123"].get_quickbar().get_contents()local requests = {}for i = 1, game.player.force.character_logistic_slot_count do requests[i] = game.players["Danielv123"].character.get_request_slot(i) end 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(requests) do o = o.."['+"'"+'"..v["name"].."'+"'"+']:"..v["count"]..", " end o = o.."}}" game.write_file("test.txt", o,true,0)
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.print(a)
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.print(e)
local inventory = game.players["Danielv123"].get_inventory(defines.inventory.player_main).get_contents()local quickbar = game.players["Danielv123"].get_quickbar().get_contents()local requests = {}for i = 1, game.player.force.character_logistic_slot_count do requests[i] = game.players["Danielv123"].character.get_request_slot(i) end 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(requests) do o = o.."['+"'"+'"..v["name"].."'+"'"+']:"..v["count"]..", " end o = o.."}}" game.write_file("test.txt", o,true,0)
-- insert items in players inventory and export leftovers
View File
Whitespace-only changes.
-100
View File
@@ -1,100 +0,0 @@
var mkdirp = require("mkdirp-promise");
var mkdirpSync = require("mkdirp").sync;
var fs = require("fs");
var events = require("events");
var path = require("path");
var objectOps = require("./../../lib/objectOps");
var LinvoDB = require("linvodb3");
// LinvoDB.dbPath = "./database/linvodb/";
let allowLogging = true;
let log = string => (allowLogging)? console.log(string):""
module.exports = class chunkStore {
constructor(name, chunkSize = 32, path = "./chunkStore/"){
if(!name || (typeof name != "string" && typeof name != "number")) throw new Error("chunkstore requires a name string as first param");
name = name.toString();
this.name = name;
this.chunkSize = chunkSize;
this.path = (path[path.length] == "/") ? path : path+"/";
mkdirpSync(path+name);
LinvoDB.dbPath = this.path;
this.db = new LinvoDB(name, {}, {});
this.eventEmitter = new events.EventEmitter();
}
setEntity(x/**{number}*/, y/**{number}*/, newEntity/**{object}*/){
return new Promise((resolve, reject) => {
x = Math.floor(x);
y = Math.floor(y);
this.db.find({x, y}, (err, docs) => {
if(typeof newEntity == "object"){
if(err) reject(err);
let entityDoc = {x, y, entity: newEntity};
if(docs && docs[0] && docs[0]._id){
entityDoc._id = docs[0]._id;
}
this.db.save(entityDoc, (err,docs) => {
if(err) reject(err);
resolve(docs);
this.eventEmitter.emit("change", docs)
});
} else {
docs.forEach(doc => {
doc.remove(()=>{
// removed :)
});
});
resolve();
this.eventEmitter.emit("change", {x,y});
}
});
});
}
getEntity(x, y){
return new Promise((resolve, reject) => {
this.db.find({x:Math.floor(x), y:Math.floor(y)}, (err, docs) => {
if(err) reject(err);
resolve(docs);
});
});
}
onEntityChange(eventhandler/**function*/){
if(typeof eventhandler == "function") {
this.eventEmitter.on("change", eventhandler);
}
}
getChunk(x, y){
return new Promise((resolve, reject) => {
/*
// make new chunk in case we can't find an existing one
let chunk = {
dataObject: {},
position:{x,y}, // fancy ES6 feature that generates to {x:x, y:y}
chunkSize:this.chunkSize
};
fs.readFile(this.path+this.name+"/"+x+"/"+y, (err, chunkFile) => {
if(err){
log(err)
resolve(chunk);
} else {
if(objectOps.isJSON(chunkFile)) {
log("returned from file")
let chunk = JSON.parse(chunkFile);
resolve(chunk);
} else {
log("file failed")
resolve(chunk);
}
}
}); // add x and y values as well and we are done?
*/
});
}
}
@@ -1,64 +0,0 @@
var assert = require("assert");
var chunkStore = require("./chunkStore.js");
describe("remoteMap/chunkStore.js", () => {
// define some values for our test
const inserterX = 14.5; // rounds to 15
const inserterY = -17.5; // rounds to -17, +64 would be 47
const inserterName = "inserter";
let store // make our store elevated
it("is a constructor", ()=>{
assert.equal(typeof chunkStore, "function");
// create a new store and elevate it
store = new chunkStore("910239"/* instanceID, use defaults for the rest */, undefined, "./database/unitTests/chunkStore/");
assert.equal(store.name, "910239");
});
it("allows you to store the locations of entities", done => {
store.setEntity(inserterX, inserterY, {name:inserterName, otherData:"can be arbitrary"}).then(() => {
done();
// nothing to do here really
});
});
it("returns entities at positions you query for", done => {
setTimeout(()=>{
store.getEntity(inserterX, inserterY).then(entities => {
entity = entities[0]
// console.log(entity)
assert.equal(entity.x, 14);
assert.equal(entity.y, -18);
assert.equal(entity.entity.name, "inserter");
assert.equal(entity.entity.otherData, "can be arbitrary");
done();
});
},300);
});
it("deletes entities when not provided with an object as 3rd arg", done => {
store.setEntity(inserterX, inserterY).then(entity => {
assert(entity === undefined);
setTimeout(()=>{
store.getEntity(inserterX, inserterY).then(entities => {
// console.log(entities)
assert.equal(entities.length, 0, "Why are we getting results, it should be empty?");
done();
});
},300);
});
});
describe("validates input by throwing when it is invalid", ()=>{
it("throws if called without a string as the first parameter", ()=>{
assert.throws(()=>{
let throwingChunkStore = new chunkStore(undefined, undefined, "asdasda");
});
assert.throws(()=>{
let throwingChunkStore = new chunkStore({}, undefined, "asdasdas");
});
});
it("converts numbers to strings automatically if they are provided as the first parameter", ()=>{
assert.ok(()=>{
let nonThrowingChunkStore = new chunkStore(42, undefined, "./database/unitTests/chunkStore/");
});
});
});
});
+1
View File
@@ -13,4 +13,5 @@ module.exports = {
description: modInfo.description,
// We'll send everything in this file to your stdin. Beware.
scriptOutputFileSubscription: "remoteMap.txt",
masterPlugin: "masterPlugin.js",
}
+59 -118
View File
@@ -1,130 +1,71 @@
const needle = require("needle");
const ioClient = require("socket.io-client");
const chunkStore = require("./chunkStore.js");
const moduleConfig = require("./config"); // not to be confused with clusterio config. This config is private to this plugin.
class remoteMap {
constructor(slaveConfig, messageInterface){
this.config = slaveConfig;
const debug = true
module.exports = class remoteMap {
constructor(mergedConfig, messageInterface, extras){
this.messageInterface = messageInterface;
this.config = mergedConfig;
this.socket = extras.socket;
this.instances = {};
// initialize chunk database
this.chunkMap = new chunkStore(this.config.unique, 64, "./database/chunkStore/");
this.chunkMap.onEntityChange(entity => {
// this.messageInterface("Entity changed: ")
// this.messageInterface(entity);
// check valid coordinates (dunno what could go wrong, but can never be too sure)
if(entity && entity.x !== undefined && !isNaN(Number(entity.x)) && entity.y !== undefined && !isNaN(Number(entity.y))){
this.socket.emit("sendEntity", entity);
this.socket.on("hello", () => this.socket.emit("registerMapServer", {
instanceID: this.config.unique, // instanceID
}));
this.socket.on("createEntity", req => {
// req = {name:"", position:{x,y}, ?direction:4}
// game.player.surface.create_entity{name="small-ship-wreck", position={game.player.position.x-2, game.player.position.y+1}, direction=..., force=...}
this.messageInterface("placing "+JSON.stringify(req));
if(req && req.name && typeof req.name == "string" && req.position && !isNaN(Number(req.position.x)) && !isNaN(Number(req.position.y))){
// delete any entity in that spot first, just in case to avoid overlaps
this.messageInterface('/silent-command local toDelete = game.surfaces[1].find_entities({{'+req.position.x+','+req.position.y+'},{'+(req.position.x+0.5)+','+(req.position.y+0.5)+'}}) for i, entity in pairs(toDelete) do entity.destroy() end');
// build the entity
let command = '/silent-command game.surfaces[1].create_entity{name="'+req.name+'", position={'+Number(req.position.x)+', '+Number(req.position.y)+'}, force="player"'
if(req.direction) command += ', direction='+req.direction;
command += '}';
this.messageInterface(command); // execute command
}
});
})
this.socket.on("deleteEntity", req => {
// req: {position: {x, y}}
this.messageInterface('/silent-command local toDelete = game.surfaces[1].find_entities({{'+req.position.x+','+req.position.y+'},{'+(req.position.x+0.5)+','+(req.position.y+0.5)+'}}) for i, entity in pairs(toDelete) do entity.destroy() end');
})
this.socket.on("getChunk", async (data, callback) => {
let chunkData = await this.messageInterface(`/silent-command remote.call("remoteMap", "exportChunk", ${data.x}, ${data.y})`)
console.log(`Sending chunk data: ${chunkData}`)
callback(chunkData);
})
// set up websocket communication and handle requests from web interface users (which are going through master)
// socket should be a global
this.socket = ioClient("http://"+this.config.masterIP+":"+this.config.masterPort);
this.socket.on("hello", data => {
this.socket.emit("registerSlaveMapper", {instanceID: this.config.unique});
setInterval(()=>{
this.socket.emit("heartbeat"); // send our heartbeat to prevent being assumed dead
},10000);
});
this.socket.on("getChunk", req => {
this.messageInterface("getChunk is not supported!");
/*this.chunkMap.getChunk(req.x, req.y).then(chunk => {
chunk.requesterID = req.requesterID;
this.socket.emit("sendChunk", chunk);
});*/
});
this.socket.on("getEntity", req => {
// check for valid coordinates
if(req.x !== undefined && !isNaN(Number(req.x)) && req.y !== undefined && !isNaN(Number(req.y))){
this.chunkMap.getEntity(req.x, req.y).then(entities => {
if(entities && entities.length > 0){
entities.forEach(entity => {
this.socket.emit("sendEntity", entity);
});
// detect new entities created/deleted on the server and update the master
setInterval(async ()=>{
let data = await this.messageInterface(`/silent-command remote.call("remoteMap", "exportTiles")`)
if(data.length > 1) this.socket.emit("updateEntities", data)
}, 2000)
}
getInstanceName(instanceID){
return new Promise((resolve, reject) => {
let instance = this.instances[instanceID];
if(!instance){
needle.get(this.config.masterIP+":"+this.config.masterPort+ '/api/slaves', (err, response) => {
if(err || response.statusCode != 200) {
console.log("Unable to get JSON master/api/slaves, master might be unaccessible");
} else if (response && response.body) {
if(Buffer.isBuffer(response.body)) {console.log(response.body.toString("utf-8")); throw new Error();}
try {
for (let index in response.body)
this.instances[index] = response.body[index].instanceName;
} catch (e){
console.log(e);
return null;
}
instance = this.instances[instanceID]
if (!instance) instance = instanceID; //somehow the master doesn't know the instance
resolve(instance);
}
});
} else {
this.messageInterface("socket.getEntity triggered called with invalid parameters");
resolve(instance);
}
});
// both placing and deleting entities via the web interface
this.socket.on("placeEntity", req => {
// game.player.surface.create_entity{name="small-ship-wreck", position={game.player.position.x-2, game.player.position.y+1}, direction=..., force=...}
this.messageInterface("placing "+JSON.stringify(req));
if(req.name && typeof req.name == "string" && req.position && !isNaN(Number(req.position.x)) && !isNaN(Number(req.position.y))){
if(req.name == "deleted"){ // {{'+req.position.x+','+req.position.y+'},{'+req.position.x+','+req.position.y+'}}
// delete entity from game world using LUA command
this.messageInterface('/silent-command local toDelete = game.surfaces[1].find_entities({{'+req.position.x+','+req.position.y+'},{'+(req.position.x+1)+','+(req.position.y+1)+'}}) for i, entity in pairs(toDelete) do entity.die() end');
// this position is now empty, delete whatever was there from DB
this.deleteEntity(req.position.x, req.position.y);
} else {
let command = '/silent-command game.surfaces[1].create_entity{name="'+req.name+'", position={'+Number(req.position.x)+', '+Number(req.position.y)+'}, force="neutral"'
if(req.direction) command += ', direction='+req.direction;
command += '}';
this.messageInterface(command); // execute command
// add entity to the chunkStore database because create_entity does not trigger any events
let name = req.name;
let entity = {name};
if(typeof req.direction == "string"){
entity.rot = req.direction;
}
this.addEntity(req.position.x, req.position.y, entity);
}
}
});
this.messageInterface("/silent-command game.print('"+moduleConfig.name+" version "+moduleConfig.version+" enabled')");
}
addEntity(x,y,entity){ // add entity to database
this.chunkMap.setEntity(Number(x), Number(y), entity).then(data => {
this.messageInterface("Added "+entity.name+" to data "+Number(x)+", "+Number(y));
}).catch((err)=>this.messageInterface(err));
}
deleteEntity(x,y){ // remove entity from database
// this position is now empty, delete whatever was there from DB
this.chunkMap.setEntity(x, y, "delete this entity").then(() => {
this.messageInterface("Deleted entity at "+x+", "+y);
}).catch((err)=>this.messageInterface(err));
}
scriptOutput(data){
if(data){
this.messageInterface(data);
// express-transport-belt,-35.5,-14.5,[...]
data = data.split(",");
if(data && data[0] == undefined){
this.messageInterface("empty: ");
this.messageInterface(data);
return
}
let name = data[0];
let xPos = data[1];
let yPos = data[2];
if(xPos && yPos && name){
if(name == "deleted"){
// this position is now empty, delete whatever was there from DB
this.deleteEntity(xPos, yPos);
} else {
let entity = {name};
for(let i = 3; i < data.length; i++){
if(data[i].includes("=")){
let kv = data[i].split("=");
entity[kv[0]] = kv[1];
}
}
// save this entity in the database
this.addEntity(xPos, yPos, entity);
/* this.chunkMap.setEntity(xPos, yPos, entity).then(data => {
this.messageInterface("Added "+name+" to data "+xPos+", "+yPos);
}).catch((err)=>this.messageInterface(err)); */
}
}
}
}
}
module.exports = remoteMap;
}
+1 -1
View File
@@ -2,7 +2,7 @@
{
"name": "remoteMap",
"title": "Clusterio remote map",
"version": "2.2.0",
"version": "3.0.0",
"date": "18-11-2017",
"author": "Danielv123",
"dependencies": ["base >= 0.15"],
+189
View File
@@ -0,0 +1,189 @@
const fs = require("fs-extra");
const path = require("path");
const sanitizer = require('sanitizer');
const editorSockets = [];
const blacklistedEntities = [
"fish",
"player",
]
class masterPlugin {
constructor({ config, pluginConfig, pluginPath, socketio, express }) {
this.config = config;
this.pluginConfig = pluginConfig;
this.pluginPath = pluginPath;
this.io = socketio;
this.app = express;
this.clients = {};
this.slaves = {};
// initialize token auth module
this.authenticate = require("./../../lib/authenticate.js")(config);
// expose UI elements embedded in the master
//this.ui = require("./js/ui.js").ui;
// handle websocket connections
this.io.on("connection", socket => {
let instanceID = "unknown";
socket.on("registerMapServer", data => {
console.log("Registered mapServer socket")
if (data.instanceID && !isNaN(Number(data.instanceID))) {
instanceID = data.instanceID;
this.slaves[instanceID] = socket;
}
socket.on("updateEntities", data => {
console.log(data)
if(data){
// transform from weirdly deliminated string to entity object
let entities = data
.split("\n")
.filter(str => str.length > 0)
.map(ent => ent.replace(/[-]/g, "_")) // replace - with _
.map(str => str
.split(",")
.filter(x => x.length > 0)
.map(prop => ({
k: prop.split("=")[0],
v: prop.split("=")[1]})
)
)
.map(arr => {
let obj = {}
arr.forEach(property => obj[property.k] = property.v)
return obj
})
console.log(entities)
editorSockets.forEach(sock => sock.emit("updateEntity", {entities}))
}
})
});
socket.on("registerMapEditor", () => {
console.log("Registered mapEditor socket")
editorSockets.push(socket);
setTimeout(()=>socket.emit("createEntity", {entity:{
name:"fast_inserter",
position: {x:30,y:30},
direction: 2,
}}),10000)
socket.on("createEntity", data => {
console.log(`Creating entity: ${JSON.stringify(data)}`)
this.slaves[Object.keys(this.slaves)[0]].emit("createEntity", data.entity)
})
socket.on("deleteEntity", data => {
console.log(`Deleting entity: ${JSON.stringify(data)}`)
this.slaves[Object.keys(this.slaves)[0]].emit("deleteEntity", data.entity)
})
socket.on("getChunk", (data, callback) => {
console.log(`Getting chunk x:${data.x} y:${data.y} from slave`)
this.slaves[Object.keys(this.slaves)[0]].emit("getChunk", data, resp => {
// parse response
// |name:transport-belt,direction:2,x:99,y:1231|name:assembly-machine,direction:6,x:939,y:88
console.log(resp)
callback(resp
.split("|") // split into entities
.map(str => str.replace("\n","")) // remove newlines that happen at the end of the last entity (maybe more as well???)
.filter(str => str.length > 0) // remove blank entities
.filter(str =>
// !str.includes("tree") && // don't send entities that can't be rendered anyways
!str.includes("dead") &&
// !str.includes("rock") &&
!str.includes("crude-oil") &&
!str.includes("remnants") &&
!str.includes("cliff") &&
!blacklistedEntities.includes(str) &&
!str.includes("ore"))
.map(ent => ent.split(",") // split entity into properties
.map(prop => [prop.split(":")[0], prop.split(":")[1]]) // split properties into KV pairs
)
.map(entity => { // turn entities into objects
let entityObj = {}
entity.forEach(property => entityObj[property[0]] = property[1])
return entityObj
})
.map(entity => {
entity.name = entity.name.replace(/[-]/g, "_") // replace - with _ in entity names as thats what factorio.blueprint-editor preffers
return entity
})
.filter(entity => Object.keys(entity).length > 1)
.filter(entity => !blacklistedEntities.includes(entity.name))
/*.map(entity => {
if(entity.name.includes("tree")) entity.name = "tree"
return entity
})*/
)
})
})
socket.on("disconnect", function () {
let i = editorSockets.indexOf(socket);
console.log("editorSocket " + (i + 1) + " disconnected, " + (editorSockets.length - 1) + " left");
editorSockets.splice(i, 1);
});
});
socket.on("gameChat", async data => {
let chatLine = data.data.replace(/(\r\n\t|\n|\r\t)/gm, "").replace("\r", "");
//if (typeof chatLine == "string") this.handleChatLine(chatLine, instanceID);
});
});
}
async onExit() {
return;
}
async onLoadFinish({ plugins }) {
this.masterPlugins = plugins;
}
parseData(data, sharedData = {}) {
let parsedData = [];
data = data.split("|");
data.forEach(player => {
if (player) {
let playerData = {};
player = player.split(`~`);
player.forEach(kv => {
kv = kv.split(":");
if (kv[1] === undefined || kv[0] === undefined) {
console.log(new Error(`Something is wrong! Key:${kv[0]} Value:${kv[1]}`));
} else {
playerData[kv[0]] = kv[1].trim();
}
});
for (let k in sharedData) {
playerData[k] = sharedData[k];
}
parsedData.push(playerData);
}
});
return parsedData;
}
}
module.exports = masterPlugin;
function getDatabaseSync(path) {
let db;
try {
db = JSON.parse(fs.readFileSync(path, "utf8"));
} catch (e) {
db = {};
}
return db;
}
async function saveDatabase(path, database) {
if (!path) {
throw new Error("No path provided!");
} else if (!database) {
throw new Error("No database provided!");
} else {
try {
await fs.writeFile(path, JSON.stringify(database, null, 4));
} catch (e) {
throw new Error("Unable to write to database! " + path);
}
}
}
+15
View File
@@ -0,0 +1,15 @@
# Remote Map
A clusterio plugin for remotely exploring and editing the factorio world
### Setup
Make a database for storing entities
docker run --name some-mariadb -e MYSQL_ROOT_PASSWORD=clusterio-remote-map --restart=unless-stopped -d mariadb:latest
### Dev setup
Make a database container and expose the ports
docker run -p 3306:3306 --name some-mariadb -e MYSQL_ROOT_PASSWORD=clusterio-remote-map -d mariadb:latest
+3 -18
View File
@@ -1,24 +1,9 @@
/silent-command
local data = {};
local force = game.forces["player"];
for _, tech in pairs(force.technologies) do
local progress;
if tech == force.current_research then
progress = force.research_progress
else
progress = force.get_saved_technology_progress(tech.name);
end
local infinite = tech.enabled == true and tech.prototype.max_level == 4294967295;
local tech_data = {
tech.name,
tostring(tech.researched),
tostring(tech.level),
tostring(progress),
tostring(infinite),
}
tech_data = table.concat(tech_data, ':')
table.insert(data, tech_data);
for _, tech in pairs(game.forces["player"].technologies) do
table.insert(data, tech.name .. ":" .. tostring(tech.researched) .. ":" .. tostring(tech.level));
end
game.write_file("researchSync.txt", table.concat(data, "\n"), false, 0)
+14 -24
View File
@@ -1,26 +1,16 @@
local function updateTech()
script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true})
if {notify} then
if 1 == {tech_infinite} then
game.print("Infinite technology {tech_name} level {tech_level} unlocked")
else
game.print("Technology {tech_name} researched")
end
game.play_sound({path="utility/research_completed"})
end
end
/silent-command
if game.forces['player'].technologies['{tech_name}'] then
if 1 == {tech_infinite} then
if (game.forces['player'].technologies['{tech_name}'].level ~= level) then
game.forces['player'].technologies['{tech_name}'].level = {tech_level}
updateTech()
end
else
if not game.forces['player'].technologies['{tech_name}'].researched then
game.forces['player'].technologies['{tech_name}'].researched = true
updateTech()
end
end
if game.forces['player'].technologies['{tech_name}'].researched ~= {tech_researched} then
game.forces['player'].technologies['{tech_name}'].researched = {tech_researched}
end
if game.forces['player'].technologies['{tech_name}'].level ~= {tech_level} then
game.forces['player'].technologies['{tech_name}'].level = {tech_level}
end
script.raise_event(defines.events.on_research_finished, {research=game.forces['player'].technologies['{tech_name}'], by_script=true})
game.play_sound({path="utility/research_completed"})
if game.forces['player'].technologies['{tech_name}'].researched == false then
game.print("Technology {tech_name} synced at level {tech_level}")
else
game.print("Technology {tech_name} synced")
end
end
+97 -347
View File
@@ -1,377 +1,127 @@
const fs = require('fs');
const path = require('path')
const needle = require("needle");
function time() {
let d = new Date()
return `${d.getUTCHours()}:${d.getUTCMinutes()}:${d.getUTCSeconds()}.${d.getUTCMilliseconds()}`
}
class ResearchSync {
constructor(slaveConfig, messageInterface, extras = {}){
this.config = slaveConfig
this.messageInterface = messageInterface
this.config = slaveConfig;
this.messageInterface = messageInterface;
this.functions = this.loadFunctions();
this.research = {};
this.functions = {
dumpResearch: this.loadFunc("dumpResearch.lua"),
enableResearch: this.loadFunc("enableResearch.lua"),
updateProgress: this.loadFunc("updateProgress.lua")
}
this.log_folder = './logs'
if (!fs.existsSync(this.log_folder))
fs.mkdirSync(this.log_folder);
this.log_file = path.join(this.log_folder, `${this.config.unique}-research.log`)
this.get_node_name_and_move_log()
this.research = {}
this.prev_research = {}
this.initial_request_own_data(
() => this.setup_sync_task(extras)
)
setInterval(() => {
this.pollResearch();
setTimeout(this.doSync.bind(this), 2000);
}, extras.researchSyncPollInterval || 5000);
}
log(data) {
try {
console.log(`researchSync: ${data}`)
fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
} catch (e) {
console.error(e)
}
}
error(data) {
try {
console.error(`researchSync: ${data}`)
fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
} catch (e) {
console.error(e)
}
}
get_node_name_and_move_log() {
const url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
needle.get(url, {compressed:true}, (err, res, slaves_data) => {
if (err)
return this.error(err)
slaves_data = Object.values(slaves_data)
let node_data = slaves_data.find(
slave_data => slave_data.unique === this.config.unique.toString()
)
if (node_data === undefined)
return this.error('slave was not found')
let node_name = node_data.instanceName
let log_file = path.join(this.log_folder, `${node_name}-research.log`)
if (fs.existsSync(log_file)) {
fs.appendFileSync(log_file, fs.readFileSync(this.log_file));
fs.unlinkSync(this.log_file)
} else {
fs.renameSync(this.log_file, log_file)
}
this.log_file = log_file
})
}
initial_request_own_data(callback) {
const url = `${this.config.masterIP}:${this.config.masterPort}/api/getSlaveMeta`
const data = {
instanceID: this.config.unique,
password: this.config.clientPassword,
}
const options = {
headers: {'x-access-token': this.config.masterAuthToken},
json: true,
compressed: true
}
needle.post(url, data, options, (err, res, techs) => {
if (err) {
this.log(`Can't get own slave data:`)
this.error(err)
return
}
if (res.statusCode === 404) {
this.log('slave is not registered yet. Delaying for 5 secs')
setTimeout(
() => this.initial_request_own_data(callback),
5000
)
return
}
if (res.statusCode !== 200) {
this.log(`Can't get own slave data:`)
this.error(`status code ${res.statusCode}, ${res.body}`)
return
}
if(techs.toString()!="") {
techs = JSON.parse(techs)
if (typeof techs.research === 'object')
this.research = techs.research
this.log('techs imported from master')
} else {
this.log('no techs imported from master, since there are none yet')
}
callback()
})
}
setup_sync_task(extras) {
const timeout = extras.researchSyncPollInterval || 5000
setInterval(() => this.sync_task(), timeout);
}
sync_task() {
pollResearch() {
// this.messageInterface("Polling Research\n")
this.messageInterface(this.functions.dumpResearch);
setTimeout(this.request_cluster_data.bind(this), 2000);
}
request_cluster_data() {
const slaves_data_url = `${this.config.masterIP}:${this.config.masterPort}/api/slaves`
needle.get(slaves_data_url, {compressed:true}, this.sync_researches.bind(this))
}
doSync() {
needle.get(this.config.masterIP + ':' + this.config.masterPort + '/api/slaves', (err, resp, slaveData) => {
if (err){
this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable");
return false;
}
if (resp.statusCode !== 200){
this.messageInterface("got error when calling slaves", resp.statusCode, resp.body);
return;
}
sync_researches(err, resp, slaves_data) {
if (err) {
this.messageInterface("Unable to post JSON master/api/slaves, master might be unreachable");
return false;
}
if (resp.statusCode !== 200) {
this.messageInterface("got error when calling slaves", resp.statusCode, resp.body);
return;
}
let needResearch = {};
slaves_data = Object.values(slaves_data)
slaves_data = slaves_data.filter(
slave_data => slave_data.unique !== this.config.unique.toString()
&& slave_data.meta && slave_data.meta.research
)
Object.keys(slaveData).forEach(instanceKey => {
if (slaveData[instanceKey].unique === this.config.unique.toString()) {
return;
}
if (!slaveData[instanceKey].hasOwnProperty('meta') || !slaveData[instanceKey].meta.hasOwnProperty('research')) {
return;
}
let researchList = slaveData[instanceKey].meta.research;
if(researchList){
Object.keys(researchList).forEach(researchName => {
if (isNaN(researchList[researchName].researched) || isNaN(researchList[researchName].level)) {
return;
}
if (needResearch.hasOwnProperty(researchName)) {
if (needResearch[researchName].researched === 0) {
needResearch[researchName].researched = parseInt(researchList[researchName].researched);
}
if (needResearch[researchName].level < parseInt(researchList[researchName].level)) {
needResearch[researchName].level = parseInt(researchList[researchName].level);
}
}
else {
needResearch[researchName] = {researched: parseInt(researchList[researchName].researched), level: parseInt(researchList[researchName].level)};
}
});
}
});
this.clear_contribution_to_researched_techs()
let cluster_techs = this.get_cluster_techs(slaves_data)
this.recount_cluster_research_progress(slaves_data, cluster_techs)
let difference = this.filterResearchDiff(this.research, needResearch);
let to_research = this.filter_researched_techs(cluster_techs)
let to_update_progress = this.filter_updated_techs(cluster_techs, to_research)
Object.keys(difference).forEach((key) => {
if (this.research[key]) {
let command = this.functions.enableResearch;
while(command.includes("{tech_name}")){
command = command.replace("{tech_name}", key);
command = command.replace("{tech_researched}", difference[key].researched);
command = command.replace("{tech_level}", difference[key].level);
}
this.messageInterface(command);
console.log('Unlocking '+ key + ': ' + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level + ', was '+ (this.research[key].researched === 0 ? 'false' : 'true') + ' at level ' + this.research[key].level);
this.messageInterface("Unlocking research: " + key + " at research state = " + (difference[key].researched === 0 ? 'false' : 'true') + ' and level ' + difference[key].level);
this.research[key] = difference[key];
}
});
this.research_technologies(to_research)
this.update_technologies_progress(to_update_progress)
this.print_own_contribution()
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
instanceID: this.config.unique,
password: this.config.clientPassword,
meta: {research: this.research}
}, {headers: {'x-access-token': this.config.masterAuthToken}, json: true, compressed:true}, (err, resp) => {
if (err)
this.error(err)
})
}
clear_contribution_to_researched_techs() {
// if between updates tech was researched
if (!this.prev_research)
return
for (let [name, research] of Object.entries(this.research)) {
let researched
if (research.infinite)
researched = this.prev_research[name].level < research.level
else
researched = this.prev_research[name].researched < research.researched
if (researched)
research.contribution = 0
}
needle.post(this.config.masterIP + ':' + this.config.masterPort + '/api/editSlaveMeta', {
instanceID: this.config.unique,
password: this.config.clientPassword,
meta: {research: this.research}
}, {headers: {'x-access-token': this.config.masterAuthToken}}, function (err, resp) {
// success?
});
});
}
get_cluster_techs(slavesData) {
let cluster_techs = {}
for (let slave_data of slavesData) {
let node_researches = slave_data.meta.research
for (let [name, node_tech] of Object.entries(node_researches)) {
if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite))
continue
if (cluster_techs[name]) {
if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) {
cluster_techs[name].level = node_tech.level
} else if (node_tech.researched > cluster_techs[name].researched) {
cluster_techs[name].researched = 1
filterResearchDiff(localResearch, remoteResearch) {
let diff = {};
Object.keys(localResearch).forEach((key) => {
if (remoteResearch.hasOwnProperty(key)) {
if ((localResearch[key].researched === 0 && localResearch[key].researched !== remoteResearch[key].researched) || localResearch[key].level < remoteResearch[key].level) {
if (!isNaN(remoteResearch[key].researched) && !isNaN(remoteResearch[key].level)) {
diff[key] = {researched: remoteResearch[key].researched, level: remoteResearch[key].level};
}
} else {
cluster_techs[name] = node_tech
}
}
}
return cluster_techs
});
return diff;
}
recount_cluster_research_progress(slaves_data, cluster_researches) {
for (let [name, research] of Object.entries(cluster_researches))
research.progress = this.research[name].contribution
for (let slave_data of slaves_data) {
for (let [name, research] of Object.entries(slave_data.meta.research)) {
if (!cluster_researches[name])
continue
if (isNaN(cluster_researches[name].progress)
|| isNaN(research.contribution)
|| isNaN(research.level)
|| isNaN(cluster_researches[name].level))
continue
if (cluster_researches[name].level === research.level)
cluster_researches[name].progress += research.contribution
}
}
for (let [name, research] of Object.entries(cluster_researches)) {
if (research.progress > 1) {
research.progress = null
research.researched = 1
this.research[name].contribution = 0
research.contribution = 0
if (this.research[name].level >= research.level)
research.level = this.research[name].level + 1
}
}
loadFunctions() {
return {
dumpResearch: this.loadFunc("dumpResearch.lua"),
enableResearch: this.loadFunc("enableResearch.lua"),
};
}
filter_researched_techs(cluster_researches) {
let local_researches = this.research
let result = {};
for (let key in local_researches) {
if (!cluster_researches[key])
continue
if (isNaN(cluster_researches[key].researched) || isNaN(cluster_researches[key].level))
continue
let researched
if (local_researches[key].infinite)
researched = local_researches[key].level < cluster_researches[key].level
else
researched = local_researches[key].researched < cluster_researches[key].researched
if (researched)
result[key] = cluster_researches[key]
}
return result;
loadFunc(path) {
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' ');
}
filter_updated_techs(cluster_techs, to_research) {
let local_techs = this.research
let result = {}
for (let name in local_techs) {
if (name in to_research || !cluster_techs[name])
continue
if (isNaN(cluster_techs[name].progress))
continue
if (local_techs[name].progress < cluster_techs[name].progress)
result[name] = cluster_techs[name]
}
return result
}
research_technologies(to_research) {
for (let name of Object.keys(to_research))
if (!this.research[name])
delete to_research[name]
const notify = Object.keys(to_research).length === 1
for (let [name, tech] of Object.entries(to_research)) {
this.research[name].contribution = 0
this.research[name].progress = null
let command = this.functions.enableResearch;
command = command.replace(/{tech_name}/g, name);
command = command.replace(/{tech_researched}/g, tech.researched);
command = command.replace(/{tech_level}/g, tech.level);
command = command.replace(/{tech_infinite}/g, tech.infinite);
command = command.replace(/{notify}/g, notify);
this.messageInterface(command);
let log_message = tech.infinite
? `Unlocking infinite research ${name} at level ${this.research[name].level}`
: `Unlocking research ${name}`
this.log(log_message);
this.messageInterface(log_message);
this.research[name] = tech;
}
}
update_technologies_progress(to_update) {
for (let [name, tech] of Object.entries(to_update)) {
if (!this.research[name])
continue
let progress = this.research[name].progress
if (progress === null)
progress = 'nil'
let command = this.functions.updateProgress
command = command.replace(/{tech_name}/g, name)
command = command.replace(/{last_check_progress}/g, progress)
command = command.replace(/{new_progress}/g, tech.progress)
this.messageInterface(command);
this.log(
`Updating ${name}: ${this.research[name].progress} += ${tech.progress - this.research[name].progress}`
);
this.research[name].progress = tech.progress
}
}
print_own_contribution() {
if (!this.prev_research)
return
for (let [name, tech] of Object.entries(this.research)) {
if (!this.prev_research[name])
continue
let diff = this.research[name].contribution - this.prev_research[name].contribution
if (Math.abs(diff) > Number.EPSILON * 1000)
this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`)
}
}
loadFunc(path, silent=true) {
let command = fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8')
command = command.replace(/\r?\n|\r/g,' ')
command = (silent ? '/silent-command ' : '/c ') + command
return command;
}
scriptOutput(data) {
let [name, researched, level, progress, infinite] = data.split(":")
researched = +(researched === 'true')
infinite = +(infinite === 'true')
level = parseInt(level);
if (progress === 'nil')
progress = null
else
progress = parseFloat(progress)
if (isNaN(level) || isNaN(researched))
return
this.prev_research[name] = this.research[name]
if (!this.prev_research[name]) {
this.prev_research[name] = {
researched: null,
level: null,
progress: null,
contribution: 0,
infinite,
}
}
this.research[name] = {
researched,
level,
progress,
contribution: this.prev_research[name].contribution,
infinite
}
if (this.prev_research[name].progress && this.research[name].progress) {
// this.prev_research[name].progress gets updated to overall cluster progress
// therefore contribution should be own research progress change over sync interval
let contribution = this.research[name].progress - this.prev_research[name].progress
this.research[name].contribution += contribution
}
if (Math.abs(this.research[name].contribution) < Number.EPSILON * 1000) {
// if contribution should be 0 but because of floating-point precision is e.g. 2.2564e-18
this.research[name].contribution = 0
scriptOutput(data){
let kv = data.split(":");
let name = kv[0];
let researched = ('true' !== kv[1]
? 0
: 1);
let level = parseInt(kv[2]);
if (!isNaN(level) && !isNaN(researched)) {
this.research[name] = {researched: researched, level: level};
}
}
}
+3 -3
View File
@@ -9,7 +9,7 @@ describe("researchSync/index.js", ()=>{
assert.equal(typeof researchSync, "function");
});
describe("class researchSync()", ()=>{
it(".filter_researched_techs(object1, object2) returns research that needs enabling on the local instance", ()=>{
it(".filterResearchDiff(object1, object2) returns research that needs enabling on the local instance", ()=>{
let reSync = new researchSync({}, function(){});
let obj1 = {
@@ -24,9 +24,9 @@ describe("researchSync/index.js", ()=>{
c:{researched: false, level: 0},
e:{researched: false, level: 50},
};
assert(typeof reSync.filter_researched_techs == "function");
assert(typeof reSync.filterResearchDiff == "function");
let diffResult = reSync.filter_researched_techs(obj1, obj2);
let diffResult = reSync.filterResearchDiff(obj1, obj2);
assert(diffResult.a === undefined);
@@ -1,33 +0,0 @@
local function get_tech_progress(force, tech)
if tech == force.current_research then
return force.research_progress
else
return force.get_saved_technology_progress(tech.name);
end
end
local function set_tech_progress(force, tech, progress)
if tech == force.current_research then
force.research_progress = progress
else
force.set_saved_technology_progress(tech.name, progress);
end
end
local force = game.forces['player']
local tech_name = '{tech_name}'
local tech = force.technologies[tech_name]
local last_check_progress = {last_check_progress}
local new_progress = {new_progress}
local progress_delta = get_tech_progress(force, tech)
if last_check_progress ~= nil then
progress_delta = progress_delta - last_check_progress
end
if progress_delta ~= nil then
new_progress = new_progress + progress_delta
end
if new_progress > 1 then
new_progress = 1
end
set_tech_progress(force, tech, new_progress)
+5 -11
View File
@@ -20,19 +20,13 @@ module.exports = class remoteCommands {
setInterval(async () => {
let stats = await this.getStats();
stats.forEach(force => {
// I have no idea why I need to do JSON.stringify before passing it to needle.post,
// but if you do forceData: force.data, it doesn't work...
let request = {
needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`, {
instanceID: this.config.unique,
force: force.forceName,
forceData: JSON.stringify(force.data),
};
needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`,
request,
needleOptionsWithTokenAuthHeader, (err, resp) => {
if(!err && resp.body && resp.body.ok == false) console.log("Error calling /api/logStats: " + resp.body);
}
);
data: force.data,
}, needleOptionsWithTokenAuthHeader, (err, resp) => {
if(!err && resp.body && resp.body.ok == false) console.log(resp.body);
});
});
}, 15000);
}
@@ -26,22 +26,18 @@ class masterPlugin {
@returns {string} failure
*/
app.post("/api/logStats", authenticate.middleware, function(req,res) {
let data = req.body.forceData;
if(typeof req.body.forceData == "string") {
data = JSON.parse(req.body.forceData);
}
// endpointHitCounter.labels(req.route.path).inc();
if(typeof req.body == "object"
&& req.body.instanceID
&& typeof req.body.instanceID == "string"
&& req.body.instanceID.length < 100
&& data
&& typeof data == "object"
&& !Array.isArray(data)){
&& req.body.data
&& typeof req.body.data == "object"
&& !Array.isArray(req.body.data)){
try{
Object.keys(data).forEach(statisticName => {
Object.keys(data[statisticName]).forEach(direction => {
let statHash = data[statisticName][direction];
Object.keys(req.body.data).forEach(statisticName => {
Object.keys(req.body.data[statisticName]).forEach(direction => {
let statHash = req.body.data[statisticName][direction];
Object.keys(statHash).forEach(item => {
let value = Number(statHash[item]);
if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value);
@@ -54,12 +50,7 @@ class masterPlugin {
console.log(e)
};
} else {
console.log(`/api/logStats: Invalid request. instanceID: ${req.body.instanceID}, dataIsArray: `
+ `${Array.isArray(data)}, ${typeof req.body.instanceID}, ${typeof data}`);
res.send({
ok:false,
msg:"Invalid request parameters"
});
res.send("failure");
}
});
}