mulligan, no kiwi
This commit is contained in:
parent
ec6f39cf4e
commit
3889e5fe08
6 changed files with 7 additions and 115 deletions
|
@ -6,7 +6,6 @@ A Planetside 2 population aggreggation API between
|
|||
- https://wt.honu.pw
|
||||
- https://saerro.ps2.live
|
||||
- https://voidwell.com
|
||||
- https://ps2.nice.kiwi
|
||||
|
||||
Has some jank filters and will average between all 4 sources with as much data as can be used.
|
||||
|
||||
|
|
|
@ -34,7 +34,7 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
return cached;
|
||||
}
|
||||
|
||||
const [saerro, fisu, honu, voidwell, kiwi] = await Promise.all([
|
||||
const [saerro, fisu, honu, voidwell] = await Promise.all([
|
||||
!flags.disableSaerro
|
||||
? saerroFetchWorld(id, cache).catch((e) => {
|
||||
console.error("SAERRO ERROR:", e);
|
||||
|
@ -54,9 +54,6 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
|||
() => defaultServiceResponse
|
||||
)
|
||||
: defaultServiceResponse,
|
||||
!flags.disableKiwi
|
||||
? kiwiFetchWorld(id, cache).catch(() => defaultServiceResponse)
|
||||
: defaultServiceResponse,
|
||||
]);
|
||||
|
||||
const debug: DebugPayload = {
|
||||
|
@ -65,21 +62,18 @@ 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,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -88,7 +82,6 @@ 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) {
|
||||
|
@ -135,7 +128,6 @@ 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,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
11
src/index.ts
11
src/index.ts
|
@ -35,14 +35,11 @@ router
|
|||
}
|
||||
)
|
||||
.get<BasicRouter>("/population~/health", async () => {
|
||||
const [saerro, voidwell, honu, fisu, kiwi] = await Promise.all([
|
||||
const [saerro, voidwell, honu, fisu] = 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(
|
||||
|
@ -51,11 +48,10 @@ router
|
|||
voidwell,
|
||||
honu,
|
||||
fisu,
|
||||
kiwi,
|
||||
}),
|
||||
{
|
||||
headers: { "content-type": "application/json" },
|
||||
status: saerro || voidwell || honu || fisu || kiwi ? 200 : 502,
|
||||
status: saerro || voidwell || honu || fisu ? 200 : 502,
|
||||
}
|
||||
);
|
||||
})
|
||||
|
@ -74,9 +70,8 @@ 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",
|
||||
fisuUsePS4EU: true, // env.FISU_USE_PS4EU === "1",
|
||||
};
|
||||
|
||||
const start = Date.now();
|
||||
|
|
|
@ -13,7 +13,6 @@ This service aggregates the population data from the following sources:
|
|||
- https://ps2.fisu.pw/
|
||||
- https://wt.honu.pw/
|
||||
- https://voidwell.com/ (caveat: no factions, non-standard counting method)
|
||||
- https://ps2.nice.kiwi/
|
||||
|
||||
## Routes
|
||||
|
||||
|
@ -31,8 +30,7 @@ GET /:id - Get one world by ID
|
|||
"saerro": 282,
|
||||
"fisu": 271,
|
||||
"honu": 292,
|
||||
"voidwell": 298,
|
||||
"kiwi": 281
|
||||
"voidwell": 298
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,14 +44,12 @@ GET /:id - Get one world by ID
|
|||
"fisu": { ... },
|
||||
"honu": { ... },
|
||||
"voidwell": { ... }
|
||||
"kiwi": { ... }
|
||||
},
|
||||
"lastFetchTimes": {
|
||||
"saerro": "2020-10-10T00:00:00.000Z",
|
||||
"fisu": "2020-10-10T00:00:00.000Z",
|
||||
"honu": "2020-10-10T00:00:00.000Z",
|
||||
"voidwell": "2020-10-10T00:00:00.000Z"
|
||||
"kiwi": "2020-10-10T00:00:00.000Z"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,8 +68,7 @@ GET /all - Get all worlds
|
|||
"saerro": 282,
|
||||
"fisu": 271,
|
||||
"honu": 292,
|
||||
"voidwell": 298,
|
||||
"kiwi": 281
|
||||
"voidwell": 298
|
||||
}
|
||||
},
|
||||
{
|
||||
|
@ -88,8 +83,7 @@ GET /all - Get all worlds
|
|||
"saerro": 95,
|
||||
"fisu": 48,
|
||||
"honu": 91,
|
||||
"voidwell": 99,
|
||||
"kiwi": 97
|
||||
"voidwell": 99
|
||||
}
|
||||
}
|
||||
]
|
||||
|
|
|
@ -1,82 +0,0 @@
|
|||
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,
|
||||
},
|
||||
};
|
||||
};
|
|
@ -22,7 +22,6 @@ 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;
|
||||
|
@ -41,7 +40,6 @@ export type OnePayload = {
|
|||
fisu: number | null;
|
||||
honu: number | null;
|
||||
voidwell: number | null;
|
||||
kiwi: number | null;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -51,21 +49,18 @@ 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;
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -74,7 +69,6 @@ export type Flags = {
|
|||
disableFisu: boolean;
|
||||
disableSaerro: boolean;
|
||||
disableVoidwell: boolean;
|
||||
disableKiwi: boolean;
|
||||
voidwellUsePS4: boolean;
|
||||
fisuUsePS4EU: boolean;
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue