ggejs
Version:
A powerful JavaScript library for interacting with the server of Goodgame Empire: Four Kingdoms
102 lines (96 loc) • 4.76 kB
JavaScript
const BaseManager = require('./BaseManager');
const {execute: searchAllianceById} = require('../e4kserver/commands/searchAllianceById');
const {WaitUntil} = require('../tools/wait');
const Localize = require("../tools/Localize");
const {execute: getHighScore} = require("../e4kserver/commands/getHighScore");
const HighScoreConst = require("../utils/HighScoreConst");
class AllianceManager extends BaseManager {
/** @param {number} id */
async getById(id) {
if (id == null) throw 'Missing alliance id!';
try {
const socket = this._socket;
searchAllianceById(socket, id);
/** @type {Alliance | MyAlliance} */
const alliance = await WaitUntil(socket, `_alliance_${id}_data`, "", 1000);
delete socket[`_alliance_${id}_data`];
return alliance;
} catch (e) {
throw Localize.text(this._client, 'errorCode_114');
}
}
/** @param {string} name */
async find(name) {
try {
const ranking = await this.getRankings(name, 'might');
const allianceId = ranking.items.find(item => item.rank === ranking.foundRank).alliance.allianceId;
return await this.getById(allianceId);
} catch (e) {
throw Localize.text(this._client, 'errorCode_114');
}
}
/** @returns {Promise<MyAlliance>} */
async getMyAlliance() {
const allianceId = this._client.clientUserData.allianceId;
if (allianceId === -1) throw "You are not in an alliance!";
return await this.getById(allianceId);
}
/**
* @param {string | number} nameOrRanking
* @param {AllianceHighScoreRankingTypes} rankingType
* @param {number} leagueId
*/
async getRankings(nameOrRanking, rankingType = "might", leagueId = 1) {
const searchValue = nameOrRanking.toString();
const normalizedName = searchValue.toLowerCase().normalize("NFD").replace(/[\u0300-\u036f]/g, "");
const listType = (() => {
switch (rankingType) {
case "honor":
return HighScoreConst.ALLIANCE_HONOR;
case "might":
return HighScoreConst.ALLIANCE_MIGHT_POINTS;
case "landMarks":
return HighScoreConst.ALLIANCE_LANDMARKS;
case "aqua":
return HighScoreConst.ALLIANCE_AQUA_POINTS;
case "tournamentFame":
return HighScoreConst.ALLIANCE_TOURNAMENT_FAME;
case "alienInvasion":
return HighScoreConst.ALLIANCE_ALIEN_INVASION_ALLIANCE;
case "nomadInvasion":
return HighScoreConst.ALLIANCE_NOMADINVASION_ALLIANCE;
case "samuraiInvasion":
return HighScoreConst.SAMURAI_ALLIANCE;
case "redAlienInvasion":
return HighScoreConst.ALLIANCE_RED_ALIEN_INVASION_ALLIANCE;
case "kingdomsLeagueSeason":
return HighScoreConst.ALLIANCE_KINGDOMS_LEAGUE_SEASON;
case "kingdomsLeagueSeasonEvent":
return HighScoreConst.ALLIANCE_KINGDOMS_LEAGUE_SEASON_EVENT;
case "daimyo":
return HighScoreConst.ALLIANCE_DAIMYO;
case "allianceBattleGroundCollector":
return HighScoreConst.ALLIANCE_BATTLE_GROUND_ALLIANCE_COLLECTOR;
case "allianceBattleGroundTower":
return HighScoreConst.ALLIANCE_BATTLE_GROUND_ALLIANCE_TOWER;
case "allianceBattleGroundPreviousRun":
return HighScoreConst.ALLIANCE_BATTLE_GROUND_PREVIOUS_RUN_ALLIANCE;
default:
return -1;
}
})();
if (listType === -1) throw "Rankings' list type not supported";
try {
getHighScore(this._socket, searchValue, listType, leagueId);
/** @type {HighScore<AllianceHighScoreItem>} */
const hghData = await WaitUntil(this._socket, `hgh_${listType}_${normalizedName}`, "", 1000);
delete this._socket[`hgh_${listType}_${normalizedName}`];
return hghData;
} catch (e) {
delete this._socket[`hgh_${listType}_${normalizedName}`];
throw Localize.text(this._socket.client, e);
}
}
}
module.exports = AllianceManager