an http based roguelike rpg about jelqing
Blame: src/game/household.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use std::collections::HashMap; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use axum::http::{StatusCode, header}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | use rand::Rng; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | use super::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | Cause, Floor, Run, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | balance::{PATIENCE, VIRAL_ERRAND}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | parse_credentials, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | use crate::problem::Problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 13 | const EXCUSES: [&str; 7] = [ |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 14 | "changing", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 15 | "homework", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 16 | "pulled a muscle", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 17 | "tidying", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 18 | "the washing up", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 19 | "fed the cat", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 20 | "revising", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 21 | ]; |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 22 | const CHRISTMAS_EXCUSES: [&str; 6] = [ |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 23 | "on a call", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 24 | "wrapping presents", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 25 | "folding the washing", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 26 | "hanging the lights", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 27 | "helping Nan", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 28 | "peeling sprouts", |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 29 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | const TRUTHS: [&str; 3] = ["the truth", "truth", "honesty"]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | const TRUE_WHEN_PULLED: &str = "pulled a muscle"; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | const BEDROOM_ERRANDS: [&str; 4] = [ |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 34 | "She has brought your washing.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | "She wants to know if you have eaten.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | "She needs the wifi password.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | "She would like to talk about your future.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | ]; |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 39 | const CHRISTMAS_ERRANDS: [&str; 4] = [ |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 40 | "Nan would like to see you.", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 41 | "Christmas dinner is in ten minutes.", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 42 | "The King's speech is about to start.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | "An uncle has asked where you went.", |
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 | fn digest_params(authorization: &str) -> Option<HashMap<String, String>> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | let (scheme, rest) = authorization.trim().split_once(' ')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | if !scheme.eq_ignore_ascii_case("digest") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | Some( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | rest.split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | .filter_map(|pair| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | let (key, value) = pair.split_once('=')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | Some(( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | key.trim().to_ascii_lowercase(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | value.trim().trim_matches('"').to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | .collect(), |
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 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | fn spoken(authorization: &str) -> String { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | authorization |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | .split_once(' ') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | .map(|(_, rest)| rest.trim().trim_matches('"').to_ascii_lowercase()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | .unwrap_or_default() |
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 | impl Run { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | pub fn excuses(&self) -> Vec<&'static str> { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 73 | if self.floor.number() < Floor::Nans.number() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | return EXCUSES.to_vec(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | EXCUSES |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | .into_iter() |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 78 | .chain(CHRISTMAS_EXCUSES) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | .filter(|e| !self.heard.iter().any(|h| h == e)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | .collect() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | pub(super) fn errand(&mut self, summoned: bool) -> &'static str { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | if summoned { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | return "She heard a noise."; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | if self.filmed_at.is_some() && self.chance(VIRAL_ERRAND) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | return "A neighbour has sent her something."; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | } |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 90 | let errands = if self.floor == Floor::Nans { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 91 | CHRISTMAS_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | BEDROOM_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | errands[self.rng.random_range(0..errands.len())] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 98 | /// Mum answers every request on the run until she has been given something she believes. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | pub fn answer_door(&mut self, authorization: Option<&str>) -> Result<(), Problem> { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 100 | let Some((errand, nonce)) = self.mum.as_ref().map(|v| (v.errand, v.nonce.clone())) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | }; |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 103 | let digest = self.floor == Floor::Nans; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | let challenge_value = if digest { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | format!("Digest realm=\"household\", nonce=\"{nonce}\"") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | "Excuse realm=\"household\"".to_owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | let challenge = |p: Problem| p.header(header::WWW_AUTHENTICATE, &challenge_value); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | let Some(authorization) = authorization else { |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 112 | let knocked_before = self.mum.as_ref().is_some_and(|visit| visit.knocked); |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 113 | if let Some(visit) = &mut self.mum { |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 114 | visit.knocked = true; |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 115 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 116 | if !knocked_before { |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 117 | self.touch(); |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 118 | return Err(challenge( |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 119 | Problem::new( |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 120 | StatusCode::UNAUTHORIZED, |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 121 | "someone-at-the-door", |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 122 | format!("Someone is at the door. {errand}"), |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 123 | ) |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 124 | .with("inventory", self.path("/inventory")), |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 125 | )); |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 126 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 127 | return Err(self.strike( |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 128 | challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 129 | StatusCode::UNAUTHORIZED, |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 130 | "knocking-again", |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 131 | format!("Mum is knocking again. {errand}"), |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 132 | )), |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 133 | Cause::Opened, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 135 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 136 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 137 | if TRUTHS.contains(&spoken(authorization).as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | self.shame += 10; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | if parse_credentials(authorization, "basic").is_some() { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 143 | return Err(self.mum_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | "basic", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 146 | "Mum does not accept Basic authentication. Nobody should.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 147 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 148 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 149 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | let excuse = if digest { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | let Some(params) = digest_params(authorization) else { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 152 | return Err(self.mum_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 153 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 154 | "digest-required", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 155 | "Mum has started using Digest authentication.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 156 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 157 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | if params.get("nonce") != Some(&nonce) { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 159 | return Err(self.mum_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | "stale-nonce", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | "That was a different conversation.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 165 | params.get("username").cloned().unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | parse_credentials(authorization, "excuse") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 168 | .unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | .to_owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 171 | let excuse = excuse.to_ascii_lowercase(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 172 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 173 | if TRUTHS.contains(&excuse.as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 174 | self.shame += 10; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 175 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 176 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 177 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 178 | let true_now = self.conditions.pulled_muscle && excuse == TRUE_WHEN_PULLED; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 179 | if digest && self.heard.contains(&excuse) && !true_now { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 180 | self.shame += 3; |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 181 | return Err(self.mum_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 183 | "excuse-replayed", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 184 | "Mum has heard that one before. She would have preferred the truth.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 185 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 186 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 187 | if true_now || self.excuses().contains(&excuse.as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 188 | self.heard.push(excuse); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 189 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 190 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 191 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 192 | self.shame += 3; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 193 | let detail = if digest { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 194 | "Mum has not heard that one before. She does not believe it. She would have preferred the truth." |
782069860236 | atagen | 2026-10-10 12:44 UTC | 195 | } else { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 196 | "Mum has not heard that one before. She does not believe it." |
782069860236 | atagen | 2026-10-10 12:44 UTC | 197 | }; |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 198 | Err(self.mum_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 199 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 200 | "excuse-rejected", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 201 | detail, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 202 | )))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 203 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 204 | |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 205 | fn mum_strike(&mut self, problem: Problem) -> Problem { |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 206 | self.strike(problem, Cause::Caught) |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 207 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 208 | |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 209 | fn strike(&mut self, problem: Problem, cause: Cause) -> Problem { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 210 | self.touch(); |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 211 | let Some(visit) = &mut self.mum else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 212 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 213 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 214 | visit.strikes += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 215 | if visit.strikes < PATIENCE { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 216 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 217 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 218 | self.end(cause) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 219 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 220 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 221 | fn leave_door(&mut self) { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 222 | self.mum = None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 223 | self.suspicion = 0; |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 224 | self.notices |
7d3fb63208bf | atagen | 2026-10-11 04:38 UTC | 225 | .push("Mum seems satisfied. She takes the washing with her."); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 226 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 227 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 228 | } |