rust rewrite

This commit is contained in:
41666 2023-05-30 01:40:00 -04:00
parent 4ded6085b7
commit 7ef2fe757c
33 changed files with 2310 additions and 4269 deletions

View file

@ -1,36 +0,0 @@
export class Cache {
private cache: Map<string, any> = new Map();
constructor(public kv: KVNamespace, public disableCache: boolean = false) {}
async get<T>(id: string): Promise<T | null> {
if (this.disableCache) {
return null;
}
// console.log("cache get", id);
let item = this.cache.get(id);
if (!item) {
// console.log("remote cache get", id);
item = await this.kv.get<T>(id, "json");
if (item) {
// console.log("local cache miss, remote cache hit", id);
this.cache.set(id, item);
}
}
return item;
}
async put<T>(id: string, world: T, ttl: number = 60 * 3): Promise<T> {
if (this.disableCache) {
return world;
}
this.cache.set(id, world);
await this.kv.put(id, JSON.stringify(world), {
expirationTtl: ttl,
});
return world;
}
}

View file

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

View file

@ -1,145 +0,0 @@
import { Cache } from "./cache";
import { fisuFetchWorld } from "./sources/fisu";
import { honuFetchWorld } from "./sources/honu";
import { saerroFetchWorld } from "./sources/saerro";
import { sanctuaryFetchWorld } from "./sources/sanctuary";
import { voidwellFetchWorld } from "./sources/voidwell";
import { DebugPayload, Flags, OnePayload, ServiceResponse } from "./types";
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: ServiceResponse<number, null> = {
population: {
total: -1,
nc: -1,
tr: -1,
vs: -1,
},
raw: null,
cachedAt: new Date(),
};
type World = {
world: OnePayload | null;
debug: DebugPayload;
};
export const getWorld = async (id: string, cache: Cache, flags: Flags) => {
const cached = await cache.get<World>(id);
if (cached) {
return cached;
}
const [saerro, fisu, honu, voidwell, sanctuary] = await Promise.all([
!flags.disableSaerro
? saerroFetchWorld(id, cache).catch((e) => {
console.error("SAERRO ERROR:", e);
return defaultServiceResponse;
})
: defaultServiceResponse,
!flags.disableFisu
? fisuFetchWorld(id, cache, flags.fisuUsePS4EU).catch(
() => defaultServiceResponse
)
: defaultServiceResponse,
!flags.disableHonu
? honuFetchWorld(id, cache).catch(() => defaultServiceResponse)
: defaultServiceResponse,
!flags.disableVoidwell
? voidwellFetchWorld(id, cache, flags.voidwellUsePS4).catch(
() => defaultServiceResponse
)
: defaultServiceResponse,
!flags.disableSanctuary
? sanctuaryFetchWorld(id, cache).catch(() => defaultServiceResponse)
: defaultServiceResponse,
]);
const debug: DebugPayload = {
raw: {
saerro: saerro.raw,
fisu: fisu.raw,
honu: honu.raw,
voidwell: voidwell.raw,
sanctuary: sanctuary.raw,
},
timings: {
saerro: saerro?.timings || null,
fisu: fisu?.timings || null,
honu: honu?.timings || null,
voidwell: voidwell?.timings || null,
sanctuary: sanctuary?.timings || null,
},
lastFetchTimes: {
saerro: saerro.cachedAt,
fisu: fisu.cachedAt,
honu: honu.cachedAt,
voidwell: voidwell.cachedAt,
sanctuary: sanctuary.cachedAt,
},
};
const totalPopulations = [
saerro.population.total,
fisu.population.total,
honu.population.total,
voidwell.population.total,
sanctuary.population.total,
].filter((x) => x > 0);
if (totalPopulations.length === 0) {
return await cache.put<World>(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,
sanctuary: 0,
},
},
debug,
});
}
const factionPopulations = [
saerro.population,
fisu.population,
honu.population,
sanctuary.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,
sanctuary: sanctuary.population.total,
},
};
return await cache.put(id, { world: payload, debug });
};

97
src/handlers.rs Normal file
View file

@ -0,0 +1,97 @@
use crate::{
sources::{fisu, honu, saerro, sanctuary, voidwell},
types::{AllResponse, Population, Response},
};
use axum::{
extract::{Path, State},
Json,
};
use tokio::task::JoinSet;
pub async fn get_one_world(State(db): State<sled::Db>, Path(world): Path<i32>) -> Json<Response> {
Json(get_world(db, world).await)
}
pub async fn get_all_worlds(State(db): State<sled::Db>) -> Json<AllResponse> {
let mut set = JoinSet::new();
let mut worlds = vec![Response::default(); 8];
for world in vec![1, 10, 13, 17, 19, 40, 1000, 2000] {
set.spawn(get_world(db.clone(), world));
}
let mut i = 0;
while let Some(response) = set.join_next().await {
worlds[i] = response.unwrap_or_default();
i += 1;
}
Json(AllResponse { worlds })
}
async fn get_world(db: sled::Db, world: i32) -> Response {
if let Ok(data) = world_from_cache(db.clone(), world) {
return data;
}
let mut response = Response::default();
response.id = world;
let mut populations: Vec<Population> = Vec::new();
let mut set = JoinSet::new();
set.spawn(async move { ("saerro", saerro(world).await) });
set.spawn(async move { ("honu", honu(world).await) });
set.spawn(async move { ("fisu", fisu(world).await) });
set.spawn(async move { ("voidwell", voidwell(world).await) });
set.spawn(async move { ("sanctuary", sanctuary(world).await) });
while let Some(data) = set.join_next().await {
let (service, population) = data.unwrap_or(("failed", Ok(Population::default())));
if service == "failed" || population.is_err() {
continue;
}
let population = population.unwrap();
populations.push(population);
response.services.insert(service.to_string(), population);
}
if (populations.len() as i32) == 0 {
return response;
}
response.average = populations.iter().map(|p| p.total).sum::<i32>() / populations.len() as i32;
response.factions.nc = populations.iter().map(|p| p.nc).sum::<i32>() / populations.len() as i32;
response.factions.tr = populations.iter().map(|p| p.tr).sum::<i32>() / populations.len() as i32;
response.factions.vs = populations.iter().map(|p| p.vs).sum::<i32>() / populations.len() as i32;
response.cached_at = chrono::Utc::now();
world_to_cache(db, world, &response);
response
}
fn world_from_cache(db: sled::Db, world: i32) -> Result<Response, ()> {
let key = format!("world:{}", world);
let value = match db.get(key) {
Ok(Some(value)) => value,
_ => return Err(()),
};
match bincode::deserialize::<Response>(&value) {
Ok(response) => {
if response.cached_at + chrono::Duration::minutes(3) < chrono::Utc::now() {
return Err(());
}
Ok(response)
}
_ => Err(()),
}
}
fn world_to_cache(db: sled::Db, world: i32, response: &Response) {
let key = format!("world:{}", world);
let value = bincode::serialize(response).unwrap();
db.insert(key, value).unwrap();
}

View file

@ -1,77 +0,0 @@
import { IRequest } from "itty-router";
import { noData } from "./errors";
import { DebugPayload, Flags, OnePayload } from "./types";
import { Cache } from "./cache";
import { getWorld } from "./fetcher";
export const handleOne = async (
{ params: { id }, query: { debug: debugParam } }: IRequest,
_1: unknown,
_2: unknown,
Cache: Cache,
flags: Flags
) => {
const { world, debug } = await getWorld(id, Cache, 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 (
{ query: { debug } }: IRequest,
_2: unknown,
_3: unknown,
cache: Cache,
flags: Flags
): Promise<Response> => {
const cached = await cache.get(`all${debug ? ".debug" : ""}`);
if (cached) {
return new Response(JSON.stringify(cached), {
headers: {
"content-type": "application/json",
},
});
}
const worlds = ["1", "10", "13", "17", "19", "40", "1000", "2000"];
const worldTasks = [];
for (const world of worlds) {
worldTasks.push(getWorld(world, cache, flags));
}
await worldTasks[0]; // Force the first one to cache for the rest
const worldData = await Promise.all(worldTasks);
if (debug === "1") {
return new Response(JSON.stringify(worldData), {
headers: {
"content-type": "application/json",
},
});
}
const worldPayloads = worldData.map((x: any) => x.world || x);
await cache.put(`all${debug ? ".debug" : ""}`, worldPayloads);
return new Response(JSON.stringify(worldPayloads), {
headers: {
"content-type": "application/json",
},
});
};

54
src/html/index.html Normal file
View file

@ -0,0 +1,54 @@
<!DOCTYPE html>
<meta charset="utf-8" />
<title>Aggregate PlanetSide 2 Population API</title>
<meta name="description" content="Multi-source population API" />
<style>
body {
font-family: sans-serif;
margin: 0;
padding: 0;
background-color: black;
color: white;
}
main {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
flex-direction: column;
font-weight: lighter;
text-align: center;
}
.big-header {
font-size: 2rem;
}
.api-list {
margin-top: 2rem;
}
.api-item {
display: block;
background-color: #334;
padding: 0.5rem 1rem;
border-radius: 0.5rem;
margin: 0.5rem;
text-decoration: none;
color: white;
font-weight: bold;
}
</style>
<main>
<div>
<b>tl;dr:</b><br />
<span class="big-header"
>( fisu + honu + saerro + sanctuary + voidwell ) / 5</span
>
</div>
<div class="api-list">
<a class="api-item" href="/worlds/1">GET /worlds/{worldID} ▶️</a>
<a class="api-item" href="/worlds/all">GET /worlds/all ▶️</a>
</div>
<p>Results are cached for 3 minutes. Requesting faster is a dumb idea.</p>
</main>

View file

@ -1,91 +0,0 @@
import { Route, Router, RouterType } from "itty-router";
import { handleAll, handleOne } from "./handlers";
import { Env, Flags } from "./types";
import { Cache } from "./cache";
import { index } from "./landing";
interface BasicRouter extends RouterType {
all: Route;
get: Route;
}
const router = <BasicRouter>Router();
router
.get<BasicRouter>(
"/",
() =>
new Response(null, { status: 303, headers: { location: "/population/" } })
)
.get<BasicRouter>("/population/", index)
.get<BasicRouter>("/population/all", handleAll)
.get<BasicRouter>("/population/:id", handleOne)
.get<BasicRouter>(
"/population~/flags",
(_1, _2, _3, cache: Cache, flags: Flags) => {
return new Response(
JSON.stringify({
...flags,
disableCache: cache.disableCache,
}),
{
headers: { "content-type": "application/json" },
}
);
}
)
.get<BasicRouter>("/population~/health", async () => {
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),
]);
return new Response(
JSON.stringify({
saerro,
voidwell,
honu,
fisu,
}),
{
headers: { "content-type": "application/json" },
status: saerro || voidwell || honu || fisu ? 200 : 502,
}
);
})
.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 Cache(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",
voidwellUsePS4: env.VOIDWELL_USE_PS4 === "1",
fisuUsePS4EU: true, // env.FISU_USE_PS4EU === "1",
};
const start = Date.now();
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"
);
response.headers.set("x-timing", `${Date.now() - start}ms`);
return response;
});
},
};

View file

@ -1,106 +0,0 @@
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
}
}
]
-- This also has a debug query parameter, but it's extremely verbose. It's good for debugging extreme async issues with the platform.
GET ~/flags - Get the current feature flags. These wiggle knobs that affect request timings, caching, and other things.
GET ~/health - Gets health of this and upstream services.
## Caching and usage limits
This service cached on a world basis for 3 minutes. Debug data is cached alongside world data too.`;
return new Response(body, {
headers: {
"content-type": "text/plain",
},
});
};

35
src/main.rs Normal file
View file

@ -0,0 +1,35 @@
use crate::handlers::{get_all_worlds, get_one_world};
use axum::{response::Html, routing::get, Router};
use std::net::SocketAddr;
use tower_http::trace::TraceLayer;
mod handlers;
mod sources;
mod types;
#[tokio::main]
async fn main() {
tracing_subscriber::fmt()
.with_env_filter("tower_http=trace")
.init();
let db = sled::open("/tmp/agg-population").expect("open");
let app = Router::new()
.route("/", get(root))
.route("/worlds/all", get(get_all_worlds))
.route("/worlds/:world", get(get_one_world))
.layer(TraceLayer::new_for_http())
.with_state(db);
let addr = SocketAddr::from(([0, 0, 0, 0], 3000));
tracing::debug!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn root() -> Html<&'static str> {
Html(include_str!("./html/index.html"))
}

227
src/sources.rs Normal file
View file

@ -0,0 +1,227 @@
use crate::types::Population;
pub async fn saerro(world: i32) -> Result<Population, ()> {
#[derive(serde::Deserialize)]
struct World {
pub population: Population,
}
#[derive(serde::Deserialize)]
struct Data {
pub world: World,
}
#[derive(serde::Deserialize)]
struct Response {
pub data: Data,
}
let url = format!(
"https://saerro.ps2.live/graphql?query={{ world(by: {{id: {}}}) {{ population {{ nc tr vs }} }}}}",
world
);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Response>()
.await
.unwrap();
Ok(Population {
nc: response.data.world.population.nc,
tr: response.data.world.population.tr,
vs: response.data.world.population.vs,
total: response.data.world.population.total(),
})
}
pub async fn honu(world: i32) -> Result<Population, ()> {
#[derive(serde::Deserialize)]
struct Response {
pub nc: i32,
pub tr: i32,
pub vs: i32,
pub ns_vs: i32,
pub ns_nc: i32,
pub ns_tr: i32,
}
let url = format!("https://wt.honu.pw/api/population/{}", world);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Response>()
.await
.unwrap();
Ok(Population {
nc: response.nc + response.ns_nc,
tr: response.tr + response.ns_tr,
vs: response.vs + response.ns_vs,
total: response.nc
+ response.tr
+ response.vs
+ response.ns_nc
+ response.ns_tr
+ response.ns_vs,
})
}
pub async fn fisu(world: i32) -> Result<Population, ()> {
#[derive(serde::Deserialize)]
struct Root {
pub result: Vec<Result>,
}
#[derive(serde::Deserialize)]
struct Result {
pub vs: i32,
pub nc: i32,
pub tr: i32,
pub ns: i32,
}
let subdomain = match world {
1000 => "ps4us.ps2",
2000 => "ps4eu.ps2",
_ => "ps2",
};
let url = format!(
"https://{}.fisu.pw/api/population/?world={}",
subdomain, world
);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Root>()
.await
.unwrap();
Ok(Population {
nc: response.result[0].nc + response.result[0].ns,
tr: response.result[0].tr + response.result[0].ns,
vs: response.result[0].vs + response.result[0].ns,
total: response.result[0].nc
+ response.result[0].tr
+ response.result[0].vs
+ response.result[0].ns,
})
}
pub async fn voidwell(world: i32) -> Result<Population, ()> {
if world == 1000 || world == 2000 {
return Err(());
}
#[derive(serde::Deserialize)]
struct Root {
#[serde(rename = "zoneStates")]
pub zone_states: Vec<ZoneState>,
}
#[derive(serde::Deserialize)]
struct ZoneState {
pub population: VoidwellPopulation,
}
#[derive(serde::Deserialize)]
struct VoidwellPopulation {
pub vs: i32,
pub nc: i32,
pub tr: i32,
pub ns: i32,
}
let platform = match world {
1000 => "ps4us",
2000 => "ps4eu",
_ => "pc",
};
let url = format!(
"https://api.voidwell.com/ps2/worldstate/{}?platform={}",
world, platform
);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Root>()
.await
.unwrap();
Ok(Population {
nc: response
.zone_states
.iter()
.map(|zone| zone.population.nc)
.sum(),
tr: response
.zone_states
.iter()
.map(|zone| zone.population.tr)
.sum(),
vs: response
.zone_states
.iter()
.map(|zone| zone.population.vs)
.sum(),
total: response
.zone_states
.iter()
.map(|zone| {
zone.population.nc + zone.population.tr + zone.population.vs + zone.population.ns
})
.sum(),
})
}
pub async fn sanctuary(world: i32) -> Result<Population, ()> {
// No PS4 nor Jaeger
if world == 1000 || world == 2000 || world == 19 {
return Err(());
}
#[derive(serde::Deserialize)]
struct Root {
pub world_population_list: Vec<World>,
}
#[derive(serde::Deserialize)]
struct World {
pub population: SanctuaryPopulation,
}
#[derive(serde::Deserialize)]
struct SanctuaryPopulation {
#[serde(rename = "VS")]
pub vs: i32,
#[serde(rename = "NC")]
pub nc: i32,
#[serde(rename = "TR")]
pub tr: i32,
#[serde(rename = "NSO")]
pub nso: i32,
}
let url = format!(
"https://census.lithafalcon.cc/get/ps2/world_population?c:censusJSON=false&world_id={}",
world
);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Root>()
.await
.unwrap();
Ok(Population {
nc: response.world_population_list[0].population.nc,
tr: response.world_population_list[0].population.tr,
vs: response.world_population_list[0].population.vs,
total: response.world_population_list[0].population.nc
+ response.world_population_list[0].population.tr
+ response.world_population_list[0].population.vs
+ response.world_population_list[0].population.nso,
})
}

View file

@ -1,110 +0,0 @@
import { Cache } from "../cache";
import { ServiceResponse } from "../types";
interface FisuResponse {
result: Record<
string,
{
worldId: number;
vs: number;
nc: number;
tr: number;
ns: number;
}[]
>;
}
const fisuFetchAllWorlds = async (
cache: Cache,
usePS4EU: boolean
): Promise<FisuResponse> => {
const cached = await cache.get<FisuResponse>("fisu");
if (cached) {
// console.log("FISU data cached", cached);
return cached;
}
const [pc, ps4us, ps4eu] = await Promise.all([
fetch(`https://ps2.fisu.pw/api/population/?world=1,10,13,17,19,40`)
.then((res) => res.json<FisuResponse>())
.catch((e) => {
console.error("FISU PC ERROR", e);
return { result: {} } as FisuResponse;
}),
fetch(`https://ps4us.ps2.fisu.pw/api/population/?world=1000`)
.then((res) => res.json<FisuResponse>())
.catch((e) => {
console.error("FISU PS4US ERROR", e);
return { result: {} } as FisuResponse;
}),
usePS4EU
? fetch(`https://ps4eu.ps2.fisu.pw/api/population/?world=2000`)
.then((res) => res.json<FisuResponse>())
.catch((e) => {
console.error("FISU PS4EU ERROR", e);
return { result: {} } as FisuResponse;
})
: ({ result: {} } as FisuResponse),
]).catch((e) => {
console.error("FISU ERROR", e);
return [{ result: {} }, { result: {} }, { result: {} }] as FisuResponse[];
});
// console.log("FISU data fetched", JSON.stringify({ pc, ps4us, ps4eu }));
const response: FisuResponse = {
result: {
...pc.result,
"1000": [ps4us.result[0] as any],
"2000": [ps4eu.result[0] as any],
},
};
return await cache.put("fisu", response);
};
export const fisuFetchWorld = async (
worldID: string,
cache: Cache,
usePS4EU: boolean
): Promise<ServiceResponse<number, any>> => {
if (!usePS4EU && worldID === "2000") {
return {
population: {
total: -1,
nc: -1,
tr: -1,
vs: -1,
},
raw: null,
cachedAt: new Date(0),
};
}
const start = Date.now();
const data: FisuResponse = await fisuFetchAllWorlds(cache, usePS4EU);
const end = Date.now();
const world = data.result[worldID];
if (!world) {
console.error(`fisu: World ${worldID} not found`);
throw new Error(`fisu: World ${worldID} not found`);
}
const { nc, tr, vs, ns } = world[0];
return {
raw: world[0],
population: {
total: vs + nc + tr + ns,
nc,
tr,
vs,
},
cachedAt: new Date(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

View file

@ -1,60 +0,0 @@
import { Cache } from "../cache";
import { ServiceResponse } from "../types";
type 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;
}[];
const honuFetchAllWorlds = async (cache: Cache): Promise<HonuResponse> => {
const cached = await cache.get<HonuResponse>("honu");
if (cached) {
return cached;
}
const req = await fetch(
`https://wt.honu.pw/api/population/multiple?worldID=1&worldID=10&worldID=13&worldID=17&worldID=19&worldID=40&worldID=1000&worldID=2000`
);
return await cache.put("honu", await req.json<HonuResponse>());
};
export const honuFetchWorld = async (
worldID: string,
cache: Cache
): Promise<ServiceResponse<number, any>> => {
const start = Date.now();
const resp = await honuFetchAllWorlds(cache);
const end = Date.now();
const data = resp.find((w) => w.worldID === Number(worldID));
if (!data) {
throw new Error(`honu: World ${worldID} not found`);
}
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(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

View file

@ -1,67 +0,0 @@
import { Cache } from "../cache";
import { Population, ServiceResponse } from "../types";
interface SaerroResponse {
data: {
allWorlds: {
id: number;
population: Population<number>;
}[];
};
}
const saerroFetchAllWorlds = async (cache: Cache): Promise<SaerroResponse> => {
const cached = await cache.get<SaerroResponse>("saerro");
if (cached) {
return cached;
}
const req = await fetch(`https://saerro.ps2.live/graphql`, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify({
query: `{
allWorlds {
id
population {
total
nc
tr
vs
}
}
}`,
}),
});
return await cache.put("saerro", await req.json<SaerroResponse>());
};
export const saerroFetchWorld = async (
id: string,
cache: Cache
): Promise<ServiceResponse<number, SaerroResponse["data"]["allWorlds"][1]>> => {
const start = Date.now();
const json: SaerroResponse = await saerroFetchAllWorlds(cache);
const end = Date.now();
const world = json.data.allWorlds.find((w) => w.id === Number(id));
if (!world) {
throw new Error(`saerro: World ${id} not found`);
}
return {
population: world.population,
raw: world,
cachedAt: new Date(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

View file

@ -1,84 +0,0 @@
import { Cache } from "../cache";
import { ServiceResponse } from "../types";
// {"world_population_list":[{"world_id":1,"last_updated":1671886187,"total":122,"population":{"VS":49,"NC":45,"TR":28,"NSO":0}},
type SanctuaryResponse = {
world_population_list: {
world_id: number;
last_updated: string;
total: number;
population: {
NC: number;
TR: number;
VS: number;
NSO: number;
};
}[];
};
const sanctuaryFetchAllWorlds = async (
cache: Cache
): Promise<SanctuaryResponse> => {
const cached = await cache.get<SanctuaryResponse>("sanctuary");
if (cached) {
return cached;
}
const req = await fetch(
"https://census.lithafalcon.cc/get/ps2/world_population?c:censusJSON=false"
);
return await cache.put("sanctuary", await req.json<SanctuaryResponse>());
};
export const sanctuaryFetchWorld = async (
worldID: string,
cache: Cache
): Promise<ServiceResponse<number, any>> => {
// No PS4 data nor Jaeger
if (worldID === "1000" || worldID === "2000" || worldID === "19") {
return {
population: {
total: -1,
nc: -1,
tr: -1,
vs: -1,
},
raw: {},
cachedAt: new Date(),
};
}
const start = Date.now();
const resp = await sanctuaryFetchAllWorlds(cache);
const end = Date.now();
const data = resp.world_population_list.find(
(w) => w.world_id === Number(worldID)
);
if (!data) {
throw new Error(`sanctuary: World ${worldID} not found`);
}
if (data.last_updated < (Date.now() / 1000 - 60 * 5).toString()) {
throw new Error(`sanctuary: World ${worldID} is stale`);
}
return {
population: {
total: data.total,
nc: data.population.NC,
tr: data.population.TR,
vs: data.population.VS,
},
raw: data,
cachedAt: new Date(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

View file

@ -1,118 +0,0 @@
import { Cache } from "../cache";
import { ServiceResponse } from "../types";
type VoidwellResponse = Array<{
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 voidwellFetchAllWorlds = async (
cache: Cache,
usePS4: boolean
): Promise<VoidwellResponse> => {
const cached = await cache.get<VoidwellResponse>("voidwell");
if (cached) {
return cached;
}
const [pc, ps4us, ps4eu] = await Promise.all([
fetch(`https://api.voidwell.com/ps2/worldstate/?platform=pc`)
.then((res) => res.json<VoidwellResponse>())
.catch((e) => {
console.error("voidwell PC ERROR", e);
return [] as VoidwellResponse;
}),
usePS4
? fetch(`https://api.voidwell.com/ps2/worldstate/?platform=ps4us`)
.then((res) => res.json<VoidwellResponse>())
.catch((e) => {
console.error("voidwell PS4US ERROR", e);
return [] as VoidwellResponse;
})
: [],
usePS4
? fetch(`https://api.voidwell.com/ps2/worldstate/?platform=ps4eu`)
.then((res) => res.json<VoidwellResponse>())
.catch((e) => {
console.error("voidwell PS4EU ERROR", e);
return [] as VoidwellResponse;
})
: [],
]);
// console.log("voidwell data fetched", JSON.stringify({ pc, ps4us, ps4eu }));
const response: VoidwellResponse = [
...pc,
...ps4us,
...ps4eu,
] as VoidwellResponse;
return await cache.put("voidwell", response);
};
// 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,
cache: Cache,
usePS4: boolean
): Promise<ServiceResponse<undefined, VoidwellResponse[0] | null>> => {
if (!usePS4 && (worldID === "1000" || worldID === "2000")) {
// Voidwell doesn't support PS4 well enough.
return {
raw: null,
population: {
total: -1,
nc: undefined,
tr: undefined,
vs: undefined,
},
cachedAt: new Date(0),
};
}
const start = Date.now();
const data = await voidwellFetchAllWorlds(cache, usePS4);
const end = Date.now();
const world = data.find((w) => w.id === Number(worldID));
if (!world) {
throw new Error(`voidwell: World ${worldID} not found`);
}
return {
raw: world,
population: {
total: world.onlineCharacters,
nc: undefined,
tr: undefined,
vs: undefined,
},
cachedAt: new Date(),
timings: {
enter: start,
exit: end,
upstream: end - start,
},
};
};

41
src/types.rs Normal file
View file

@ -0,0 +1,41 @@
use std::collections::HashMap;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct AllResponse {
pub worlds: Vec<Response>,
}
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct Response {
pub id: i32,
pub average: i32,
pub factions: Factions,
pub services: HashMap<String, Population>,
#[serde(default)]
pub cached_at: DateTime<Utc>,
}
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct Factions {
pub nc: i32,
pub tr: i32,
pub vs: i32,
}
#[derive(Deserialize, Serialize, Debug, Clone, Copy, Default)]
pub struct Population {
pub nc: i32,
pub tr: i32,
pub vs: i32,
#[serde(default)]
pub total: i32,
}
impl Population {
pub fn total(&self) -> i32 {
self.nc + self.tr + self.vs
}
}

View file

@ -1,80 +0,0 @@
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;
timings?: {
enter: number;
upstream: number;
exit: number;
};
}
export interface Env {
CACHE: KVNamespace;
DISABLE_HONU: "1" | undefined;
DISABLE_FISU: "1" | undefined;
DISABLE_SAERRO: "1" | undefined;
DISABLE_VOIDWELL: "1" | undefined;
DISABLE_SANCTUARY: "1" | undefined;
DISABLE_CACHE: "1" | undefined;
VOIDWELL_USE_PS4: "1" | undefined;
FISU_USE_PS4EU: "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;
sanctuary: number | null;
};
};
export type DebugPayload = {
raw: {
saerro: any;
fisu: any;
honu: any;
voidwell: any;
sanctuary: any;
};
timings: {
saerro: any;
fisu: any;
honu: any;
voidwell: any;
sanctuary: any;
};
lastFetchTimes: {
saerro?: Date;
fisu?: Date;
honu?: Date;
voidwell?: Date;
sanctuary?: Date;
};
};
export type Flags = {
disableHonu: boolean;
disableFisu: boolean;
disableSaerro: boolean;
disableVoidwell: boolean;
disableSanctuary: boolean;
voidwellUsePS4: boolean;
fisuUsePS4EU: boolean;
};