atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/http/runs.rs

782069860236atagen2026-10-10 12:44 UTC1
//! The resources under `/runs/{id}`.
782069860236atagen2026-10-10 12:44 UTC2
782069860236atagen2026-10-10 12:44 UTC3
use std::{convert::Infallible, fmt::Write as _};
782069860236atagen2026-10-10 12:44 UTC4
782069860236atagen2026-10-10 12:44 UTC5
use axum::{
782069860236atagen2026-10-10 12:44 UTC6
    Json,
782069860236atagen2026-10-10 12:44 UTC7
    body::{Body, Bytes},
782069860236atagen2026-10-10 12:44 UTC8
    http::{StatusCode, header},
782069860236atagen2026-10-10 12:44 UTC9
    response::{IntoResponse, Response},
782069860236atagen2026-10-10 12:44 UTC10
};
782069860236atagen2026-10-10 12:44 UTC11
use serde_json::{Value, json};
782069860236atagen2026-10-10 12:44 UTC12
782069860236atagen2026-10-10 12:44 UTC13
use super::{
782069860236atagen2026-10-10 12:44 UTC14
    ctx::Ctx,
782069860236atagen2026-10-10 12:44 UTC15
    resource::{Resource, Verb},
782069860236atagen2026-10-10 12:44 UTC16
    response::respond,
782069860236atagen2026-10-10 12:44 UTC17
    run::{Greeting, RunResource, Scope},
782069860236atagen2026-10-10 12:44 UTC18
};
782069860236atagen2026-10-10 12:44 UTC19
use crate::{
782069860236atagen2026-10-10 12:44 UTC20
    game::{
782069860236atagen2026-10-10 12:44 UTC21
        Cause, Floor, Grip, Item, LUBRICANTS, Lube, PatchOp, Run, Technique,
782069860236atagen2026-10-10 12:44 UTC22
        balance::{EQ_BAND, MM_PER_INCH},
782069860236atagen2026-10-10 12:44 UTC23
        shaft,
782069860236atagen2026-10-10 12:44 UTC24
    },
782069860236atagen2026-10-10 12:44 UTC25
    headers::ACCEPT_PATCH,
782069860236atagen2026-10-10 12:44 UTC26
    problem::Problem,
782069860236atagen2026-10-10 12:44 UTC27
};
782069860236atagen2026-10-10 12:44 UTC28
782069860236atagen2026-10-10 12:44 UTC29
const fn home(verbs: &'static [Verb<Run>]) -> RunResource {
782069860236atagen2026-10-10 12:44 UTC30
    RunResource {
782069860236atagen2026-10-10 12:44 UTC31
        scope: Scope::Home,
782069860236atagen2026-10-10 12:44 UTC32
        resource: Resource::new(verbs),
782069860236atagen2026-10-10 12:44 UTC33
    }
782069860236atagen2026-10-10 12:44 UTC34
}
782069860236atagen2026-10-10 12:44 UTC35
782069860236atagen2026-10-10 12:44 UTC36
const fn pockets(verbs: &'static [Verb<Run>]) -> RunResource {
782069860236atagen2026-10-10 12:44 UTC37
    RunResource {
782069860236atagen2026-10-10 12:44 UTC38
        scope: Scope::Pockets,
782069860236atagen2026-10-10 12:44 UTC39
        resource: Resource::new(verbs),
782069860236atagen2026-10-10 12:44 UTC40
    }
782069860236atagen2026-10-10 12:44 UTC41
}
782069860236atagen2026-10-10 12:44 UTC42
782069860236atagen2026-10-10 12:44 UTC43
fn not_in_clinic(run: &Run) -> bool {
782069860236atagen2026-10-10 12:44 UTC44
    run.floor != Floor::Clinic
782069860236atagen2026-10-10 12:44 UTC45
}
782069860236atagen2026-10-10 12:44 UTC46
782069860236atagen2026-10-10 12:44 UTC47
fn never(_: &Run) -> bool {
782069860236atagen2026-10-10 12:44 UTC48
    false
782069860236atagen2026-10-10 12:44 UTC49
}
782069860236atagen2026-10-10 12:44 UTC50
782069860236atagen2026-10-10 12:44 UTC51
fn unreachable_handler(_: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC52
    StatusCode::METHOD_NOT_ALLOWED.into_response()
782069860236atagen2026-10-10 12:44 UTC53
}
782069860236atagen2026-10-10 12:44 UTC54
782069860236atagen2026-10-10 12:44 UTC55
pub static RUN: RunResource = RunResource {
782069860236atagen2026-10-10 12:44 UTC56
    scope: Scope::Home,
782069860236atagen2026-10-10 12:44 UTC57
    resource: Resource {
782069860236atagen2026-10-10 12:44 UTC58
        verbs: &[
782069860236atagen2026-10-10 12:44 UTC59
            Verb::new("GET", get_run),
782069860236atagen2026-10-10 12:44 UTC60
            Verb::new("JELQ", jelq)
782069860236atagen2026-10-10 12:44 UTC61
                .only_when(not_in_clinic, "JELQ is not performed in the clinic."),
782069860236atagen2026-10-10 12:44 UTC62
            Verb::new("PUT", put_run),
782069860236atagen2026-10-10 12:44 UTC63
            Verb::new("PATCH", patch_run),
782069860236atagen2026-10-10 12:44 UTC64
            Verb::new("DELETE", delete_run),
782069860236atagen2026-10-10 12:44 UTC65
            Verb::new("TRACE", trace_run),
782069860236atagen2026-10-10 12:44 UTC66
        ],
782069860236atagen2026-10-10 12:44 UTC67
        accept_patch: true,
782069860236atagen2026-10-10 12:44 UTC68
    },
782069860236atagen2026-10-10 12:44 UTC69
};
782069860236atagen2026-10-10 12:44 UTC70
pub static LENGTH: RunResource = home(&[Verb::new("GET", get_length)]);
782069860236atagen2026-10-10 12:44 UTC71
pub static FORESKIN: RunResource = home(&[
782069860236atagen2026-10-10 12:44 UTC72
    Verb::new("GET", get_foreskin),
782069860236atagen2026-10-10 12:44 UTC73
    Verb::new("DELETE", delete_foreskin),
782069860236atagen2026-10-10 12:44 UTC74
]);
782069860236atagen2026-10-10 12:44 UTC75
pub static INVENTORY: RunResource = pockets(&[Verb::new("GET", get_inventory)]);
782069860236atagen2026-10-10 12:44 UTC76
pub static ITEM: RunResource =
782069860236atagen2026-10-10 12:44 UTC77
    pockets(&[Verb::new("GET", get_item), Verb::new("DELETE", delete_item)]);
782069860236atagen2026-10-10 12:44 UTC78
pub static TECHNIQUES: RunResource = home(&[Verb::new("GET", get_techniques)]);
782069860236atagen2026-10-10 12:44 UTC79
pub static DRAWER: RunResource = home(&[Verb::new("POST", search_drawer)]);
782069860236atagen2026-10-10 12:44 UTC80
pub static BRANCH: RunResource = home(&[Verb::new("POST", choose_branch)]);
782069860236atagen2026-10-10 12:44 UTC81
pub static FLOOR: RunResource =
782069860236atagen2026-10-10 12:44 UTC82
    pockets(&[Verb::new("GET", get_floor), Verb::new("POST", leave_floor)]);
782069860236atagen2026-10-10 12:44 UTC83
pub static SHOP: RunResource = home(&[Verb::new("GET", get_shop)]);
782069860236atagen2026-10-10 12:44 UTC84
pub static BUY: RunResource = home(&[Verb::new("POST", buy)]);
782069860236atagen2026-10-10 12:44 UTC85
pub static PORTAL: RunResource = pockets(&[
782069860236atagen2026-10-10 12:44 UTC86
    Verb::new("GET", get_portal),
782069860236atagen2026-10-10 12:44 UTC87
    Verb::new("POST", accept_portal),
782069860236atagen2026-10-10 12:44 UTC88
]);
782069860236atagen2026-10-10 12:44 UTC89
pub static ELDER: RunResource = home(&[Verb::new("GET", get_elder)]);
782069860236atagen2026-10-10 12:44 UTC90
pub static GRIP: RunResource = home(&[Verb::new("GET", get_grip)]);
782069860236atagen2026-10-10 12:44 UTC91
pub static DIAGNOSIS: RunResource = pockets(&[Verb::new("GET", get_diagnosis)]);
782069860236atagen2026-10-10 12:44 UTC92
pub static FOOTAGE: RunResource = pockets(&[
782069860236atagen2026-10-10 12:44 UTC93
    Verb::new("GET", get_footage),
782069860236atagen2026-10-10 12:44 UTC94
    Verb::new("DELETE", unreachable_handler).only_when(
782069860236atagen2026-10-10 12:44 UTC95
        never,
782069860236atagen2026-10-10 12:44 UTC96
        "The footage has been cached by intermediaries. It cannot be deleted.",
782069860236atagen2026-10-10 12:44 UTC97
    ),
782069860236atagen2026-10-10 12:44 UTC98
]);
782069860236atagen2026-10-10 12:44 UTC99
782069860236atagen2026-10-10 12:44 UTC100
fn get_run(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC101
    if ctx
782069860236atagen2026-10-10 12:44 UTC102
        .header(header::ACCEPT)
782069860236atagen2026-10-10 12:44 UTC103
        .is_some_and(|accept| accept.contains("reassurance"))
782069860236atagen2026-10-10 12:44 UTC104
    {
782069860236atagen2026-10-10 12:44 UTC105
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC106
            StatusCode::NOT_ACCEPTABLE,
782069860236atagen2026-10-10 12:44 UTC107
            "no-reassurance",
782069860236atagen2026-10-10 12:44 UTC108
            "The server does not produce reassurance.",
782069860236atagen2026-10-10 12:44 UTC109
        )
782069860236atagen2026-10-10 12:44 UTC110
        .into_response();
782069860236atagen2026-10-10 12:44 UTC111
    }
782069860236atagen2026-10-10 12:44 UTC112
    (
782069860236atagen2026-10-10 12:44 UTC113
        StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC114
        [(header::ETAG, run.etag())],
782069860236atagen2026-10-10 12:44 UTC115
        Json(run.view()),
782069860236atagen2026-10-10 12:44 UTC116
    )
782069860236atagen2026-10-10 12:44 UTC117
        .into_response()
782069860236atagen2026-10-10 12:44 UTC118
}
782069860236atagen2026-10-10 12:44 UTC119
782069860236atagen2026-10-10 12:44 UTC120
fn parse_techniques(run: &Run, ctx: &Ctx) -> Result<Vec<Technique>, Problem> {
782069860236atagen2026-10-10 12:44 UTC121
    let required = || {
782069860236atagen2026-10-10 12:44 UTC122
        Problem::new(
782069860236atagen2026-10-10 12:44 UTC123
            StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC124
            "technique-required",
782069860236atagen2026-10-10 12:44 UTC125
            "No technique was specified.",
782069860236atagen2026-10-10 12:44 UTC126
        )
782069860236atagen2026-10-10 12:44 UTC127
        .with("techniques", run.path("/techniques"))
782069860236atagen2026-10-10 12:44 UTC128
    };
782069860236atagen2026-10-10 12:44 UTC129
    let names = ctx
782069860236atagen2026-10-10 12:44 UTC130
        .query("technique")
782069860236atagen2026-10-10 12:44 UTC131
        .filter(|n| !n.trim().is_empty())
782069860236atagen2026-10-10 12:44 UTC132
        .ok_or_else(required)?;
782069860236atagen2026-10-10 12:44 UTC133
    names
782069860236atagen2026-10-10 12:44 UTC134
        .split(',')
782069860236atagen2026-10-10 12:44 UTC135
        .map(str::trim)
782069860236atagen2026-10-10 12:44 UTC136
        .filter(|n| !n.is_empty())
782069860236atagen2026-10-10 12:44 UTC137
        .map(|name| {
782069860236atagen2026-10-10 12:44 UTC138
            Technique::parse(name).ok_or_else(|| {
782069860236atagen2026-10-10 12:44 UTC139
                Problem::new(
782069860236atagen2026-10-10 12:44 UTC140
                    StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC141
                    "unknown-technique",
782069860236atagen2026-10-10 12:44 UTC142
                    format!("{name} is not a technique."),
782069860236atagen2026-10-10 12:44 UTC143
                )
782069860236atagen2026-10-10 12:44 UTC144
                .with("techniques", run.path("/techniques"))
782069860236atagen2026-10-10 12:44 UTC145
            })
782069860236atagen2026-10-10 12:44 UTC146
        })
782069860236atagen2026-10-10 12:44 UTC147
        .collect()
782069860236atagen2026-10-10 12:44 UTC148
}
782069860236atagen2026-10-10 12:44 UTC149
782069860236atagen2026-10-10 12:44 UTC150
fn parse_intensity(ctx: &Ctx) -> Result<i32, Problem> {
782069860236atagen2026-10-10 12:44 UTC151
    match ctx.query("intensity").map(str::parse::<i32>) {
782069860236atagen2026-10-10 12:44 UTC152
        None => Ok(5),
782069860236atagen2026-10-10 12:44 UTC153
        Some(Ok(i)) if (1..=10).contains(&i) => Ok(i),
782069860236atagen2026-10-10 12:44 UTC154
        Some(_) => Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC155
            StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC156
            "intensity-out-of-range",
782069860236atagen2026-10-10 12:44 UTC157
            "Intensity is measured from 1 to 10.",
782069860236atagen2026-10-10 12:44 UTC158
        )),
782069860236atagen2026-10-10 12:44 UTC159
    }
782069860236atagen2026-10-10 12:44 UTC160
}
782069860236atagen2026-10-10 12:44 UTC161
782069860236atagen2026-10-10 12:44 UTC162
fn jelq(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC163
    let parsed = parse_techniques(run, ctx).and_then(|techniques| {
782069860236atagen2026-10-10 12:44 UTC164
        let intensity = parse_intensity(ctx)?;
782069860236atagen2026-10-10 12:44 UTC165
        let lube = Lube::from_content_type(ctx.header(header::CONTENT_TYPE))?;
782069860236atagen2026-10-10 12:44 UTC166
        Ok((techniques, intensity, lube))
782069860236atagen2026-10-10 12:44 UTC167
    });
782069860236atagen2026-10-10 12:44 UTC168
    match parsed {
782069860236atagen2026-10-10 12:44 UTC169
        Ok((techniques, intensity, lube)) => {
782069860236atagen2026-10-10 12:44 UTC170
            respond(run.jelq(&techniques, intensity, lube, ctx.now))
782069860236atagen2026-10-10 12:44 UTC171
        }
782069860236atagen2026-10-10 12:44 UTC172
        Err(problem) => problem.into_response(),
782069860236atagen2026-10-10 12:44 UTC173
    }
782069860236atagen2026-10-10 12:44 UTC174
}
782069860236atagen2026-10-10 12:44 UTC175
782069860236atagen2026-10-10 12:44 UTC176
fn put_run(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC177
    run.put().into_response()
782069860236atagen2026-10-10 12:44 UTC178
}
782069860236atagen2026-10-10 12:44 UTC179
782069860236atagen2026-10-10 12:44 UTC180
fn patch_run(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC181
    let is_patch = ctx
782069860236atagen2026-10-10 12:44 UTC182
        .header(header::CONTENT_TYPE)
782069860236atagen2026-10-10 12:44 UTC183
        .is_some_and(|ct| ct.starts_with("application/json-patch+json"));
782069860236atagen2026-10-10 12:44 UTC184
    if !is_patch {
782069860236atagen2026-10-10 12:44 UTC185
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC186
            StatusCode::UNSUPPORTED_MEDIA_TYPE,
782069860236atagen2026-10-10 12:44 UTC187
            "patch-format",
782069860236atagen2026-10-10 12:44 UTC188
            "Patches are accepted as application/json-patch+json.",
782069860236atagen2026-10-10 12:44 UTC189
        )
782069860236atagen2026-10-10 12:44 UTC190
        .header(ACCEPT_PATCH, "application/json-patch+json")
782069860236atagen2026-10-10 12:44 UTC191
        .into_response();
782069860236atagen2026-10-10 12:44 UTC192
    }
782069860236atagen2026-10-10 12:44 UTC193
    match serde_json::from_slice::<Vec<PatchOp>>(&ctx.body) {
782069860236atagen2026-10-10 12:44 UTC194
        Ok(ops) => respond(run.patch(&ops)),
782069860236atagen2026-10-10 12:44 UTC195
        Err(_) => Problem::new(
782069860236atagen2026-10-10 12:44 UTC196
            StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC197
            "malformed-patch",
782069860236atagen2026-10-10 12:44 UTC198
            "The patch could not be parsed.",
782069860236atagen2026-10-10 12:44 UTC199
        )
782069860236atagen2026-10-10 12:44 UTC200
        .into_response(),
782069860236atagen2026-10-10 12:44 UTC201
    }
782069860236atagen2026-10-10 12:44 UTC202
}
782069860236atagen2026-10-10 12:44 UTC203
782069860236atagen2026-10-10 12:44 UTC204
fn delete_run(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC205
    run.end(Cause::Abandoned);
782069860236atagen2026-10-10 12:44 UTC206
    StatusCode::NO_CONTENT.into_response()
782069860236atagen2026-10-10 12:44 UTC207
}
782069860236atagen2026-10-10 12:44 UTC208
782069860236atagen2026-10-10 12:44 UTC209
fn trace_run(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC210
    run.shame += 1;
782069860236atagen2026-10-10 12:44 UTC211
    let mut message = format!("{} {} {:?}\r\n", ctx.method, ctx.uri, ctx.version);
782069860236atagen2026-10-10 12:44 UTC212
    for (name, value) in &ctx.headers {
782069860236atagen2026-10-10 12:44 UTC213
        let _ = write!(
782069860236atagen2026-10-10 12:44 UTC214
            message,
782069860236atagen2026-10-10 12:44 UTC215
            "{name}: {}\r\n",
782069860236atagen2026-10-10 12:44 UTC216
            value.to_str().unwrap_or("<binary>")
782069860236atagen2026-10-10 12:44 UTC217
        );
782069860236atagen2026-10-10 12:44 UTC218
    }
782069860236atagen2026-10-10 12:44 UTC219
    message.push_str("\r\n");
782069860236atagen2026-10-10 12:44 UTC220
    (
782069860236atagen2026-10-10 12:44 UTC221
        StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC222
        [(header::CONTENT_TYPE, "message/http")],
782069860236atagen2026-10-10 12:44 UTC223
        message,
782069860236atagen2026-10-10 12:44 UTC224
    )
782069860236atagen2026-10-10 12:44 UTC225
        .into_response()
782069860236atagen2026-10-10 12:44 UTC226
}
782069860236atagen2026-10-10 12:44 UTC227
782069860236atagen2026-10-10 12:44 UTC228
fn millimetres(inches: f64) -> usize {
782069860236atagen2026-10-10 12:44 UTC229
    (inches * MM_PER_INCH).round() as usize
782069860236atagen2026-10-10 12:44 UTC230
}
782069860236atagen2026-10-10 12:44 UTC231
782069860236atagen2026-10-10 12:44 UTC232
fn parse_inch_range(range: &str, total: f64) -> Option<(f64, f64)> {
782069860236atagen2026-10-10 12:44 UTC233
    let (start, end) = range.strip_prefix("inches=")?.split_once('-')?;
782069860236atagen2026-10-10 12:44 UTC234
    let start: f64 = start.trim().parse().ok()?;
782069860236atagen2026-10-10 12:44 UTC235
    let end: f64 = match end.trim() {
782069860236atagen2026-10-10 12:44 UTC236
        "" => total,
782069860236atagen2026-10-10 12:44 UTC237
        end => end.parse().ok()?,
782069860236atagen2026-10-10 12:44 UTC238
    };
782069860236atagen2026-10-10 12:44 UTC239
    (start >= 0.0 && start < total && start <= end).then_some((start, end.min(total)))
782069860236atagen2026-10-10 12:44 UTC240
}
782069860236atagen2026-10-10 12:44 UTC241
782069860236atagen2026-10-10 12:44 UTC242
fn get_length(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC243
    let inches = if run.items.ruler {
782069860236atagen2026-10-10 12:44 UTC244
        run.current_length()
782069860236atagen2026-10-10 12:44 UTC245
    } else {
782069860236atagen2026-10-10 12:44 UTC246
        run.self_reported()
782069860236atagen2026-10-10 12:44 UTC247
    };
782069860236atagen2026-10-10 12:44 UTC248
    let body = shaft(millimetres(inches));
782069860236atagen2026-10-10 12:44 UTC249
    let content_type = (
782069860236atagen2026-10-10 12:44 UTC250
        header::CONTENT_TYPE,
782069860236atagen2026-10-10 12:44 UTC251
        "text/plain; charset=us-ascii".to_owned(),
782069860236atagen2026-10-10 12:44 UTC252
    );
782069860236atagen2026-10-10 12:44 UTC253
    let accept_ranges = (header::ACCEPT_RANGES, "inches".to_owned());
782069860236atagen2026-10-10 12:44 UTC254
782069860236atagen2026-10-10 12:44 UTC255
    if let Some(range) = ctx.header(header::RANGE)
782069860236atagen2026-10-10 12:44 UTC256
        && range.starts_with("inches=")
782069860236atagen2026-10-10 12:44 UTC257
    {
782069860236atagen2026-10-10 12:44 UTC258
        let Some((start, end)) = parse_inch_range(range, inches) else {
782069860236atagen2026-10-10 12:44 UTC259
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC260
                StatusCode::RANGE_NOT_SATISFIABLE,
782069860236atagen2026-10-10 12:44 UTC261
                "range-not-satisfiable",
782069860236atagen2026-10-10 12:44 UTC262
                format!("The requested range is not available. {inches:.2} inches are."),
782069860236atagen2026-10-10 12:44 UTC263
            )
782069860236atagen2026-10-10 12:44 UTC264
            .header(header::CONTENT_RANGE, format!("inches */{inches:.2}"))
782069860236atagen2026-10-10 12:44 UTC265
            .into_response();
782069860236atagen2026-10-10 12:44 UTC266
        };
782069860236atagen2026-10-10 12:44 UTC267
        let from = millimetres(start).min(body.len() - 1);
782069860236atagen2026-10-10 12:44 UTC268
        let to = millimetres(end).clamp(from + 1, body.len());
782069860236atagen2026-10-10 12:44 UTC269
        let content_range = (
782069860236atagen2026-10-10 12:44 UTC270
            header::CONTENT_RANGE,
782069860236atagen2026-10-10 12:44 UTC271
            format!("inches {start:.2}-{end:.2}/{inches:.2}"),
782069860236atagen2026-10-10 12:44 UTC272
        );
782069860236atagen2026-10-10 12:44 UTC273
        return (
782069860236atagen2026-10-10 12:44 UTC274
            StatusCode::PARTIAL_CONTENT,
782069860236atagen2026-10-10 12:44 UTC275
            [content_type, accept_ranges, content_range],
782069860236atagen2026-10-10 12:44 UTC276
            body[from..to].to_owned(),
782069860236atagen2026-10-10 12:44 UTC277
        )
782069860236atagen2026-10-10 12:44 UTC278
            .into_response();
782069860236atagen2026-10-10 12:44 UTC279
    }
782069860236atagen2026-10-10 12:44 UTC280
782069860236atagen2026-10-10 12:44 UTC281
    if run.items.ruler {
782069860236atagen2026-10-10 12:44 UTC282
        return (StatusCode::OK, [content_type, accept_ranges], body).into_response();
782069860236atagen2026-10-10 12:44 UTC283
    }
782069860236atagen2026-10-10 12:44 UTC284
    let chunks: Vec<Result<Bytes, Infallible>> = body
782069860236atagen2026-10-10 12:44 UTC285
        .into_bytes()
782069860236atagen2026-10-10 12:44 UTC286
        .chunks(16)
782069860236atagen2026-10-10 12:44 UTC287
        .map(|c| Ok(Bytes::copy_from_slice(c)))
782069860236atagen2026-10-10 12:44 UTC288
        .collect();
782069860236atagen2026-10-10 12:44 UTC289
    let body = Body::from_stream(futures_util::stream::iter(chunks));
782069860236atagen2026-10-10 12:44 UTC290
    (StatusCode::OK, [content_type, accept_ranges], body).into_response()
782069860236atagen2026-10-10 12:44 UTC291
}
782069860236atagen2026-10-10 12:44 UTC292
782069860236atagen2026-10-10 12:44 UTC293
fn get_foreskin(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC294
    if run.foreskin.is_none() {
782069860236atagen2026-10-10 12:44 UTC295
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC296
            StatusCode::GONE,
782069860236atagen2026-10-10 12:44 UTC297
            "foreskin-gone",
782069860236atagen2026-10-10 12:44 UTC298
            "The foreskin is gone. It will not be returning.",
782069860236atagen2026-10-10 12:44 UTC299
        )
782069860236atagen2026-10-10 12:44 UTC300
        .into_response();
782069860236atagen2026-10-10 12:44 UTC301
    }
782069860236atagen2026-10-10 12:44 UTC302
    (StatusCode::NO_CONTENT, [(header::ETAG, run.etag())]).into_response()
782069860236atagen2026-10-10 12:44 UTC303
}
782069860236atagen2026-10-10 12:44 UTC304
782069860236atagen2026-10-10 12:44 UTC305
fn delete_foreskin(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC306
    respond(run.delete_foreskin(ctx.header(header::IF_MATCH)))
782069860236atagen2026-10-10 12:44 UTC307
}
782069860236atagen2026-10-10 12:44 UTC308
782069860236atagen2026-10-10 12:44 UTC309
fn get_inventory(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC310
    let items: Vec<Value> = run
782069860236atagen2026-10-10 12:44 UTC311
        .items
782069860236atagen2026-10-10 12:44 UTC312
        .owned()
782069860236atagen2026-10-10 12:44 UTC313
        .into_iter()
782069860236atagen2026-10-10 12:44 UTC314
        .map(|item| {
782069860236atagen2026-10-10 12:44 UTC315
            json!({
782069860236atagen2026-10-10 12:44 UTC316
                "name": item.slug(),
782069860236atagen2026-10-10 12:44 UTC317
                "href": run.path(&format!("/inventory/{}", item.slug())),
782069860236atagen2026-10-10 12:44 UTC318
            })
782069860236atagen2026-10-10 12:44 UTC319
        })
782069860236atagen2026-10-10 12:44 UTC320
        .collect();
782069860236atagen2026-10-10 12:44 UTC321
    Json(json!({
782069860236atagen2026-10-10 12:44 UTC322
        "items": items,
782069860236atagen2026-10-10 12:44 UTC323
        "ice_packs": run.items.ice_packs,
782069860236atagen2026-10-10 12:44 UTC324
        "supplements": run.items.supplements,
782069860236atagen2026-10-10 12:44 UTC325
        "excuses": run.excuses(),
782069860236atagen2026-10-10 12:44 UTC326
    }))
782069860236atagen2026-10-10 12:44 UTC327
    .into_response()
782069860236atagen2026-10-10 12:44 UTC328
}
782069860236atagen2026-10-10 12:44 UTC329
782069860236atagen2026-10-10 12:44 UTC330
fn owned_item(run: &Run, ctx: &Ctx) -> Result<Item, Problem> {
782069860236atagen2026-10-10 12:44 UTC331
    Item::parse(ctx.param("item"))
782069860236atagen2026-10-10 12:44 UTC332
        .filter(|&item| run.items.has(item))
782069860236atagen2026-10-10 12:44 UTC333
        .ok_or_else(|| {
782069860236atagen2026-10-10 12:44 UTC334
            Problem::new(
782069860236atagen2026-10-10 12:44 UTC335
                StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC336
                "no-such-item",
782069860236atagen2026-10-10 12:44 UTC337
                "You do not have that.",
782069860236atagen2026-10-10 12:44 UTC338
            )
782069860236atagen2026-10-10 12:44 UTC339
        })
782069860236atagen2026-10-10 12:44 UTC340
}
782069860236atagen2026-10-10 12:44 UTC341
782069860236atagen2026-10-10 12:44 UTC342
fn get_item(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC343
    match owned_item(run, ctx) {
782069860236atagen2026-10-10 12:44 UTC344
        Ok(item) => {
782069860236atagen2026-10-10 12:44 UTC345
            Json(json!({ "name": item.slug(), "description": item.description() })).into_response()
782069860236atagen2026-10-10 12:44 UTC346
        }
782069860236atagen2026-10-10 12:44 UTC347
        Err(problem) => problem.into_response(),
782069860236atagen2026-10-10 12:44 UTC348
    }
782069860236atagen2026-10-10 12:44 UTC349
}
782069860236atagen2026-10-10 12:44 UTC350
782069860236atagen2026-10-10 12:44 UTC351
fn delete_item(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC352
    match owned_item(run, ctx) {
782069860236atagen2026-10-10 12:44 UTC353
        Ok(item) => respond(run.drop_item(item)),
782069860236atagen2026-10-10 12:44 UTC354
        Err(problem) => problem.into_response(),
782069860236atagen2026-10-10 12:44 UTC355
    }
782069860236atagen2026-10-10 12:44 UTC356
}
782069860236atagen2026-10-10 12:44 UTC357
782069860236atagen2026-10-10 12:44 UTC358
fn get_techniques(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC359
    let listed: Vec<Value> = Technique::ALL
782069860236atagen2026-10-10 12:44 UTC360
        .into_iter()
782069860236atagen2026-10-10 12:44 UTC361
        .map(|t| t.describe(run.forum_read()))
782069860236atagen2026-10-10 12:44 UTC362
        .collect();
782069860236atagen2026-10-10 12:44 UTC363
    Json(json!({
782069860236atagen2026-10-10 12:44 UTC364
        "usage": format!("JELQ {}?technique=<name>[,<name>]&intensity=<1-10>", run.path("")),
782069860236atagen2026-10-10 12:44 UTC365
        "lubricant": "Declared with Content-Type.",
782069860236atagen2026-10-10 12:44 UTC366
        "lubricants": LUBRICANTS,
782069860236atagen2026-10-10 12:44 UTC367
        "eq_band": [EQ_BAND.start(), EQ_BAND.end()],
782069860236atagen2026-10-10 12:44 UTC368
        "techniques": listed,
782069860236atagen2026-10-10 12:44 UTC369
    }))
782069860236atagen2026-10-10 12:44 UTC370
    .into_response()
782069860236atagen2026-10-10 12:44 UTC371
}
782069860236atagen2026-10-10 12:44 UTC372
782069860236atagen2026-10-10 12:44 UTC373
fn search_drawer(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC374
    respond(run.search_drawer())
782069860236atagen2026-10-10 12:44 UTC375
}
782069860236atagen2026-10-10 12:44 UTC376
782069860236atagen2026-10-10 12:44 UTC377
fn choose_branch(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC378
    respond(run.choose(ctx.param("choice")))
782069860236atagen2026-10-10 12:44 UTC379
}
782069860236atagen2026-10-10 12:44 UTC380
782069860236atagen2026-10-10 12:44 UTC381
fn get_floor(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC382
    Json(run.floor_view()).into_response()
782069860236atagen2026-10-10 12:44 UTC383
}
782069860236atagen2026-10-10 12:44 UTC384
782069860236atagen2026-10-10 12:44 UTC385
fn leave_floor(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC386
    respond(run.advance())
782069860236atagen2026-10-10 12:44 UTC387
}
782069860236atagen2026-10-10 12:44 UTC388
782069860236atagen2026-10-10 12:44 UTC389
fn get_shop(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC390
    respond(run.shop_view().map(Json))
782069860236atagen2026-10-10 12:44 UTC391
}
782069860236atagen2026-10-10 12:44 UTC392
782069860236atagen2026-10-10 12:44 UTC393
fn buy(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC394
    respond(run.buy(ctx.param("item")))
782069860236atagen2026-10-10 12:44 UTC395
}
782069860236atagen2026-10-10 12:44 UTC396
782069860236atagen2026-10-10 12:44 UTC397
fn get_portal(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC398
    Json(json!({
782069860236atagen2026-10-10 12:44 UTC399
        "terms": "By connecting you agree that the hotel is not responsible for anything that happens in this room, and that it does not want to know.",
782069860236atagen2026-10-10 12:44 UTC400
        "accept": { "href": run.path("/portal"), "method": "POST" },
782069860236atagen2026-10-10 12:44 UTC401
    }))
782069860236atagen2026-10-10 12:44 UTC402
    .into_response()
782069860236atagen2026-10-10 12:44 UTC403
}
782069860236atagen2026-10-10 12:44 UTC404
782069860236atagen2026-10-10 12:44 UTC405
fn accept_portal(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC406
    respond(run.accept_terms())
782069860236atagen2026-10-10 12:44 UTC407
}
782069860236atagen2026-10-10 12:44 UTC408
782069860236atagen2026-10-10 12:44 UTC409
fn get_elder(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC410
    respond(run.elder(
782069860236atagen2026-10-10 12:44 UTC411
        ctx.header(header::PROXY_AUTHORIZATION),
782069860236atagen2026-10-10 12:44 UTC412
        ctx.header(header::IF_MODIFIED_SINCE),
782069860236atagen2026-10-10 12:44 UTC413
    ))
782069860236atagen2026-10-10 12:44 UTC414
}
782069860236atagen2026-10-10 12:44 UTC415
782069860236atagen2026-10-10 12:44 UTC416
fn requested_grip(ctx: &Ctx) -> Option<Grip> {
782069860236atagen2026-10-10 12:44 UTC417
    let connection = ctx.header(header::CONNECTION)?.to_ascii_lowercase();
782069860236atagen2026-10-10 12:44 UTC418
    if !connection.split(',').any(|token| token.trim() == "upgrade") {
782069860236atagen2026-10-10 12:44 UTC419
        return None;
782069860236atagen2026-10-10 12:44 UTC420
    }
782069860236atagen2026-10-10 12:44 UTC421
    ctx.header(header::UPGRADE)?
782069860236atagen2026-10-10 12:44 UTC422
        .split(',')
782069860236atagen2026-10-10 12:44 UTC423
        .find_map(|protocol| Grip::parse(protocol.trim()))
782069860236atagen2026-10-10 12:44 UTC424
}
782069860236atagen2026-10-10 12:44 UTC425
782069860236atagen2026-10-10 12:44 UTC426
fn get_grip(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC427
    if let Some(grip) = requested_grip(ctx) {
782069860236atagen2026-10-10 12:44 UTC428
        run.set_grip(grip);
782069860236atagen2026-10-10 12:44 UTC429
        let mut response = (
782069860236atagen2026-10-10 12:44 UTC430
            StatusCode::SWITCHING_PROTOCOLS,
782069860236atagen2026-10-10 12:44 UTC431
            [
782069860236atagen2026-10-10 12:44 UTC432
                (header::UPGRADE, grip.name()),
782069860236atagen2026-10-10 12:44 UTC433
                (header::CONNECTION, "upgrade"),
782069860236atagen2026-10-10 12:44 UTC434
            ],
782069860236atagen2026-10-10 12:44 UTC435
        )
782069860236atagen2026-10-10 12:44 UTC436
            .into_response();
782069860236atagen2026-10-10 12:44 UTC437
        response.extensions_mut().insert(Greeting(format!(
782069860236atagen2026-10-10 12:44 UTC438
            "You are now jelqing {}.\r\n",
782069860236atagen2026-10-10 12:44 UTC439
            grip.name()
782069860236atagen2026-10-10 12:44 UTC440
        )));
782069860236atagen2026-10-10 12:44 UTC441
        return response;
782069860236atagen2026-10-10 12:44 UTC442
    }
782069860236atagen2026-10-10 12:44 UTC443
    let detail = match ctx.header(header::UPGRADE) {
782069860236atagen2026-10-10 12:44 UTC444
        Some(protocol) => {
782069860236atagen2026-10-10 12:44 UTC445
            format!("The server does not know the {protocol} grip. Grips are protocols.")
782069860236atagen2026-10-10 12:44 UTC446
        }
782069860236atagen2026-10-10 12:44 UTC447
        None => "Grips are protocols. Change grip by upgrading the connection.".to_owned(),
782069860236atagen2026-10-10 12:44 UTC448
    };
782069860236atagen2026-10-10 12:44 UTC449
    (
782069860236atagen2026-10-10 12:44 UTC450
        StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC451
        [(header::UPGRADE, "overhand, underhand")],
782069860236atagen2026-10-10 12:44 UTC452
        Json(json!({ "grip": run.grip.name(), "detail": detail })),
782069860236atagen2026-10-10 12:44 UTC453
    )
782069860236atagen2026-10-10 12:44 UTC454
        .into_response()
782069860236atagen2026-10-10 12:44 UTC455
}
782069860236atagen2026-10-10 12:44 UTC456
782069860236atagen2026-10-10 12:44 UTC457
fn get_diagnosis(run: &mut Run, ctx: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC458
    respond(run.diagnose(
782069860236atagen2026-10-10 12:44 UTC459
        ctx.header(header::AUTHORIZATION),
782069860236atagen2026-10-10 12:44 UTC460
        ctx.header(header::IF_NONE_MATCH),
782069860236atagen2026-10-10 12:44 UTC461
    ))
782069860236atagen2026-10-10 12:44 UTC462
}
782069860236atagen2026-10-10 12:44 UTC463
782069860236atagen2026-10-10 12:44 UTC464
fn get_footage(run: &mut Run, _: &Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC465
    let Some((filmed, age)) = run.footage() else {
782069860236atagen2026-10-10 12:44 UTC466
        return Problem::new(
782069860236atagen2026-10-10 12:44 UTC467
            StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC468
            "no-footage",
782069860236atagen2026-10-10 12:44 UTC469
            "There is no footage. Yet.",
782069860236atagen2026-10-10 12:44 UTC470
        )
782069860236atagen2026-10-10 12:44 UTC471
        .into_response();
782069860236atagen2026-10-10 12:44 UTC472
    };
782069860236atagen2026-10-10 12:44 UTC473
    (
782069860236atagen2026-10-10 12:44 UTC474
        [
782069860236atagen2026-10-10 12:44 UTC475
            (
782069860236atagen2026-10-10 12:44 UTC476
                header::CACHE_CONTROL,
782069860236atagen2026-10-10 12:44 UTC477
                "public, max-age=31536000, immutable".to_owned(),
782069860236atagen2026-10-10 12:44 UTC478
            ),
782069860236atagen2026-10-10 12:44 UTC479
            (header::AGE, age.to_string()),
782069860236atagen2026-10-10 12:44 UTC480
            (header::LAST_MODIFIED, httpdate::fmt_http_date(filmed)),
782069860236atagen2026-10-10 12:44 UTC481
        ],
782069860236atagen2026-10-10 12:44 UTC482
        Json(json!({
782069860236atagen2026-10-10 12:44 UTC483
            "detail": "A recording. It is grainy. It is unmistakably you. It has been shared.",
782069860236atagen2026-10-10 12:44 UTC484
            "views": 1 + age * 37,
782069860236atagen2026-10-10 12:44 UTC485
        })),
782069860236atagen2026-10-10 12:44 UTC486
    )
782069860236atagen2026-10-10 12:44 UTC487
        .into_response()
782069860236atagen2026-10-10 12:44 UTC488
}