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

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

3
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,3 @@
{
"editor.tabSize": 2
}

3879
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

18
package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "agg-population",
"version": "0.0.0",
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
"@miniflare/tre": "^3.0.0-next.10",
"itty-cors": "^0.3.5",
"itty-router": "^3.0.8",
"typescript": "^4.9.4",
"wrangler": "2.6.2"
},
"private": true,
"scripts": {
"start": "wrangler dev --local",
"deploy": "wrangler publish",
"wrangler": "wrangler"
}
}

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;
};

105
tsconfig.json Normal file
View file

@ -0,0 +1,105 @@
{
"compilerOptions": {
/* Visit https://aka.ms/tsconfig.json to read more about this file */
/* Projects */
// "incremental": true, /* Enable incremental compilation */
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
/* Language and Environment */
"target": "es2021" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"lib": [
"es2021"
] /* Specify a set of bundled library declaration files that describe the target runtime environment. */,
"jsx": "react" /* Specify what JSX code is generated. */,
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
/* Modules */
"module": "es2022" /* Specify what module code is generated. */,
// "rootDir": "./", /* Specify the root folder within your source files. */
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
"types": [
"@cloudflare/workers-types"
] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
"resolveJsonModule": true /* Enable importing .json files */,
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
/* JavaScript Support */
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */,
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
/* Emit */
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
// "outDir": "./", /* Specify an output folder for all emitted files. */
// "removeComments": true, /* Disable emitting comments. */
"noEmit": true /* Disable emitting files from a compilation. */,
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
// "newLine": "crlf", /* Set the newline character for emitting files. */
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
/* Interop Constraints */
"isolatedModules": true /* Ensure that each file can be safely transpiled without relying on other imports. */,
"allowSyntheticDefaultImports": true /* Allow 'import x from y' when a module doesn't have a default export. */,
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
/* Type Checking */
"strict": true /* Enable all strict type-checking options. */,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
/* Completeness */
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}

15
wrangler.toml Normal file
View file

@ -0,0 +1,15 @@
name = "agg-population"
main = "src/index.ts"
compatibility_date = "2022-12-19"
[vars]
DISABLE_HONU = "0"
DISABLE_FISU = "0"
DISABLE_SAERRO = "0"
DISABLE_VOIDWELL = "0"
DISABLE_CACHE = "0"
[[kv_namespaces]]
binding = "CACHE"
id = "4b3b7f55a14c40cc84824fd0a3f58ff4"
preview_id = "3c0ce37f56be4bd998fc6f36a7a685dc"