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 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | const EXCUSES: [&str; 3] = ["changing", "homework", "pulled a muscle"]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | const THANKSGIVING_EXCUSES: [&str; 3] = ["on a call", "folding laundry", "praying"]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | const TRUTHS: [&str; 3] = ["the truth", "truth", "honesty"]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | const TRUE_WHEN_PULLED: &str = "pulled a muscle"; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | const BEDROOM_ERRANDS: [&str; 4] = [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | "She has brought laundry.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | "She wants to know if you have eaten.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | "She needs the wifi password.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | "She would like to talk about your future.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | const THANKSGIVING_ERRANDS: [&str; 4] = [ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | "Grandma would like to see you.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | "Dinner is in ten minutes.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | "She needs help with the folding table.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | "An uncle has asked where you went.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | fn digest_params(authorization: &str) -> Option<HashMap<String, String>> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | let (scheme, rest) = authorization.trim().split_once(' ')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | if !scheme.eq_ignore_ascii_case("digest") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | Some( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | rest.split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | .filter_map(|pair| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | let (key, value) = pair.split_once('=')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | Some(( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | key.trim().to_ascii_lowercase(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | value.trim().trim_matches('"').to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | .collect(), |
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 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | fn spoken(authorization: &str) -> String { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | authorization |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | .split_once(' ') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | .map(|(_, rest)| rest.trim().trim_matches('"').to_ascii_lowercase()) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | .unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | impl Run { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | pub fn excuses(&self) -> Vec<&'static str> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | if self.floor.number() < Floor::Grandmas.number() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | return EXCUSES.to_vec(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | EXCUSES |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | .into_iter() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | .chain(THANKSGIVING_EXCUSES) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | .filter(|e| !self.heard.iter().any(|h| h == e)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | .collect() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | pub(super) fn errand(&mut self, summoned: bool) -> &'static str { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | if summoned { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | return "She heard a noise."; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | if self.filmed_at.is_some() && self.chance(VIRAL_ERRAND) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | return "A neighbour has sent her something."; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | let errands = if self.floor == Floor::Grandmas { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | THANKSGIVING_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | BEDROOM_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | errands[self.rng.random_range(0..errands.len())] |
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 | /// Mom answers every request on the run until she has been given something she believes. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | pub fn answer_door(&mut self, authorization: Option<&str>) -> Result<(), Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | let Some((errand, nonce)) = self.mom.as_ref().map(|v| (v.errand, v.nonce.clone())) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | let digest = self.floor == Floor::Grandmas; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | let challenge_value = if digest { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | format!("Digest realm=\"household\", nonce=\"{nonce}\"") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | "Excuse realm=\"household\"".to_owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | let challenge = |p: Problem| p.header(header::WWW_AUTHENTICATE, &challenge_value); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | let Some(authorization) = authorization else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | return Err(challenge( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | "someone-at-the-door", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | format!("Someone is at the door. {errand}"), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | .with("inventory", self.path("/inventory")), |
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 | if TRUTHS.contains(&spoken(authorization).as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | self.shame += 10; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | if parse_credentials(authorization, "basic").is_some() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | return Err(self.mom_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | "basic", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | "Mom does not accept Basic authentication. Nobody should.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | )))); |
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 | let excuse = if digest { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | let Some(params) = digest_params(authorization) else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 122 | return Err(self.mom_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 123 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 124 | "digest-required", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 125 | "Mom has started using Digest authentication.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 126 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 127 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 128 | if params.get("nonce") != Some(&nonce) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 129 | return Err(self.mom_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 130 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 131 | "stale-nonce", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 132 | "That was a different conversation.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 133 | )))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 134 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 135 | params.get("username").cloned().unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 136 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 137 | parse_credentials(authorization, "excuse") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 138 | .unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 139 | .to_owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 140 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 141 | let excuse = excuse.to_ascii_lowercase(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 142 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 143 | if TRUTHS.contains(&excuse.as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | self.shame += 10; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 146 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 147 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 148 | let true_now = self.conditions.pulled_muscle && excuse == TRUE_WHEN_PULLED; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 149 | if digest && self.heard.contains(&excuse) && !true_now { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 150 | self.shame += 3; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 151 | return Err(self.mom_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 152 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 153 | "excuse-replayed", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 154 | "Mom has heard that one before. She would have preferred the truth.", |
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 | if true_now || self.excuses().contains(&excuse.as_str()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 158 | self.heard.push(excuse); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 159 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 160 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 161 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | self.shame += 3; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | let detail = if digest { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | "Mom 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 | 165 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | "Mom has not heard that one before. She does not believe it." |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 168 | Err(self.mom_strike(challenge(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | StatusCode::UNAUTHORIZED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | "excuse-rejected", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 171 | detail, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 172 | )))) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 173 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 174 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 175 | fn mom_strike(&mut self, problem: Problem) -> Problem { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 176 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 177 | let Some(visit) = &mut self.mom else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 178 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 179 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 180 | visit.strikes += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 181 | if visit.strikes < PATIENCE { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 183 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 184 | self.end(Cause::Caught) |
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 | fn leave_door(&mut self) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 188 | self.mom = None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 189 | self.suspicion = 0; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 190 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 191 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 192 | } |