Compare commits

..

1 commit

Author SHA1 Message Date
d22a0762ab allow PORT env var, cargo update 2023-08-23 14:47:12 -04:00
6 changed files with 337 additions and 452 deletions

670
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,27 +0,0 @@
job "agg-population" {
type = "service"
update {
max_parallel = 1
stagger = "10s"
}
group "api" {
count = 1
network {
port "http" {
static = 3000
}
}
task "api" {
driver = "docker"
config {
image = "ghcr.io/genudine/agg-population/agg-population:latest"
ports = ["http"]
}
}
}
}

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
sources::{fisu, honu, niumside, saerro, sanctuary, voidwell}, sources::{fisu, honu, saerro, sanctuary, voidwell},
types::{Population, Response}, types::{Population, Response},
}; };
use axum::{ use axum::{
@ -52,10 +52,9 @@ pub async fn get_world(
let mut populations: Vec<Population> = Vec::new(); let mut populations: Vec<Population> = Vec::new();
let mut set = JoinSet::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 { ("honu", honu(world).await) });
set.spawn(async move { ("fisu", fisu(world).await) }); set.spawn(async move { ("fisu", fisu(world).await) });
set.spawn(async move { ("saerro", saerro(world).await) });
set.spawn(async move { ("niumside", niumside(world).await) });
set.spawn(async move { ("voidwell", voidwell(world).await) }); set.spawn(async move { ("voidwell", voidwell(world).await) });
set.spawn(async move { ("sanctuary", sanctuary(world).await) }); set.spawn(async move { ("sanctuary", sanctuary(world).await) });

View file

@ -43,7 +43,7 @@
<div> <div>
<b>tl;dr:</b><br /> <b>tl;dr:</b><br />
<span class="big-header" <span class="big-header"
>( fisu + honu + saerro + sanctuary + voidwell + niumside ) / 6</span >( fisu + honu + saerro + sanctuary + voidwell ) / 5</span
> >
</div> </div>
<div class="api-list"> <div class="api-list">

View file

@ -49,8 +49,14 @@ async fn main() {
} }
}); });
let addr = SocketAddr::from(([0, 0, 0, 0], 3000)); let addr = SocketAddr::from((
tracing::debug!("listening on {}", addr); [0, 0, 0, 0],
std::env::var("PORT")
.unwrap_or("3000".to_string())
.parse()
.unwrap(),
));
tracing::debug!("listening on http://{}", addr);
axum::Server::bind(&addr) axum::Server::bind(&addr)
.serve(app.into_make_service()) .serve(app.into_make_service())
.await .await

View file

@ -247,78 +247,3 @@ pub async fn sanctuary(world: i32) -> Result<Population, ()> {
+ response.world_population_list[0].population.nso, + response.world_population_list[0].population.nso,
}) })
} }
pub async fn niumside(world: i32) -> Result<Population, ()> {
#[derive(serde::Deserialize)]
struct Root {
pub pop: Vec<Pop>,
}
#[derive(serde::Deserialize)]
struct Pop {
pub zones: Vec<Zone>,
}
#[derive(serde::Deserialize)]
struct Zone {
pub factions: Vec<Faction>,
}
#[derive(serde::Deserialize)]
struct Faction {
pub faction_id: i32,
pub teams: Vec<Team>,
}
#[derive(serde::Deserialize)]
struct Team {
pub team_id: i32,
pub team_population: i32,
}
let url = format!(
"https://niumside-poptracker.shuttleapp.rs/api/population?world={}",
world
);
let response = reqwest::get(url)
.await
.unwrap()
.json::<Root>()
.await
.unwrap();
fn extract(root: &Root, team_id: i32) -> i32 {
root.pop
.iter()
.map(|pop| {
pop.zones
.iter()
.map(|zone| {
let faction = match zone.factions.iter().find(|faction| {
faction.faction_id == team_id || faction.faction_id == 4
}) {
Some(faction) => faction,
None => return 0,
};
match faction.teams.iter().find(|team| team.team_id == team_id) {
Some(team) => team.team_population,
None => 0,
}
})
.sum::<i32>()
})
.sum::<i32>()
}
let vs = extract(&response, 1);
let nc = extract(&response, 2);
let tr = extract(&response, 3);
Ok(Population {
nc,
tr,
vs,
total: nc + tr + vs,
})
}