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 | |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 3 | use axum::http::StatusCode; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | use super::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 6 | Cause, Floor, Notice, Run, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | balance::{PATIENCE, VIRAL_ERRAND}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 8 | credentials::{Scheme, parse_credentials}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | use crate::problem::Problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 12 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 13 | pub enum Excuse { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 14 | Changing, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 15 | Homework, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 16 | PulledAMuscle, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 17 | Tidying, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 18 | TheWashingUp, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 19 | FedTheCat, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 20 | Revising, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 21 | OnACall, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 22 | WrappingPresents, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 23 | FoldingTheWashing, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 24 | HangingTheLights, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 25 | HelpingNan, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 26 | PeelingSprouts, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 27 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 28 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 29 | impl Excuse { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 30 | const EVERYDAY: [Self; 7] = [ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 31 | Self::Changing, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 32 | Self::Homework, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 33 | Self::PulledAMuscle, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 34 | Self::Tidying, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 35 | Self::TheWashingUp, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 36 | Self::FedTheCat, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 37 | Self::Revising, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 38 | ]; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 39 | const CHRISTMAS: [Self; 6] = [ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 40 | Self::OnACall, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 41 | Self::WrappingPresents, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 42 | Self::FoldingTheWashing, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 43 | Self::HangingTheLights, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 44 | Self::HelpingNan, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 45 | Self::PeelingSprouts, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 46 | ]; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 47 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 48 | pub fn phrase(self) -> &'static str { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 49 | match self { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 50 | Self::Changing => "changing", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 51 | Self::Homework => "homework", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 52 | Self::PulledAMuscle => "pulled a muscle", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 53 | Self::Tidying => "tidying", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 54 | Self::TheWashingUp => "the washing up", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 55 | Self::FedTheCat => "fed the cat", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 56 | Self::Revising => "revising", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 57 | Self::OnACall => "on a call", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 58 | Self::WrappingPresents => "wrapping presents", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 59 | Self::FoldingTheWashing => "folding the washing", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 60 | Self::HangingTheLights => "hanging the lights", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 61 | Self::HelpingNan => "helping Nan", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 62 | Self::PeelingSprouts => "peeling sprouts", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 63 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 64 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 65 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 66 | fn all() -> impl Iterator<Item = Self> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 67 | Self::EVERYDAY.into_iter().chain(Self::CHRISTMAS) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 68 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 69 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 70 | fn parse(spoken: &str) -> Option<Self> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 71 | Self::all().find(|excuse| excuse.phrase().eq_ignore_ascii_case(spoken)) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 72 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 73 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 74 | |
c4d70621d377 | atagen | 2026-10-12 11:56 UTC | 75 | /// Every excuse Mum might hear, so a localised one can be traced back to the phrase she checks. |
c4d70621d377 | atagen | 2026-10-12 11:56 UTC | 76 | pub fn vocabulary() -> impl Iterator<Item = &'static str> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 77 | Excuse::all().map(Excuse::phrase) |
c4d70621d377 | atagen | 2026-10-12 11:56 UTC | 78 | } |
c4d70621d377 | atagen | 2026-10-12 11:56 UTC | 79 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 80 | fn is_truth(spoken: &str) -> bool { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 81 | ["the truth", "truth", "honesty"] |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 82 | .iter() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 83 | .any(|truth| truth.eq_ignore_ascii_case(spoken)) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 84 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 86 | /// Why Mum came to the door, and how she leaves once she believes you. |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 87 | #[derive(Clone, Copy)] |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 88 | pub struct Errand { |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 89 | pub(crate) arrival: &'static str, |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 90 | pub(crate) knock: &'static str, |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 91 | pub(crate) parting: &'static str, |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 92 | } |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 93 | |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 94 | macro_rules! errand { |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 95 | ($knock:literal, $parting:literal) => { |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 96 | Errand { |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 97 | arrival: concat!("Mum is at the door. ", $knock), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 98 | knock: $knock, |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 99 | parting: concat!("Mum seems satisfied. ", $parting), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 100 | } |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 101 | }; |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 102 | } |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 103 | |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 104 | pub const HEARD_A_NOISE: Errand = errand!("She heard a noise.", "It must have been the pipes."); |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 105 | const VIRAL: Errand = errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 106 | "A neighbour has sent her something.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 107 | "She has decided not to open the attachment." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 108 | ); |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 109 | const BEDROOM_ERRANDS: [Errand; 4] = [ |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 110 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 111 | "She has brought your washing.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 112 | "She leaves the washing on the chair." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 113 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 114 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 115 | "She wants to know if you have eaten.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 116 | "There will be a plate in the oven." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 117 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 118 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 119 | "She wants the wifi password.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 120 | "She has written the wifi password on her hand." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 121 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 122 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 123 | "She would like to talk about your future.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 124 | "The future can wait until dinner." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 125 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 126 | ]; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 127 | const CHRISTMAS_ERRANDS: [Errand; 4] = [ |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 128 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 129 | "Nan would like to see you.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 130 | "She tells Nan you are on your way." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 131 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 132 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 133 | "Christmas dinner is in ten minutes.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 134 | "She will save you a pig in a blanket." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 135 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 136 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 137 | "The King's speech is about to start.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 138 | "She will tell you what the King said." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 139 | ), |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 140 | errand!( |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 141 | "An uncle has asked where you went.", |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 142 | "She tells the uncle you are helping in the kitchen." |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 143 | ), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 144 | ]; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 145 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 146 | #[derive(Default)] |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 147 | pub struct Household { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 148 | pub(crate) mum: Option<MumVisit>, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 149 | /// Every excuse Mum has accepted this run. Digest Mum remembers them all. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 150 | pub(crate) heard: Vec<Excuse>, |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 151 | /// Set when the request now being answered sent Mum away, so the same turn cannot fetch her back. |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 152 | pub(crate) just_left: bool, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 153 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 154 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 155 | pub struct MumVisit { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 156 | pub(crate) errand: Errand, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 157 | pub(crate) nonce: String, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 158 | pub(crate) strikes: u8, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 159 | pub(crate) knocked: bool, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 160 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 161 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 162 | fn digest_params(authorization: &str) -> Option<HashMap<String, String>> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 163 | let (scheme, rest) = authorization.trim().split_once(' ')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 164 | if !scheme.eq_ignore_ascii_case("digest") { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 165 | return None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 166 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 167 | Some( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 168 | rest.split(',') |
782069860236 | atagen | 2026-10-10 12:44 UTC | 169 | .filter_map(|pair| { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 170 | let (key, value) = pair.split_once('=')?; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 171 | Some(( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 172 | key.trim().to_ascii_lowercase(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 173 | value.trim().trim_matches('"').to_owned(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 174 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 175 | }) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 176 | .collect(), |
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 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 180 | fn spoken(authorization: &str) -> &str { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 181 | authorization |
782069860236 | atagen | 2026-10-10 12:44 UTC | 182 | .split_once(' ') |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 183 | .map_or("", |(_, rest)| rest.trim().trim_matches('"')) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 184 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 185 | |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 186 | fn names_a_scheme(authorization: &str) -> bool { |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 187 | authorization |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 188 | .trim() |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 189 | .split_once(' ') |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 190 | .is_some_and(|(scheme, _)| { |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 191 | ["basic", "digest", "excuse"] |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 192 | .iter() |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 193 | .any(|known| known.eq_ignore_ascii_case(scheme)) |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 194 | }) |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 195 | } |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 196 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 197 | fn turned_away(code: &'static str, detail: &'static str) -> Problem { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 198 | Problem::new(StatusCode::UNAUTHORIZED, code, detail) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 199 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 200 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 201 | impl Run { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 202 | pub fn excuses(&self) -> Vec<Excuse> { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 203 | if self.floor.number() < Floor::Nans.number() { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 204 | return Excuse::EVERYDAY.to_vec(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 205 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 206 | Excuse::all() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 207 | .filter(|excuse| !self.household.heard.contains(excuse)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 208 | .collect() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 209 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 210 | |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 211 | pub(super) fn errand(&mut self, summoned: bool) -> Errand { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 212 | if summoned { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 213 | return HEARD_A_NOISE; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 214 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 215 | if self.online.filmed_at.is_some() |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 216 | && self.chance( |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 217 | self.decision("viral", "Has a neighbour already sent Mum the footage?"), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 218 | VIRAL_ERRAND, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 219 | ) |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 220 | { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 221 | return VIRAL; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 222 | } |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 223 | let errands = if self.floor == Floor::Nans { |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 224 | CHRISTMAS_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 225 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 226 | BEDROOM_ERRANDS |
782069860236 | atagen | 2026-10-10 12:44 UTC | 227 | }; |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 228 | let weights = vec![1; errands.len()]; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 229 | let knocks = errands.map(|errand| errand.knock); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 230 | let i = self.oracle.pick( |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 231 | self.decision("errand", "What has brought Mum to the door?"), |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 232 | &weights, |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 233 | &knocks, |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 234 | ); |
3f9091557ec3 | atagen | 2026-10-11 11:43 UTC | 235 | errands[i] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 236 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 237 | |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 238 | /// Mum answers every request on the run until she has been given something she believes. |
782069860236 | atagen | 2026-10-10 12:44 UTC | 239 | pub fn answer_door(&mut self, authorization: Option<&str>) -> Result<(), Problem> { |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 240 | self.household.just_left = false; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 241 | if self.household.mum.is_none() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 242 | return Ok(()); |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 243 | } |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 244 | match authorization { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 245 | None => Err(self.knock()), |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 246 | Some(authorization) => self.hear(authorization, self.floor == Floor::Nans), |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 247 | } |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 248 | } |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 249 | |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 250 | /// Sent on every response while she is at the door, so a client always holds the current nonce. |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 251 | pub fn mum_challenge(&self) -> Option<String> { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 252 | let visit = self |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 253 | .household |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 254 | .mum |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 255 | .as_ref() |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 256 | .filter(|_| self.ended.is_none())?; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 257 | Some(if self.floor == Floor::Nans { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 258 | format!("Digest realm=\"household\", nonce=\"{}\"", visit.nonce) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 259 | } else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 260 | "Excuse realm=\"household\"".to_owned() |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 261 | }) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 262 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 263 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 264 | /// The first knock is free. Every one after it is a strike. |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 265 | fn knock(&mut self) -> Problem { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 266 | let Some(visit) = &mut self.household.mum else { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 267 | unreachable!("knock is only called while Mum is at the door"); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 268 | }; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 269 | let errand = visit.errand.knock; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 270 | if !std::mem::replace(&mut visit.knocked, true) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 271 | self.touch(); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 272 | return Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 273 | StatusCode::UNAUTHORIZED, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 274 | "someone-at-the-door", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 275 | format!("Someone is at the door. {errand}"), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 276 | ) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 277 | .with("inventory", self.path("/inventory")); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 278 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 279 | self.strike( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 280 | Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 281 | StatusCode::UNAUTHORIZED, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 282 | "knocking-again", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 283 | format!("Mum is knocking again. {errand}"), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 284 | ), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 285 | Cause::Opened, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 286 | ) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 287 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 288 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 289 | fn hear(&mut self, authorization: &str, digest: bool) -> Result<(), Problem> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 290 | if is_truth(spoken(authorization)) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 291 | self.confess(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 292 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 293 | } |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 294 | if !names_a_scheme(authorization) { |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 295 | self.touch(); |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 296 | return Err(turned_away( |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 297 | "excuse-unheard", |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 298 | if digest { |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 299 | "Mum did not catch that. At Christmas she uses Digest, with the excuse as the username." |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 300 | } else { |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 301 | "Mum did not catch that. Excuses are given as Excuse \"homework\"." |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 302 | }, |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 303 | )); |
f16ab17c8ff6 | atagen | 2026-10-13 11:14 UTC | 304 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 305 | if parse_credentials(authorization, Scheme::Basic).is_some() { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 306 | return Err(self.mum_strike(turned_away( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 307 | "basic", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 308 | "Mum does not accept Basic authentication. Nobody should.", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 309 | ))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 310 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 311 | let said = if digest { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 312 | self.digest_username(authorization)? |
782069860236 | atagen | 2026-10-10 12:44 UTC | 313 | } else { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 314 | parse_credentials(authorization, Scheme::Excuse) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 315 | .unwrap_or_default() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 316 | .to_owned() |
782069860236 | atagen | 2026-10-10 12:44 UTC | 317 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 318 | if is_truth(&said) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 319 | self.confess(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 320 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 321 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 322 | let excuse = Excuse::parse(&said); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 323 | let true_now = self.body.conditions.pulled_muscle && excuse == Some(Excuse::PulledAMuscle); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 324 | if digest && !true_now && excuse.is_some_and(|e| self.household.heard.contains(&e)) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 325 | self.standing.shame += 3; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 326 | return Err(self.mum_strike(turned_away( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 327 | "excuse-replayed", |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 328 | "Mum has heard that one before. She would have preferred the truth.", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 329 | ))); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 330 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 331 | if let Some(excuse) = excuse.filter(|e| true_now || self.excuses().contains(e)) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 332 | self.household.heard.push(excuse); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 333 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 334 | return Ok(()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 335 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 336 | self.standing.shame += 3; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 337 | Err(self.mum_strike(turned_away( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 338 | "excuse-rejected", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 339 | if digest { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 340 | "Mum has not heard that one before. She does not believe it. She would have preferred the truth." |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 341 | } else { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 342 | "Mum has not heard that one before. She does not believe it." |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 343 | }, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 344 | ))) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 345 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 346 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 347 | fn digest_username(&mut self, authorization: &str) -> Result<String, Problem> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 348 | let Some(mut params) = digest_params(authorization) else { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 349 | return Err(self.mum_strike(turned_away( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 350 | "digest-required", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 351 | "Mum has started using Digest authentication.", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 352 | ))); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 353 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 354 | let current = self |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 355 | .household |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 356 | .mum |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 357 | .as_ref() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 358 | .is_some_and(|visit| params.get("nonce") == Some(&visit.nonce)); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 359 | if !current { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 360 | self.touch(); |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 361 | return Err(turned_away( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 362 | "stale-nonce", |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 363 | "That was a different conversation. Mum has started a new one, with a new nonce.", |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 364 | )); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 365 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 366 | Ok(params.remove("username").unwrap_or_default()) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 367 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 368 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 369 | fn confess(&mut self) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 370 | self.standing.shame += 10; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 371 | self.leave_door(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 372 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 373 | |
9683736a2c5e | atagen | 2026-10-10 13:15 UTC | 374 | fn mum_strike(&mut self, problem: Problem) -> Problem { |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 375 | self.strike(problem, Cause::Caught) |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 376 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 377 | |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 378 | fn strike(&mut self, problem: Problem, cause: Cause) -> Problem { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 379 | self.touch(); |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 380 | let Some(visit) = &mut self.household.mum else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 381 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 382 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 383 | visit.strikes += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 384 | if visit.strikes < PATIENCE { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 385 | return problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 386 | } |
0d604ee39751 | atagen | 2026-10-10 14:15 UTC | 387 | self.end(cause) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 388 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 389 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 390 | fn leave_door(&mut self) { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 391 | if let Some(visit) = self.household.mum.take() { |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 392 | self.notices |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 393 | .push(Notice::MumSatisfied(visit.errand.parting)); |
2176d9a94bd9 | atagen | 2026-10-14 11:03 UTC | 394 | self.household.just_left = true; |
886159fd0fe7 | atagen | 2026-10-13 12:48 UTC | 395 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 396 | self.standing.suspicion = 0; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 397 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 398 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 399 | } |