Skip to content

Commit 8eb7461

Browse files
committed
Add polling of info, time, teamInfo, and mapMarkers
1 parent ca3b5ae commit 8eb7461

File tree

6 files changed

+82
-71
lines changed

6 files changed

+82
-71
lines changed

src/discordEvents/ready.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ export async function execute(dm: DiscordManager) {
112112
const gInstance = gim.getGuildInstance(guild.id) as GuildInstance;
113113
for (const [serverId, content] of Object.entries(gInstance.serverInfoMap)) {
114114
if (content.active) {
115-
if (rpm.addInstance(guild.id, serverId, content.mainRequesterSteamId)) {
115+
if (rpm.addInstance(guild.id, serverId)) {
116116
const rpInstance = rpm.getInstance(guild.id, serverId);
117117
if (rpInstance) {
118118
await rpInstance.startup();

src/handlers/buttonHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ async function serverConnectButtonHandler(dm: DiscordManager, interaction: disco
425425
server.active = true;
426426
gim.updateGuildInstance(guildId);
427427

428-
if (rpm.addInstance(guildId, serverId, server.mainRequesterSteamId)) {
428+
if (rpm.addInstance(guildId, serverId)) {
429429
const rpInstance = rpm.getInstance(guildId, serverId);
430430
if (rpInstance) {
431431
await rpInstance.startup();

src/managers/rustPlusManager.ts

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import * as rp from 'rustplus-ts';
2222
import * as fs from 'fs';
2323
import * as path from 'path';
24+
import { Logger } from 'winston';
2425

2526
import { log, discordManager as dm, guildInstanceManager as gim, config } from '../../index';
2627
import * as constants from '../utils/constants';
@@ -59,7 +60,7 @@ export class RustPlusManager {
5960
return false;
6061
}
6162

62-
public addInstance(guildId: types.GuildId, serverId: types.ServerId, mainRequesterSteamId: types.SteamId): boolean {
63+
public addInstance(guildId: types.GuildId, serverId: types.ServerId): boolean {
6364
const funcName = '[RustPlusManager: addInstance]';
6465
const ipAndPort = getIpAndPort(serverId);
6566
const logParam = { guildId: guildId, serverId: serverId };
@@ -73,8 +74,7 @@ export class RustPlusManager {
7374
return false;
7475
}
7576

76-
this.rustPlusInstanceMap[guildId][serverId] = new RustPlusInstance(guildId, ipAndPort.ip, ipAndPort.port,
77-
mainRequesterSteamId);
77+
this.rustPlusInstanceMap[guildId][serverId] = new RustPlusInstance(guildId, ipAndPort.ip, ipAndPort.port);
7878
log.info(`${funcName} Instance added.`, logParam);
7979
return true;
8080
}
@@ -109,7 +109,6 @@ export class RustPlusInstance {
109109
public guildId: types.GuildId;
110110
public ip: string;
111111
public port: string;
112-
public mainRequesterSteamId: types.SteamId;
113112
public serverId: types.ServerId;
114113
public serverName: string;
115114

@@ -130,11 +129,10 @@ export class RustPlusInstance {
130129
//private appMapMarkers: rustplus.AppMapMarkers | null;
131130

132131

133-
constructor(guildId: types.GuildId, ip: string, port: string, mainRequesterSteamId: types.SteamId) {
132+
constructor(guildId: types.GuildId, ip: string, port: string) {
134133
this.guildId = guildId;
135134
this.ip = ip;
136135
this.port = port;
137-
this.mainRequesterSteamId = mainRequesterSteamId
138136
this.serverId = getServerId(ip, port);
139137

140138
const gInstance = gim.getGuildInstance(this.guildId) as GuildInstance;
@@ -273,17 +271,28 @@ export class RustPlusInstance {
273271
}
274272

275273
private async serverPolling(firstPoll: boolean = false) {
276-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
277274
const funcName = '[RustPlusManager: serverPolling]';
278-
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
279275
const logParam = { guildId: this.guildId, serverId: this.serverId, serverName: this.serverName };
280276

281-
this.lastServerPollSuccessful = false;
282-
// TODO! Retrieve rustplus data below:
283-
// - getInfo
284-
// - getTime
285-
// - getTeamInfo
286-
// - getMapMarkers
277+
const gInstance = gim.getGuildInstance(this.guildId) as GuildInstance;
278+
const server = gInstance.serverInfoMap[this.serverId];
279+
const mainRequesterSteamId = server.mainRequesterSteamId;
280+
const pairingData = gInstance.pairingDataMap[this.serverId]?.[mainRequesterSteamId] ?? null;
281+
282+
if (!pairingData) {
283+
this.lastServerPollSuccessful = false;
284+
log.warn(`${funcName} pairingData for ${mainRequesterSteamId} could not be found.`, logParam);
285+
return;
286+
}
287+
288+
const rpInfo = await this.rustPlus.getInfoAsync(pairingData.steamId, pairingData.playerToken);
289+
if (!this.validateServerPollResponse(rpInfo, 'info', rp.isValidAppInfo)) return;
290+
const rpTime = await this.rustPlus.getTimeAsync(pairingData.steamId, pairingData.playerToken);
291+
if (!this.validateServerPollResponse(rpTime, 'time', rp.isValidAppTime)) return;
292+
const rpTeamInfo = await this.rustPlus.getTeamInfoAsync(pairingData.steamId, pairingData.playerToken);
293+
if (!this.validateServerPollResponse(rpTeamInfo, 'teamInfo', rp.isValidAppTeamInfo)) return;
294+
const rpMapMarkers = await this.rustPlus.getMapMarkersAsync(pairingData.steamId, pairingData.playerToken);
295+
if (!this.validateServerPollResponse(rpMapMarkers, 'mapMarkers', rp.isValidAppMapMarkers)) return;
287296

288297
this.lastServerPollSuccessful = true;
289298
this.lastServerPollSuccessfulTimestampSeconds = Math.floor(Date.now() / 1000);
@@ -313,6 +322,59 @@ export class RustPlusInstance {
313322
// TODO! informationChannelHandler
314323
}
315324

325+
public async validateServerPollResponse(response: rp.AppResponse | Error | rp.ConsumeTokensError,
326+
responseParam: keyof rp.AppResponse, validationCallback: (input: unknown, logger: Logger | null) => boolean):
327+
Promise<boolean> {
328+
const funcName = `[RustPlusManager: validateServerPollResponse]`
329+
const logParam = { guildId: this.guildId, serverId: this.serverId, serverName: this.serverName };
330+
331+
const gInstance = gim.getGuildInstance(this.guildId) as GuildInstance;
332+
const server = gInstance.serverInfoMap[this.serverId];
333+
const mainRequesterSteamId = server.mainRequesterSteamId;
334+
const pairingData = gInstance.pairingDataMap[this.serverId]?.[mainRequesterSteamId] ?? null;
335+
336+
if (rp.isValidAppResponse(response, log)) {
337+
if (!validationCallback(response[responseParam], log)) {
338+
if (rp.isValidAppError(response.error, log)) {
339+
log.warn(`${funcName} AppError: ${response.error.error}`, logParam);
340+
if (this.rustPlus.getAppResponseError(response) === rp.AppResponseError.NotFound) {
341+
/* pairingData is no longer valid. */
342+
if (pairingData && pairingData.valid) {
343+
pairingData.valid = false;
344+
gim.updateGuildInstance(this.guildId);
345+
await sendServerMessage(dm, this.guildId, this.serverId, this.connectionStatus);
346+
}
347+
}
348+
}
349+
else {
350+
log.error(`${funcName} We got completely wrong response: ${JSON.stringify(response)}`, logParam);
351+
}
352+
this.lastServerPollSuccessful = false;
353+
return false;
354+
}
355+
else {
356+
if (pairingData && !pairingData.valid) {
357+
pairingData.valid = true;
358+
gim.updateGuildInstance(this.guildId);
359+
await sendServerMessage(dm, this.guildId, this.serverId, this.connectionStatus);
360+
}
361+
}
362+
}
363+
else {
364+
/* Error or rp.ConsumeTokensError */
365+
if (response instanceof Error) {
366+
log.error(`${funcName} Error: ${response.message}`, logParam);
367+
}
368+
else {
369+
log.error(`${funcName} ConsumeTokensError: ${response}`, logParam);
370+
}
371+
this.lastServerPollSuccessful = false;
372+
return false;
373+
}
374+
375+
return true;
376+
}
377+
316378
public async setupSmartDevices() {
317379

318380
// TODO! Go through all smart devices to get the status of them

src/rustPlusEvents/connected.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
1919
*/
2020

21-
//import * as rp from 'rustplus-ts';
22-
2321
import { log, discordManager as dm } from '../../index';
2422
import { RustPlusInstance, ConnectionStatus } from "../managers/rustPlusManager";
2523
import { sendServerMessage } from '../discordUtils/discordMessages';
@@ -44,41 +42,6 @@ export async function execute(rpInstance: RustPlusInstance) {
4442

4543
rpInstance.startServerPollingHandler();
4644

47-
//const gInstance = gim.getGuildInstance(rpInstance.guildId) as GuildInstance;
48-
//const pairingData = gInstance.pairingDataMap[rpInstance.serverId]?.[rpInstance.mainSteamId] ?? null;
49-
50-
//if (!pairingData) {
51-
// // TODO! Change mainSteamId because current dont work anymore.
52-
// return;
53-
//}
54-
55-
//const response = await rpInstance.rustPlus.getInfoAsync(pairingData.steamId + '1', pairingData.playerToken);
56-
//const isValidResponse = rp.isValidAppResponse(response, log);
57-
//if (isValidResponse) {
58-
// if (rp.isValidAppError(response.error, log)) {
59-
// const error = response.error.error;
60-
// }
61-
// else if (!rp.isValidAppInfo(response.info, log)) {
62-
63-
// }
64-
//}
65-
//else {
66-
// /* Error or rp.ConsumeTokensError */
67-
//}
68-
69-
70-
71-
72-
//console.log(response)
73-
//if (!(rp.isValidAppResponse(response, log) && rp.isValidAppInfo(response.info, log))) {
74-
// if (rp.isValidAppResponse(response, log) && rp.isValidAppError(response.error, log)) {
75-
// console.log(response.error.error)
76-
// }
77-
// console.log('INVALID')
78-
//}
79-
80-
81-
8245
rpInstance.connectionStatus = ConnectionStatus.Connected;
8346
await sendServerMessage(dm, rpInstance.guildId, rpInstance.serverId, ConnectionStatus.Connected);
8447
}

src/rustPlusEvents/message.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@
2020

2121
import * as rp from 'rustplus-ts';
2222

23-
import { log } from '../../index';
2423
import { RustPlusInstance } from "../managers/rustPlusManager";
2524

2625
export const name = 'message';
2726

27+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
2828
export async function execute(rpInstance: RustPlusInstance, appMessage: rp.AppMessage, handled: boolean) {
29-
const funcName = `[rustPlusEvent: ${name}]`;
30-
const logParam = {
31-
guildId: rpInstance.guildId,
32-
serverId: rpInstance.serverId,
33-
serverName: rpInstance.serverName
34-
};
35-
36-
log.info(`${funcName} ${JSON.stringify(appMessage)}, ${handled}`, logParam);
29+
/* TBD */
3730
}

src/rustPlusEvents/request.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,11 @@
2020

2121
import * as rp from 'rustplus-ts';
2222

23-
import { log } from '../../index';
2423
import { RustPlusInstance } from "../managers/rustPlusManager";
2524

2625
export const name = 'request';
2726

27+
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
2828
export async function execute(rpInstance: RustPlusInstance, appRequest: rp.AppRequest) {
29-
const funcName = `[rustPlusEvent: ${name}]`;
30-
const logParam = {
31-
guildId: rpInstance.guildId,
32-
serverId: rpInstance.serverId,
33-
serverName: rpInstance.serverName
34-
};
35-
36-
log.info(`${funcName} ${JSON.stringify(appRequest)}`, logParam);
29+
/* Not used */
3730
}

0 commit comments

Comments
 (0)