use actix_web::{web, HttpResponse};
use crate::pages::insert_security_headers;
use crate::pages::templates::SLOW_GAME;
use crate::WebData;
#[derive(serde_derive::Serialize)]
struct GameData<'a> {
gameuid: &'a str,
gameuidenc: String,
}
#[actix_web::get("slow-game-{path}.html")]
pub async fn slow_game(path: web::Path<String>, data: web::Data<WebData<'_>>) -> HttpResponse {
let gameuid = path.into_inner();
let gameuid = match gameuid.char_indices().nth(128) {
None => gameuid,
Some((idx, _)) => (&gameuid[..idx]).to_string(),
};
let dbclient = match data.pool_ro.get().await {
Ok(client) => client,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let stmt = match dbclient.prepare("select g.status, g.notes_html, g.fo_forum_url, (select count(*) from games.players p where p.game_uid = g.game_uid and p.client_type = 'p'), MIN(t.turn_ts), MAX(t.turn_ts), COUNT(t.turn) from games.games g left join games.turns t on t.game_uid = g.game_uid where g.game_uid = $1 group by g.game_uid;").await {
Ok(stmt) => stmt,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let rows = match dbclient.query_opt(&stmt, &[&gameuid]).await {
Ok(rows) => rows,
Err(e) => {
log::error!("Pool RO query error {}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
let _row = match rows {
Some(r) => r,
None => {
return HttpResponse::NotFound().body("Not found");
}
};
let body = match data.handlebars_xml.render(
SLOW_GAME,
&GameData {
gameuid: &gameuid,
gameuidenc: pct_str::PctString::encode(gameuid.chars(), pct_str::URIReserved)
.into_string(),
},
) {
Ok(b) => b,
Err(e) => {
log::error!("{}", e);
return HttpResponse::ServiceUnavailable().body(actix_web::body::None::new());
}
};
insert_security_headers(HttpResponse::Ok()).body(body)
}