add sanctuary with saleness checking
This commit is contained in:
parent
3889e5fe08
commit
4ded6085b7
4 changed files with 103 additions and 1272 deletions
1271
package-lock.json
generated
1271
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -1,8 +1,8 @@
|
||||||
import { Cache } from "./cache";
|
import { Cache } from "./cache";
|
||||||
import { fisuFetchWorld } from "./sources/fisu";
|
import { fisuFetchWorld } from "./sources/fisu";
|
||||||
import { honuFetchWorld } from "./sources/honu";
|
import { honuFetchWorld } from "./sources/honu";
|
||||||
import { kiwiFetchWorld } from "./sources/kiwi";
|
|
||||||
import { saerroFetchWorld } from "./sources/saerro";
|
import { saerroFetchWorld } from "./sources/saerro";
|
||||||
|
import { sanctuaryFetchWorld } from "./sources/sanctuary";
|
||||||
import { voidwellFetchWorld } from "./sources/voidwell";
|
import { voidwellFetchWorld } from "./sources/voidwell";
|
||||||
import { DebugPayload, Flags, OnePayload, ServiceResponse } from "./types";
|
import { DebugPayload, Flags, OnePayload, ServiceResponse } from "./types";
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
return cached;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [saerro, fisu, honu, voidwell] = await Promise.all([
|
const [saerro, fisu, honu, voidwell, sanctuary] = await Promise.all([
|
||||||
!flags.disableSaerro
|
!flags.disableSaerro
|
||||||
? saerroFetchWorld(id, cache).catch((e) => {
|
? saerroFetchWorld(id, cache).catch((e) => {
|
||||||
console.error("SAERRO ERROR:", e);
|
console.error("SAERRO ERROR:", e);
|
||||||
|
@ -54,6 +54,9 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
() => defaultServiceResponse
|
() => defaultServiceResponse
|
||||||
)
|
)
|
||||||
: defaultServiceResponse,
|
: defaultServiceResponse,
|
||||||
|
!flags.disableSanctuary
|
||||||
|
? sanctuaryFetchWorld(id, cache).catch(() => defaultServiceResponse)
|
||||||
|
: defaultServiceResponse,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const debug: DebugPayload = {
|
const debug: DebugPayload = {
|
||||||
|
@ -62,18 +65,21 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
fisu: fisu.raw,
|
fisu: fisu.raw,
|
||||||
honu: honu.raw,
|
honu: honu.raw,
|
||||||
voidwell: voidwell.raw,
|
voidwell: voidwell.raw,
|
||||||
|
sanctuary: sanctuary.raw,
|
||||||
},
|
},
|
||||||
timings: {
|
timings: {
|
||||||
saerro: saerro?.timings || null,
|
saerro: saerro?.timings || null,
|
||||||
fisu: fisu?.timings || null,
|
fisu: fisu?.timings || null,
|
||||||
honu: honu?.timings || null,
|
honu: honu?.timings || null,
|
||||||
voidwell: voidwell?.timings || null,
|
voidwell: voidwell?.timings || null,
|
||||||
|
sanctuary: sanctuary?.timings || null,
|
||||||
},
|
},
|
||||||
lastFetchTimes: {
|
lastFetchTimes: {
|
||||||
saerro: saerro.cachedAt,
|
saerro: saerro.cachedAt,
|
||||||
fisu: fisu.cachedAt,
|
fisu: fisu.cachedAt,
|
||||||
honu: honu.cachedAt,
|
honu: honu.cachedAt,
|
||||||
voidwell: voidwell.cachedAt,
|
voidwell: voidwell.cachedAt,
|
||||||
|
sanctuary: sanctuary.cachedAt,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -82,6 +88,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
fisu.population.total,
|
fisu.population.total,
|
||||||
honu.population.total,
|
honu.population.total,
|
||||||
voidwell.population.total,
|
voidwell.population.total,
|
||||||
|
sanctuary.population.total,
|
||||||
].filter((x) => x > 0);
|
].filter((x) => x > 0);
|
||||||
|
|
||||||
if (totalPopulations.length === 0) {
|
if (totalPopulations.length === 0) {
|
||||||
|
@ -103,6 +110,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
fisu: 0,
|
fisu: 0,
|
||||||
honu: 0,
|
honu: 0,
|
||||||
voidwell: 0,
|
voidwell: 0,
|
||||||
|
sanctuary: 0,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
debug,
|
debug,
|
||||||
|
@ -113,6 +121,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
saerro.population,
|
saerro.population,
|
||||||
fisu.population,
|
fisu.population,
|
||||||
honu.population,
|
honu.population,
|
||||||
|
sanctuary.population,
|
||||||
].filter((x) => x.total > 0);
|
].filter((x) => x.total > 0);
|
||||||
|
|
||||||
const payload: OnePayload = {
|
const payload: OnePayload = {
|
||||||
|
@ -128,6 +137,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
fisu: fisu.population.total,
|
fisu: fisu.population.total,
|
||||||
honu: honu.population.total,
|
honu: honu.population.total,
|
||||||
voidwell: voidwell.population.total,
|
voidwell: voidwell.population.total,
|
||||||
|
sanctuary: sanctuary.population.total,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
84
src/sources/sanctuary.ts
Normal file
84
src/sources/sanctuary.ts
Normal 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,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
|
@ -22,6 +22,7 @@ export interface Env {
|
||||||
DISABLE_FISU: "1" | undefined;
|
DISABLE_FISU: "1" | undefined;
|
||||||
DISABLE_SAERRO: "1" | undefined;
|
DISABLE_SAERRO: "1" | undefined;
|
||||||
DISABLE_VOIDWELL: "1" | undefined;
|
DISABLE_VOIDWELL: "1" | undefined;
|
||||||
|
DISABLE_SANCTUARY: "1" | undefined;
|
||||||
DISABLE_CACHE: "1" | undefined;
|
DISABLE_CACHE: "1" | undefined;
|
||||||
VOIDWELL_USE_PS4: "1" | undefined;
|
VOIDWELL_USE_PS4: "1" | undefined;
|
||||||
FISU_USE_PS4EU: "1" | undefined;
|
FISU_USE_PS4EU: "1" | undefined;
|
||||||
|
@ -40,6 +41,7 @@ export type OnePayload = {
|
||||||
fisu: number | null;
|
fisu: number | null;
|
||||||
honu: number | null;
|
honu: number | null;
|
||||||
voidwell: number | null;
|
voidwell: number | null;
|
||||||
|
sanctuary: number | null;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -49,18 +51,21 @@ export type DebugPayload = {
|
||||||
fisu: any;
|
fisu: any;
|
||||||
honu: any;
|
honu: any;
|
||||||
voidwell: any;
|
voidwell: any;
|
||||||
|
sanctuary: any;
|
||||||
};
|
};
|
||||||
timings: {
|
timings: {
|
||||||
saerro: any;
|
saerro: any;
|
||||||
fisu: any;
|
fisu: any;
|
||||||
honu: any;
|
honu: any;
|
||||||
voidwell: any;
|
voidwell: any;
|
||||||
|
sanctuary: any;
|
||||||
};
|
};
|
||||||
lastFetchTimes: {
|
lastFetchTimes: {
|
||||||
saerro?: Date;
|
saerro?: Date;
|
||||||
fisu?: Date;
|
fisu?: Date;
|
||||||
honu?: Date;
|
honu?: Date;
|
||||||
voidwell?: Date;
|
voidwell?: Date;
|
||||||
|
sanctuary?: Date;
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -69,6 +74,7 @@ export type Flags = {
|
||||||
disableFisu: boolean;
|
disableFisu: boolean;
|
||||||
disableSaerro: boolean;
|
disableSaerro: boolean;
|
||||||
disableVoidwell: boolean;
|
disableVoidwell: boolean;
|
||||||
|
disableSanctuary: boolean;
|
||||||
voidwellUsePS4: boolean;
|
voidwellUsePS4: boolean;
|
||||||
fisuUsePS4EU: boolean;
|
fisuUsePS4EU: boolean;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue