add sanctuary with saleness checking

This commit is contained in:
41666 2022-12-24 16:43:25 +00:00
parent 3889e5fe08
commit 4ded6085b7
4 changed files with 103 additions and 1272 deletions

1271
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,8 @@
import { Cache } from "./cache";
import { fisuFetchWorld } from "./sources/fisu";
import { honuFetchWorld } from "./sources/honu";
import { kiwiFetchWorld } from "./sources/kiwi";
import { saerroFetchWorld } from "./sources/saerro";
import { sanctuaryFetchWorld } from "./sources/sanctuary";
import { voidwellFetchWorld } from "./sources/voidwell";
import { DebugPayload, Flags, OnePayload, ServiceResponse } from "./types";
@ -34,7 +34,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
return cached;
}
const [saerro, fisu, honu, voidwell] = await Promise.all([
const [saerro, fisu, honu, voidwell, sanctuary] = await Promise.all([
!flags.disableSaerro
? saerroFetchWorld(id, cache).catch((e) => {
console.error("SAERRO ERROR:", e);
@ -54,6 +54,9 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
() => defaultServiceResponse
)
: defaultServiceResponse,
!flags.disableSanctuary
? sanctuaryFetchWorld(id, cache).catch(() => defaultServiceResponse)
: defaultServiceResponse,
]);
const debug: DebugPayload = {
@ -62,18 +65,21 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
fisu: fisu.raw,
honu: honu.raw,
voidwell: voidwell.raw,
sanctuary: sanctuary.raw,
},
timings: {
saerro: saerro?.timings || null,
fisu: fisu?.timings || null,
honu: honu?.timings || null,
voidwell: voidwell?.timings || null,
sanctuary: sanctuary?.timings || null,
},
lastFetchTimes: {
saerro: saerro.cachedAt,
fisu: fisu.cachedAt,
honu: honu.cachedAt,
voidwell: voidwell.cachedAt,
sanctuary: sanctuary.cachedAt,
},
};
@ -82,6 +88,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
fisu.population.total,
honu.population.total,
voidwell.population.total,
sanctuary.population.total,
].filter((x) => x > 0);
if (totalPopulations.length === 0) {
@ -103,6 +110,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
fisu: 0,
honu: 0,
voidwell: 0,
sanctuary: 0,
},
},
debug,
@ -113,6 +121,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
saerro.population,
fisu.population,
honu.population,
sanctuary.population,
].filter((x) => x.total > 0);
const payload: OnePayload = {
@ -128,6 +137,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
fisu: fisu.population.total,
honu: honu.population.total,
voidwell: voidwell.population.total,
sanctuary: sanctuary.population.total,
},
};

84
src/sources/sanctuary.ts Normal file
View file

@ -0,0 +1,84 @@
import { Cache } from "../cache";
import { ServiceResponse } from "../types";
// {"world_population_list":[{"world_id":1,"last_updated":1671886187,"total":122,"population":{"VS":49,"NC":45,"TR":28,"NSO":0}},
type SanctuaryResponse = {
world_population_list: {
world_id: number;
last_updated: string;
total: number;
population: {
NC: number;
TR: number;
VS: number;
NSO: number;
};
}[];
};
const sanctuaryFetchAllWorlds = async (
cache: Cache
): Promise<SanctuaryResponse> => {
const cached = await cache.get<SanctuaryResponse>("sanctuary");
if (cached) {
return cached;
}
const req = await fetch(
"https://census.lithafalcon.cc/get/ps2/world_population?c:censusJSON=false"
);
return await cache.put("sanctuary", await req.json<SanctuaryResponse>());
};
export const sanctuaryFetchWorld = async (
worldID: string,
cache: Cache
): Promise<ServiceResponse<number, any>> => {
// No PS4 data nor Jaeger
if (worldID === "1000" || worldID === "2000" || worldID === "19") {
return {
population: {
total: -1,
nc: -1,
tr: -1,
vs: -1,
},
raw: {},
cachedAt: new Date(),
};
}
const start = Date.now();
const resp = await sanctuaryFetchAllWorlds(cache);
const end = Date.now();
const data = resp.world_population_list.find(
(w) => w.world_id === Number(worldID)
);
if (!data) {
throw new Error(`sanctuary: World ${worldID} not found`);
}
if (data.last_updated < (Date.now() / 1000 - 60 * 5).toString()) {
throw new Error(`sanctuary: World ${worldID} is stale`);
}
return {
population: {
total: data.total,
nc: data.population.NC,
tr: data.population.TR,
vs: data.population.VS,
},
raw: data,
cachedAt: new Date(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

View file

@ -22,6 +22,7 @@ export interface Env {
DISABLE_FISU: "1" | undefined;
DISABLE_SAERRO: "1" | undefined;
DISABLE_VOIDWELL: "1" | undefined;
DISABLE_SANCTUARY: "1" | undefined;
DISABLE_CACHE: "1" | undefined;
VOIDWELL_USE_PS4: "1" | undefined;
FISU_USE_PS4EU: "1" | undefined;
@ -40,6 +41,7 @@ export type OnePayload = {
fisu: number | null;
honu: number | null;
voidwell: number | null;
sanctuary: number | null;
};
};
@ -49,18 +51,21 @@ export type DebugPayload = {
fisu: any;
honu: any;
voidwell: any;
sanctuary: any;
};
timings: {
saerro: any;
fisu: any;
honu: any;
voidwell: any;
sanctuary: any;
};
lastFetchTimes: {
saerro?: Date;
fisu?: Date;
honu?: Date;
voidwell?: Date;
sanctuary?: Date;
};
};
@ -69,6 +74,7 @@ export type Flags = {
disableFisu: boolean;
disableSaerro: boolean;
disableVoidwell: boolean;
disableSanctuary: boolean;
voidwellUsePS4: boolean;
fisuUsePS4EU: boolean;
};