-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorio.js
120 lines (101 loc) · 2.69 KB
/
factorio.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const Rcon = require('rcon');
const cron = require('node-cron');
const { host, port, password } = require('./data/config.json');
const { format, parse } = require('lua-json');
// connect to factorio
const factorio = new Rcon(host, port, password);
let activeConnection = false;
const stats = {};
const factorioInit = () =>
{
factorio.on('auth', function()
{
activeConnection = true;
console.log('Connected');
// update stats
factorio.send('/players online');
// Turned off for Space Age
// factorio.send('/silent-command rcon.print("[TICK] " .. game.tick)');
// updateCME();
}).on('response', function(str)
{
parseResponse(str);
}).on('error', function(err)
{
console.log('RCON: ' + err);
console.log("Using" + host + ":" + port + ", " + password);
}).on('end', function()
{
activeConnection = false;
console.log('Connection closed');
});
factorio.connect();
// This data doesn't actually change very often so every 6 hours is plenty
//cron.schedule('0 */6 * * *', () =>
//{
// updateCME();
//});
// because ticks can vary in length we need to update the current tick regularly
cron.schedule('* * * * *', () =>
{
// factorio.send('/silent-command rcon.print("[TICK] " .. game.tick)');
factorio.send('/players online');
});
};
// Returns boolean whether there is currently an active connection
const isConnected = () =>
{
return activeConnection;
};
const updateOnlinePlayers = () =>
{
if (!activeConnection)
{
factorioInit();
}
else
{
factorio.send('/players online');
}
};
function updateCME()
{
if (!activeConnection)
{
console.log('Could not run command, no active connection');
return false;
}
const command = '/silent-command rcon.print("[CME] " .. serpent.line(remote.call("space-exploration", "get_solar_flares")))';
factorio.send(command);
// update the current tick as well
factorio.send('/silent-command rcon.print("[TICK] " .. game.tick)');
}
function relayDiscordMessage(message)
{
if (!activeConnection)
{
factorioInit();
}
const cleanMessage = message.replace(/'/g, '\\\'');
const command = `/silent-command game.print('[color=#7289DA][Discord]${cleanMessage}[/color]')`;
factorio.send(command);
}
function parseResponse(string)
{
const prefixes = { players: 'Online players ', cme: '[CME] ', tick: '[TICK] ' };
for (const property in prefixes)
{
if (string.startsWith(prefixes[property]))
{
let data = string.replace(prefixes[property], '');
if (property == 'cme')
{
console.log(data);
// convert = to : for json parsing
data = parse('return ' + data);
}
stats[property] = data;
}
}
}
module.exports = { factorio, factorioInit, updateOnlinePlayers, stats, relayDiscordMessage, isConnected, updateCME };