Compare commits

...
Author SHA1 Message Date
Godmave d819644b01 Merge pull request #186 from Zeroto/master
Fix notifying about the same research being finished
2019-07-30 08:12:21 +02:00
Sander Homan 042230dc2c Fix notifying about the same research being finished
This is just a bandaid to fix a symptom. The underlying cause is still there, but at least it shouldn't spam the players anymore.
2019-07-29 20:31:42 +02:00
Hornwitser a00183e94c Switch to rcon-client fork (#183)
Switch to using fork of rcon-client where the commands and the responses
are not converted to ASCII before being sent over the wire.  Should fix
the issue with Russian text showing up as garbled letters in the chat as
well as the issue with train schedules using >= and <= conditions.

If rcon-client upstream is updated with the utf8 patch this commit can
be reverted with the version the patch was included in.
2019-07-28 14:42:44 +02:00
Arvids Godjuks c95d9f9464 Added "Doomsday" scenario (#184)
* Added doomsday scenario
2019-07-27 22:59:23 +03:00
Danielv123 a1237d8cfd Added CLI script for deleting players from playerManager 2019-07-26 23:55:33 +02:00
Godmave 95d06293f5 Merge pull request #181 from Hornwitser/patch-1
Fix wrong argument order
2019-07-26 18:53:58 +02:00
Hornwitser 2714b09e5d Fix wrong argument order
Fix compression not applying because the order of the callback and
options arguments were swapped in the call to Needle.get for the
request_cluster_data function.
2019-07-26 18:51:16 +02:00
Godmave e733cfde03 Merge pull request #180 from Godmave/master
add compression to research sync
2019-07-25 23:23:57 +02:00
Godmave 008ea139c6 #add compression 2019-07-25 23:22:46 +02:00
psihius 3338c439dd Empty meta fix for querying new slave check 2019-07-25 22:20:34 +02:00
psihius b956438d8d INVALID REQUEST is not a valid json key for a variable, end up being "undefined" 2019-07-25 22:08:33 +02:00
Godmave cdbbdd41c7 Merge pull request #9 from Danielv123/master
sync
2019-07-23 20:34:51 +02:00
Master-Guy e2775adcc3 /api/rconPasswords fix & /api/slaves security fix (#179)
Fix for /api/slaves to not send rconPasswords
Fix for /api/rconPasswords to work properly and send partial information
2019-07-20 15:52:47 +02:00
Master-Guy d909b4aa47 Fix for /issues/177 (#178)
/api/editSlaveMeta: Removed unneeded rcon check
/api/getSlaveMeta: Removed useless password check
/api/checkMod: Added authenticate.middleware
/api/slaves: Renamed to /api/rconPasswords and changed to authenticate.middleware
/api/runCommand: Changed to authenticate.middleware
2019-07-20 14:33:54 +02:00
Master-Guy 49611a2062 /api/slaves POST (optimized) (#176)
* Adds a /api/slaves POST call that requires authentication. It also returns the rconPasswords, unlike the GET call.
2019-07-19 15:37:49 +03:00
AreYouScared ee6ab6ab72 Remove GPS pings in crosschat (#172)
* Remove GPS pings in crosschat

Cross-server gps pings are annoying, lets fix that

* Lets just do it the right way <3

Should be better

* Take 2

Let's try that again
2019-07-19 12:05:03 +02:00
Arvids Godjuks de92ca96b9 Merge pull request #173 from Master-Guy/master
Add "info" field between client and master
2019-07-18 18:15:37 +03:00
Arvids Godjuks 77281d0662 Merge pull request #174 from MrRozdor/log-research-sync
Add logging to researchSync plugin
2019-07-18 16:25:27 +03:00
TechnoStrife 2831c07000 Add logging to researchSync plugin 2019-07-17 23:21:57 +03:00
Master-Guy 6ac8c0711f JSON fix
Small fix for JSON
2019-07-17 19:23:53 +02:00
Master-Guy c9cbc48d95 2/2 Add "info" field between client and master
Adds a "info" field to config.json and allows the client to send the contents of this info field to the master, so it also shows up (i.e. with /api/slaves)
2019-07-17 18:40:01 +02:00
Master-Guy 50370bf858 1/2 Add "info" field between client and master
Adds a "info" field to config.json and allows the client to send the contents of this info field to the master, so it also shows up (i.e. with /api/slaves)
2019-07-17 18:35:02 +02:00
Godmave d1fd4c3565 Merge pull request #171 from MCRcortex/patch-1
added res.sendStatus(200); to stop client errors throwing
2019-07-17 14:07:14 +02:00
MCRcortex 8672afe940 added res.sendStatus(200); to stop client errors throwing
according to the docs
"The methods on the response object (res) in the following table can send a response to the client, and terminate the request-response cycle. If none of these methods are called from a route handler, the client request will be left hanging."
Last sentence is the critical one.
Sending a status code 200 should fix the issue
2019-07-17 22:04:29 +10:00
Godmave adf6763a0d Merge pull request #170 from MrRozdor/fix-researchsync
Fix researchSync that it counts progress only from techs on same level
2019-07-17 07:16:48 +02:00
TechnoStrife d48a824a69 Fix researchSync that it counts progress only from techs on same level 2019-07-17 00:31:15 +03:00
Godmave f28b73cf31 Merge pull request #169 from MrRozdor/spread_researching
Fix for migation to new researchSync version
2019-07-15 21:50:38 +02:00
TechnoStrife fec3cf5ddf Fix for migation to new researchSync version 2019-07-15 22:49:39 +03:00
Godmave c7cfb8ce70 Merge pull request #168 from MrRozdor/spread_researching
Make any node be able to research anything
2019-07-15 21:27:49 +02:00
TechnoStrife 93c210f0c7 Merge branch 'master' into spread_researching
# Conflicts:
#	sharedPlugins/researchSync/dumpResearch.lua
#	sharedPlugins/researchSync/index.js
2019-07-15 22:14:09 +03:00
TechnoStrife 251ec696a3 Fixes && debugging 2019-07-15 18:29:53 +03:00
Godmave 773a2c412e Merge pull request #167 from veladon/pr/3
fix for stats exporter console errors
2019-07-15 15:05:21 +02:00
veladon c0499e7f27 Merge branch 'master' into pr/3 2019-07-15 13:57:27 +01:00
veladon 0a65946bf4 fix for /api/logstats 2019-07-15 13:47:53 +01:00
veladon 233410add7 fix for stats exporter console errors (#166) 2019-07-15 13:49:49 +02:00
veladon fea9fb28ca Merge branch 'master' of https://github.com/veladon/factorioClusterio 2019-07-15 12:46:45 +01:00
veladon e1b510b441 fix for stats exporter console errors 2019-07-15 12:46:38 +01:00
veladon 34301eb381 Merge pull request #2 from Danielv123/master
merge
2019-07-15 12:44:43 +01:00
TechnoStrife 4a0956d174 Fix a bug causing all slaves data gets cleared on master startup
Cherry-picked from fix-slaves-data-cleanup-on-startup
2019-07-15 11:39:11 +03:00
TechnoStrife 3b814eeea4 Fix a bug causing all slaves data gets cleared on master startup (#165) 2019-07-15 10:34:02 +02:00
TechnoStrife 98c6795f8f Make researchSync work for any research on any node 2019-07-15 00:46:21 +03:00
Godmave 21dd80df9b Merge pull request #8 from Danielv123/master
sync
2019-07-14 20:54:46 +02:00
Arvids Godjuks 0b34f07954 Dump only enabled research (#164) 2019-07-14 17:48:20 +02:00
Daniel c978e8e748 Merge pull request #163 from psihius/master
Mass sync does not spam
2019-07-14 15:58:07 +02:00
psihius 71870daaba Made so mass tech sync does not make sounds or messages, only notifies at the end 2019-07-14 15:00:55 +02:00
Daniel 5a60f317ba Merge pull request #162 from psihius/master
Update to research sync plugin
2019-07-14 14:06:24 +02:00
psihius b666a909ab Formatting and logging adjustments 2019-07-14 01:32:34 +02:00
psihius 0afe35b67c Updated research sync to handle infinite science and hopefully stop it from apply same research in a loop annoying everyone 2019-07-14 01:25:48 +02:00
veladon 096ead5bc4 Merge pull request #1 from Danielv123/master
merge
2019-07-13 18:14:51 +01:00
TechnoStrife fa4c10dce2 Code refactor 2019-07-12 18:52:47 +03:00
Daniel Vestøl 1f8ac50950 Better error message for statisticsExporter 2019-07-09 22:50:28 +02:00
Godmave 10d5b0da9b Merge pull request #7 from Danielv123/master
pull updates
2019-07-06 19:57:53 +02:00
Daniel Vestøl 6c32271c56 Fixed rcon timeout in the right place, set to 200s for 1250kB commands 2019-07-06 12:44:31 +02:00
Daniel Vestøl a122f2b09e Increased rcon timeout to 20s, increased maxPending to 5 2019-07-06 00:46:22 +02:00
Daniel Vestøl f35be2f355 Set max pending rcon requests to 1 2019-07-05 23:42:51 +02:00
Daniel Vestøl d66d22a9cd Updated rcon-client 2019-07-05 23:07:25 +02:00
Godmave 7d6a5bb6d2 Merge pull request #6 from Danielv123/master
getting up to date
2019-06-24 18:51:21 +02:00
Daniel bf2e78d530 Merge pull request #159 from veladon/master
Added cluster model js to /lib/
2019-06-10 14:11:04 -07:00
veladon bf4b7fe1b8 Added cluster model js to /lib/ 2019-06-10 21:40:31 +01:00
Daniel 66ba706c56 Use neural net dole divider by default on new installs
See "useNeuralNetDoleDivider": true,

If this causes issues, simply set it to false in config.json. If you find normal dole division to work badly as well, set disableFairItemDistribution to true.
2019-06-02 15:27:50 +02:00
Daniel 79c9ac839e Merge pull request #158 from axapaxa123/master
New NN, added debt system
2019-06-02 06:26:11 -07:00
Daniel 7f59b200b1 Merge pull request #153 from veladon/master
player to character inventory renames and extra info for EACCESS problem
2019-06-02 06:24:23 -07:00
axapaxa123 d719a73328 New NN, added debt system 2019-06-02 15:13:49 +02:00
axapaxa123 616bb9cc8f Merge pull request #1 from Danielv123/master
Merge pull request #152 from axapaxa123/master
2019-06-02 15:04:32 +02:00
veladon 3ae55cbd27 Updated defines.inventory.player_<x> to defines.inventory.character_<x> as per 0.17.35 change to rename character entity type from "player" to "character". 2019-05-13 20:41:15 +01:00
Daniel 7ae9ff6649 Merge pull request #152 from axapaxa123/master
New NN, updated required data
2019-05-13 10:42:53 -07:00
axapaxa123 0b3007d22d New NN, updated required data 2019-05-13 19:22:55 +02:00
veladon bd7a116860 Added extra instructions to readme for the EACCESS common problem 2019-05-12 18:10:44 +01:00
Daniel Vestøl 0c9ed582cc Fixed scenario_loader 2019-04-22 13:45:09 +02:00
42 changed files with 2336 additions and 283 deletions

No files matched your search

+3
View File
@@ -64,3 +64,6 @@ sharedPlugins/scenario_loader/scenarios
sharedPlugins/*/DISABLED sharedPlugins/*/DISABLED
\.cache-require-paths\.json \.cache-require-paths\.json
# IDE
.idea/
@@ -1,5 +1,13 @@
const needle = require("needle"); const needle = require("needle");
const config = require("./config.json"); 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 masterIP = `${config.masterIP}:${config.masterPort}`; const masterIP = `${config.masterIP}:${config.masterPort}`;
const token = config.masterAuthToken const token = config.masterAuthToken
+19
View File
@@ -0,0 +1,19 @@
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,6 +226,10 @@ 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. 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 ### InitializationError: First argument must be an abstract-leveldown compliant store
do: do:
+11 -6
View File
@@ -317,6 +317,7 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
"clientPort": args["rcon-port"] || process.env.RCONPORT || Math.floor(Math.random() * 65535), "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.", "__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), "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("Clusterio | Created instance with settings:")
console.log(instconf); console.log(instconf);
@@ -364,12 +365,14 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
data = data.toString("utf8").replace(/(\r\n\t|\n|\r\t)/gm,""); data = data.toString("utf8").replace(/(\r\n\t|\n|\r\t)/gm,"");
console.log(data); console.log(data);
if(data.includes("Starting RCON interface")){ if(data.includes("Starting RCON interface")){
let client = new Rcon(); let client = new Rcon({
packetResponseTimeout: 200000,
maxPending: 5
});
client.connect({ client.connect({
host: 'localhost', host: 'localhost',
port: Number(process.env.RCONPORT) || instconf.clientPort, port: Number(process.env.RCONPORT) || instconf.clientPort,
password: instconf.clientPassword, password: instconf.clientPassword
timeout: 5000
}); });
client.onDidAuthenticate(() => { client.onDidAuthenticate(() => {
console.log('Clusterio | RCON Authenticated!'); console.log('Clusterio | RCON Authenticated!');
@@ -493,8 +496,10 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
// connect to the server with rcon // connect to the server with rcon
if(true || process.platform != "linux"){ if(true || process.platform != "linux"){
// IP, port, password // IP, port, password
client = new Rcon(); client = new Rcon({
packetResponseTimeout: 200000, // 200s, should allow for commands up to 1250kB in length
maxPending: 5
});
// check the logfile to see if the RCON interface is running as there is no way to continue without it // 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 // we read the log every 2 seconds and stop looping when we start connecting to factorio
function checkRcon() { function checkRcon() {
@@ -505,7 +510,6 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
host: 'localhost', host: 'localhost',
port: args["rcon-port"] || Number(process.env.RCONPORT) || instanceconfig.clientPort, port: args["rcon-port"] || Number(process.env.RCONPORT) || instanceconfig.clientPort,
password: args["rcon-password"] || instanceconfig.clientPassword, password: args["rcon-password"] || instanceconfig.clientPassword,
timeout: 5000
}); });
} else { } else {
setTimeout(function(){ setTimeout(function(){
@@ -670,6 +674,7 @@ 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 publicIP: config.publicIP, // IP of the server should be global for all instances, so we pull that straight from the config
mods:modHashes, mods:modHashes,
instanceName: instance, instanceName: instance,
info: instanceconfig.info,
} }
if(playerCount){ if(playerCount){
payload.playerCount = playerCount.replace(/(\r\n\t|\n|\r\t)/gm, ""); payload.playerCount = playerCount.replace(/(\r\n\t|\n|\r\t)/gm, "");
+1 -1
View File
@@ -49,7 +49,7 @@
"logItemTransfers": false, "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.", "__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, "disableFairItemDistribution": false,
"useNeuralNetDoleDivider": false, "useNeuralNetDoleDivider": true,
"uploadModsToMaster": true, "uploadModsToMaster": true,
"msBetweenCommands": 10, "msBetweenCommands": 10,
"allowRemoteCommandExecution": true, "allowRemoteCommandExecution": true,
+57
View File
@@ -0,0 +1,57 @@
"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
@@ -0,0 +1,112 @@
"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
@@ -0,0 +1,8 @@
"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
@@ -0,0 +1,177 @@
"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
@@ -0,0 +1,10 @@
"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
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=IApiSlaves.js.map
+3
View File
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=IClusterioMasterProxy.js.map
+27
View File
@@ -0,0 +1,27 @@
"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
@@ -0,0 +1,24 @@
"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
@@ -0,0 +1,61 @@
"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
@@ -0,0 +1,29 @@
"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
@@ -0,0 +1,57 @@
"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
@@ -0,0 +1,16 @@
"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
@@ -0,0 +1,8 @@
"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
@@ -0,0 +1,64 @@
"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
@@ -0,0 +1,10 @@
"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
@@ -0,0 +1,10 @@
"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
@@ -0,0 +1,13 @@
"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 -35
View File
@@ -33,12 +33,14 @@ function sigmoid3(x)
//carry - carry for item X slave Y //carry - carry for item X slave Y
//prev_req - Numreq for previous request 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 //* 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 //Returns: Array of 3 elements
//0.Number of items to give in that dose //0.Number of items to give in that dose
//1.New dole for item X //1.New dole for item X
//2.New carry for item X slave Y //2.New carry for item X slave Y
function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_adj) //3.New debt
function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_adj,debt)
{ {
numreq=Number(numreq); numreq=Number(numreq);
instore=Number(instore); instore=Number(instore);
@@ -47,32 +49,37 @@ function Dose(numreq,instore,store_last_tick,dole,carry,prev_req,numreq_total_ad
var b=Math.trunc(prev_req/instore_adj*numreq); var b=Math.trunc(prev_req/instore_adj*numreq);
var inp1=[numreq,instore_adj,store_last_tick/numreq,dole,carry];//0..4 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];//5..9 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 inputs=inp1.concat(inp2); var inputs=inp1.concat(inp2);
var internal=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]; var internal=new Array(20).fill(0);
var outputs=[0,0,0]; var outputs=[0,0,0,0];
//NN: Please don't touch :), code is generated //NN: Please don't touch :), code is generated
outputs[0]=inputs[2]*3.101320028; outputs[0]=inputs[7]*0.8058582544;
outputs[0]=outputs[0]+inputs[7]*0.5471593142; outputs[0]=sigmoid2(outputs[0]);
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[1]=sigmoid0(outputs[1]);
outputs[2]=sigmoid2(outputs[2]); outputs[2]=sigmoid0(outputs[2]);
outputs[3]=inputs[6]*0.2294657379;
outputs[3]=sigmoid0(outputs[3]);
outputs[0]=Number(outputs[0]); debt=debt || 0;
if (outputs[0]<0) outputs[0]=0; if (outputs[0]>0.99) outputs[0]=1.0;
outputs[0]=outputs[0]*numreq;
var res1=Math.round(outputs[0]); var res1=Number(outputs[0])*numreq+outputs[3]+debt;
if (res1>numreq) res1=numreq; var res_capped=Math.round(res1);
if (res1>instore) res1=instore;
if (res1<0) res1=0; if (res_capped>numreq) res_capped=numreq;
carry=sigmoid0(outputs[2]); if (res_capped>instore) res_capped=instore;
return [res1,sigmoid0(outputs[1]),carry]; 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];
} }
//TICK - Updates values for item X, should be called once for each item for each round of all slaves requesting that item //TICK - Updates values for item X, should be called once for each item for each round of all slaves requesting that item
@@ -90,22 +97,16 @@ function Tick(instore,dole,store_last_tick,numreq_total_adj)
{ {
var instore_adj=instore; var instore_adj=instore;
if (instore_adj==0) instore_adj=0.1; if (instore_adj==0) instore_adj=0.1;
var inputs=[instore,dole,store_last_tick/instore_adj,store_last_tick/numreq_total_adj]; var inputs=[instore,dole,store_last_tick/instore_adj,store_last_tick/numreq_total_adj,store_last_tick,numreq_total_adj];
var internal=[0,0,0,0,0]; var internal=new Array(10).fill(0);
var outputs=[0,0]; var outputs=[0,0];
//NN: Please don't touch :), code is generated //NN: Please don't touch :), code is generated
internal[2]=sigmoid0(internal[2]); outputs[0]=inputs[3]*0.9364774823;
outputs[0]=internal[2]*0.4049594998; outputs[0]=outputs[0]*1.0*0.4590721726;
outputs[0]=outputs[0]+1.0*-5.026565075; outputs[0]=sigmoid2(outputs[0]);
outputs[0]=outputs[0]+inputs[0]*0.9100194573; outputs[1]=outputs[0]*1.828422785;
outputs[0]=outputs[0]+inputs[1]*-1.063795686; outputs[1]=sigmoid3(outputs[1]);
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]; 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 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
+87 -62
View File
@@ -235,7 +235,7 @@ const db = {};
(function(){ (function(){
try{ 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")`); console.log(`loading slaves from path.resolve(config.databaseDirectory, "slaves.json")`);
slaves = JSON.parse(fs.readFileSync(path.resolve(config.databaseDirectory, "slaves.json"))); slaves = JSON.parse(fs.readFileSync(path.resolve(config.databaseDirectory, "slaves.json")));
} catch (e){ } catch (e){
@@ -352,16 +352,18 @@ app.post("/api/editSlaveMeta", authenticate.middleware, function(req,res) {
// request.body should be an object // request.body should be an object
// {instanceID, pass, meta:{x,y,z}} // {instanceID, pass, meta:{x,y,z}}
if(req.body && req.body.instanceID && req.body.password && req.body.meta){ if(req.body && req.body.instanceID && req.body.meta){
// check for editing permissions if(slaves[req.body.instanceID]){
if(slaves[req.body.instanceID] && slaves[req.body.instanceID].rconPassword == req.body.password){ if(!slaves[req.body.instanceID].meta) {
if(!slaves[req.body.instanceID].meta){
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}); slaves[req.body.instanceID].meta = deepmerge(slaves[req.body.instanceID].meta, req.body.meta, {clone:true});
let metaPortion = JSON.stringify(req.body.meta); 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); // 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 { } else {
res.send("ERROR: Invalid instanceID or password") res.send("ERROR: Invalid instanceID or password")
} }
@@ -377,12 +379,20 @@ POST Get metadata from a single slave. For whenever you find the data returned b
app.post("/api/getSlaveMeta", function (req, res) { app.post("/api/getSlaveMeta", function (req, res) {
endpointHitCounter.labels(req.route.path).inc(); endpointHitCounter.labels(req.route.path).inc();
console.log("body", req.body); console.log("body", req.body);
if(req.body && req.body.instanceID && req.body.password){ if(req.body && req.body.instanceID){
console.log("returning meta for ", req.body.instanceID); if (slaves[req.body.instanceID]) {
res.send(JSON.stringify(slaves[req.body.instanceID].meta)) 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"}')
}
} else { } else {
res.status(400); res.status(400);
res.send('{"INVALID REQUEST":1}'); res.send('{"INVALID_REQUEST":1}');
} }
}); });
// mod management // mod management
@@ -393,7 +403,7 @@ POST Check if a mod has been uploaded to the master before. Only checks against
@instance @instance
@alias /api/checkMod @alias /api/checkMod
*/ */
app.post("/api/checkMod", function(req,res) { app.post("/api/checkMod", authenticate.middleware, function(req,res) {
endpointHitCounter.labels(req.route.path).inc(); endpointHitCounter.labels(req.route.path).inc();
let files = fs.readdirSync(masterModFolder); let files = fs.readdirSync(masterModFolder);
let found = false; let found = false;
@@ -434,37 +444,59 @@ 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 GET endpoint for getting information about all our slaves
@memberof clusterioMaster @memberof clusterioMaster
@instance @instance
@alias /api/slaves @alias /api/slaves
*/ */
let slaveCache = {
timestamp: Date.now(),
};
app.get("/api/slaves", function(req, res) { app.get("/api/slaves", function(req, res) {
endpointHitCounter.labels(req.route.path).inc(); endpointHitCounter.labels(req.route.path).inc();
res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if(!slaveCache.cache || Date.now() - slaveCache.timestamp > 5000){ let slaveCache = getSlaves();
let copyOfSlaves = JSON.parse(JSON.stringify(slaves)); for(key in slaveCache.cache) {
// filter out the rcon password because thats kindof not a safe thing to share slaveCache.cache[key].rconPassword = "hidden";
for(key in copyOfSlaves){
copyOfSlaves[key].rconPassword = "hidden";
}
slaveCache.cache = copyOfSlaves;
slaveCache.timestamp = Date.now();
} }
res.send(slaveCache.cache); res.send(slaveCache.cache);
}); });
var recievedItemStatisticsBySlaveID = {}; /**
var sentItemStatisticsBySlaveID = {}; 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 recievedItemStatistics = new averagedTimeSeries({ var recievedItemStatistics = new averagedTimeSeries({
maxEntries: config.itemStats.maxEntries, maxEntries: config.itemStats.maxEntries,
entriesPerSecond: config.itemStats.entriesPerSecond, entriesPerSecond: config.itemStats.entriesPerSecond,
}, console.log);*/ }, console.log);*/
var recievedItemStatisticsBySlaveID = {};
var sentItemStatisticsBySlaveID = {};
// //
/** /**
POST endpoint for storing items in master's inventory. POST endpoint for storing items in master's inventory.
@@ -697,45 +729,38 @@ 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')"} @param {object} JSON {instanceID:19412312, broadcast:false, command:"/c game.print('hello')"}
@returns {object} Status {auth: bool, message: "Informative error", data:{}} @returns {object} Status {auth: bool, message: "Informative error", data:{}}
*/ */
app.post("/api/runCommand", (req,res) => { app.post("/api/runCommand", authenticate.middleware, function(req,res) {
endpointHitCounter.labels(req.route.path).inc(); endpointHitCounter.labels(req.route.path).inc();
var token = req.headers['x-access-token']; // validate request.body
if(!token) return res.status(401).send({ auth: false, message: 'No token provided.' }); let body = req.body;
jwt.verify(token, config.masterAuthSecret, function(err, decoded) { if (
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.' }); body.broadcast
&& body.command
// validate request.body && typeof body.command == "string"
let body = req.body; && body.command[0] == "/")
{
if ( let instanceResponses = [];
body.broadcast for (let instanceID in wsSlaves) {
&& body.command // skip loop if the property is from prototype
&& typeof body.command == "string" if (!wsSlaves.hasOwnProperty(instanceID)) continue;
&& body.command[0] == "/") wsSlaves[instanceID].runCommand(body.command);
{
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. GET endpoint. Returns factorio's base locale as a JSON object.
+5 -3
View File
@@ -30,6 +30,7 @@
"bcryptjs": "^2.4.3", "bcryptjs": "^2.4.3",
"body-parser": "^1.18.2", "body-parser": "^1.18.2",
"cache-require-paths": "^0.3.0", "cache-require-paths": "^0.3.0",
"cli": "^1.0.1",
"cli-interact": "^0.1.9", "cli-interact": "^0.1.9",
"codecov": "^3.0.1", "codecov": "^3.0.1",
"compression": "^1.7.2", "compression": "^1.7.2",
@@ -56,19 +57,20 @@
"node-factorio-api": "^0.3.1-alpha", "node-factorio-api": "^0.3.1-alpha",
"node-forge": "^0.7.1", "node-forge": "^0.7.1",
"prom-client": "^10.2.3", "prom-client": "^10.2.3",
"rcon-client": "^3.0.2", "rcon-client": "git+https://gitlab.com/Hornwitser/rcon-client.git",
"request": "^2.83.0", "request": "^2.83.0",
"right-pad": "^1.0.1", "right-pad": "^1.0.1",
"rmdir-sync": "^1.0.1", "rmdir-sync": "^1.0.1",
"sanitizer": "^0.1.3", "sanitizer": "^0.1.3",
"simple-git": "^1.96.0", "simple-git": "^1.96.0",
"single-line-log": "^1.1.2", "single-line-log": "^1.1.2",
"snyk": "^1.134.2",
"socket.io": "^2.0.4", "socket.io": "^2.0.4",
"socket.io-client": "^2.0.4", "socket.io-client": "^2.0.4",
"socket.io-prometheus": "^0.2.1", "socket.io-prometheus": "^0.2.1",
"sync-request": "^4.0.1", "sync-request": "^4.0.1",
"underscore": "^1.8.3", "typescript-collections": "^1.3.2",
"snyk": "^1.134.2" "underscore": "^1.8.3"
}, },
"devDependencies": { "devDependencies": {
"documentation": "^5.4.0", "documentation": "^5.4.0",
+4 -1
View File
@@ -33,6 +33,7 @@ class neuralDole {
this.carry = {} this.carry = {}
this.lastRequest = {} this.lastRequest = {}
this.stats=[] this.stats=[]
this.debt= {}
setInterval(()=>{ setInterval(()=>{
for(let name in this.items){ for(let name in this.items){
@@ -71,7 +72,8 @@ class neuralDole {
this.dole[object.name], this.dole[object.name],
this.carry[object.name+" "+object.instanceID] || 0, this.carry[object.name+" "+object.instanceID] || 0,
this.lastRequest[object.name+"_"+object.instanceID+"_"+object.instanceName] || 0, this.lastRequest[object.name+"_"+object.instanceID+"_"+object.instanceName] || 0,
this.getRequestStats(object.name,5) this.getRequestStats(object.name,5),
this.debt[object.name+" "+object.instanceID] || 0
) )
if ((this.stats[object.name] || []).length>0) if ((this.stats[object.name] || []).length>0)
{ {
@@ -84,6 +86,7 @@ class neuralDole {
//2.New carry for item X slave Y //2.New carry for item X slave Y
this.dole[object.name] = magicData[1] this.dole[object.name] = magicData[1]
this.carry[object.name+" "+object.instanceID] = magicData[2] this.carry[object.name+" "+object.instanceID] = magicData[2]
this.debt[object.name+" "+object.instanceID] = magicData[3]
// Remove item from DB and send it // Remove item from DB and send it
if(this.items.removeItem({count: magicData[0], name: object.name})){ if(this.items.removeItem({count: magicData[0], name: object.name})){
+149
View File
@@ -0,0 +1,149 @@
--[[
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
@@ -0,0 +1,251 @@
-- 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
@@ -0,0 +1,266 @@
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
@@ -0,0 +1,309 @@
-- 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
+8 -5
View File
@@ -22,12 +22,15 @@ module.exports = class remoteCommands {
words.shift(); words.shift();
words.shift(); words.shift();
words.shift(); words.shift();
let message = words.join(" ").replace(/[";]/g, " ").replace(/[']/g,"").replace("/shout ", "").replace("!shout", ""); let message = words.join(" ").replace(/[";]/g, " ").replace(/[']/g,"").replace("/shout ", "").replace("!shout ","").replace(/\[gps=.*?\]/g, "").replace(/\[train=.*?\]/g, "");
let instance = await this.getInstanceName(data.instanceID); let instance = await this.getInstanceName(data.instanceID);
if(data.data.includes("[gps=")){
this.messageInterface("/silent-command game.print('[" + instance + "] " + message + "')"); //Nothing
} }else{
this.messageInterface("/silent-command game.print('[" + instance + "] " + message + "')");
}
}
} }
}); });
} }
@@ -1,7 +1,7 @@
-- Lua cheatsheet -- 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_quickbar().get_contents()))
/c game.write_file("test.txt",serpent.block(game.players["Danielv123"].get_inventory(defines.inventory.player_main).get_contents())) /c game.write_file("test.txt",serpent.block(game.players["Danielv123"].get_inventory(defines.inventory.character_main).get_contents()))
-- print JSON of entire inventory and request slots to file -- print JSON of entire inventory and request slots to file
-- TODO: Get requester slot data as well -- TODO: Get requester slot data as well
@@ -34,7 +34,7 @@ for a,b in pairs(game.players) do
else else
notFirst = true notFirst = true
end end
local inventory = game.players[a].get_inventory(defines.inventory.player_main).get_contents() local inventory = game.players[a].get_inventory(defines.inventory.character_main).get_contents()
local quickbar = game.players[a].get_quickbar().get_contents() local quickbar = game.players[a].get_quickbar().get_contents()
--[[ Get contents of requester slots into a table ]] --[[ Get contents of requester slots into a table ]]
local requests = {} local requests = {}
@@ -95,13 +95,13 @@ enclose in ''
]] ]]
-- minified strings (newest on the top) -- minified strings (newest on the top)
local a='+"'"+'{"players":{'+"'"+'local b=false;for c,d in pairs(game.players)do if d.connected then if b then a=a..'+"'"+','+"'"+'else b=true end;local e=game.players[c].get_inventory(defines.inventory.player_main).get_contents()local f=game.players[c].get_quickbar().get_contents()local g={}for h=1,game.players[c].force.character_logistic_slot_count do g[h]=game.players[c].character.get_request_slot(h)end;a=a..'+"'"+'"'+"'"+'..c..'+"'"+'":{"inventory":{'+"'"+'local i=false;for j,k in pairs(e)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;for j,k in pairs(f)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..j..'+"'"+'":'+"'"+'..k end;a=a..'+"'"+'},"requestSlots":{'+"'"+'i=false;for j,k in pairs(g)do if i then a=a..'+"'"+','+"'"+'else i=true end;a=a..'+"'"+'"'+"'"+'..k['+"'"+'name'+"'"+']..'+"'"+'":'+"'"+'..k['+"'"+'count'+"'"+']end;a=a..'+"'"+'}}'+"'"+'end end;a=a.."}}"game.write_file("'+outputFile+'",a,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.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)
-- offline playes -- offline playes
local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.player_main).get_contents()local e=game.players[b].get_quickbar().get_contents()local f={}for g=1,game.players[b].force.character_logistic_slot_count do f[g]=game.players[b].character.get_request_slot(g)end;a=a..b..":{inventory:{"for h,i in pairs(d)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;for h,i in pairs(e)do a=a.."['+"'"+'"..h.."'+"'"+']:"..i..","end;a=a.."},requestSlots:{"for h,i in pairs(f)do a=a.."['+"'"+'"..i["name"].."'+"'"+']:"..i["count"]..", "end;a=a.."}},"end end;a=a.."}"game.print(a) local a="{"for b,c in pairs(game.players)do if c.connected then local d=game.players[b].get_inventory(defines.inventory.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)
-- multiple players -- multiple players
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="{"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.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 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.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) 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)
-- insert items in players inventory and export leftovers -- insert items in players inventory and export leftovers
+18 -3
View File
@@ -1,9 +1,24 @@
/silent-command
local data = {}; local data = {};
local force = game.forces["player"];
for _, tech in pairs(game.forces["player"].technologies) do for _, tech in pairs(force.technologies) do
table.insert(data, tech.name .. ":" .. tostring(tech.researched) .. ":" .. tostring(tech.level)); 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);
end end
game.write_file("researchSync.txt", table.concat(data, "\n"), false, 0) game.write_file("researchSync.txt", table.concat(data, "\n"), false, 0)
+24 -14
View File
@@ -1,16 +1,26 @@
/silent-command
if game.forces['player'].technologies['{tech_name}'] then local function updateTech()
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}) 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 {notify} then
if game.forces['player'].technologies['{tech_name}'].researched == false then if 1 == {tech_infinite} then
game.print("Technology {tech_name} synced at level {tech_level}") game.print("Infinite technology {tech_name} level {tech_level} unlocked")
else else
game.print("Technology {tech_name} synced") game.print("Technology {tech_name} researched")
end end
game.play_sound({path="utility/research_completed"})
end
end
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
end end
+362 -112
View File
@@ -1,127 +1,377 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path')
const needle = require("needle"); const needle = require("needle");
function time() {
let d = new Date()
return `${d.getUTCHours()}:${d.getUTCMinutes()}:${d.getUTCSeconds()}.${d.getUTCMilliseconds()}`
}
class ResearchSync { class ResearchSync {
constructor(slaveConfig, messageInterface, extras = {}){ constructor(slaveConfig, messageInterface, extras = {}){
this.config = slaveConfig; this.config = slaveConfig
this.messageInterface = messageInterface; this.messageInterface = messageInterface
this.functions = this.loadFunctions();
this.research = {}; this.functions = {
setInterval(() => {
this.pollResearch();
setTimeout(this.doSync.bind(this), 2000);
}, extras.researchSyncPollInterval || 5000);
}
pollResearch() {
// this.messageInterface("Polling Research\n")
this.messageInterface(this.functions.dumpResearch);
}
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;
}
let needResearch = {};
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)};
}
});
}
});
let difference = this.filterResearchDiff(this.research, needResearch);
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];
}
});
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?
});
});
}
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};
}
}
}
});
return diff;
}
loadFunctions() {
return {
dumpResearch: this.loadFunc("dumpResearch.lua"), dumpResearch: this.loadFunc("dumpResearch.lua"),
enableResearch: this.loadFunc("enableResearch.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)
)
} }
loadFunc(path) { log(data) {
return fs.readFileSync("sharedPlugins/researchSync/" + path,'utf-8').replace(/\r?\n|\r/g,' '); try {
console.log(`researchSync: ${data}`)
fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
} catch (e) {
console.error(e)
}
} }
scriptOutput(data){
let kv = data.split(":"); error(data) {
let name = kv[0]; try {
let researched = ('true' !== kv[1] console.error(`researchSync: ${data}`)
? 0 fs.appendFileSync(this.log_file, `${time()}: ${data}\n`);
: 1); } catch (e) {
let level = parseInt(kv[2]); console.error(e)
if (!isNaN(level) && !isNaN(researched)) { }
this.research[name] = {researched: researched, level: level}; }
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() {
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))
}
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;
}
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
)
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 to_research = this.filter_researched_techs(cluster_techs)
let to_update_progress = this.filter_updated_techs(cluster_techs, to_research)
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
}
}
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
}
} else {
cluster_techs[name] = node_tech
}
}
}
return cluster_techs
}
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
}
}
}
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;
}
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
} }
} }
} }
+3 -3
View File
@@ -9,7 +9,7 @@ describe("researchSync/index.js", ()=>{
assert.equal(typeof researchSync, "function"); assert.equal(typeof researchSync, "function");
}); });
describe("class researchSync()", ()=>{ describe("class researchSync()", ()=>{
it(".filterResearchDiff(object1, object2) returns research that needs enabling on the local instance", ()=>{ it(".filter_researched_techs(object1, object2) returns research that needs enabling on the local instance", ()=>{
let reSync = new researchSync({}, function(){}); let reSync = new researchSync({}, function(){});
let obj1 = { let obj1 = {
@@ -24,9 +24,9 @@ describe("researchSync/index.js", ()=>{
c:{researched: false, level: 0}, c:{researched: false, level: 0},
e:{researched: false, level: 50}, e:{researched: false, level: 50},
}; };
assert(typeof reSync.filterResearchDiff == "function"); assert(typeof reSync.filter_researched_techs == "function");
let diffResult = reSync.filterResearchDiff(obj1, obj2); let diffResult = reSync.filter_researched_techs(obj1, obj2);
assert(diffResult.a === undefined); assert(diffResult.a === undefined);
@@ -0,0 +1,33 @@
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)
+11 -18
View File
@@ -42,25 +42,8 @@ module.exports = class remoteCommands {
data, data,
}); });
} }
let fileMap = await loadFiles("", scenPath);
let files = {};
fileMap.forEach(map => {
files[map.name] = map.data;
console.log(map.name);
});
let fileImportString = `\{`;
for(let k in files){
let name = k.replace(/\\/g, '/');
name = name.replace(/ /g, '');
// make sure *not* to include control.lua as it is provided as a seperate argument
if(name != "control") fileImportString += `["${name}"] = '${files[k]}', `;
}
fileImportString += `\}`;
if(files.control) var returnValue = await messageInterface(`/silent-command remote.call('hotpatch', 'update', '${scenarios[i]}', '1.0.0', '${files.control}', ${fileImportString})`);
if(returnValue) messageInterface(returnValue);
messageInterface(`Loaded scenario ${scenarios[i]} in ${Math.floor(Date.now()-startTime)}ms`);
} }
return returnFiles;
} }
let fileMap = await loadFiles("", scenPath); let fileMap = await loadFiles("", scenPath);
let files = {}; let files = {};
@@ -120,4 +103,14 @@ class AsyncArray extends Array {
super(arr) super(arr)
this.data = arr; // In place of Array subclassing this.data = arr; // In place of Array subclassing
} }
async filterAsync(predicate) {
// Take a copy of the array, it might mutate by the time we've finished
const data = Array.from(this.data);
// Transform all the elements into an array of promises using the predicate
// as the promise
const result = await Promise.all(data.map((element, index) => predicate(element, index, data)));
return data.filter((element, index) => {
return result[index];
});
}
} }
+11 -5
View File
@@ -20,13 +20,19 @@ module.exports = class remoteCommands {
setInterval(async () => { setInterval(async () => {
let stats = await this.getStats(); let stats = await this.getStats();
stats.forEach(force => { stats.forEach(force => {
needle.post(`${mergedConfig.masterIP}:${mergedConfig.masterPort}/api/logStats`, { // 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 = {
instanceID: this.config.unique, instanceID: this.config.unique,
force: force.forceName, force: force.forceName,
data: force.data, forceData: JSON.stringify(force.data),
}, needleOptionsWithTokenAuthHeader, (err, resp) => { };
if(!err && resp.body && resp.body.ok == false) console.log(resp.body); 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);
}
);
}); });
}, 15000); }, 15000);
} }
@@ -26,18 +26,22 @@ class masterPlugin {
@returns {string} failure @returns {string} failure
*/ */
app.post("/api/logStats", authenticate.middleware, function(req,res) { 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(); // endpointHitCounter.labels(req.route.path).inc();
if(typeof req.body == "object" if(typeof req.body == "object"
&& req.body.instanceID && req.body.instanceID
&& typeof req.body.instanceID == "string" && typeof req.body.instanceID == "string"
&& req.body.instanceID.length < 100 && req.body.instanceID.length < 100
&& req.body.data && data
&& typeof req.body.data == "object" && typeof data == "object"
&& !Array.isArray(req.body.data)){ && !Array.isArray(data)){
try{ try{
Object.keys(req.body.data).forEach(statisticName => { Object.keys(data).forEach(statisticName => {
Object.keys(req.body.data[statisticName]).forEach(direction => { Object.keys(data[statisticName]).forEach(direction => {
let statHash = req.body.data[statisticName][direction]; let statHash = data[statisticName][direction];
Object.keys(statHash).forEach(item => { Object.keys(statHash).forEach(item => {
let value = Number(statHash[item]); let value = Number(statHash[item]);
if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value); if(value) prometheusStatisticsGauge.labels(req.body.instanceID, req.body.force, statisticName, direction, item).set(value);
@@ -50,7 +54,12 @@ class masterPlugin {
console.log(e) console.log(e)
}; };
} else { } else {
res.send("failure"); 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"
});
} }
}); });
} }