an http based roguelike rpg about jelqing
Blame: src/game/clinic.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use axum::http::{StatusCode, header}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | use serde_json::json; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | use super::{Floor, Outcome, Run, etag_matches, parse_credentials, round2}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | use crate::{headers::X_DIAGNOSIS, problem::Problem}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | const INTAKE: [&str; 3] = [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | "The clinic does not accept excuses.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | "The clinic accepts one token. You have not been carrying it.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | "Be honest.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | impl Run { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | pub fn diagnose( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | &mut self, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | authorization: Option<&str>, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | if_none_match: Option<&str>, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | ) -> Result<Outcome, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | if self.floor != Floor::Clinic { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | let floors = Floor::Clinic.number() - self.floor.number(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | StatusCode::TOO_EARLY, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | "no-appointment", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | format!("The clinic's next available appointment is {floors} floors away."), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | let intake = Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | "intake", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 30 | "The clinic requires authorisation.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | let Some(authorization) = authorization else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | return Err(intake.header(header::WWW_AUTHENTICATE, "Bearer realm=\"clinic\"")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | let truthful = parse_credentials(authorization, "bearer") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | .is_some_and(|token| token.eq_ignore_ascii_case("honesty")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | if !truthful { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | let hint = INTAKE[self.clinic.intake_attempts.min(INTAKE.len() - 1)]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | self.clinic.intake_attempts += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | return Err(intake.header( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | header::WWW_AUTHENTICATE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | format!( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | "Bearer realm=\"clinic\", error=\"invalid_token\", error_description=\"{hint}\"" |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | ), |
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 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | self.conditions.peyronie = false; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | self.conditions.priapism_turns = 0; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | let etag = self.origin_etag(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | if if_none_match.is_some_and(|inm| etag_matches(inm, &etag)) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | self.clinic.cured = true; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | return Ok(Outcome::empty(StatusCode::NOT_MODIFIED) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | .header(header::ETAG, etag) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | .header(X_DIAGNOSIS, "normal")); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | if !self.clinic.measured { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | self.clinic.measured = true; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | if !self.items.ruler { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | self.shame += 15; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | Ok(Outcome::json( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | json!({ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | "diagnosis": "within normal range", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | "measured_length": round2(self.length), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | "prescription": "Please stop.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | }), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | .header(header::ETAG, etag)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | } |