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