atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
9374b3933189ddeefe1c4e1c10256e0d8285b2c7
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::{
159f0998263batagen2026-10-12 11:01 UTC18
    game::{ELDER_LAST_MODIFIED, FORUM_THREAD, Run, balance::MM_PER_INCH, forum_thread, volume},
782069860236atagen2026-10-10 12:44 UTC19
    headers::X_SEED,
782069860236atagen2026-10-10 12:44 UTC20
    problem::{PROBLEM_BASE, Problem},
159f0998263batagen2026-10-12 11:01 UTC21
    records::Record,
782069860236atagen2026-10-10 12:44 UTC22
};
782069860236atagen2026-10-10 12:44 UTC23
159f0998263batagen2026-10-12 11:01 UTC24
use super::registry::hold;
159f0998263batagen2026-10-12 11:01 UTC25
782069860236atagen2026-10-10 12:44 UTC26
pub static INDEX: Resource<Shared> = Resource::new(&[Verb::new("GET", index)]);
782069860236atagen2026-10-10 12:44 UTC27
pub static RUNS: Resource<Shared> = Resource::new(&[Verb::new("POST", create_run)]);
782069860236atagen2026-10-10 12:44 UTC28
pub static THREAD: Resource<Shared> = Resource::new(&[Verb::new("GET", thread)]);
782069860236atagen2026-10-10 12:44 UTC29
pub static GRAVEYARD: Resource<Shared> = Resource::new(&[Verb::new("GET", graveyard)]);
782069860236atagen2026-10-10 12:44 UTC30
pub static LEADERBOARD: Resource<Shared> = Resource::new(&[Verb::new("GET", leaderboard)]);
782069860236atagen2026-10-10 12:44 UTC31
782069860236atagen2026-10-10 12:44 UTC32
fn index(_: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC33
    let wants_html = ctx
782069860236atagen2026-10-10 12:44 UTC34
        .header(header::ACCEPT)
782069860236atagen2026-10-10 12:44 UTC35
        .is_some_and(|accept| accept.contains("text/html"));
782069860236atagen2026-10-10 12:44 UTC36
    if wants_html {
782069860236atagen2026-10-10 12:44 UTC37
        return (
782069860236atagen2026-10-10 12:44 UTC38
            StatusCode::SEE_OTHER,
782069860236atagen2026-10-10 12:44 UTC39
            [(header::LOCATION, "/ui/"), (header::VARY, "Accept")],
782069860236atagen2026-10-10 12:44 UTC40
        )
782069860236atagen2026-10-10 12:44 UTC41
            .into_response();
782069860236atagen2026-10-10 12:44 UTC42
    }
782069860236atagen2026-10-10 12:44 UTC43
    (
782069860236atagen2026-10-10 12:44 UTC44
        [(header::VARY, "Accept")],
782069860236atagen2026-10-10 12:44 UTC45
        Json(json!({
782069860236atagen2026-10-10 12:44 UTC46
            "detail": "This is jelqd. It is a roguelike. It is played over HTTP.",
782069860236atagen2026-10-10 12:44 UTC47
            "start": { "href": "/runs", "method": "POST" },
782069860236atagen2026-10-10 12:44 UTC48
            "leaderboard": "/leaderboard",
782069860236atagen2026-10-10 12:44 UTC49
            "graveyard": "/graveyard",
782069860236atagen2026-10-10 12:44 UTC50
            "forum": FORUM_THREAD,
782069860236atagen2026-10-10 12:44 UTC51
            "ui": "/ui/",
782069860236atagen2026-10-10 12:44 UTC52
        })),
782069860236atagen2026-10-10 12:44 UTC53
    )
782069860236atagen2026-10-10 12:44 UTC54
        .into_response()
782069860236atagen2026-10-10 12:44 UTC55
}
782069860236atagen2026-10-10 12:44 UTC56
782069860236atagen2026-10-10 12:44 UTC57
fn create_run(state: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC58
    let seeded = ctx.query("seed").is_some();
782069860236atagen2026-10-10 12:44 UTC59
    let seed = match ctx.query("seed").map(str::parse::<u64>) {
782069860236atagen2026-10-10 12:44 UTC60
        None => rand::rng().random(),
782069860236atagen2026-10-10 12:44 UTC61
        Some(Ok(seed)) => seed,
782069860236atagen2026-10-10 12:44 UTC62
        Some(Err(_)) => {
782069860236atagen2026-10-10 12:44 UTC63
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC64
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC65
                "bad-seed",
782069860236atagen2026-10-10 12:44 UTC66
                "The seed must be an unsigned 64-bit integer.",
782069860236atagen2026-10-10 12:44 UTC67
            )
782069860236atagen2026-10-10 12:44 UTC68
            .into_response();
782069860236atagen2026-10-10 12:44 UTC69
        }
782069860236atagen2026-10-10 12:44 UTC70
    };
782069860236atagen2026-10-10 12:44 UTC71
    let config = &state.config;
159f0998263batagen2026-10-12 11:01 UTC72
    for slot in state.runs.evict_idle(ctx.now, config.idle_ttl) {
159f0998263batagen2026-10-12 11:01 UTC73
        state.records.save(&Record::of(&hold(&slot)));
e807266c323batagen2026-10-12 09:46 UTC74
    }
159f0998263batagen2026-10-12 11:01 UTC75
    if state.runs.len() >= config.max_runs {
782069860236atagen2026-10-10 12:44 UTC76
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC77
            StatusCode::SERVICE_UNAVAILABLE,
782069860236atagen2026-10-10 12:44 UTC78
            "server-full",
782069860236atagen2026-10-10 12:44 UTC79
            "The server is full. Everyone is in there.",
782069860236atagen2026-10-10 12:44 UTC80
        )
782069860236atagen2026-10-10 12:44 UTC81
        .header(header::RETRY_AFTER, "600")
782069860236atagen2026-10-10 12:44 UTC82
        .into_response();
782069860236atagen2026-10-10 12:44 UTC83
    }
782069860236atagen2026-10-10 12:44 UTC84
    let id = loop {
782069860236atagen2026-10-10 12:44 UTC85
        let id = format!("{:032x}", rand::rng().random::<u128>());
159f0998263batagen2026-10-12 11:01 UTC86
        if !state.runs.contains(&id) && state.records.find(&id).is_none() {
782069860236atagen2026-10-10 12:44 UTC87
            break id;
782069860236atagen2026-10-10 12:44 UTC88
        }
782069860236atagen2026-10-10 12:44 UTC89
    };
782069860236atagen2026-10-10 12:44 UTC90
    let public_id = format!("{:08x}", rand::rng().random::<u32>());
3f9091557ec3atagen2026-10-11 11:43 UTC91
    let mut run = Run::with_oracle(
3f9091557ec3atagen2026-10-11 11:43 UTC92
        id.clone(),
3f9091557ec3atagen2026-10-11 11:43 UTC93
        public_id,
3f9091557ec3atagen2026-10-11 11:43 UTC94
        config.secret.mix_seed(seed),
3f9091557ec3atagen2026-10-11 11:43 UTC95
        &config.oracle,
3f9091557ec3atagen2026-10-11 11:43 UTC96
    );
782069860236atagen2026-10-10 12:44 UTC97
    run.seeded = seeded;
782069860236atagen2026-10-10 12:44 UTC98
    run.last_seen = ctx.now;
782069860236atagen2026-10-10 12:44 UTC99
    let response = (
782069860236atagen2026-10-10 12:44 UTC100
        StatusCode::CREATED,
782069860236atagen2026-10-10 12:44 UTC101
        [
782069860236atagen2026-10-10 12:44 UTC102
            (header::LOCATION, run.path("")),
782069860236atagen2026-10-10 12:44 UTC103
            (header::ETAG, run.etag()),
782069860236atagen2026-10-10 12:44 UTC104
            (X_SEED, seed.to_string()),
782069860236atagen2026-10-10 12:44 UTC105
        ],
782069860236atagen2026-10-10 12:44 UTC106
        Json(run.view()),
782069860236atagen2026-10-10 12:44 UTC107
    )
782069860236atagen2026-10-10 12:44 UTC108
        .into_response();
782069860236atagen2026-10-10 12:44 UTC109
    let response = run::finish(&mut run, ctx, &config.secret, response);
159f0998263batagen2026-10-12 11:01 UTC110
    state.runs.insert(run);
782069860236atagen2026-10-10 12:44 UTC111
    response
782069860236atagen2026-10-10 12:44 UTC112
}
782069860236atagen2026-10-10 12:44 UTC113
782069860236atagen2026-10-10 12:44 UTC114
fn thread(_: &mut Shared, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC115
    match ctx.param("year").parse::<u32>() {
782069860236atagen2026-10-10 12:44 UTC116
        Ok(2009) => (
782069860236atagen2026-10-10 12:44 UTC117
            [(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)],
782069860236atagen2026-10-10 12:44 UTC118
            Json(forum_thread()),
782069860236atagen2026-10-10 12:44 UTC119
        )
782069860236atagen2026-10-10 12:44 UTC120
            .into_response(),
782069860236atagen2026-10-10 12:44 UTC121
        Ok(year) if year > 2009 => Problem::new(
782069860236atagen2026-10-10 12:44 UTC122
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC123
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC124
            "Nobody has posted since 2009.",
782069860236atagen2026-10-10 12:44 UTC125
        )
782069860236atagen2026-10-10 12:44 UTC126
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC127
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC128
        _ => Problem::new(
782069860236atagen2026-10-10 12:44 UTC129
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC130
            "no-thread",
782069860236atagen2026-10-10 12:44 UTC131
            "The forum did not exist yet. The problem did.",
782069860236atagen2026-10-10 12:44 UTC132
        )
782069860236atagen2026-10-10 12:44 UTC133
        .with("thread", FORUM_THREAD)
782069860236atagen2026-10-10 12:44 UTC134
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC135
    }
782069860236atagen2026-10-10 12:44 UTC136
}
782069860236atagen2026-10-10 12:44 UTC137
782069860236atagen2026-10-10 12:44 UTC138
fn graveyard(state: &mut Shared, _: &Ctx) -> Response {
159f0998263batagen2026-10-12 11:01 UTC139
    let dead: Vec<Value> = state
159f0998263batagen2026-10-12 11:01 UTC140
        .records
159f0998263batagen2026-10-12 11:01 UTC141
        .dead()
159f0998263batagen2026-10-12 11:01 UTC142
        .into_iter()
159f0998263batagen2026-10-12 11:01 UTC143
        .filter_map(|record| {
159f0998263batagen2026-10-12 11:01 UTC144
            let ending = record.ending?;
782069860236atagen2026-10-10 12:44 UTC145
            Some(json!({
159f0998263batagen2026-10-12 11:01 UTC146
                "public_id": record.public_id,
159f0998263batagen2026-10-12 11:01 UTC147
                "cause": format!("{PROBLEM_BASE}{}", ending.cause),
159f0998263batagen2026-10-12 11:01 UTC148
                "detail": ending.detail,
159f0998263batagen2026-10-12 11:01 UTC149
                "turn": record.turn,
782069860236atagen2026-10-10 12:44 UTC150
            }))
782069860236atagen2026-10-10 12:44 UTC151
        })
782069860236atagen2026-10-10 12:44 UTC152
        .collect();
782069860236atagen2026-10-10 12:44 UTC153
    Json(json!({ "runs": dead })).into_response()
782069860236atagen2026-10-10 12:44 UTC154
}
782069860236atagen2026-10-10 12:44 UTC155
058650ec79d6atagen2026-10-12 10:49 UTC156
#[derive(Clone, Copy, PartialEq, Eq)]
058650ec79d6atagen2026-10-12 10:49 UTC157
enum Sort {
058650ec79d6atagen2026-10-12 10:49 UTC158
    SelfReported,
058650ec79d6atagen2026-10-12 10:49 UTC159
    ContentLength,
058650ec79d6atagen2026-10-12 10:49 UTC160
    /// Self-reported length against real girth, the only metric the forum respects.
058650ec79d6atagen2026-10-12 10:49 UTC161
    Volume,
058650ec79d6atagen2026-10-12 10:49 UTC162
}
058650ec79d6atagen2026-10-12 10:49 UTC163
782069860236atagen2026-10-10 12:44 UTC164
fn leaderboard(state: &mut Shared, ctx: &Ctx) -> Response {
058650ec79d6atagen2026-10-12 10:49 UTC165
    let sort = match ctx.query("sort") {
058650ec79d6atagen2026-10-12 10:49 UTC166
        None | Some("self-reported-length") => Sort::SelfReported,
058650ec79d6atagen2026-10-12 10:49 UTC167
        Some("volume") => Sort::Volume,
782069860236atagen2026-10-10 12:44 UTC168
        Some("content-length") => match ctx.cookie("ruler") {
058650ec79d6atagen2026-10-12 10:49 UTC169
            Some(ruler) if state.config.secret.verify("ruler", ruler) == Some("1") => {
058650ec79d6atagen2026-10-12 10:49 UTC170
                Sort::ContentLength
058650ec79d6atagen2026-10-12 10:49 UTC171
            }
782069860236atagen2026-10-10 12:44 UTC172
            Some(_) => {
782069860236atagen2026-10-10 12:44 UTC173
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC174
                    StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC175
                    "exaggeration",
782069860236atagen2026-10-10 12:44 UTC176
                    "The server has measured. Lying is not a technique.",
782069860236atagen2026-10-10 12:44 UTC177
                )
782069860236atagen2026-10-10 12:44 UTC178
                .with("cookie", "ruler")
782069860236atagen2026-10-10 12:44 UTC179
                .into_response();
782069860236atagen2026-10-10 12:44 UTC180
            }
782069860236atagen2026-10-10 12:44 UTC181
            None => {
782069860236atagen2026-10-10 12:44 UTC182
                return Problem::new(
782069860236atagen2026-10-10 12:44 UTC183
                    StatusCode::FORBIDDEN,
782069860236atagen2026-10-10 12:44 UTC184
                    "ruler-required",
782069860236atagen2026-10-10 12:44 UTC185
                    "Sorting by the truth requires a Ruler.",
782069860236atagen2026-10-10 12:44 UTC186
                )
782069860236atagen2026-10-10 12:44 UTC187
                .into_response();
782069860236atagen2026-10-10 12:44 UTC188
            }
782069860236atagen2026-10-10 12:44 UTC189
        },
782069860236atagen2026-10-10 12:44 UTC190
        Some(_) => {
782069860236atagen2026-10-10 12:44 UTC191
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC192
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC193
                "unknown-sort",
058650ec79d6atagen2026-10-12 10:49 UTC194
                "The leaderboard sorts by self-reported-length, volume or content-length.",
782069860236atagen2026-10-10 12:44 UTC195
            )
782069860236atagen2026-10-10 12:44 UTC196
            .into_response();
782069860236atagen2026-10-10 12:44 UTC197
        }
782069860236atagen2026-10-10 12:44 UTC198
    };
782069860236atagen2026-10-10 12:44 UTC199
    let include_seeded = ctx.query("seeded") == Some("include");
159f0998263batagen2026-10-12 11:01 UTC200
    let mut entries: Vec<Record> = state.runs.each(Record::of);
159f0998263batagen2026-10-12 11:01 UTC201
    entries.extend(
159f0998263batagen2026-10-12 11:01 UTC202
        state
159f0998263batagen2026-10-12 11:01 UTC203
            .records
159f0998263batagen2026-10-12 11:01 UTC204
            .all()
159f0998263batagen2026-10-12 11:01 UTC205
            .into_iter()
159f0998263batagen2026-10-12 11:01 UTC206
            .filter(|record| !state.runs.contains(&record.id)),
159f0998263batagen2026-10-12 11:01 UTC207
    );
159f0998263batagen2026-10-12 11:01 UTC208
    entries.retain(|record| include_seeded || !record.seeded);
159f0998263batagen2026-10-12 11:01 UTC209
    let key = |record: &Record| match sort {
159f0998263batagen2026-10-12 11:01 UTC210
        Sort::SelfReported => record.self_reported,
159f0998263batagen2026-10-12 11:01 UTC211
        Sort::ContentLength => record.length,
159f0998263batagen2026-10-12 11:01 UTC212
        Sort::Volume => volume(record.self_reported, record.girth),
782069860236atagen2026-10-10 12:44 UTC213
    };
782069860236atagen2026-10-10 12:44 UTC214
    entries.sort_by(|a, b| key(b).total_cmp(&key(a)));
782069860236atagen2026-10-10 12:44 UTC215
    let entries: Vec<Value> = entries
782069860236atagen2026-10-10 12:44 UTC216
        .into_iter()
159f0998263batagen2026-10-12 11:01 UTC217
        .map(|record| {
782069860236atagen2026-10-10 12:44 UTC218
            let mut entry = json!({
159f0998263batagen2026-10-12 11:01 UTC219
                "public_id": record.public_id,
159f0998263batagen2026-10-12 11:01 UTC220
                "status": record.status,
159f0998263batagen2026-10-12 11:01 UTC221
                "floor": record.floor,
159f0998263batagen2026-10-12 11:01 UTC222
                "self_reported_length": (record.self_reported * 100.0).round() / 100.0,
159f0998263batagen2026-10-12 11:01 UTC223
                "ranked": !record.seeded,
159f0998263batagen2026-10-12 11:01 UTC224
                "viral": record.viral,
782069860236atagen2026-10-10 12:44 UTC225
            });
058650ec79d6atagen2026-10-12 10:49 UTC226
            if sort == Sort::Volume {
159f0998263batagen2026-10-12 11:01 UTC227
                entry["volume"] =
159f0998263batagen2026-10-12 11:01 UTC228
                    json!((volume(record.self_reported, record.girth) * 100.0).round() / 100.0);
058650ec79d6atagen2026-10-12 10:49 UTC229
            }
058650ec79d6atagen2026-10-12 10:49 UTC230
            if sort == Sort::ContentLength {
159f0998263batagen2026-10-12 11:01 UTC231
                entry["content_length"] = json!((record.length * MM_PER_INCH).round());
782069860236atagen2026-10-10 12:44 UTC232
            }
782069860236atagen2026-10-10 12:44 UTC233
            entry
782069860236atagen2026-10-10 12:44 UTC234
        })
782069860236atagen2026-10-10 12:44 UTC235
        .collect();
782069860236atagen2026-10-10 12:44 UTC236
    Json(json!({ "entries": entries })).into_response()
782069860236atagen2026-10-10 12:44 UTC237
}
782069860236atagen2026-10-10 12:44 UTC238
782069860236atagen2026-10-10 12:44 UTC239
pub async fn not_found() -> Problem {
782069860236atagen2026-10-10 12:44 UTC240
    Problem::new(
782069860236atagen2026-10-10 12:44 UTC241
        StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC242
        "not-found",
782069860236atagen2026-10-10 12:44 UTC243
        "The resource does not exist. This is not about you.",
782069860236atagen2026-10-10 12:44 UTC244
    )
782069860236atagen2026-10-10 12:44 UTC245
}