add ps2.nice.kiwi as source
This commit is contained in:
parent
b9e2a1879e
commit
2a843d2274
12 changed files with 612 additions and 7 deletions
|
@ -20,7 +20,7 @@ export class Cache {
|
|||
return item;
|
||||
}
|
||||
|
||||
async put<T>(id: string, world: T): Promise<T> {
|
||||
async put<T>(id: string, world: T, ttl: number = 60 * 3): Promise<T> {
|
||||
if (this.disableCache) {
|
||||
return world;
|
||||
}
|
||||
|
@ -28,7 +28,7 @@ export class Cache {
|
|||
this.cache.set(id, world);
|
||||
|
||||
await this.kv.put(id, JSON.stringify(world), {
|
||||
expirationTtl: 60 * 3,
|
||||
expirationTtl: ttl,
|
||||
});
|
||||
|
||||
return world;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
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 { voidwellFetchWorld } from "./sources/voidwell";
|
||||
import { DebugPayload, Flags, OnePayload, ServiceResponse } from "./types";
|
||||
|
@ -33,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, kiwi] = await Promise.all([
|
||||
!flags.disableSaerro
|
||||
? saerroFetchWorld(id, cache).catch((e) => {
|
||||
console.error("SAERRO ERROR:", e);
|
||||
|
@ -53,6 +54,9 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
() => defaultServiceResponse
|
||||
)
|
||||
: defaultServiceResponse,
|
||||
!flags.disableKiwi
|
||||
? kiwiFetchWorld(id, cache).catch(() => defaultServiceResponse)
|
||||
: defaultServiceResponse,
|
||||
]);
|
||||
|
||||
const debug: DebugPayload = {
|
||||
|
@ -61,18 +65,21 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
fisu: fisu.raw,
|
||||
honu: honu.raw,
|
||||
voidwell: voidwell.raw,
|
||||
kiwi: kiwi.raw,
|
||||
},
|
||||
timings: {
|
||||
saerro: saerro?.timings || null,
|
||||
fisu: fisu?.timings || null,
|
||||
honu: honu?.timings || null,
|
||||
voidwell: voidwell?.timings || null,
|
||||
kiwi: kiwi?.timings || null,
|
||||
},
|
||||
lastFetchTimes: {
|
||||
saerro: saerro.cachedAt,
|
||||
fisu: fisu.cachedAt,
|
||||
honu: honu.cachedAt,
|
||||
voidwell: voidwell.cachedAt,
|
||||
kiwi: kiwi.cachedAt,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -81,6 +88,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
fisu.population.total,
|
||||
honu.population.total,
|
||||
voidwell.population.total,
|
||||
kiwi.population.total,
|
||||
].filter((x) => x > 0);
|
||||
|
||||
if (totalPopulations.length === 0) {
|
||||
|
@ -127,6 +135,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
fisu: fisu.population.total,
|
||||
honu: honu.population.total,
|
||||
voidwell: voidwell.population.total,
|
||||
kiwi: kiwi.population.total,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ export const handleAll = async (
|
|||
flags: Flags
|
||||
): Promise<Response> => {
|
||||
const cached = await cache.get(`all${debug ? ".debug" : ""}`);
|
||||
if (cached) {
|
||||
if (false && cached) {
|
||||
return new Response(JSON.stringify(cached), {
|
||||
headers: {
|
||||
"content-type": "application/json",
|
||||
|
|
|
@ -35,11 +35,14 @@ router
|
|||
}
|
||||
)
|
||||
.get<BasicRouter>("/population~/health", async () => {
|
||||
const [saerro, voidwell, honu, fisu] = await Promise.all([
|
||||
const [saerro, voidwell, honu, fisu, kiwi] = await Promise.all([
|
||||
fetch("https://saerro.ps2.live/health").then((r) => r.status === 200),
|
||||
fetch("https://voidwell.com/").then((r) => r.status === 200),
|
||||
fetch("https://wt.honu.pw/api/health").then((r) => r.status === 200),
|
||||
fetch("https://ps2.fisu.pw").then((r) => r.status === 200),
|
||||
fetch("https://planetside-2-api.herokuapp.com").then(
|
||||
(r) => r.status === 200
|
||||
),
|
||||
]);
|
||||
|
||||
return new Response(
|
||||
|
@ -48,10 +51,11 @@ router
|
|||
voidwell,
|
||||
honu,
|
||||
fisu,
|
||||
kiwi,
|
||||
}),
|
||||
{
|
||||
headers: { "content-type": "application/json" },
|
||||
status: saerro || voidwell || honu || fisu ? 200 : 502,
|
||||
status: saerro || voidwell || honu || fisu || kiwi ? 200 : 502,
|
||||
}
|
||||
);
|
||||
})
|
||||
|
@ -70,6 +74,7 @@ export default {
|
|||
disableHonu: env.DISABLE_HONU === "1",
|
||||
disableSaerro: env.DISABLE_SAERRO === "1",
|
||||
disableVoidwell: env.DISABLE_VOIDWELL === "1",
|
||||
disableKiwi: env.DISABLE_KIWI === "1",
|
||||
voidwellUsePS4: env.VOIDWELL_USE_PS4 === "1",
|
||||
fisuUsePS4EU: env.FISU_USE_PS4EU === "1",
|
||||
};
|
||||
|
|
82
src/sources/kiwi.ts
Normal file
82
src/sources/kiwi.ts
Normal file
|
@ -0,0 +1,82 @@
|
|||
import { Cache } from "../cache";
|
||||
import { ServiceResponse } from "../types";
|
||||
|
||||
type KiwiResponse = {
|
||||
worldId: number;
|
||||
stats: {
|
||||
population: {
|
||||
nc: number;
|
||||
tr: number;
|
||||
vs: number;
|
||||
total: number;
|
||||
};
|
||||
};
|
||||
}[];
|
||||
|
||||
const kiwiFetchAllWorlds = async (cache: Cache): Promise<KiwiResponse> => {
|
||||
return new Promise(async (resolve, reject) => {
|
||||
const cached = await cache.get<KiwiResponse>("kiwi");
|
||||
if (cached) {
|
||||
return cached;
|
||||
}
|
||||
let resp = await fetch(
|
||||
"https://planetside-2-api.herokuapp.com/socket.io/?EIO=3&transport=websocket",
|
||||
{
|
||||
headers: {
|
||||
Upgrade: "websocket",
|
||||
Origin: "https://ps2.nice.kiwi",
|
||||
},
|
||||
}
|
||||
);
|
||||
const ws = resp.webSocket;
|
||||
if (!ws) {
|
||||
throw new Error("kiwi: No websocket");
|
||||
}
|
||||
|
||||
ws.accept();
|
||||
|
||||
ws.addEventListener("message", async (e) => {
|
||||
let payload = e.data as string;
|
||||
if (payload.startsWith("42")) {
|
||||
ws.close();
|
||||
|
||||
const [, data]: [string, KiwiResponse] = JSON.parse(payload.slice(2));
|
||||
await cache.put("kiwi", data);
|
||||
resolve(data);
|
||||
}
|
||||
});
|
||||
|
||||
ws.send(`42["worlds-update-request"]`);
|
||||
});
|
||||
};
|
||||
|
||||
export const kiwiFetchWorld = async (
|
||||
worldID: string,
|
||||
cache: Cache
|
||||
): Promise<ServiceResponse<number, any>> => {
|
||||
const start = Date.now();
|
||||
const resp = await kiwiFetchAllWorlds(cache);
|
||||
const end = Date.now();
|
||||
|
||||
const data = resp.find((w) => w.worldId === Number(worldID));
|
||||
|
||||
if (!data) {
|
||||
throw new Error(`kiwi: World ${worldID} not found`);
|
||||
}
|
||||
|
||||
return {
|
||||
population: {
|
||||
total: data.stats.population.total,
|
||||
nc: data.stats.population.nc,
|
||||
tr: data.stats.population.tr,
|
||||
vs: data.stats.population.vs,
|
||||
},
|
||||
raw: data,
|
||||
cachedAt: new Date(),
|
||||
timings: {
|
||||
enter: start,
|
||||
exit: end,
|
||||
upstream: end - start,
|
||||
},
|
||||
};
|
||||
};
|
|
@ -51,7 +51,7 @@ export const saerroFetchWorld = async (
|
|||
const world = json.data.allWorlds.find((w) => w.id === Number(id));
|
||||
|
||||
if (!world) {
|
||||
throw new Error(`World ${id} not found`);
|
||||
throw new Error(`saerro: World ${id} not found`);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
@ -22,6 +22,7 @@ export interface Env {
|
|||
DISABLE_FISU: "1" | undefined;
|
||||
DISABLE_SAERRO: "1" | undefined;
|
||||
DISABLE_VOIDWELL: "1" | undefined;
|
||||
DISABLE_KIWI: "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;
|
||||
kiwi: number | null;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -49,18 +51,21 @@ export type DebugPayload = {
|
|||
fisu: any;
|
||||
honu: any;
|
||||
voidwell: any;
|
||||
kiwi: any;
|
||||
};
|
||||
timings: {
|
||||
saerro: any;
|
||||
fisu: any;
|
||||
honu: any;
|
||||
voidwell: any;
|
||||
kiwi: any;
|
||||
};
|
||||
lastFetchTimes: {
|
||||
saerro?: Date;
|
||||
fisu?: Date;
|
||||
honu?: Date;
|
||||
voidwell?: Date;
|
||||
kiwi?: Date;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -69,6 +74,7 @@ export type Flags = {
|
|||
disableFisu: boolean;
|
||||
disableSaerro: boolean;
|
||||
disableVoidwell: boolean;
|
||||
disableKiwi: boolean;
|
||||
voidwellUsePS4: boolean;
|
||||
fisuUsePS4EU: boolean;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue