ggejs
Version:
A powerful Node.js module for interacting with the server of Goodgame Empire & Goodgame Empire: Four Kingdoms
47 lines (41 loc) • 1.48 kB
JavaScript
const BaseClient = require("./BaseClient");
const {registerGbdListener} = require("./commands/gbd");
const {registerOrLogin} = require('./commands/tle');
const EmpireError = require("./tools/EmpireError");
const {ConnectionStatus, Events} = require("./utils/Constants");
class ExternalClient extends BaseClient {
#loginToken = ""
async _reconnect() {
return await this.connect(this.#loginToken);
}
/**
* @param {string} loginToken
* @return {Promise<this>}
*/
async connect(loginToken) {
if (this.socketManager.connectionStatus === ConnectionStatus.Connected) return this;
await this.socketManager.connect();
await this._loginWithToken(loginToken);
(async () => {
try {
await this._sendPingPong();
this.emit(Events.CONNECTED);
} catch (e) {
this.logger.w(e);
}
})().then()
return this;
}
/** @param {string} loginToken ExternalServer _login token */
async _loginWithToken(loginToken) {
try {
this.#loginToken = loginToken;
await Promise.all([registerGbdListener(this), registerOrLogin(this, loginToken)])
} catch (errorCode) {
if (errorCode === 423) this.#loginToken = "";
throw new EmpireError(this, errorCode);
}
}
}
module.exports = ExternalClient;