Skip to content

Commit 1049e50

Browse files
committed
🎨 formatting fix
1 parent 0747f1d commit 1049e50

File tree

1 file changed

+133
-133
lines changed

1 file changed

+133
-133
lines changed

src/helpers/serverUPSHandler.ts

Lines changed: 133 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,138 +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
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
19130
*/
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;
131+
exportData(): UPSServer[] {
132+
return this.servers;
133+
}
134+
}
135+
136+
const UPSHandler = new UPSManager(serversJS);
137+
export default UPSHandler;
138138

0 commit comments

Comments
 (0)