import Lacuna from '../lacuna'; import Endpoint from '../core/endpoint'; import type { ServerResponse } from '../core/server'; import { ensureTrailingSlash, fixNumbers } from '../core/util'; import * as S from '../types/stats'; class Stats extends Endpoint { lacuna: Lacuna; module: string; constructor(lacuna: Lacuna, module: string) { super(); this.lacuna = lacuna; this.module = module; } credits(params: S.CreditsParams = {}) { return this.callWithSession('credits', params); } empireRank(params: S.EmpireRankParams = {}) { return this.callWithSession('empire_rank', params); } findEmpireRank(params: S.FindEmpireRankParams) { return this.callWithSession('find_empire_rank', params); } allianceRank(params: S.AllianceRankParams) { return this.callWithSession('alliance_rank', params); } findAllianceRank(params: S.FindAllianceRankParams) { return this.callWithSession('find_alliance_rank', params); } colonyRank(params: S.ColonyRankParams = {}) { return this.callWithSession('colony_rank', params); } spyRank(params: S.SpyRankParams = {}) { return this.callWithSession('spy_rank', params); } weeklyMedalWinners(params: S.WeeklyMedalWinnersParams = {}) { return this.callWithSession('weekly_medal_winners', params); } /** * The in-game "Server" / "General" stats. This is served as a static * `server_overview.json` from the server root, *not* as a JSON-RPC method, so * it takes no params and no session. Resolves to the same * `{ result } | { error }` shape as every other endpoint call. */ async serverOverview(): Promise> { // serverUrl can be reassigned after construction, so re-ensure the trailing // slash - without it URL resolution would drop the last path segment. const url = new URL( 'server/server_overview.json', ensureTrailingSlash(this.lacuna.config.serverUrl) ).href; try { const res = await fetch(url); if (!res.ok) { return { error: { code: res.status, message: `server_overview.json responded ${res.status}`, data: {}, }, }; } return { result: fixNumbers(await res.json()) as S.ServerOverview }; } catch (e) { return { error: { code: -1, message: 'Could not fetch server_overview.json', data: { cause: e } }, }; } } } export default Stats;