Skip to content

Commit aa0254f

Browse files
authored
Merge pull request #129 from oof2win2/bugfix/upshandler
2 parents a71eb4c + 58570fd commit aa0254f

File tree

1 file changed

+115
-116
lines changed

1 file changed

+115
-116
lines changed

src/helpers/serverUPSHandler.ts

Lines changed: 115 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -5,135 +5,134 @@ import rcon from "./rcon";
55
import Tails, { playerJoinData, playerLeaveData } from "../base/Tails";
66
import serversJS from "../servers";
77
import { FactorioServer } from "../types";
8-
import discord from "discord.js";
98

109
// Known issue: servers will report a UPS of 0 if the bot starts and nobody is online on the server
1110

1211
interface UPSServer extends FactorioServer {
13-
ups: number;
14-
previousTick: number;
15-
playercount: number;
12+
ups: number;
13+
previousTick: number;
14+
playercount: number;
1615
}
1716

1817
/**
1918
* @classdesc UPS handler, generates data that can be fetched. Doesn't historically store it
2019
*/
2120
class UPSManager {
22-
private servers: UPSServer[];
23-
private _processing: boolean;
24-
constructor(servers: FactorioServer[]) {
25-
this._processing = false;
26-
this.servers = servers.map((server) => {
27-
return {
28-
...server,
29-
playercount: 0,
30-
ups: 0,
31-
previousTick: 0,
32-
};
33-
});
34-
Object.keys(servers).forEach((serverKey) => {
35-
this.servers[serverKey].ups = 0;
36-
this.servers[serverKey].playercount = 0;
37-
this.servers[serverKey].previousTick = 0;
38-
setTimeout(() => {
39-
rcon
40-
.rconCommand(
41-
`/sc global.ext = {}; rcon.print(#game.connected_players)`,
42-
this.servers[serverKey].discordid
43-
)
44-
.then((output) => {
45-
try {
46-
this.servers[serverKey].playercount = parseInt(output.resp);
47-
} catch {}
48-
})
49-
.catch(() => {});
50-
}, 2000); // wait for rcon to init
51-
});
52-
Tails.on("playerJoin", (log) => this.playerStuff(log));
53-
Tails.on("playerLeave", (log) => this.playerStuff(log));
54-
setInterval(() => {
55-
if (!this._processing) this.getData();
56-
}, 1000);
57-
}
21+
private servers: UPSServer[];
22+
private _processing: boolean;
23+
constructor(servers: FactorioServer[]) {
24+
this._processing = false;
25+
this.servers = servers.map((server) => {
26+
return {
27+
...server,
28+
playercount: 0,
29+
ups: 0,
30+
previousTick: 0,
31+
};
32+
});
33+
Object.keys(servers).forEach((serverKey) => {
34+
this.servers[serverKey].ups = 0;
35+
this.servers[serverKey].playercount = 0;
36+
this.servers[serverKey].previousTick = 0;
37+
setTimeout(() => {
38+
rcon
39+
.rconCommand(
40+
`/sc global.ext = {}; rcon.print(#game.connected_players)`,
41+
this.servers[serverKey].discordid
42+
)
43+
.then((output) => {
44+
try {
45+
this.servers[serverKey].playercount = parseInt(output.resp);
46+
} catch { }
47+
})
48+
.catch(() => { });
49+
}, 2000); // wait for rcon to init
50+
});
51+
Tails.on("playerJoin", (log) => this.playerStuff(log));
52+
Tails.on("playerLeave", (log) => this.playerStuff(log));
53+
setInterval(() => {
54+
if (!this._processing) this.getData();
55+
}, 1000);
56+
}
5857

59-
get processing() {
60-
return this._processing;
61-
}
58+
get processing() {
59+
return this._processing;
60+
}
6261

63-
playerStuff(data: playerJoinData | playerLeaveData) {
64-
const line = data.line;
65-
const server = data.server;
66-
if (line.type === "join") {
67-
Object.keys(this.servers).forEach((serverKey) => {
68-
if (this.servers[serverKey]?.discordid === server.discordid)
69-
this.servers[serverKey].playercount++;
70-
if (this.servers[serverKey] == 1) {
71-
rcon.rconCommand("/sc game.tick_paused = true", server.discordid)
62+
playerStuff(data: playerJoinData | playerLeaveData) {
63+
const line = data.line;
64+
const server = data.server;
65+
if (line.type === "join") {
66+
Object.keys(this.servers).forEach((serverKey) => {
67+
if (this.servers[serverKey]?.discordid === server.discordid)
68+
this.servers[serverKey].playercount++;
69+
if (this.servers[serverKey].playercount == 1) {
70+
rcon.rconCommand("/sc game.tick_paused = false", server.discordid)
71+
}
72+
});
7273
}
73-
});
74-
}
75-
if (line.type === "leave") {
76-
Object.keys(this.servers).forEach((serverKey) => {
77-
if (this.servers[serverKey]?.discordid === server.discordid)
78-
this.servers[serverKey].playercount--;
79-
if (this.servers[serverKey] == 0) {
80-
rcon.rconCommand("/sc game.tick_paused = true", server.discordid)
81-
}
82-
});
83-
}
84-
}
85-
private async getData() {
86-
this._processing = true;
87-
let promiseArray = this.servers.map(async (server) => {
88-
if (server.playercount !== 0) {
89-
try {
90-
let response = await rcon
91-
.rconCommand("/sc rcon.print(game.tick)", server.discordid)
92-
.catch(() => {});
93-
if (response) {
94-
server.ups = Math.abs(
95-
server.previousTick - parseInt(response.resp)
96-
);
97-
server.previousTick = parseInt(response.resp);
98-
}
99-
} catch {}
100-
try {
101-
rcon
102-
.rconCommand(
103-
`/sc global.ext = game.json_to_table('${JSON.stringify({
104-
var: {
105-
server_ups: server.ups,
106-
status: "Online",
107-
},
108-
servers: [
109-
{
110-
name: "All-Weekend Factorio",
111-
welcome: "Welcome to All-Weekend Factorio servers!",
112-
reset_time: "Depends on server",
113-
branch: "Unknown",
114-
},
115-
],
116-
current: 1,
117-
})}')`,
118-
server.discordid
119-
)
120-
.then(() => {})
121-
.catch(() => {});
122-
} catch {}
123-
}
124-
return server;
125-
});
126-
let serversUpdated = await Promise.all(promiseArray);
127-
this.servers = serversUpdated;
128-
this._processing = false;
129-
}
130-
/**
131-
* Get current data for handling
132-
* @returns {UPS[]} - Collected data
133-
*/
134-
exportData(): UPSServer[] {
135-
return this.servers;
136-
}
74+
if (line.type === "leave") {
75+
Object.keys(this.servers).forEach((serverKey) => {
76+
if (this.servers[serverKey]?.discordid === server.discordid)
77+
this.servers[serverKey].playercount--;
78+
if (this.servers[serverKey].playercount == 0) {
79+
rcon.rconCommand("/sc game.tick_paused = true", server.discordid)
80+
}
81+
});
82+
}
83+
}
84+
private async getData() {
85+
this._processing = true;
86+
let promiseArray = this.servers.map(async (server) => {
87+
if (server.playercount !== 0) {
88+
try {
89+
let response = await rcon
90+
.rconCommand("/sc rcon.print(game.tick)", server.discordid)
91+
.catch(() => { });
92+
if (response) {
93+
server.ups = Math.abs(
94+
server.previousTick - parseInt(response.resp)
95+
);
96+
server.previousTick = parseInt(response.resp);
97+
}
98+
} catch { }
99+
try {
100+
rcon
101+
.rconCommand(
102+
`/sc global.ext = game.json_to_table('${JSON.stringify({
103+
var: {
104+
server_ups: server.ups,
105+
status: "Online",
106+
},
107+
servers: [
108+
{
109+
name: "All-Weekend Factorio",
110+
welcome: "Welcome to All-Weekend Factorio servers!",
111+
reset_time: "Depends on server",
112+
branch: "Unknown",
113+
},
114+
],
115+
current: 1,
116+
})}')`,
117+
server.discordid
118+
)
119+
.then(() => { })
120+
.catch(() => { });
121+
} catch { }
122+
}
123+
return server;
124+
});
125+
let serversUpdated = await Promise.all(promiseArray);
126+
this.servers = serversUpdated;
127+
this._processing = false;
128+
}
129+
/**
130+
* Get current data for handling
131+
* @returns {UPS[]} - Collected data
132+
*/
133+
exportData(): UPSServer[] {
134+
return this.servers;
135+
}
137136
}
138137

139138
const UPSHandler = new UPSManager(serversJS);

0 commit comments

Comments
 (0)