an http based roguelike rpg about jelqing
Blame: src/http/run.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | //! Plumbing shared by every resource under `/runs/{id}`. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use axum::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | http::{HeaderName, HeaderValue, StatusCode, header}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | response::{IntoResponse, Response}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | use hyper_util::rt::TokioIo; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | use tokio::io::AsyncWriteExt; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 10 | use super::{AppState, ctx::Ctx, meta, middleware::Compress, resource::Resource, runs}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | use crate::{ |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 12 | game::{ |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 13 | Elder, FORUM_THREAD, Floor, Run, SHOP, Story, |
e807266c323b | atagen | 2026-10-12 09:46 UTC | 14 | balance::{FRICTION_MAX, KITCHEN_TELLINGS, SUSPICION_LIMIT}, |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 15 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | headers::{ |
5dfebcc0ac43 | atagen | 2026-10-11 05:28 UTC | 17 | X_CASH, X_EQ, X_FLOOR, X_FORESKIN, X_FRICTION, X_GIRTH, X_LENGTH, X_OBSERVED_BY, |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 18 | X_SELF_REPORTED_LENGTH, X_SHAME, X_STAMINA, X_SUSPICION, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | }, |
ef7d6dee5976 | atagen | 2026-10-10 13:23 UTC | 20 | locale::Locale, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | problem::{PROBLEM_BASE, Problem}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | secret::Secret, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | pub const REL_BASE: &str = "https://jelq.dev/rels/"; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | const COOKIE_ATTRIBUTES: &str = "Max-Age=31536000; Path=/; HttpOnly; SameSite=Strict"; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | /// Which household hazards stand between a request and the run. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | #[derive(Clone, Copy, PartialEq, Eq)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | pub enum Scope { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | Home, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | Pockets, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | pub struct RunResource { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | pub scope: Scope, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | pub resource: Resource<Run>, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | /// What the server writes over the connection after a `101`. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | #[derive(Clone)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | pub struct Greeting(pub String); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | pub fn serve(state: &AppState, mut ctx: Ctx, target: &RunResource) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | let mut runs = state.runs(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | let Some(run) = runs.get_mut(ctx.param("id")) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | return Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | StatusCode::NOT_FOUND, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | "no-such-run", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | "There is no such run. There may never have been.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | if let Some(ending) = &run.ended { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | return Problem::new(StatusCode::GONE, "run-ended", "This run has ended.") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | .with("cause", format!("{PROBLEM_BASE}{}", ending.cause.slug())) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | .with("turn", ending.turn) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | .with("graveyard", "/graveyard") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | .into_response(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | run.last_seen = ctx.now; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | run.observed = None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | let response = |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | gate(run, &ctx, target.scope).unwrap_or_else(|| target.resource.dispatch(run, &ctx)); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | let response = finish(run, &ctx, &state.config.secret, response); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | drop(runs); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | greet(response, &mut ctx) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | fn gate(run: &mut Run, ctx: &Ctx, scope: Scope) -> Option<Response> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | if scope == Scope::Pockets { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | if run.floor == Floor::Hotel && !run.here.wifi_accepted { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | let portal = run.path("/portal"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | return Some( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | StatusCode::NETWORK_AUTHENTICATION_REQUIRED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | "captive-portal", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | "The network requires authentication. The hotel would like you to agree to some terms.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | .with("portal", portal.as_str()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | .header(header::LINK, format!("<{portal}>; rel=\"{REL_BASE}portal\"")) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | .into_response(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | } |
ef7d6dee5976 | atagen | 2026-10-10 13:23 UTC | 86 | let authorization = ctx |
ef7d6dee5976 | atagen | 2026-10-10 13:23 UTC | 87 | .header(header::AUTHORIZATION) |
ef7d6dee5976 | atagen | 2026-10-10 13:23 UTC | 88 | .map(|text| Locale::negotiate(ctx.header(header::ACCEPT_LANGUAGE)).canonicalise(text)); |
ef7d6dee5976 | atagen | 2026-10-10 13:23 UTC | 89 | run.answer_door(authorization.as_deref()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | .err() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | .map(IntoResponse::into_response) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 94 | /// Every resource the run can reach right now, then every technique a JELQ could use. The `allow` |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 95 | /// and `lubricant` extension attributes (RFC 8288 §3) say what each accepts, so a client never guesses. |
a6caa2f99786 | atagen | 2026-10-12 10:38 UTC | 96 | fn links(run: &Run) -> String { |
e807266c323b | atagen | 2026-10-12 09:46 UTC | 97 | let thread_reachable = run.thread_reachable(); |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 98 | let linked: [(&str, &RunResource, bool); 15] = [ |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 99 | ("", &runs::RUN, true), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 100 | ("floor", &runs::FLOOR, true), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 101 | ("length", &runs::LENGTH, true), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 102 | ("foreskin", &runs::FORESKIN, run.foreskin.is_some()), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 103 | ("inventory", &runs::INVENTORY, true), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 104 | ("techniques", &runs::TECHNIQUES, true), |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 105 | ("drawer", &runs::DRAWER, run.drawer_worth_searching()), |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 106 | ("kitchen", &runs::KITCHEN, { |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 107 | run.floor.has_kitchen() && run.kitchen_tellings < KITCHEN_TELLINGS |
0a0bfee8329e | atagen | 2026-10-11 05:11 UTC | 108 | }), |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 109 | ("re-read", &runs::REREAD, thread_reachable), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 110 | ("grip", &runs::GRIP, true), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 111 | ("shop", &runs::SHOP, run.floor.shop_open()), |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 112 | ( |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 113 | "portal", |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 114 | &runs::PORTAL, |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 115 | run.floor == Floor::Hotel && !run.here.wifi_accepted, |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 116 | ), |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 117 | ( |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 118 | "elder", |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 119 | &runs::ELDER, |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 120 | run.floor == Floor::Forum && run.elder != Elder::Beaten, |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 121 | ), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 122 | ("footage", &runs::FOOTAGE, run.filmed_at.is_some()), |
d604371299fb | atagen | 2026-10-10 13:54 UTC | 123 | ("diagnosis", &runs::DIAGNOSIS, run.floor == Floor::Clinic), |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 124 | ]; |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 125 | linked |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 126 | .into_iter() |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 127 | .filter(|(_, _, relevant)| *relevant) |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 128 | .map(|(rel, target, _)| { |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 129 | let (path, name) = if rel.is_empty() { |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 130 | (run.path(""), "run") |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 131 | } else { |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 132 | (run.path(&format!("/{rel}")), rel) |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 133 | }; |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 134 | let allow = target.resource.allow(run); |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 135 | format!("<{path}>; rel=\"{REL_BASE}{name}\"; allow=\"{allow}\"") |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 136 | }) |
b85a0d4536c3 | atagen | 2026-10-11 08:50 UTC | 137 | .chain(thread_reachable.then(|| { |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 138 | format!( |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 139 | "<{FORUM_THREAD}>; rel=\"{REL_BASE}thread\"; allow=\"{}\"", |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 140 | meta::THREAD.allow_any() |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 141 | ) |
0fdc47364c8c | atagen | 2026-10-11 04:45 UTC | 142 | }),) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 143 | .chain((run.story == Story::Pending).then(|| { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 144 | let choice = |slug: &str, title: &str| { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 145 | format!( |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 146 | "<{}>; rel=\"{REL_BASE}branch\"; title=\"{title}\"; allow=\"POST\"", |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 147 | run.path(&format!("/branches/{slug}")) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 148 | ) |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 149 | }; |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 150 | [choice("forum", "Read the forum"), choice("close-tab", "Close the tab")].join(", ") |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 151 | })) |
a6caa2f99786 | atagen | 2026-10-12 10:38 UTC | 152 | .chain(run.techniques_now().into_iter().map(|technique| { |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 153 | let name = technique.name(); |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 154 | let lubricant = technique |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 155 | .lubricant() |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 156 | .map_or(String::new(), |rule| format!("; lubricant=\"{rule}\"")); |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 157 | format!( |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 158 | "<{}?technique={name}>; rel=\"{REL_BASE}technique\"; title=\"{name}\"; allow=\"JELQ\"{lubricant}", |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 159 | run.path("") |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 160 | ) |
58e1fb3943e6 | atagen | 2026-10-10 13:30 UTC | 161 | })) |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 162 | .chain(run.here.shopping.then(|| { |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 163 | SHOP.iter() |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 164 | .filter_map(|item| { |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 165 | let price = item.price()?; |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 166 | Some(format!( |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 167 | "<{}>; rel=\"{REL_BASE}shop-item\"; title=\"{} for {price}\"; allow=\"POST\"", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 168 | run.path(&format!("/shop/{}", item.slug())), |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 169 | item.slug() |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 170 | )) |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 171 | }) |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 172 | .collect::<Vec<_>>() |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 173 | .join(", ") |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 174 | })) |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 175 | .collect::<Vec<_>>() |
185851447ceb | atagen | 2026-10-10 12:58 UTC | 176 | .join(", ") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 177 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 178 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 179 | /// Shame lives in a signed cookie so it follows the player across runs. Each response adds this |
782069860236 | atagen | 2026-10-10 12:44 UTC | 180 | /// run's new shame to whatever total the client presents, so two runs at once both count. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 181 | fn settle_shame(run: &mut Run, ctx: &Ctx, secret: &Secret) -> u32 { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | let presented = ctx |
782069860236 | atagen | 2026-10-10 12:44 UTC | 183 | .cookie("shame") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 184 | .and_then(|signed| secret.verify("shame", signed)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 185 | .and_then(|total| total.parse::<u32>().ok()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 186 | let total = presented.unwrap_or(run.shame_total) + (run.shame - run.shame_reported); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 187 | run.shame_reported = run.shame; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 188 | run.shame_total = total; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 189 | total |
782069860236 | atagen | 2026-10-10 12:44 UTC | 190 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 191 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 192 | pub fn finish(run: &mut Run, ctx: &Ctx, secret: &Secret, mut response: Response) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 193 | let status = response.status(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 194 | if status == StatusCode::NOT_MODIFIED { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 195 | return response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 196 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 197 | let shame = settle_shame(run, ctx, secret); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 198 | let mut stats: Vec<(HeaderName, String)> = vec![ |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 199 | if run.items.ruler { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 200 | (X_LENGTH, format!("{:.2}", run.current_length())) |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 201 | } else { |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 202 | ( |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 203 | X_SELF_REPORTED_LENGTH, |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 204 | format!("{:.2}", run.self_reported()), |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 205 | ) |
f39b10e20a0b | atagen | 2026-10-10 14:33 UTC | 206 | }, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 207 | (X_GIRTH, format!("{:.2}", run.girth)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 208 | ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 209 | X_FORESKIN, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 210 | run.foreskin.map_or("gone".into(), |f| format!("{f:.2}")), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 211 | ), |
5dfebcc0ac43 | atagen | 2026-10-11 05:28 UTC | 212 | (X_FRICTION, format!("{}/{}", run.friction, FRICTION_MAX)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 213 | (X_EQ, run.eq.to_string()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 214 | (X_STAMINA, format!("{}/{}", run.stamina, run.max_stamina())), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 215 | (X_SUSPICION, format!("{}/{SUSPICION_LIMIT}", run.suspicion)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 216 | (X_SHAME, shame.to_string()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 217 | (X_CASH, run.cash.to_string()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 218 | (X_FLOOR, run.floor.number().to_string()), |
a6caa2f99786 | atagen | 2026-10-12 10:38 UTC | 219 | (header::LINK, links(run)), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 220 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 221 | if let Some(observer) = run.observed { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 222 | stats.push((X_OBSERVED_BY, observer.name().into())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 223 | } |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 224 | if run.mum.is_some() { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 225 | stats.push((header::VIA, "1.1 mum".into())); |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 226 | stats.push((header::VARY, "Mum".into())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 227 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 228 | // RFC 9111 obsoleted Warning, which is why nobody heeds it. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 229 | if run.conditions.pulled_muscle { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 230 | stats.push((header::WARNING, "199 - \"a muscle has been pulled\"".into())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 231 | } |
5dfebcc0ac43 | atagen | 2026-10-11 05:28 UTC | 232 | if run.conditions.friction_burn { |
5dfebcc0ac43 | atagen | 2026-10-11 05:28 UTC | 233 | stats.push((header::WARNING, "199 - \"friction burn\"".into())); |
5dfebcc0ac43 | atagen | 2026-10-11 05:28 UTC | 234 | } |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 235 | for notice in run.notices.drain(..) { |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 236 | stats.push((header::WARNING, format!("199 - \"{notice}\""))); |
d46f9d0b953a | atagen | 2026-10-10 14:41 UTC | 237 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 238 | if run.eq > 70 && run.ended.is_none() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 239 | stats.push((header::WARNING, "199 - \"it is turning purple\"".into())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 240 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 241 | stats.push(( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 242 | header::SET_COOKIE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 243 | format!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 244 | "shame={}; {COOKIE_ATTRIBUTES}", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 245 | secret.sign("shame", &shame.to_string()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 246 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 247 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 248 | if run.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 249 | stats.push(( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 250 | header::SET_COOKIE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 251 | format!("ruler={}; {COOKIE_ATTRIBUTES}", secret.sign("ruler", "1")), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 252 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 253 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 254 | let headers = response.headers_mut(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 255 | for (name, value) in stats { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 256 | if let Ok(value) = HeaderValue::from_str(&value) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 257 | headers.append(name, value); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 258 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 259 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 260 | let bodiless = status.is_informational() || status == StatusCode::NO_CONTENT; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 261 | if run.items.ring && !bodiless { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 262 | response.extensions_mut().insert(Compress); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 263 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 264 | response |
782069860236 | atagen | 2026-10-10 12:44 UTC | 265 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 266 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 267 | fn greet(response: Response, ctx: &mut Ctx) -> Response { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 268 | if response.status() != StatusCode::SWITCHING_PROTOCOLS { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 269 | return response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 270 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 271 | let (Some(Greeting(text)), Some(upgrade)) = ( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 272 | response.extensions().get::<Greeting>().cloned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 273 | ctx.upgrade.take(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 274 | ) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 275 | return response; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 276 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 277 | tokio::spawn(async move { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 278 | if let Ok(upgraded) = upgrade.await { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 279 | let mut io = TokioIo::new(upgraded); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 280 | let _ = io.write_all(text.as_bytes()).await; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 281 | let _ = io.shutdown().await; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 282 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 283 | }); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 284 | response |
782069860236 | atagen | 2026-10-10 12:44 UTC | 285 | } |