atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
782069860236703e2dd3c52bb589b9e2d8d48176
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/http/meta.rs

782069860236atagen2026-10-10 12:44 UTC1
//! Resources that do not belong to a single run.
782069860236atagen2026-10-10 12:44 UTC2
782069860236atagen2026-10-10 12:44 UTC3
use axum::{
782069860236atagen2026-10-10 12:44 UTC4
    Json,
782069860236atagen2026-10-10 12:44 UTC5
    http::{StatusCode, header},
782069860236atagen2026-10-10 12:44 UTC6
    response::{IntoResponse, Response},
782069860236atagen2026-10-10 12:44 UTC7
};
782069860236atagen2026-10-10 12:44 UTC8
use rand::Rng;
782069860236atagen2026-10-10 12:44 UTC9
use serde_json::{Value, json};
782069860236atagen2026-10-10 12:44 UTC10
782069860236atagen2026-10-10 12:44 UTC11
use super::{
782069860236atagen2026-10-10 12:44 UTC12
    Shared,
782069860236atagen2026-10-10 12:44 UTC13
    ctx::Ctx,
782069860236atagen2026-10-10 12:44 UTC14
    resource::{Resource, Verb},
782069860236atagen2026-10-10 12:44 UTC15
    run,
782069860236atagen2026-10-10 12:44 UTC16
};
782069860236atagen2026-10-10 12:44 UTC17
use crate::{
782069860236atagen2026-10-10 12:44 UTC18
    game::{ELDER_LAST_MODIFIED, FORUM_THREAD, Run, balance::MM_PER_INCH, forum_thread},
782069860236atagen2026-10-10 12:44 UTC19
    headers::X_SEED,
782069860236atagen2026-10-10 12:44 UTC20
    problem::{PROBLEM_BASE, Problem},
782069860236atagen2026-10-10 12:44 UTC21
};
782069860236atagen2026-10-10 12:44 UTC22
782069860236atagen2026-10-10 12:44 UTC23
pub static INDEX: Resource<Shared> = Resource::new(&[Verb::new("GET", index)]);
782069860236atagen2026-10-10 12:44 UTC24
pub static RUNS: Resource<Shared> = Resource::new(&[Verb::new("POST", create_run)]);
782069860236atagen2026-10-10 12:44 UTC25
pub static THREAD: Resource<Shared> = Resource::new(&[Verb::new("GET", thread)]);
782069860236atagen2026-10-10 12:44 UTC26
pub static GRAVEYARD: Resource<Shared> = Resource::new(&[Verb::new("GET", graveyard)]);
782069860236atagen2026-10-10 12:44 UTC27
pub static LEADERBOARD: Resource<Shared> = Resource::new(&[Verb::new("GET", leaderboard)]);
782069860236atagen2026-10-10 12:44 UTC28
782069860236atagen2026-10-10 12:44 UTC29
fn index(_: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC30
    let wants_html = ctx
782069860236atagen2026-10-10 12:44 UTC31
        .header(header::ACCEPT)
782069860236atagen2026-10-10 12:44 UTC32
        .is_some_and(|accept| accept.contains("text/html"));
782069860236atagen2026-10-10 12:44 UTC33
    if wants_html {
782069860236atagen2026-10-10 12:44 UTC34
        return (
782069860236atagen2026-10-10 12:44 UTC35
            StatusCode::SEE_OTHER,
782069860236atagen2026-10-10 12:44 UTC36
            [(header::LOCATION, "/ui/"), (header::VARY, "Accept")],
782069860236atagen2026-10-10 12:44 UTC37
        )
782069860236atagen2026-10-10 12:44 UTC38
            .into_response();
782069860236atagen2026-10-10 12:44 UTC39
    }
782069860236atagen2026-10-10 12:44 UTC40
    (
782069860236atagen2026-10-10 12:44 UTC41
        [(header::VARY, "Accept")],
782069860236atagen2026-10-10 12:44 UTC42
        Json(json!({
782069860236atagen2026-10-10 12:44 UTC43
            "detail": "This is jelqd. It is a roguelike. It is played over HTTP.",
782069860236atagen2026-10-10 12:44 UTC44
            "start": { "href": "/runs", "method": "POST" },
782069860236atagen2026-10-10 12:44 UTC45
            "leaderboard": "/leaderboard",
782069860236atagen2026-10-10 12:44 UTC46
            "graveyard": "/graveyard",
782069860236atagen2026-10-10 12:44 UTC47
            "forum": FORUM_THREAD,
782069860236atagen2026-10-10 12:44 UTC48
            "ui": "/ui/",
782069860236atagen2026-10-10 12:44 UTC49
        })),
782069860236atagen2026-10-10 12:44 UTC50
    )
782069860236atagen2026-10-10 12:44 UTC51
        .into_response()
782069860236atagen2026-10-10 12:44 UTC52
}
782069860236atagen2026-10-10 12:44 UTC53
782069860236atagen2026-10-10 12:44 UTC54
fn create_run(state: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC55
    let seeded = ctx.query("seed").is_some();
782069860236atagen2026-10-10 12:44 UTC56
    let seed = match ctx.query("seed").map(str::parse::<u64>) {
782069860236atagen2026-10-10 12:44 UTC57
        None => rand::rng().random(),
782069860236atagen2026-10-10 12:44 UTC58
        Some(Ok(seed)) => seed,
782069860236atagen2026-10-10 12:44 UTC59
        Some(Err(_)) => {
782069860236atagen2026-10-10 12:44 UTC60
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC61
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC62
                "bad-seed",
782069860236atagen2026-10-10 12:44 UTC63
                "The seed must be an unsigned 64-bit integer.",
782069860236atagen2026-10-10 12:44 UTC64
            )
782069860236atagen2026-10-10 12:44 UTC65
            .into_response();
782069860236atagen2026-10-10 12:44 UTC66
        }
782069860236atagen2026-10-10 12:44 UTC67
    };
782069860236atagen2026-10-10 12:44 UTC68
    let config = &state.config;
782069860236atagen2026-10-10 12:44 UTC69
    let mut runs = state.runs();
782069860236atagen2026-10-10 12:44 UTC70
    runs.retain(|_, run| ctx.now.duration_since(run.last_seen) < config.idle_ttl);
782069860236atagen2026-10-10 12:44 UTC71
    if runs.len() >= config.max_runs {
782069860236atagen2026-10-10 12:44 UTC72
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC73
            StatusCode::SERVICE_UNAVAILABLE,
782069860236atagen2026-10-10 12:44 UTC74
            "server-full",
782069860236atagen2026-10-10 12:44 UTC75
            "The server is full. Everyone is in there.",
782069860236atagen2026-10-10 12:44 UTC76
        )
782069860236atagen2026-10-10 12:44 UTC77
        .header(header::RETRY_AFTER, "600")
782069860236atagen2026-10-10 12:44 UTC78
        .into_response();
782069860236atagen2026-10-10 12:44 UTC79
    }
782069860236atagen2026-10-10 12:44 UTC80
    let id = loop {
782069860236atagen2026-10-10 12:44 UTC81
        let id = format!("{:032x}", rand::rng().random::<u128>());
782069860236atagen2026-10-10 12:44 UTC82
        if !runs.contains_key(&id) {
782069860236atagen2026-10-10 12:44 UTC83
            break id;
782069860236atagen2026-10-10 12:44 UTC84
        }
782069860236atagen2026-10-10 12:44 UTC85
    };
782069860236atagen2026-10-10 12:44 UTC86
    let public_id = format!("{:08x}", rand::rng().random::<u32>());
782069860236atagen2026-10-10 12:44 UTC87
    let mut run = Run::new(id.clone(), public_id, config.secret.mix_seed(seed));
782069860236atagen2026-10-10 12:44 UTC88
    run.seeded = seeded;
782069860236atagen2026-10-10 12:44 UTC89
    run.refractory = config.refractory;
782069860236atagen2026-10-10 12:44 UTC90
    run.last_seen = ctx.now;
782069860236atagen2026-10-10 12:44 UTC91
    let response = (
782069860236atagen2026-10-10 12:44 UTC92
        StatusCode::CREATED,
782069860236atagen2026-10-10 12:44 UTC93
        [
782069860236atagen2026-10-10 12:44 UTC94
            (header::LOCATION, run.path("")),
782069860236atagen2026-10-10 12:44 UTC95
            (header::ETAG, run.etag()),
782069860236atagen2026-10-10 12:44 UTC96
            (X_SEED, seed.to_string()),
782069860236atagen2026-10-10 12:44 UTC97
        ],
782069860236atagen2026-10-10 12:44 UTC98
        Json(run.view()),
782069860236atagen2026-10-10 12:44 UTC99
    )
782069860236atagen2026-10-10 12:44 UTC100
        .into_response();
782069860236atagen2026-10-10 12:44 UTC101
    let response = run::finish(&mut run, ctx, &config.secret, response);
782069860236atagen2026-10-10 12:44 UTC102
    runs.insert(id, run);
782069860236atagen2026-10-10 12:44 UTC103
    response
782069860236atagen2026-10-10 12:44 UTC104
}
782069860236atagen2026-10-10 12:44 UTC105
782069860236atagen2026-10-10 12:44 UTC106
fn thread(_: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC107
    match ctx.param("year").parse::<u32>() {
782069860236atagen2026-10-10 12:44 UTC108
        Ok(2009) => (
782069860236atagen2026-10-10 12:44 UTC109
            [(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)],
782069860236atagen2026-10-10 12:44 UTC110
            Json(forum_thread()),
782069860236atagen2026-10-10 12:44 UTC111
        )
782069860236atagen2026-10-10 12:44 UTC112
            .into_response(),
782069860236atagen2026-10-10 12:44 UTC113
        Ok(year) if year > 2009 => Problem::new(
782069860236atagen2026-10-10 12:44 UTC114
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC115
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC116
            "Nobody has posted since 2009.",
782069860236atagen2026-10-10 12:44 UTC117
        )
782069860236atagen2026-10-10 12:44 UTC118
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC119
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC120
        _ => Problem::new(
782069860236atagen2026-10-10 12:44 UTC121
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC122
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC123
            "The forum did not exist yet. The problem did.",
782069860236atagen2026-10-10 12:44 UTC124
        )
782069860236atagen2026-10-10 12:44 UTC125
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC126
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC127
    }
782069860236atagen2026-10-10 12:44 UTC128
}
782069860236atagen2026-10-10 12:44 UTC129
782069860236atagen2026-10-10 12:44 UTC130
fn graveyard(state: &mut Shared, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC131
    let runs = state.runs();
782069860236atagen2026-10-10 12:44 UTC132
    let dead: Vec<Value> = runs
782069860236atagen2026-10-10 12:44 UTC133
        .values()
782069860236atagen2026-10-10 12:44 UTC134
        .filter_map(|run| {
782069860236atagen2026-10-10 12:44 UTC135
            let ending = run.ended.as_ref()?;
782069860236atagen2026-10-10 12:44 UTC136
            Some(json!({
782069860236atagen2026-10-10 12:44 UTC137
                "public_id": run.public_id,
782069860236atagen2026-10-10 12:44 UTC138
                "cause": format!("{PROBLEM_BASE}{}", ending.cause.slug()),
782069860236atagen2026-10-10 12:44 UTC139
                "detail": ending.cause.detail(),
782069860236atagen2026-10-10 12:44 UTC140
                "turn": ending.turn,
782069860236atagen2026-10-10 12:44 UTC141
            }))
782069860236atagen2026-10-10 12:44 UTC142
        })
782069860236atagen2026-10-10 12:44 UTC143
        .collect();
782069860236atagen2026-10-10 12:44 UTC144
    Json(json!({ "runs": dead })).into_response()
782069860236atagen2026-10-10 12:44 UTC145
}
782069860236atagen2026-10-10 12:44 UTC146
782069860236atagen2026-10-10 12:44 UTC147
fn leaderboard(state: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC148
    let truthful = match ctx.query("sort") {
782069860236atagen2026-10-10 12:44 UTC149
        None | Some("self-reported-length") => false,
782069860236atagen2026-10-10 12:44 UTC150
        Some("content-length") => match ctx.cookie("ruler") {
782069860236atagen2026-10-10 12:44 UTC151
            Some(ruler) if state.config.secret.verify("ruler", ruler) == Some("1") => true,
782069860236atagen2026-10-10 12:44 UTC152
            Some(_) => {
782069860236atagen2026-10-10 12:44 UTC153
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC154
                    StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC155
                    "exaggeration",
782069860236atagen2026-10-10 12:44 UTC156
                    "The server has measured. Lying is not a technique.",
782069860236atagen2026-10-10 12:44 UTC157
                )
782069860236atagen2026-10-10 12:44 UTC158
                .with("cookie", "ruler")
782069860236atagen2026-10-10 12:44 UTC159
                .into_response();
782069860236atagen2026-10-10 12:44 UTC160
            }
782069860236atagen2026-10-10 12:44 UTC161
            None => {
782069860236atagen2026-10-10 12:44 UTC162
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC163
                    StatusCode::FORBIDDEN,
782069860236atagen2026-10-10 12:44 UTC164
                    "ruler-required",
782069860236atagen2026-10-10 12:44 UTC165
                    "Sorting by the truth requires a Ruler.",
782069860236atagen2026-10-10 12:44 UTC166
                )
782069860236atagen2026-10-10 12:44 UTC167
                .into_response();
782069860236atagen2026-10-10 12:44 UTC168
            }
782069860236atagen2026-10-10 12:44 UTC169
        },
782069860236atagen2026-10-10 12:44 UTC170
        Some(_) => {
782069860236atagen2026-10-10 12:44 UTC171
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC172
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC173
                "unknown-sort",
782069860236atagen2026-10-10 12:44 UTC174
                "The leaderboard sorts by self-reported-length or content-length.",
782069860236atagen2026-10-10 12:44 UTC175
            )
782069860236atagen2026-10-10 12:44 UTC176
            .into_response();
782069860236atagen2026-10-10 12:44 UTC177
        }
782069860236atagen2026-10-10 12:44 UTC178
    };
782069860236atagen2026-10-10 12:44 UTC179
    let include_seeded = ctx.query("seeded") == Some("include");
782069860236atagen2026-10-10 12:44 UTC180
    let runs = state.runs();
782069860236atagen2026-10-10 12:44 UTC181
    let mut entries: Vec<&Run> = runs
782069860236atagen2026-10-10 12:44 UTC182
        .values()
782069860236atagen2026-10-10 12:44 UTC183
        .filter(|run| include_seeded || !run.seeded)
782069860236atagen2026-10-10 12:44 UTC184
        .collect();
782069860236atagen2026-10-10 12:44 UTC185
    let key = |run: &Run| {
782069860236atagen2026-10-10 12:44 UTC186
        if truthful {
782069860236atagen2026-10-10 12:44 UTC187
            run.current_length()
782069860236atagen2026-10-10 12:44 UTC188
        } else {
782069860236atagen2026-10-10 12:44 UTC189
            run.self_reported()
782069860236atagen2026-10-10 12:44 UTC190
        }
782069860236atagen2026-10-10 12:44 UTC191
    };
782069860236atagen2026-10-10 12:44 UTC192
    entries.sort_by(|a, b| key(b).total_cmp(&key(a)));
782069860236atagen2026-10-10 12:44 UTC193
    let entries: Vec<Value> = entries
782069860236atagen2026-10-10 12:44 UTC194
        .into_iter()
782069860236atagen2026-10-10 12:44 UTC195
        .map(|run| {
782069860236atagen2026-10-10 12:44 UTC196
            let mut entry = json!({
782069860236atagen2026-10-10 12:44 UTC197
                "public_id": run.public_id,
782069860236atagen2026-10-10 12:44 UTC198
                "status": run.status(),
782069860236atagen2026-10-10 12:44 UTC199
                "floor": run.floor.number(),
782069860236atagen2026-10-10 12:44 UTC200
                "self_reported_length": (run.self_reported() * 100.0).round() / 100.0,
782069860236atagen2026-10-10 12:44 UTC201
                "ranked": !run.seeded,
782069860236atagen2026-10-10 12:44 UTC202
                "viral": run.filmed_at.is_some(),
782069860236atagen2026-10-10 12:44 UTC203
            });
782069860236atagen2026-10-10 12:44 UTC204
            if truthful {
782069860236atagen2026-10-10 12:44 UTC205
                entry["content_length"] = json!((run.current_length() * MM_PER_INCH).round());
782069860236atagen2026-10-10 12:44 UTC206
            }
782069860236atagen2026-10-10 12:44 UTC207
            entry
782069860236atagen2026-10-10 12:44 UTC208
        })
782069860236atagen2026-10-10 12:44 UTC209
        .collect();
782069860236atagen2026-10-10 12:44 UTC210
    Json(json!({ "entries": entries })).into_response()
782069860236atagen2026-10-10 12:44 UTC211
}
782069860236atagen2026-10-10 12:44 UTC212
782069860236atagen2026-10-10 12:44 UTC213
pub async fn not_found() -> Problem {
782069860236atagen2026-10-10 12:44 UTC214
    Problem::new(
782069860236atagen2026-10-10 12:44 UTC215
        StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC216
        "not-found",
782069860236atagen2026-10-10 12:44 UTC217
        "The resource does not exist. This is not about you.",
782069860236atagen2026-10-10 12:44 UTC218
    )
782069860236atagen2026-10-10 12:44 UTC219
}