implement state update on background (basics for subscription feature)

This commit is contained in:
yggverse 2026-03-26 20:58:31 +02:00
parent e77d101b70
commit 8daa729bf9
8 changed files with 167 additions and 96 deletions

View file

@ -10,6 +10,7 @@ categories = ["parsing", "text-processing", "value-formatting"]
repository = "https://github.com/YGGverse/hlstate-rs"
[dependencies]
chrono = "0.4.44"
#observer = { git = "https://github.com/YGGverse/xash3d-master.git", package = "xash3d-observer", branch = "ip6-only" }
#protocol = { git = "https://github.com/YGGverse/xash3d-master.git", package = "xash3d-protocol", branch = "ip6-only" }
#observer = { path = "../../../xash3d-master/observer", package = "xash3d-observer" }
@ -18,4 +19,4 @@ clap = { version = "4.5.54", features = ["derive"] }
#mysql = { package = "hlstate-mysql", version = "0.1.0", path = "../mysql" }
rocket = { version = "0.5.1", features = ["json"] }
rocket_dyn_templates = { version = "0.2.0", features = ["tera"] }
toml = "0.9.10"
toml = "0.9.10"

View file

@ -12,5 +12,11 @@ debug = true
# Path to `xash3d-query` bin
query = "xash3d-query"
# Interval to update master scrape, in seconds
refresh = 60
# Define at least one master to scrape game servers from
masters = ["[202:68d0:f0d5:b88d:1d1a:555e:2f6b:3148]:27010", "[300:dee4:d3c0:953b::2019]:27010"]
masters = [
"[202:68d0:f0d5:b88d:1d1a:555e:2f6b:3148]:27010",
"[300:dee4:d3c0:953b::2019]:27010"
]

View file

@ -14,4 +14,5 @@ pub struct Config {
pub port: u16,
pub query: PathBuf,
pub title: String,
pub refresh: u64,
}

View file

@ -7,64 +7,82 @@ mod global;
mod meta;
mod scrape;
use chrono::{DateTime, Utc};
use global::Global;
use meta::Meta;
use rocket::{State, http::Status};
use rocket::{
State,
http::Status,
tokio::{spawn, sync::RwLock, time::sleep},
};
use rocket_dyn_templates::{Template, context};
use std::{collections::HashSet, net::SocketAddr, path::PathBuf, sync::Arc, time::Duration};
struct Online {
scrape: scrape::Result,
update: DateTime<Utc>,
}
type Snap = Arc<RwLock<Option<Online>>>;
#[get("/")]
fn index(meta: &State<Meta>, global: &State<Global>) -> Result<Template, Status> {
// @TODO: requires library impl
// https://github.com/FWGS/xash3d-master/issues/4
let scrape = std::process::Command::new(&global.query)
.arg("all")
.arg("-M")
.arg(
global
.masters
.iter()
.map(|a| a.to_string())
.collect::<Vec<_>>()
.join(","),
)
.arg("-j")
.output()
.map_err(|e| {
error!("Make sure `xash3d-query` is installed: {e}");
Status::InternalServerError
})?;
if scrape.status.success() {
let result: scrape::Result = rocket::serde::json::serde_json::from_str(
str::from_utf8(&scrape.stdout).map_err(|e| {
error!("stdout parse error: {e}");
Status::InternalServerError
})?,
)
.map_err(|e| {
error!("JSON parse error: {e}");
Status::InternalServerError
})?;
Ok(Template::render(
"index",
context! {
masters: &global.masters,
title: &meta.title,
version: &meta.version,
servers: result.servers,
},
))
} else {
error!("Make sure `xash3d-query` is installed!");
Err(Status::InternalServerError)
}
async fn index(
meta: &State<Meta>,
global: &State<Global>,
online: &State<Snap>,
) -> Result<Template, Status> {
let snap = online.read().await;
Ok(Template::render(
"index",
context! {
masters: &global.masters,
title: &meta.title,
version: &meta.version,
servers: snap.as_ref().map(|s|s.scrape.servers.clone()),
updated: snap.as_ref().map(|s|s.update.to_rfc2822())
},
))
}
#[launch]
fn rocket() -> _ {
async fn rocket() -> _ {
use clap::Parser;
let argument = argument::Argument::parse();
let config: config::Config =
toml::from_str(&std::fs::read_to_string(argument.config).unwrap()).unwrap();
let online: Snap = Arc::new(RwLock::new(None));
spawn({
let online = online.clone();
let query = config.query.clone();
let masters = config.masters.clone();
async move {
loop {
match scrape(&query, &masters) {
Ok(s) => match str::from_utf8(&s.stdout) {
Ok(r) => {
if s.status.success() {
*online.write().await =
match rocket::serde::json::serde_json::from_str(r) {
Ok(scrape) => Some(Online {
scrape,
update: Utc::now(),
}),
Err(e) => {
error!("Could not decode scrape response: `{e}`");
None
}
}
} else {
error!("Scrape request failed");
}
}
Err(e) => error!("Could not decode UTF-8: `{e}`"),
},
Err(e) => error!("Scrape error: `{e}`"),
}
sleep(Duration::from_secs(config.refresh)).await;
}
}
});
rocket::build()
.attach(Template::fairing())
.configure(rocket::Config {
@ -76,6 +94,7 @@ fn rocket() -> _ {
rocket::Config::release_default()
}
})
.manage(online)
.manage(Global {
masters: config.masters,
query: config.query,
@ -86,3 +105,24 @@ fn rocket() -> _ {
})
.mount("/", routes![index])
}
/// Get servers online using `bin` from given `masters`
fn scrape(
bin: &PathBuf,
masters: &HashSet<SocketAddr>,
) -> Result<std::process::Output, std::io::Error> {
// @TODO: requires library impl
// https://github.com/FWGS/xash3d-master/issues/4
std::process::Command::new(bin)
.arg("all")
.arg("-M")
.arg(
masters
.iter()
.map(|a| a.to_string())
.collect::<Vec<_>>()
.join(","),
)
.arg("-j")
.output()
}

View file

@ -12,7 +12,7 @@ pub struct Result {
pub servers: Vec<Info>,
}
#[derive(Debug, Deserialize, Serialize)]
#[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(crate = "rocket::serde")]
pub struct Info {
pub time: i64,

View file

@ -2,46 +2,53 @@
{% block content %}
<h2>Game</h2>
{% if servers %}
<table>
<thead>
<tr>
<th>Address</th>
<th>Host</th>
<th>Ping</th>
<th>Protocol</th>
<th>Gamedir</th>
<th>Map</th>
<th>Team</th>
<th>Coop</th>
<th>Password</th>
<th>Dedicated</th>
<th>DM</th>
<th>Max</th>
<th>Online</th>
<th>Status</th>
</tr>
<thead>
</tbody>
{% for server in servers %}
<tr>
<td>{{ server.address }}</td>
<td>{{ server.host }}</td>
<td>{{ server.ping }}</td>
<td>{{ server.protocol }}</td>
<td>{{ server.gamedir }}</td>
<td>{{ server.map }}</td>
<td>{{ server.team }}</td>
<td>{{ server.coop }}</td>
<td>{{ server.password }}</td>
<td>{{ server.dedicated }}</td>
<td>{{ server.dm }}</td>
<td>{{ server.maxcl }}</td>
<td>{{ server.numcl }}</td>
<td>{{ server.status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<form name="subscribe" method="get" action="/">
<table>
<thead>
<tr>
{# @TODO subscribe<th></th>#}
<th>Address</th>
<th>Host</th>
<th>Ping</th>
<th>Protocol</th>
<th>Gamedir</th>
<th>Map</th>
<th>Team</th>
<th>Coop</th>
<th>Password</th>
<th>Dedicated</th>
<th>DM</th>
<th>Max</th>
<th>Online</th>
<th>Status</th>
</tr>
<thead>
</tbody>
{% for server in servers %}
<tr>
{# @TODO subscribe<td><input type="checkbox" name="server[]" value="{{ server.address }}" /></td>#}
<td>{{ server.address }}</td>
<td>{{ server.host }}</td>
<td>{{ server.ping }}</td>
<td>{{ server.protocol }}</td>
<td>{{ server.gamedir }}</td>
<td>{{ server.map }}</td>
<td>{{ server.team }}</td>
<td>{{ server.coop }}</td>
<td>{{ server.password }}</td>
<td>{{ server.dedicated }}</td>
<td>{{ server.dm }}</td>
<td>{{ server.maxcl }}</td>
<td>{{ server.numcl }}</td>
<td>{{ server.status }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{# @TODO subscribe
<input type="number" name="online" value="1" />
<input type="submit" value="Subscribe" />#}
</form>
{% else %}
<div>
<p>Nobody.</p>

View file

@ -9,7 +9,7 @@
padding: 0;
font-family: monospace;
color-scheme: light dark;
--container-max-width: 1024px;
--container-max-width: 1128px;
--color-success: #4bc432;
--color-warning: #f37b21;
--color-error: #ff6363;
@ -75,6 +75,19 @@
position: relative;
}
header > div {
display: inline-block;
}
header > div:first-child {
float: left;
}
header > div:last-child {
float: right;
text-align: right;
}
main {
display: block;
margin: 16px auto;
@ -101,10 +114,7 @@
<body>
<header>
<h1></h1>
<div class="float-left">
</div>
<div class="float-right">
<div>
<strong>
<a href="/">{{ title }}</a> |
<a href="https://store.steampowered.com/app/70/HalfLife/" target="_blank">Game</a> |
@ -113,18 +123,21 @@
<a href="https://github.com/YGGverse/hlstate-rs" target="_blank" title="v{{ version }}">GitHub</a>
</strong>
</div>
<div>
{% if updated %}{{ updated }}{% endif %}
</div>
</header>
<main>
{% block content %}{% endblock content %}
</main>
<footer>
<h2>Master</h2>
<h2>Masters</h2>
{% if masters %}
<ul>
{% for master in masters %}
<li{# class="online" #}>{{ master }}</li>
{% endfor %}
<ul>
</ul>
{% endif %}
</footer>
</body>