2121import * as rp from 'rustplus-ts' ;
2222import * as fs from 'fs' ;
2323import * as path from 'path' ;
24+ import { Logger } from 'winston' ;
2425
2526import { log , discordManager as dm , guildInstanceManager as gim , config } from '../../index' ;
2627import * 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
0 commit comments