This commit is contained in:
41666 2022-12-20 12:50:02 -05:00
commit bd89eee6e8
15 changed files with 4623 additions and 0 deletions

30
src/cache.ts Normal file
View file

@ -0,0 +1,30 @@
import { DebugPayload, OnePayload } from "./types";
type WorldObject = {
world: OnePayload | null;
debug: DebugPayload;
};
export class WorldCache {
constructor(public kv: KVNamespace, public disableCache: boolean = false) {}
async get(id: string): Promise<WorldObject | null> {
if (this.disableCache) {
return null;
}
const world = await this.kv.get<WorldObject>(id, "json");
return world;
}
async put(id: string, world: WorldObject): Promise<WorldObject> {
if (this.disableCache) {
return world;
}
await this.kv.put(id, JSON.stringify(world), {
expirationTtl: 60 * 3,
});
return world;
}
}

7
src/errors.ts Normal file
View file

@ -0,0 +1,7 @@
export const noData = () =>
new Response(
JSON.stringify({
error: "No data available",
}),
{ status: 404 }
);

270
src/handlers.ts Normal file
View file

@ -0,0 +1,270 @@
import { IRequest } from "itty-router";
import { saerroFetchWorld } from "./sources/saerro";
import { fisuFetchWorld } from "./sources/fisu";
import { honuFetchWorld } from "./sources/honu";
import { voidwellFetchWorld } from "./sources/voidwell";
import { noData } from "./errors";
import { DebugPayload, Flags, OnePayload } from "./types";
import { WorldCache } from "./cache";
const avgOf = (arr: number[]) =>
Math.floor(arr.reduce((a, b) => a + b, 0) / arr.length);
const flatMapBy = (arr: any[], key: string) =>
arr.reduce((a, b) => [...a, b[key]], []);
const defaultServiceResponse = {
population: {
total: -1,
nc: null,
tr: null,
vs: null,
},
raw: null,
cachedAt: undefined,
};
export const getWorld = async (id: string, cache: WorldCache, flags: Flags) => {
const cached = await cache.get(id);
if (cached) {
return cached;
}
const [saerro, fisu, honu, voidwell] = await Promise.all([
!flags.disableSaerro
? saerroFetchWorld(id).catch(() => defaultServiceResponse)
: defaultServiceResponse,
!flags.disableFisu
? fisuFetchWorld(id).catch(() => defaultServiceResponse)
: defaultServiceResponse,
!flags.disableHonu
? honuFetchWorld(id).catch(() => defaultServiceResponse)
: defaultServiceResponse,
!flags.disableVoidwell
? voidwellFetchWorld(id).catch(() => defaultServiceResponse)
: defaultServiceResponse,
]);
const debug: DebugPayload = {
raw: {
saerro: saerro.raw,
fisu: fisu.raw,
honu: honu.raw,
voidwell: voidwell.raw,
},
lastFetchTimes: {
saerro: saerro.cachedAt,
fisu: fisu.cachedAt,
honu: honu.cachedAt,
voidwell: voidwell.cachedAt,
},
};
const totalPopulations = [
saerro.population.total,
fisu.population.total,
honu.population.total,
voidwell.population.total,
].filter((x) => x > 0);
if (totalPopulations.length === 0) {
return await cache.put(id, {
world:
id !== "19"
? null
: {
// Jaeger gets a special case, we assume it's always up, but empty.
id: 19,
average: 0,
factions: {
nc: 0,
tr: 0,
vs: 0,
},
services: {
saerro: 0,
fisu: 0,
honu: 0,
voidwell: 0,
},
},
debug,
});
}
const factionPopulations = [
saerro.population,
fisu.population,
honu.population,
].filter((x) => x.total > 0);
const payload: OnePayload = {
id: Number(id),
average: avgOf(totalPopulations),
factions: {
nc: avgOf(flatMapBy(factionPopulations, "nc")),
tr: avgOf(flatMapBy(factionPopulations, "tr")),
vs: avgOf(flatMapBy(factionPopulations, "vs")),
},
services: {
saerro: saerro.population.total,
fisu: fisu.population.total,
honu: honu.population.total,
voidwell: voidwell.population.total,
},
};
return await cache.put(id, { world: payload, debug });
};
export const handleOne = async (
{ params: { id }, query: { debug: debugParam } }: IRequest,
_1: unknown,
_2: unknown,
worldCache: WorldCache,
flags: Flags
) => {
const { world, debug } = await getWorld(id, worldCache, flags);
if (world === null) {
return noData();
}
let output: OnePayload | (OnePayload & DebugPayload) = world;
if (debugParam) {
output = { ...output, ...debug };
}
return new Response(JSON.stringify(output), {
headers: {
"content-type": "application/json",
},
});
};
export const handleAll = async (
_1: unknown,
_2: unknown,
_3: unknown,
worldCache: WorldCache,
flags: Flags
): Promise<Response> => {
const worlds = ["1", "10", "13", "17", "19", "40", "1000", "2000"];
const worldData = await Promise.all(
worlds.map((x) =>
getWorld(x, worldCache, flags).catch(() => {
error: "World data is missing. Is it down?";
})
)
);
const worldPayloads = worldData.map((x) => x?.world || x);
return new Response(JSON.stringify(worldPayloads), {
headers: {
"content-type": "application/json",
},
});
};
export const index = (): Response => {
const body = `Aggregate Planetside 2 World Population
GitHub: https://github.com/genudine/agg-population
Production: https://agg.ps2.live/population
Need help with this data?
## Methodology
This service aggregates the population data from the following sources:
- https://saerro.ps2.live/
- https://ps2.fisu.pw/
- https://wt.honu.pw/
- https://voidwell.com/ (caveat: no factions, non-standard counting method)
## Routes
GET /:id - Get one world by ID
{
"id": 17,
"average": 285,
"factions": {
"nc": 91,
"tr": 92,
"vs": 91
},
"services": {
"saerro": 282,
"fisu": 271,
"honu": 292,
"voidwell": 298
}
}
Query Parameters:
?debug=1 - Adds these fields to the response:
{
/// ... other fields
"raw": {
"saerro": { ... },
"fisu": { ... },
"honu": { ... },
"voidwell": { ... }
},
"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"
}
}
GET /all - Get all worlds
[
{
"id": 17,
"average": 285,
"factions": {
"nc": 91,
"tr": 92,
"vs": 91
},
"services": {
"saerro": 282,
"fisu": 271,
"honu": 292,
"voidwell": 298
}
},
{
"id": 1,
"average": 83,
"factions": {
"nc": 30,
"tr": 15,
"vs": 29
},
"services": {
"saerro": 95,
"fisu": 48,
"honu": 91,
"voidwell": 99
}
}
]
## Caching and usage limits
This service cached on a world basis for 3 minutes.`;
return new Response(body, {
headers: {
"content-type": "text/plain",
},
});
};

45
src/index.ts Normal file
View file

@ -0,0 +1,45 @@
import { Route, Router, RouterType } from "itty-router";
import { handleAll, handleOne, index } from "./handlers";
import { Env, Flags } from "./types";
import { WorldCache } from "./cache";
interface BasicRouter extends RouterType {
all: Route;
get: Route;
}
const router = <BasicRouter>Router();
router
.get<BasicRouter>("/", index)
.get<BasicRouter>("/all", handleAll)
.get<BasicRouter>("/:id", handleOne)
.all<BasicRouter>("*", () => {
return new Response("Not found", {
headers: { "content-type": "text/plain" },
});
});
export default {
fetch: async (request: Request, env: Env, ctx: ExecutionContext) => {
const worldCache = new WorldCache(env.CACHE, env.DISABLE_CACHE === "1");
const flags: Flags = {
disableFisu: env.DISABLE_FISU === "1",
disableHonu: env.DISABLE_HONU === "1",
disableSaerro: env.DISABLE_SAERRO === "1",
disableVoidwell: env.DISABLE_VOIDWELL === "1",
};
return router
.handle(request as any, env, ctx, worldCache, flags)
.then((response) => {
response.headers.set("access-control-allow-origin", "*");
response.headers.set(
"access-control-allow-method",
"GET, HEAD, OPTIONS"
);
return response;
});
},
};

56
src/sources/fisu.ts Normal file
View file

@ -0,0 +1,56 @@
import { Population, ServiceResponse } from "../types";
const subdomain = (worldID: string) => {
switch (worldID) {
case "1000":
return "ps4us.ps2";
case "2000":
return "ps4eu.ps2";
default:
return "ps2";
}
};
interface FisuResponse {
config: {
world: string[];
};
result: {
worldId: number;
vs: number;
nc: number;
tr: number;
ns: number;
}[];
timing: {
"start-ms": number;
"query-ms": number;
"total-ms": number;
"process-ms": number;
};
}
export const fisuFetchWorld = async (
worldID: string
): Promise<ServiceResponse<number | undefined, FisuResponse | null>> => {
const url = `https://${subdomain(
worldID
)}.fisu.pw/api/population/?world=${worldID}`;
const res = await fetch(url);
const data: FisuResponse = await res.json();
const { vs, nc, tr, ns } = data.result[0];
return {
raw: data,
population: {
total: vs + nc + tr + ns,
nc,
tr,
vs,
},
cachedAt: new Date(),
};
};

33
src/sources/honu.ts Normal file
View file

@ -0,0 +1,33 @@
import { ServiceResponse } from "../types";
interface HonuResponse {
worldID: number;
timestamp: string;
cachedUntil: string;
total: number;
nc: number;
tr: number;
vs: number;
ns_vs: number;
ns_tr: number;
ns_nc: number;
nsOther: number;
}
export const honuFetchWorld = async (
worldID: string
): Promise<ServiceResponse<number, any>> => {
const res = await fetch(`https://wt.honu.pw/api/population/${worldID}`);
const data: HonuResponse = await res.json();
return {
population: {
total: data.total,
nc: data.nc + data.ns_nc,
tr: data.tr + data.ns_tr,
vs: data.vs + data.ns_vs,
},
raw: data,
cachedAt: new Date(),
};
};

42
src/sources/saerro.ts Normal file
View file

@ -0,0 +1,42 @@
import { Population, ServiceResponse } from "../types";
interface OneResponse {
data: {
world: {
id: string;
population: Population<number>;
};
};
}
export const saerroFetchWorld = async (
id: string
): Promise<ServiceResponse<number, OneResponse>> => {
const req = await fetch(`https://saerro.ps2.live/graphql`, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `{
world(by: {id: ${id}}) {
id
population {
total
nc
tr
vs
}
}
}`,
}),
});
const json: OneResponse = await req.json();
return {
population: json.data.world.population,
raw: json,
cachedAt: new Date(),
};
};

60
src/sources/voidwell.ts Normal file
View file

@ -0,0 +1,60 @@
import { ServiceResponse } from "../types";
interface VoidwellResponse {
id: number;
name: string;
isOnline: boolean;
onlineCharacters: number;
zoneStates: {
id: number;
name: string;
isTracking: boolean;
lockState: {
state: string;
timestamp: string;
metagameEventId: number;
triggeringFaction: number;
};
population: {
vs: number;
nc: number;
tr: number;
ns: number;
}[];
};
}
const platform = (worldID: string) => {
switch (worldID) {
case "1000":
return "ps4us";
case "2000":
return "ps4eu";
default:
return "pc";
}
};
// Voidwell is missing Oshur, and since zoneStates are the only way we can get a faction-specific population count,
// we're stuck with not counting faction populations.
export const voidwellFetchWorld = async (
worldID: string
): Promise<ServiceResponse<undefined, VoidwellResponse>> => {
const res = await fetch(
`https://api.voidwell.com/ps2/worldstate/${worldID}?platform=${platform(
worldID
)}`
);
const data: VoidwellResponse = await res.json();
return {
raw: data,
population: {
total: data.onlineCharacters,
nc: undefined,
tr: undefined,
vs: undefined,
},
cachedAt: new Date(),
};
};

59
src/types.ts Normal file
View file

@ -0,0 +1,59 @@
export interface Population<T extends number | undefined> {
total: number;
nc: T;
tr: T;
vs: T;
}
export interface ServiceResponse<PT extends number | undefined, Raw> {
population: Population<PT>;
raw: Raw;
cachedAt: Date;
}
export interface Env {
CACHE: KVNamespace;
DISABLE_HONU: "1" | undefined;
DISABLE_FISU: "1" | undefined;
DISABLE_SAERRO: "1" | undefined;
DISABLE_VOIDWELL: "1" | undefined;
DISABLE_CACHE: "1" | undefined;
}
export type OnePayload = {
id: number;
average: number;
factions: {
nc: number;
tr: number;
vs: number;
};
services: {
saerro: number | null;
fisu: number | null;
honu: number | null;
voidwell: number | null;
};
};
export type DebugPayload = {
raw: {
saerro: any;
fisu: any;
honu: any;
voidwell: any;
};
lastFetchTimes: {
saerro?: Date;
fisu?: Date;
honu?: Date;
voidwell?: Date;
};
};
export type Flags = {
disableHonu: boolean;
disableFisu: boolean;
disableSaerro: boolean;
disableVoidwell: boolean;
};