Skip to content

Commit 0747f1d

Browse files
committed
🐛 fix stop server command getting sent to all
1 parent aa0254f commit 0747f1d

File tree

1 file changed

+135
-136
lines changed

1 file changed

+135
-136
lines changed

src/helpers/serverUPSHandler.ts

Lines changed: 135 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,138 @@
11
/**
22
* @file Handles UPS
33
*/
4-
import rcon from "./rcon";
5-
import Tails, { playerJoinData, playerLeaveData } from "../base/Tails";
6-
import serversJS from "../servers";
7-
import { FactorioServer } from "../types";
8-
9-
// Known issue: servers will report a UPS of 0 if the bot starts and nobody is online on the server
10-
11-
interface UPSServer extends FactorioServer {
12-
ups: number;
13-
previousTick: number;
14-
playercount: number;
15-
}
16-
17-
/**
18-
* @classdesc UPS handler, generates data that can be fetched. Doesn't historically store it
19-
*/
20-
class UPSManager {
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-
}
57-
58-
get processing() {
59-
return this._processing;
60-
}
61-
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-
});
73-
}
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-
}
136-
}
137-
138-
const UPSHandler = new UPSManager(serversJS);
139-
export default UPSHandler;
4+
import rcon from "./rcon";
5+
import Tails, { playerJoinData, playerLeaveData } from "../base/Tails";
6+
import serversJS from "../servers";
7+
import { FactorioServer } from "../types";
8+
9+
// Known issue: servers will report a UPS of 0 if the bot starts and nobody is online on the server
10+
11+
interface UPSServer extends FactorioServer {
12+
ups: number;
13+
previousTick: number;
14+
playercount: number;
15+
}
16+
17+
/**
18+
* @classdesc UPS handler, generates data that can be fetched. Doesn't historically store it
19+
*/
20+
class UPSManager {
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+
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+
}
52+
} catch { }
53+
})
54+
.catch(() => { });
55+
}, 2000); // wait for rcon to init
56+
});
57+
Tails.on("playerJoin", (log) => this.playerStuff(log));
58+
Tails.on("playerLeave", (log) => this.playerStuff(log));
59+
setInterval(() => {
60+
if (!this._processing) this.getData();
61+
}, 1000);
62+
}
63+
64+
get processing() {
65+
return this._processing;
66+
}
67+
68+
playerStuff(data: playerJoinData | playerLeaveData) {
69+
const line = data.line;
70+
const server = this.servers.find(s => s.discordid === data.server.discordid)
71+
if (line.type === "join") {
72+
server.playercount++
73+
if (server.playercount > 0)
74+
rcon.rconCommand("/sc game.tick_paused = false", server.discordid)
75+
}
76+
if (line.type === "leave") {
77+
server.playercount--
78+
if (server.playercount === 0)
79+
rcon.rconCommand("/sc game.tick_paused = true", server.discordid)
80+
}
81+
}
82+
private async getData() {
83+
this._processing = true;
84+
let promiseArray = this.servers.map(async (server) => {
85+
if (server.playercount !== 0) {
86+
try {
87+
let response = await rcon
88+
.rconCommand("/sc rcon.print(game.tick)", server.discordid)
89+
.catch(() => { });
90+
if (response) {
91+
server.ups = Math.abs(
92+
server.previousTick - parseInt(response.resp)
93+
);
94+
server.previousTick = parseInt(response.resp);
95+
}
96+
} catch { }
97+
try {
98+
rcon
99+
.rconCommand(
100+
`/sc global.ext = game.json_to_table('${JSON.stringify({
101+
var: {
102+
server_ups: server.ups,
103+
status: "Online",
104+
},
105+
servers: [
106+
{
107+
name: "All-Weekend Factorio",
108+
welcome: "Welcome to All-Weekend Factorio servers!",
109+
reset_time: "Depends on server",
110+
branch: "Unknown",
111+
},
112+
],
113+
current: 1,
114+
})}')`,
115+
server.discordid
116+
)
117+
.then(() => { })
118+
.catch(() => { });
119+
} catch { }
120+
}
121+
return server;
122+
});
123+
let serversUpdated = await Promise.all(promiseArray);
124+
this.servers = serversUpdated;
125+
this._processing = false;
126+
}
127+
/**
128+
* Get current data for handling
129+
* @returns {UPS[]} - Collected data
130+
*/
131+
exportData(): UPSServer[] {
132+
return this.servers;
133+
}
134+
}
135+
136+
const UPSHandler = new UPSManager(serversJS);
137+
export default UPSHandler;
138+

0 commit comments

Comments
 (0)