forked from Danielv123/factorioClusterioClient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
122 lines (110 loc) · 3.59 KB
/
main.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
121
122
const {ipcRenderer} = require('electron');
const ipc = ipcRenderer;
const Config = require('electron-config');
const config = new Config();
const electron = require("electron");
const remote = require("electron").remote;
const mainProcess = remote.require("./index");
setInterval(function(){
// IPC send sends text/json/whatever to index.js or anything on the nodeside that cares to listen
ipc.send('getServers', "plz send");
}, 500)
var globalData;
ipc.on("setServers", function(event, data){
//console.log(data)
globalData = data;
let html = "";
// generate title row
let keys = {
"Slave ID":"unique",
"IP":"publicIP",
"mods":"mods",
"Status":"time",
"Players":"playerCount",
}
// start with printing keys
html += "<tr>"
for (let key in keys) {
html += "<td>"+key+"</td>";
}
html += "</tr>"
// keep going with printing slaves corresponding to the keys
for(let slave in data){
// only print servers that has been online some time the last 60 seconds (in ms)
if(Date.now() - data[slave].time < 120000) {
html += "<tr>"
for (let key in keys) {
let currentKey = keys[key]
if(currentKey == "time") {
// print time in seconds instead of unix time
let seconds = Math.floor((Date.now()-data[slave][currentKey])/1000)
// if slave pinged last 15 seconds, display it as online
// otherwise alert us to the fact that it is missing.
if (seconds < 15) {
html += "<td style='min-width:110px;'>Online</td>";
} else {
html += "<td style='min-width:110px;'>seen "+seconds+"s ago</td>";
}
} else if (currentKey == "mods") {
// Make modlist look nice
html += "<td>"
for(let i = 0;i<data[slave].mods.length;i++){
// print one mod per line
html += data[slave].mods[i].modName + "\n"
}
html += "</td>"
} else if (currentKey == "publicIP") {
html += "<td>"+data[slave][currentKey] + ":" + data[slave].serverPort
} else {
if (data[slave][keys[key]]){
html += "<td>"+data[slave][keys[key]]+"</td>"
} else {
html += "<td>Unsupported</td>"
}
}
}
html += '<td id="temp" onclick="launchFactorio(this)"><button class="button launchButton">Join server</button></td>'
html += "</tr>"
}
}
// turn our HTML text into dom elements for comparison
var div = document.createElement('div');
div.innerHTML = html;
var div2 = document.createElement('div');
div2.innerHTML = document.querySelector("#slaves").innerHTML;
// use special browser function for comparing dom elements
if(!div.isEqualNode(div2)) {
document.querySelector("#slaves").innerHTML = html
}
});
// tell node to launch factorio
// this is some shitty patchwork solution that is 100% guaranteed to break
function launchFactorio(element) {
console.log(element.parentElement.querySelectorAll("td")[0].innerHTML)
data = globalData[element.parentElement.querySelectorAll("td")[0].innerHTML]
let object = {
ip:data.publicIP,
port:data.serverPort,
rconPort:data.rconPort,
mods:data.mods,
};
console.log(object);
ipc.send("launchFactorio", object);
}
// populate master address box at load
document.querySelector('#masterInput').value = config.get("masterAddress")
// change config when master address box changes
function setMasterAddress() {
// check that the port is probable and won't crash us
let str = document.querySelector('#masterInput').value;
if(str.substr(str.indexOf(":") + 1) < 65535) {
config.set('masterAddress', str);
}
}
// IPC listeners
setInterval(function(){
ipc.send("tick")
}, 100);
ipc.on("printStatus", function(event, string){
document.querySelector("#dataDisplay").innerHTML = string
});