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://wt.honu.pw
|
||||||
- https://saerro.ps2.live
|
- https://saerro.ps2.live
|
||||||
- https://voidwell.com
|
- 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.
|
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;
|
return cached;
|
||||||
}
|
}
|
||||||
|
|
||||||
const [saerro, fisu, honu, voidwell, kiwi] = await Promise.all([
|
const [saerro, fisu, honu, voidwell] = 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,9 +54,6 @@ export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
|
||||||
() => defaultServiceResponse
|
() => defaultServiceResponse
|
||||||
)
|
)
|
||||||
: defaultServiceResponse,
|
: defaultServiceResponse,
|
||||||
!flags.disableKiwi
|
|
||||||
? kiwiFetchWorld(id, cache).catch(() => defaultServiceResponse)
|
|
||||||
: defaultServiceResponse,
|
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const debug: DebugPayload = {
|
const debug: DebugPayload = {
|
||||||
|
@ -65,21 +62,18 @@ 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,
|
||||||
kiwi: kiwi.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,
|
||||||
kiwi: kiwi?.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,
|
||||||
kiwi: kiwi.cachedAt,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -88,7 +82,6 @@ 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,
|
||||||
kiwi.population.total,
|
|
||||||
].filter((x) => x > 0);
|
].filter((x) => x > 0);
|
||||||
|
|
||||||
if (totalPopulations.length === 0) {
|
if (totalPopulations.length === 0) {
|
||||||
|
@ -135,7 +128,6 @@ 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,
|
||||||
kiwi: kiwi.population.total,
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
11
src/index.ts
11
src/index.ts
|
@ -35,14 +35,11 @@ router
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.get<BasicRouter>("/population~/health", async () => {
|
.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://saerro.ps2.live/health").then((r) => r.status === 200),
|
||||||
fetch("https://voidwell.com/").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://wt.honu.pw/api/health").then((r) => r.status === 200),
|
||||||
fetch("https://ps2.fisu.pw").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(
|
return new Response(
|
||||||
|
@ -51,11 +48,10 @@ router
|
||||||
voidwell,
|
voidwell,
|
||||||
honu,
|
honu,
|
||||||
fisu,
|
fisu,
|
||||||
kiwi,
|
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
headers: { "content-type": "application/json" },
|
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",
|
disableHonu: env.DISABLE_HONU === "1",
|
||||||
disableSaerro: env.DISABLE_SAERRO === "1",
|
disableSaerro: env.DISABLE_SAERRO === "1",
|
||||||
disableVoidwell: env.DISABLE_VOIDWELL === "1",
|
disableVoidwell: env.DISABLE_VOIDWELL === "1",
|
||||||
disableKiwi: env.DISABLE_KIWI === "1",
|
|
||||||
voidwellUsePS4: env.VOIDWELL_USE_PS4 === "1",
|
voidwellUsePS4: env.VOIDWELL_USE_PS4 === "1",
|
||||||
fisuUsePS4EU: env.FISU_USE_PS4EU === "1",
|
fisuUsePS4EU: true, // env.FISU_USE_PS4EU === "1",
|
||||||
};
|
};
|
||||||
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
|
|
|
@ -13,7 +13,6 @@ This service aggregates the population data from the following sources:
|
||||||
- https://ps2.fisu.pw/
|
- https://ps2.fisu.pw/
|
||||||
- https://wt.honu.pw/
|
- https://wt.honu.pw/
|
||||||
- https://voidwell.com/ (caveat: no factions, non-standard counting method)
|
- https://voidwell.com/ (caveat: no factions, non-standard counting method)
|
||||||
- https://ps2.nice.kiwi/
|
|
||||||
|
|
||||||
## Routes
|
## Routes
|
||||||
|
|
||||||
|
@ -31,8 +30,7 @@ GET /:id - Get one world by ID
|
||||||
"saerro": 282,
|
"saerro": 282,
|
||||||
"fisu": 271,
|
"fisu": 271,
|
||||||
"honu": 292,
|
"honu": 292,
|
||||||
"voidwell": 298,
|
"voidwell": 298
|
||||||
"kiwi": 281
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,14 +44,12 @@ GET /:id - Get one world by ID
|
||||||
"fisu": { ... },
|
"fisu": { ... },
|
||||||
"honu": { ... },
|
"honu": { ... },
|
||||||
"voidwell": { ... }
|
"voidwell": { ... }
|
||||||
"kiwi": { ... }
|
|
||||||
},
|
},
|
||||||
"lastFetchTimes": {
|
"lastFetchTimes": {
|
||||||
"saerro": "2020-10-10T00:00:00.000Z",
|
"saerro": "2020-10-10T00:00:00.000Z",
|
||||||
"fisu": "2020-10-10T00:00:00.000Z",
|
"fisu": "2020-10-10T00:00:00.000Z",
|
||||||
"honu": "2020-10-10T00:00:00.000Z",
|
"honu": "2020-10-10T00:00:00.000Z",
|
||||||
"voidwell": "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,
|
"saerro": 282,
|
||||||
"fisu": 271,
|
"fisu": 271,
|
||||||
"honu": 292,
|
"honu": 292,
|
||||||
"voidwell": 298,
|
"voidwell": 298
|
||||||
"kiwi": 281
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -88,8 +83,7 @@ GET /all - Get all worlds
|
||||||
"saerro": 95,
|
"saerro": 95,
|
||||||
"fisu": 48,
|
"fisu": 48,
|
||||||
"honu": 91,
|
"honu": 91,
|
||||||
"voidwell": 99,
|
"voidwell": 99
|
||||||
"kiwi": 97
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
@ -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_FISU: "1" | undefined;
|
||||||
DISABLE_SAERRO: "1" | undefined;
|
DISABLE_SAERRO: "1" | undefined;
|
||||||
DISABLE_VOIDWELL: "1" | undefined;
|
DISABLE_VOIDWELL: "1" | undefined;
|
||||||
DISABLE_KIWI: "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;
|
||||||
|
@ -41,7 +40,6 @@ export type OnePayload = {
|
||||||
fisu: number | null;
|
fisu: number | null;
|
||||||
honu: number | null;
|
honu: number | null;
|
||||||
voidwell: number | null;
|
voidwell: number | null;
|
||||||
kiwi: number | null;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -51,21 +49,18 @@ export type DebugPayload = {
|
||||||
fisu: any;
|
fisu: any;
|
||||||
honu: any;
|
honu: any;
|
||||||
voidwell: any;
|
voidwell: any;
|
||||||
kiwi: any;
|
|
||||||
};
|
};
|
||||||
timings: {
|
timings: {
|
||||||
saerro: any;
|
saerro: any;
|
||||||
fisu: any;
|
fisu: any;
|
||||||
honu: any;
|
honu: any;
|
||||||
voidwell: any;
|
voidwell: any;
|
||||||
kiwi: any;
|
|
||||||
};
|
};
|
||||||
lastFetchTimes: {
|
lastFetchTimes: {
|
||||||
saerro?: Date;
|
saerro?: Date;
|
||||||
fisu?: Date;
|
fisu?: Date;
|
||||||
honu?: Date;
|
honu?: Date;
|
||||||
voidwell?: Date;
|
voidwell?: Date;
|
||||||
kiwi?: Date;
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -74,7 +69,6 @@ export type Flags = {
|
||||||
disableFisu: boolean;
|
disableFisu: boolean;
|
||||||
disableSaerro: boolean;
|
disableSaerro: boolean;
|
||||||
disableVoidwell: boolean;
|
disableVoidwell: boolean;
|
||||||
disableKiwi: boolean;
|
|
||||||
voidwellUsePS4: boolean;
|
voidwellUsePS4: boolean;
|
||||||
fisuUsePS4EU: boolean;
|
fisuUsePS4EU: boolean;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Reference in a new issue