an http based roguelike rpg about jelqing
Blame: src/http/runs.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | //! The resources under `/runs/{id}`. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use std::{convert::Infallible, fmt::Write as _}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | Json, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | body::{Body, Bytes}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | http::{StatusCode, header}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | response::{IntoResponse, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | use serde_json::{Value, json}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | use super::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | ctx::Ctx, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | resource::{Resource, Verb}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | response::respond, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | run::{Greeting, RunResource, Scope}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | use crate::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | game::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | Cause, Floor, Grip, Item, LUBRICANTS, Lube, PatchOp, Run, Technique, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | balance::{EQ_BAND, MM_PER_INCH}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | shaft, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | headers::ACCEPT_PATCH, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | problem::Problem, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | const fn home(verbs: &'static [Verb<Run>]) -> RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | scope: Scope::Home, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | resource: Resource::new(verbs), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | const fn pockets(verbs: &'static [Verb<Run>]) -> RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | scope: Scope::Pockets, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | resource: Resource::new(verbs), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | fn not_in_clinic(run: &Run) -> bool { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | run.floor != Floor::Clinic |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | fn never(_: &Run) -> bool { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | false |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | fn unreachable_handler(_: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | StatusCode::METHOD_NOT_ALLOWED.into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | pub static RUN: RunResource = RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | scope: Scope::Home, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | resource: Resource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | verbs: &[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | Verb::new("GET", get_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | Verb::new("JELQ", jelq) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | .only_when(not_in_clinic, "JELQ is not performed in the clinic."), |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 62 | Verb::new("HOLD", hold_run).only_when( |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 63 | Run::can_hold, |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 64 | "HOLD is only available above 70 EQ or at 2 stamina or less.", |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 65 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | Verb::new("PUT", put_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | Verb::new("PATCH", patch_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | Verb::new("DELETE", delete_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | Verb::new("TRACE", trace_run), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | accept_patch: true, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | pub static LENGTH: RunResource = home(&[Verb::new("GET", get_length)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | pub static FORESKIN: RunResource = home(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | Verb::new("GET", get_foreskin), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | Verb::new("DELETE", delete_foreskin), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | pub static INVENTORY: RunResource = pockets(&[Verb::new("GET", get_inventory)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | pub static ITEM: RunResource = |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | pockets(&[Verb::new("GET", get_item), Verb::new("DELETE", delete_item)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | pub static TECHNIQUES: RunResource = home(&[Verb::new("GET", get_techniques)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | pub static DRAWER: RunResource = home(&[Verb::new("POST", search_drawer)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | pub static BRANCH: RunResource = home(&[Verb::new("POST", choose_branch)]); |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 85 | pub static KITCHEN: RunResource = home(&[Verb::new("POST", raid_kitchen)]); |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 86 | pub static MELON: RunResource = home(&[Verb::new("POST", commit_melon)]); |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 87 | pub static REREAD: RunResource = pockets(&[Verb::new("POST", re_read)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | pub static FLOOR: RunResource = |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 89 | home(&[Verb::new("GET", get_floor), Verb::new("POST", leave_floor)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | pub static SHOP: RunResource = home(&[Verb::new("GET", get_shop)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | pub static BUY: RunResource = home(&[Verb::new("POST", buy)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | pub static PORTAL: RunResource = pockets(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | Verb::new("GET", get_portal), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | Verb::new("POST", accept_portal), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | pub static ELDER: RunResource = home(&[Verb::new("GET", get_elder)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | pub static GRIP: RunResource = home(&[Verb::new("GET", get_grip)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | pub static DIAGNOSIS: RunResource = pockets(&[Verb::new("GET", get_diagnosis)]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | pub static FOOTAGE: RunResource = pockets(&[ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | Verb::new("GET", get_footage), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | Verb::new("DELETE", unreachable_handler).only_when( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | never, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | "The footage has been cached by intermediaries. It cannot be deleted.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | ]); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | fn get_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | if ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | .header(header::ACCEPT) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | .is_some_and(|accept| accept.contains("reassurance")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | StatusCode::NOT_ACCEPTABLE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | "no-reassurance", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | "The server does not produce reassurance.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 118 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 119 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 120 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | [(header::ETAG, run.etag())], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 122 | Json(run.view()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 123 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 124 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 125 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 126 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 127 | fn parse_techniques(run: &Run, ctx: &Ctx) -> Result<Vec<Technique>, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 128 | let required = || { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 129 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 130 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 131 | "technique-required", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 132 | "No technique was specified.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 133 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | .with("techniques", run.path("/techniques")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 135 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 136 | let names = ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 137 | .query("technique") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | .filter(|n| !n.trim().is_empty()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | .ok_or_else(required)?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | names |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | .split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | .map(str::trim) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 143 | .filter(|n| !n.is_empty()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | .map(|name| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | Technique::parse(name).ok_or_else(|| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 146 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 147 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 148 | "unknown-technique", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 149 | format!("{name} is not a technique."), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | .with("techniques", run.path("/techniques")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 152 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 153 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 154 | .collect() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 155 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 156 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 157 | fn parse_intensity(ctx: &Ctx) -> Result<i32, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | match ctx.query("intensity").map(str::parse::<i32>) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 159 | None => Ok(5), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | Some(Ok(i)) if (1..=10).contains(&i) => Ok(i), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | Some(_) => Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | "intensity-out-of-range", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | "Intensity is measured from 1 to 10.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 165 | )), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 168 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | fn jelq(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | let parsed = parse_techniques(run, ctx).and_then(|techniques| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 171 | let intensity = parse_intensity(ctx)?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 172 | let lube = Lube::from_content_type(ctx.header(header::CONTENT_TYPE))?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 173 | Ok((techniques, intensity, lube)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 174 | }); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 175 | match parsed { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 176 | Ok((techniques, intensity, lube)) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 177 | respond(run.jelq(&techniques, intensity, lube, ctx.now)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 178 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 179 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 180 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 181 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 183 | fn put_run(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 184 | run.put().into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 185 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 186 | |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 187 | fn hold_run(run: &mut Run, ctx: &Ctx) -> Response { |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 188 | respond(run.hold(ctx.now)) |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 189 | } |
4347977b8192 | atagen | 2026-10-11 04:56 UTC | 190 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 191 | fn patch_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 192 | let is_patch = ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 193 | .header(header::CONTENT_TYPE) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 194 | .is_some_and(|ct| ct.starts_with("application/json-patch+json")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 195 | if !is_patch { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 196 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 197 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 198 | "patch-format", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 199 | "Patches are accepted as application/json-patch+json.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 200 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 201 | .header(ACCEPT_PATCH, "application/json-patch+json") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 202 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 203 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 204 | match serde_json::from_slice::<Vec<PatchOp>>(&ctx.body) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 205 | Ok(ops) => respond(run.patch(&ops)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 206 | Err(_) => Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 207 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 208 | "malformed-patch", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 209 | "The patch could not be parsed.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 210 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 211 | .into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 212 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 213 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 214 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 215 | fn delete_run(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 216 | run.end(Cause::Abandoned); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 217 | StatusCode::NO_CONTENT.into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 218 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 219 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 220 | fn trace_run(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 221 | run.shame += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 222 | let mut message = format!("{} {} {:?}\r\n", ctx.method, ctx.uri, ctx.version); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 223 | for (name, value) in &ctx.headers { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 224 | let _ = write!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 225 | message, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 226 | "{name}: {}\r\n", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 227 | value.to_str().unwrap_or("<binary>") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 228 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 229 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 230 | message.push_str("\r\n"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 231 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 232 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 233 | [(header::CONTENT_TYPE, "message/http")], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 234 | message, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 235 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 236 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 237 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 238 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 239 | fn millimetres(inches: f64) -> usize { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 240 | (inches * MM_PER_INCH).round() as usize |
782069860236 | atagen | 2026-10-10 12:44 UTC | 241 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 242 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 243 | enum InchRange { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 244 | Satisfiable(f64, f64), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 245 | Unsatisfiable, |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 246 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 247 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 248 | /// `None` means the header is not a valid range, which RFC 9110 §14.2 says to ignore rather than refuse. |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 249 | fn parse_inch_range(range: &str, total: f64) -> Option<InchRange> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 250 | let (start, end) = range.strip_prefix("inches=")?.split_once('-')?; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 251 | let number = |text: &str| { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 252 | text.parse::<f64>() |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 253 | .ok() |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 254 | .filter(|n| n.is_finite() && *n >= 0.0) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 255 | }; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 256 | let (start, end) = match (start.trim(), end.trim()) { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 257 | ("", "") => return None, |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 258 | ("", suffix) => ((total - number(suffix)?).max(0.0), total), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 259 | (start, "") => (number(start)?, total), |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 260 | (start, end) => { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 261 | let (start, end) = (number(start)?, number(end)?); |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 262 | if start > end { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 263 | return None; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 264 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 265 | (start, end) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 266 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 267 | }; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 268 | Some(if start < total { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 269 | InchRange::Satisfiable(start, end.min(total)) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 270 | } else { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 271 | InchRange::Unsatisfiable |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 272 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 273 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 274 | |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 275 | fn unsatisfied_range(inches: f64) -> Response { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 276 | Problem::new( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 277 | StatusCode::RANGE_NOT_SATISFIABLE, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 278 | "range-not-satisfiable", |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 279 | format!("The requested range is not available. {inches:.2} inches are."), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 280 | ) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 281 | .header(header::CONTENT_RANGE, format!("inches */{inches:.2}")) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 282 | .into_response() |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 283 | } |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 284 | |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 285 | fn partial_inches(body: &str, offset: usize, start: f64, end: f64, total: f64) -> Response { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 286 | let from = (millimetres(start) + offset).min(body.len() - 1); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 287 | let to = (millimetres(end) + offset).clamp(from + 1, body.len()); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 288 | ( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 289 | StatusCode::PARTIAL_CONTENT, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 290 | [ |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 291 | ( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 292 | header::CONTENT_TYPE, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 293 | "text/plain; charset=us-ascii".to_owned(), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 294 | ), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 295 | (header::ACCEPT_RANGES, "inches".to_owned()), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 296 | ( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 297 | header::CONTENT_RANGE, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 298 | format!("inches {start:.2}-{end:.2}/{total:.2}"), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 299 | ), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 300 | ], |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 301 | body[from..to].to_owned(), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 302 | ) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 303 | .into_response() |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 304 | } |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 305 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 306 | fn get_length(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 307 | let inches = if run.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 308 | run.current_length() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 309 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 310 | run.self_reported() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 311 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 312 | let body = shaft(millimetres(inches)); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 313 | let content_type = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 314 | header::CONTENT_TYPE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 315 | "text/plain; charset=us-ascii".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 316 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 317 | let accept_ranges = (header::ACCEPT_RANGES, "inches".to_owned()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 318 | |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 319 | if let Some(range) = ctx |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 320 | .header(header::RANGE) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 321 | .and_then(|range| parse_inch_range(range, inches)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 322 | { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 323 | match range { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 324 | InchRange::Satisfiable(start, end) => { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 325 | return partial_inches(&body, 0, start, end, inches); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 326 | } |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 327 | InchRange::Unsatisfiable => return unsatisfied_range(inches), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 328 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 329 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 330 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 331 | if run.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 332 | return (StatusCode::OK, [content_type, accept_ranges], body).into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 333 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 334 | let chunks: Vec<Result<Bytes, Infallible>> = body |
782069860236 | atagen | 2026-10-10 12:44 UTC | 335 | .into_bytes() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 336 | .chunks(16) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 337 | .map(|c| Ok(Bytes::copy_from_slice(c))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 338 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 339 | let body = Body::from_stream(futures_util::stream::iter(chunks)); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 340 | (StatusCode::OK, [content_type, accept_ranges], body).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 341 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 342 | |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 343 | fn get_foreskin(run: &mut Run, ctx: &Ctx) -> Response { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 344 | let Some(inches) = run.foreskin else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 345 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 346 | StatusCode::GONE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 347 | "foreskin-gone", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 348 | "The foreskin is gone. It will not be returning.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 349 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 350 | .into_response(); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 351 | }; |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 352 | let body = format!("8({})D", "~".repeat(millimetres(inches))); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 353 | if let Some(range) = ctx |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 354 | .header(header::RANGE) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 355 | .and_then(|range| parse_inch_range(range, inches)) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 356 | { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 357 | match range { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 358 | InchRange::Satisfiable(start, end) => { |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 359 | return partial_inches(&body, 2, start, end, inches); |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 360 | } |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 361 | InchRange::Unsatisfiable => return unsatisfied_range(inches), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 362 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 363 | } |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 364 | ( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 365 | StatusCode::OK, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 366 | [ |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 367 | (header::ETAG, run.etag()), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 368 | ( |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 369 | header::CONTENT_TYPE, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 370 | "text/plain; charset=us-ascii".to_owned(), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 371 | ), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 372 | (header::ACCEPT_RANGES, "inches".to_owned()), |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 373 | ], |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 374 | body, |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 375 | ) |
ec6fd4bb5b60 | atagen | 2026-10-11 04:34 UTC | 376 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 377 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 378 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 379 | fn delete_foreskin(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 380 | respond(run.delete_foreskin(ctx.header(header::IF_MATCH))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 381 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 382 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 383 | fn get_inventory(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 384 | let items: Vec<Value> = run |
782069860236 | atagen | 2026-10-10 12:44 UTC | 385 | .items |
782069860236 | atagen | 2026-10-10 12:44 UTC | 386 | .owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 387 | .into_iter() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 388 | .map(|item| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 389 | json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 390 | "name": item.slug(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 391 | "href": run.path(&format!("/inventory/{}", item.slug())), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 392 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 393 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 394 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 395 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 396 | "items": items, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 397 | "ice_packs": run.items.ice_packs, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 398 | "supplements": run.items.supplements, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 399 | "excuses": run.excuses(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 400 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 401 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 402 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 403 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 404 | fn owned_item(run: &Run, ctx: &Ctx) -> Result<Item, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 405 | Item::parse(ctx.param("item")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 406 | .filter(|&item| run.items.has(item)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 407 | .ok_or_else(|| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 408 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 409 | StatusCode::NOT_FOUND, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 410 | "no-such-item", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 411 | "You do not have that.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 412 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 413 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 414 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 415 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 416 | fn get_item(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 417 | match owned_item(run, ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 418 | Ok(item) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 419 | Json(json!({ "name": item.slug(), "description": item.description() })).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 420 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 421 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 422 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 423 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 424 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 425 | fn delete_item(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 426 | match owned_item(run, ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 427 | Ok(item) => respond(run.drop_item(item)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 428 | Err(problem) => problem.into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 429 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 430 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 431 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 432 | fn get_techniques(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 433 | let listed: Vec<Value> = Technique::ALL |
782069860236 | atagen | 2026-10-10 12:44 UTC | 434 | .into_iter() |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 435 | .map(|t| t.describe(run.uli_taught)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 436 | .collect(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 437 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 438 | "usage": format!("JELQ {}?technique=<name>[,<name>]&intensity=<1-10>", run.path("")), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 439 | "lubricant": "Declared with Content-Type.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 440 | "lubricants": LUBRICANTS, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 441 | "eq_band": [EQ_BAND.start(), EQ_BAND.end()], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 442 | "techniques": listed, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 443 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 444 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 445 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 446 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 447 | fn search_drawer(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 448 | respond(run.search_drawer()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 449 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 450 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 451 | fn choose_branch(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 452 | respond(run.choose(ctx.param("choice"))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 453 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 454 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 455 | fn get_floor(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 456 | Json(run.floor_view()).into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 457 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 458 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 459 | fn leave_floor(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 460 | respond(run.advance()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 461 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 462 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 463 | fn get_shop(run: &mut Run, _: &Ctx) -> Response { |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 464 | let view = run.shop_view(); |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 465 | if view.is_ok() { |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 466 | run.here.shopping = true; |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 467 | } |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 468 | respond(view.map(Json)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 469 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 470 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 471 | fn buy(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 472 | respond(run.buy(ctx.param("item"))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 473 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 474 | |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 475 | fn raid_kitchen(run: &mut Run, _: &Ctx) -> Response { |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 476 | respond(run.raid_kitchen()) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 477 | } |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 478 | |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 479 | fn commit_melon(run: &mut Run, _: &Ctx) -> Response { |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 480 | respond(run.commit_melon()) |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 481 | } |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 482 | |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 483 | fn re_read(run: &mut Run, _: &Ctx) -> Response { |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 484 | respond(run.read_thread()) |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 485 | } |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 486 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 487 | fn get_portal(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 488 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 489 | "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.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 490 | "accept": { "href": run.path("/portal"), "method": "POST" }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 491 | })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 492 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 493 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 494 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 495 | fn accept_portal(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 496 | respond(run.accept_terms()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 497 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 498 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 499 | fn get_elder(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 500 | respond(run.elder( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 501 | ctx.header(header::PROXY_AUTHORIZATION), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 502 | ctx.header(header::IF_MODIFIED_SINCE), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 503 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 504 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 505 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 506 | fn requested_grip(ctx: &Ctx) -> Option<Grip> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 507 | let connection = ctx.header(header::CONNECTION)?.to_ascii_lowercase(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 508 | if !connection.split(',').any(|token| token.trim() == "upgrade") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 509 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 510 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 511 | ctx.header(header::UPGRADE)? |
782069860236 | atagen | 2026-10-10 12:44 UTC | 512 | .split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 513 | .find_map(|protocol| Grip::parse(protocol.trim())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 514 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 515 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 516 | fn get_grip(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 517 | if let Some(grip) = requested_grip(ctx) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 518 | run.set_grip(grip); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 519 | let mut response = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 520 | StatusCode::SWITCHING_PROTOCOLS, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 521 | [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 522 | (header::UPGRADE, grip.name()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 523 | (header::CONNECTION, "upgrade"), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 524 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 525 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 526 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 527 | response.extensions_mut().insert(Greeting(format!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 528 | "You are now jelqing {}.\r\n", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 529 | grip.name() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 530 | ))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 531 | return response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 532 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 533 | let detail = match ctx.header(header::UPGRADE) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 534 | Some(protocol) => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 535 | format!("The server does not know the {protocol} grip. Grips are protocols.") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 536 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 537 | None => "Grips are protocols. Change grip by upgrading the connection.".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 538 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 539 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 540 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 541 | [(header::UPGRADE, "overhand, underhand")], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 542 | Json(json!({ "grip": run.grip.name(), "detail": detail })), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 543 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 544 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 545 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 546 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 547 | fn get_diagnosis(run: &mut Run, ctx: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 548 | respond(run.diagnose( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 549 | ctx.header(header::AUTHORIZATION), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 550 | ctx.header(header::IF_NONE_MATCH), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 551 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 552 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 553 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 554 | fn get_footage(run: &mut Run, _: &Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 555 | let Some((filmed, age)) = run.footage() else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 556 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 557 | StatusCode::NOT_FOUND, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 558 | "no-footage", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 559 | "There is no footage. Yet.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 560 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 561 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 562 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 563 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 564 | [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 565 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 566 | header::CACHE_CONTROL, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 567 | "public, max-age=31536000, immutable".to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 568 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 569 | (header::AGE, age.to_string()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 570 | (header::LAST_MODIFIED, httpdate::fmt_http_date(filmed)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 571 | ], |
782069860236 | atagen | 2026-10-10 12:44 UTC | 572 | Json(json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 573 | "detail": "A recording. It is grainy. It is unmistakably you. It has been shared.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 574 | "views": 1 + age * 37, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 575 | })), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 576 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 577 | .into_response() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 578 | } |