atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
3f9091557ec3a198324147b05397e1bf5c50247a
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>());
3f9091557ec3atagen2026-10-11 11:43 UTC87
    let mut run = Run::with_oracle(
3f9091557ec3atagen2026-10-11 11:43 UTC88
        id.clone(),
3f9091557ec3atagen2026-10-11 11:43 UTC89
        public_id,
3f9091557ec3atagen2026-10-11 11:43 UTC90
        config.secret.mix_seed(seed),
3f9091557ec3atagen2026-10-11 11:43 UTC91
        &config.oracle,
3f9091557ec3atagen2026-10-11 11:43 UTC92
    );
782069860236atagen2026-10-10 12:44 UTC93
    run.seeded = seeded;
782069860236atagen2026-10-10 12:44 UTC94
    run.refractory = config.refractory;
782069860236atagen2026-10-10 12:44 UTC95
    run.last_seen = ctx.now;
782069860236atagen2026-10-10 12:44 UTC96
    let response = (
782069860236atagen2026-10-10 12:44 UTC97
        StatusCode::CREATED,
782069860236atagen2026-10-10 12:44 UTC98
        [
782069860236atagen2026-10-10 12:44 UTC99
            (header::LOCATION, run.path("")),
782069860236atagen2026-10-10 12:44 UTC100
            (header::ETAG, run.etag()),
782069860236atagen2026-10-10 12:44 UTC101
            (X_SEED, seed.to_string()),
782069860236atagen2026-10-10 12:44 UTC102
        ],
782069860236atagen2026-10-10 12:44 UTC103
        Json(run.view()),
782069860236atagen2026-10-10 12:44 UTC104
    )
782069860236atagen2026-10-10 12:44 UTC105
        .into_response();
782069860236atagen2026-10-10 12:44 UTC106
    let response = run::finish(&mut run, ctx, &config.secret, response);
782069860236atagen2026-10-10 12:44 UTC107
    runs.insert(id, run);
782069860236atagen2026-10-10 12:44 UTC108
    response
782069860236atagen2026-10-10 12:44 UTC109
}
782069860236atagen2026-10-10 12:44 UTC110
782069860236atagen2026-10-10 12:44 UTC111
fn thread(_: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC112
    match ctx.param("year").parse::<u32>() {
782069860236atagen2026-10-10 12:44 UTC113
        Ok(2009) => (
782069860236atagen2026-10-10 12:44 UTC114
            [(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)],
782069860236atagen2026-10-10 12:44 UTC115
            Json(forum_thread()),
782069860236atagen2026-10-10 12:44 UTC116
        )
782069860236atagen2026-10-10 12:44 UTC117
            .into_response(),
782069860236atagen2026-10-10 12:44 UTC118
        Ok(year) if year > 2009 => Problem::new(
782069860236atagen2026-10-10 12:44 UTC119
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC120
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC121
            "Nobody has posted since 2009.",
782069860236atagen2026-10-10 12:44 UTC122
        )
782069860236atagen2026-10-10 12:44 UTC123
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC124
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC125
        _ => Problem::new(
782069860236atagen2026-10-10 12:44 UTC126
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC127
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC128
            "The forum did not exist yet. The problem did.",
782069860236atagen2026-10-10 12:44 UTC129
        )
782069860236atagen2026-10-10 12:44 UTC130
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC131
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC132
    }
782069860236atagen2026-10-10 12:44 UTC133
}
782069860236atagen2026-10-10 12:44 UTC134
782069860236atagen2026-10-10 12:44 UTC135
fn graveyard(state: &mut Shared, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC136
    let runs = state.runs();
782069860236atagen2026-10-10 12:44 UTC137
    let dead: Vec<Value> = runs
782069860236atagen2026-10-10 12:44 UTC138
        .values()
782069860236atagen2026-10-10 12:44 UTC139
        .filter_map(|run| {
782069860236atagen2026-10-10 12:44 UTC140
            let ending = run.ended.as_ref()?;
782069860236atagen2026-10-10 12:44 UTC141
            Some(json!({
782069860236atagen2026-10-10 12:44 UTC142
                "public_id": run.public_id,
782069860236atagen2026-10-10 12:44 UTC143
                "cause": format!("{PROBLEM_BASE}{}", ending.cause.slug()),
782069860236atagen2026-10-10 12:44 UTC144
                "detail": ending.cause.detail(),
782069860236atagen2026-10-10 12:44 UTC145
                "turn": ending.turn,
782069860236atagen2026-10-10 12:44 UTC146
            }))
782069860236atagen2026-10-10 12:44 UTC147
        })
782069860236atagen2026-10-10 12:44 UTC148
        .collect();
782069860236atagen2026-10-10 12:44 UTC149
    Json(json!({ "runs": dead })).into_response()
782069860236atagen2026-10-10 12:44 UTC150
}
782069860236atagen2026-10-10 12:44 UTC151
782069860236atagen2026-10-10 12:44 UTC152
fn leaderboard(state: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC153
    let truthful = match ctx.query("sort") {
782069860236atagen2026-10-10 12:44 UTC154
        None | Some("self-reported-length") => false,
782069860236atagen2026-10-10 12:44 UTC155
        Some("content-length") => match ctx.cookie("ruler") {
782069860236atagen2026-10-10 12:44 UTC156
            Some(ruler) if state.config.secret.verify("ruler", ruler) == Some("1") => true,
782069860236atagen2026-10-10 12:44 UTC157
            Some(_) => {
782069860236atagen2026-10-10 12:44 UTC158
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC159
                    StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC160
                    "exaggeration",
782069860236atagen2026-10-10 12:44 UTC161
                    "The server has measured. Lying is not a technique.",
782069860236atagen2026-10-10 12:44 UTC162
                )
782069860236atagen2026-10-10 12:44 UTC163
                .with("cookie", "ruler")
782069860236atagen2026-10-10 12:44 UTC164
                .into_response();
782069860236atagen2026-10-10 12:44 UTC165
            }
782069860236atagen2026-10-10 12:44 UTC166
            None => {
782069860236atagen2026-10-10 12:44 UTC167
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC168
                    StatusCode::FORBIDDEN,
782069860236atagen2026-10-10 12:44 UTC169
                    "ruler-required",
782069860236atagen2026-10-10 12:44 UTC170
                    "Sorting by the truth requires a Ruler.",
782069860236atagen2026-10-10 12:44 UTC171
                )
782069860236atagen2026-10-10 12:44 UTC172
                .into_response();
782069860236atagen2026-10-10 12:44 UTC173
            }
782069860236atagen2026-10-10 12:44 UTC174
        },
782069860236atagen2026-10-10 12:44 UTC175
        Some(_) => {
782069860236atagen2026-10-10 12:44 UTC176
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC177
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC178
                "unknown-sort",
782069860236atagen2026-10-10 12:44 UTC179
                "The leaderboard sorts by self-reported-length or content-length.",
782069860236atagen2026-10-10 12:44 UTC180
            )
782069860236atagen2026-10-10 12:44 UTC181
            .into_response();
782069860236atagen2026-10-10 12:44 UTC182
        }
782069860236atagen2026-10-10 12:44 UTC183
    };
782069860236atagen2026-10-10 12:44 UTC184
    let include_seeded = ctx.query("seeded") == Some("include");
782069860236atagen2026-10-10 12:44 UTC185
    let runs = state.runs();
782069860236atagen2026-10-10 12:44 UTC186
    let mut entries: Vec<&Run> = runs
782069860236atagen2026-10-10 12:44 UTC187
        .values()
782069860236atagen2026-10-10 12:44 UTC188
        .filter(|run| include_seeded || !run.seeded)
782069860236atagen2026-10-10 12:44 UTC189
        .collect();
782069860236atagen2026-10-10 12:44 UTC190
    let key = |run: &Run| {
782069860236atagen2026-10-10 12:44 UTC191
        if truthful {
782069860236atagen2026-10-10 12:44 UTC192
            run.current_length()
782069860236atagen2026-10-10 12:44 UTC193
        } else {
782069860236atagen2026-10-10 12:44 UTC194
            run.self_reported()
782069860236atagen2026-10-10 12:44 UTC195
        }
782069860236atagen2026-10-10 12:44 UTC196
    };
782069860236atagen2026-10-10 12:44 UTC197
    entries.sort_by(|a, b| key(b).total_cmp(&key(a)));
782069860236atagen2026-10-10 12:44 UTC198
    let entries: Vec<Value> = entries
782069860236atagen2026-10-10 12:44 UTC199
        .into_iter()
782069860236atagen2026-10-10 12:44 UTC200
        .map(|run| {
782069860236atagen2026-10-10 12:44 UTC201
            let mut entry = json!({
782069860236atagen2026-10-10 12:44 UTC202
                "public_id": run.public_id,
782069860236atagen2026-10-10 12:44 UTC203
                "status": run.status(),
782069860236atagen2026-10-10 12:44 UTC204
                "floor": run.floor.number(),
782069860236atagen2026-10-10 12:44 UTC205
                "self_reported_length": (run.self_reported() * 100.0).round() / 100.0,
782069860236atagen2026-10-10 12:44 UTC206
                "ranked": !run.seeded,
782069860236atagen2026-10-10 12:44 UTC207
                "viral": run.filmed_at.is_some(),
782069860236atagen2026-10-10 12:44 UTC208
            });
782069860236atagen2026-10-10 12:44 UTC209
            if truthful {
782069860236atagen2026-10-10 12:44 UTC210
                entry["content_length"] = json!((run.current_length() * MM_PER_INCH).round());
782069860236atagen2026-10-10 12:44 UTC211
            }
782069860236atagen2026-10-10 12:44 UTC212
            entry
782069860236atagen2026-10-10 12:44 UTC213
        })
782069860236atagen2026-10-10 12:44 UTC214
        .collect();
782069860236atagen2026-10-10 12:44 UTC215
    Json(json!({ "entries": entries })).into_response()
782069860236atagen2026-10-10 12:44 UTC216
}
782069860236atagen2026-10-10 12:44 UTC217
782069860236atagen2026-10-10 12:44 UTC218
pub async fn not_found() -> Problem {
782069860236atagen2026-10-10 12:44 UTC219
    Problem::new(
782069860236atagen2026-10-10 12:44 UTC220
        StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC221
        "not-found",
782069860236atagen2026-10-10 12:44 UTC222
        "The resource does not exist. This is not about you.",
782069860236atagen2026-10-10 12:44 UTC223
    )
782069860236atagen2026-10-10 12:44 UTC224
}