-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
V1.1 Enable handling of multiple servers
- Loading branch information
Showing
9 changed files
with
154 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
# Gamedig Query provider specific | ||
GAME=minecraft | ||
HOST=mysite.com || 0.0.0.0 | ||
GAME_QUERY_PORT=25565 | ||
# Gamedig specific | ||
GAME_URLS='minecraft:mysite.com:25565,przomboid:mysite.com' | ||
|
||
# Discord settings (required) | ||
# Discord settings | ||
DISCORD_TOKEN= | ||
DISCORD_CHANNEL=000000000000 | ||
SERVER_UP_MESSAGE='The minecraft server is available!' | ||
SERVER_DOWN_MESSAGE='The minecraft server is no longer available!' | ||
UP.przomboid=':zombie: The Project Zomboid server is available!' | ||
UP.minecraft=':pick: The Minecraft server is available!' | ||
DOWN.minecraft=':pick: The Minecraft server is no longer available!' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Type } from "gamedig"; | ||
|
||
export class GameUrl { | ||
|
||
game: Type; | ||
host: string; | ||
port: number | undefined; | ||
|
||
constructor(game: Type, host: string, port?: number) { | ||
this.game = game; | ||
this.host = host; | ||
this.port = port; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {QueryResult, Type} from 'gamedig'; | ||
|
||
export class ServerResponse { | ||
game: Type; | ||
result: QueryResult | undefined; | ||
|
||
constructor(game: Type, result?: QueryResult){ | ||
this.game = game; | ||
this.result = result | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,36 @@ | ||
import {PollingProvider} from './polling-provider'; | ||
import {query, QueryResult, Type} from 'gamedig'; | ||
import {query, Type} from 'gamedig'; | ||
import { GameUrl } from '../models/game-url'; | ||
import { ServerResponse } from '../models/server-response'; | ||
|
||
export class GamedigQueryProvider extends PollingProvider { | ||
|
||
private game: Type; | ||
private host: string; | ||
private port: number | undefined; | ||
private gameUrls: GameUrl[]; | ||
|
||
constructor() { | ||
super(); | ||
this.game = process.env.GAME as Type; | ||
this.host = process.env.HOST || ''; | ||
this.port = process.env.PORT ? parseInt(process.env.PORT || '0') : undefined; | ||
let rawGameUrls = (process.env.GAME_URLS || '').split(','); | ||
this.gameUrls = rawGameUrls.filter((raw) => raw.split(':').length > 1).map(g => { | ||
let rawUrl = g.split(':'); | ||
return { | ||
game: rawUrl[0] as Type, | ||
host: rawUrl[1], | ||
port: rawUrl[2] ? parseInt(rawUrl[2]) : undefined | ||
} as GameUrl; | ||
}); | ||
} | ||
|
||
protected async retrieve(): Promise<QueryResult> { | ||
return await query({ | ||
type: this.game, | ||
host: this.host, | ||
port: this.port, | ||
}); | ||
protected async retrieve(): Promise<ServerResponse[]> { | ||
let results = new Array<ServerResponse>(); | ||
for (let g of this.gameUrls) { | ||
results.push(new ServerResponse(g.game, | ||
await query({ | ||
type: g.game, | ||
host: g.host, | ||
port: g.port, | ||
}).catch((e) => undefined) | ||
)); | ||
} | ||
return results; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,20 @@ | ||
import {asyncScheduler, delayWhen, from, Observable, retryWhen, Subject, tap, timer} from 'rxjs'; | ||
import {from, Observable, Subject, timer} from 'rxjs'; | ||
import {switchMap} from 'rxjs/operators'; | ||
import { QueryResult } from 'gamedig'; | ||
import { ServerResponse } from '../models/server-response'; | ||
|
||
export abstract class PollingProvider { | ||
private try = 1; | ||
private interval: number = 10000; | ||
private resultSubject = new Subject<QueryResult | undefined>(); | ||
private resultSubject = new Subject<ServerResponse[] | undefined>(); | ||
|
||
protected constructor() { | ||
timer(0, this.interval).pipe( | ||
switchMap(() => from(this.retrieve())), | ||
retryWhen((errors => { | ||
return errors.pipe( | ||
tap(val => { | ||
this.resultSubject.next(undefined); | ||
if (this.try > 15) { | ||
throw new Error('PollingProvider became unhealty, exiting... Check the configuration and make sure the game server is online.'); | ||
} | ||
console.log('PollingProvider errored, retrying in ' + (this.try + 1) * 2 + ' seconds for max 15 tries (' + this.try + '. try). Error:', val.message); | ||
this.try++; | ||
}), | ||
delayWhen(() => { | ||
return timer(this.try * 2 * 1000, asyncScheduler); | ||
}), | ||
); | ||
})), | ||
tap(() => { | ||
this.try = 1; | ||
}), | ||
).subscribe((val) => this.resultSubject.next(val)); | ||
} | ||
|
||
provide(): Observable<QueryResult | undefined> { | ||
provide(): Observable<ServerResponse[] | undefined> { | ||
return this.resultSubject.asObservable(); | ||
} | ||
|
||
protected abstract retrieve(): Promise<QueryResult>; | ||
protected abstract retrieve(): Promise<ServerResponse[]>; | ||
} |