Skip to content

Commit 22b3259

Browse files
authored
Merge pull request #130 from oof2win2/bugfix/upshandler
Tested on server and works (for now)
2 parents aa0254f + 1049e50 commit 22b3259

File tree

1 file changed

+20
-21
lines changed

1 file changed

+20
-21
lines changed

src/helpers/serverUPSHandler.ts

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ interface UPSServer extends FactorioServer {
1515
}
1616

1717
/**
18-
* @classdesc UPS handler, generates data that can be fetched. Doesn't historically store it
19-
*/
18+
* @classdesc UPS handler, generates data that can be fetched. Doesn't historically store it
19+
*/
2020
class UPSManager {
2121
private servers: UPSServer[];
2222
private _processing: boolean;
@@ -42,7 +42,13 @@ class UPSManager {
4242
)
4343
.then((output) => {
4444
try {
45-
this.servers[serverKey].playercount = parseInt(output.resp);
45+
const playercount = parseInt(output.resp)
46+
this.servers[serverKey].playercount = playercount;
47+
if (playercount > 0) {
48+
rcon.rconCommand(`/interface game.tick_paused = false`, this.servers[serverKey].discordid)
49+
} else {
50+
rcon.rconCommand(`/interface game.tick_paused = true`, this.servers[serverKey].discordid)
51+
}
4652
} catch { }
4753
})
4854
.catch(() => { });
@@ -61,24 +67,16 @@ class UPSManager {
6167

6268
playerStuff(data: playerJoinData | playerLeaveData) {
6369
const line = data.line;
64-
const server = data.server;
70+
const server = this.servers.find(s => s.discordid === data.server.discordid)
6571
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-
});
72+
server.playercount++
73+
if (server.playercount > 0)
74+
rcon.rconCommand("/sc game.tick_paused = false", server.discordid)
7375
}
7476
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-
});
77+
server.playercount--
78+
if (server.playercount === 0)
79+
rcon.rconCommand("/sc game.tick_paused = true", server.discordid)
8280
}
8381
}
8482
private async getData() {
@@ -127,13 +125,14 @@ class UPSManager {
127125
this._processing = false;
128126
}
129127
/**
130-
* Get current data for handling
131-
* @returns {UPS[]} - Collected data
132-
*/
128+
* Get current data for handling
129+
* @returns {UPS[]} - Collected data
130+
*/
133131
exportData(): UPSServer[] {
134132
return this.servers;
135133
}
136134
}
137135

138136
const UPSHandler = new UPSManager(serversJS);
139137
export default UPSHandler;
138+

0 commit comments

Comments
 (0)