atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
0d604ee3975169bdb2d104b1e7423ace1c71e71a
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/game/household.rs

782069860236atagen2026-10-10 12:44 UTC1
use std::collections::HashMap;
782069860236atagen2026-10-10 12:44 UTC2
782069860236atagen2026-10-10 12:44 UTC3
use axum::http::{StatusCode, header};
782069860236atagen2026-10-10 12:44 UTC4
use rand::Rng;
782069860236atagen2026-10-10 12:44 UTC5
782069860236atagen2026-10-10 12:44 UTC6
use super::{
782069860236atagen2026-10-10 12:44 UTC7
    Cause, Floor, Run,
782069860236atagen2026-10-10 12:44 UTC8
    balance::{PATIENCE, VIRAL_ERRAND},
782069860236atagen2026-10-10 12:44 UTC9
    parse_credentials,
782069860236atagen2026-10-10 12:44 UTC10
};
782069860236atagen2026-10-10 12:44 UTC11
use crate::problem::Problem;
782069860236atagen2026-10-10 12:44 UTC12
782069860236atagen2026-10-10 12:44 UTC13
const EXCUSES: [&str; 3] = ["changing", "homework", "pulled a muscle"];
9683736a2c5eatagen2026-10-10 13:15 UTC14
const CHRISTMAS_EXCUSES: [&str; 3] = ["on a call", "wrapping presents", "folding the washing"];
782069860236atagen2026-10-10 12:44 UTC15
const TRUTHS: [&str; 3] = ["the truth", "truth", "honesty"];
782069860236atagen2026-10-10 12:44 UTC16
const TRUE_WHEN_PULLED: &str = "pulled a muscle";
782069860236atagen2026-10-10 12:44 UTC17
782069860236atagen2026-10-10 12:44 UTC18
const BEDROOM_ERRANDS: [&str; 4] = [
9683736a2c5eatagen2026-10-10 13:15 UTC19
    "She has brought your washing.",
782069860236atagen2026-10-10 12:44 UTC20
    "She wants to know if you have eaten.",
782069860236atagen2026-10-10 12:44 UTC21
    "She needs the wifi password.",
782069860236atagen2026-10-10 12:44 UTC22
    "She would like to talk about your future.",
782069860236atagen2026-10-10 12:44 UTC23
];
9683736a2c5eatagen2026-10-10 13:15 UTC24
const CHRISTMAS_ERRANDS: [&str; 4] = [
9683736a2c5eatagen2026-10-10 13:15 UTC25
    "Nan would like to see you.",
9683736a2c5eatagen2026-10-10 13:15 UTC26
    "Christmas dinner is in ten minutes.",
9683736a2c5eatagen2026-10-10 13:15 UTC27
    "The King's speech is about to start.",
782069860236atagen2026-10-10 12:44 UTC28
    "An uncle has asked where you went.",
782069860236atagen2026-10-10 12:44 UTC29
];
782069860236atagen2026-10-10 12:44 UTC30
782069860236atagen2026-10-10 12:44 UTC31
fn digest_params(authorization: &str) -> Option<HashMap<String, String>> {
782069860236atagen2026-10-10 12:44 UTC32
    let (scheme, rest) = authorization.trim().split_once(' ')?;
782069860236atagen2026-10-10 12:44 UTC33
    if !scheme.eq_ignore_ascii_case("digest") {
782069860236atagen2026-10-10 12:44 UTC34
        return None;
782069860236atagen2026-10-10 12:44 UTC35
    }
782069860236atagen2026-10-10 12:44 UTC36
    Some(
782069860236atagen2026-10-10 12:44 UTC37
        rest.split(',')
782069860236atagen2026-10-10 12:44 UTC38
            .filter_map(|pair| {
782069860236atagen2026-10-10 12:44 UTC39
                let (key, value) = pair.split_once('=')?;
782069860236atagen2026-10-10 12:44 UTC40
                Some((
782069860236atagen2026-10-10 12:44 UTC41
                    key.trim().to_ascii_lowercase(),
782069860236atagen2026-10-10 12:44 UTC42
                    value.trim().trim_matches('"').to_owned(),
782069860236atagen2026-10-10 12:44 UTC43
                ))
782069860236atagen2026-10-10 12:44 UTC44
            })
782069860236atagen2026-10-10 12:44 UTC45
            .collect(),
782069860236atagen2026-10-10 12:44 UTC46
    )
782069860236atagen2026-10-10 12:44 UTC47
}
782069860236atagen2026-10-10 12:44 UTC48
782069860236atagen2026-10-10 12:44 UTC49
fn spoken(authorization: &str) -> String {
782069860236atagen2026-10-10 12:44 UTC50
    authorization
782069860236atagen2026-10-10 12:44 UTC51
        .split_once(' ')
782069860236atagen2026-10-10 12:44 UTC52
        .map(|(_, rest)| rest.trim().trim_matches('"').to_ascii_lowercase())
782069860236atagen2026-10-10 12:44 UTC53
        .unwrap_or_default()
782069860236atagen2026-10-10 12:44 UTC54
}
782069860236atagen2026-10-10 12:44 UTC55
782069860236atagen2026-10-10 12:44 UTC56
impl Run {
782069860236atagen2026-10-10 12:44 UTC57
    pub fn excuses(&self) -> Vec<&'static str> {
9683736a2c5eatagen2026-10-10 13:15 UTC58
        if self.floor.number() < Floor::Nans.number() {
782069860236atagen2026-10-10 12:44 UTC59
            return EXCUSES.to_vec();
782069860236atagen2026-10-10 12:44 UTC60
        }
782069860236atagen2026-10-10 12:44 UTC61
        EXCUSES
782069860236atagen2026-10-10 12:44 UTC62
            .into_iter()
9683736a2c5eatagen2026-10-10 13:15 UTC63
            .chain(CHRISTMAS_EXCUSES)
782069860236atagen2026-10-10 12:44 UTC64
            .filter(|e| !self.heard.iter().any(|h| h == e))
782069860236atagen2026-10-10 12:44 UTC65
            .collect()
782069860236atagen2026-10-10 12:44 UTC66
    }
782069860236atagen2026-10-10 12:44 UTC67
782069860236atagen2026-10-10 12:44 UTC68
    pub(super) fn errand(&mut self, summoned: bool) -> &'static str {
782069860236atagen2026-10-10 12:44 UTC69
        if summoned {
782069860236atagen2026-10-10 12:44 UTC70
            return "She heard a noise.";
782069860236atagen2026-10-10 12:44 UTC71
        }
782069860236atagen2026-10-10 12:44 UTC72
        if self.filmed_at.is_some() && self.chance(VIRAL_ERRAND) {
782069860236atagen2026-10-10 12:44 UTC73
            return "A neighbour has sent her something.";
782069860236atagen2026-10-10 12:44 UTC74
        }
9683736a2c5eatagen2026-10-10 13:15 UTC75
        let errands = if self.floor == Floor::Nans {
9683736a2c5eatagen2026-10-10 13:15 UTC76
            CHRISTMAS_ERRANDS
782069860236atagen2026-10-10 12:44 UTC77
        } else {
782069860236atagen2026-10-10 12:44 UTC78
            BEDROOM_ERRANDS
782069860236atagen2026-10-10 12:44 UTC79
        };
782069860236atagen2026-10-10 12:44 UTC80
        errands[self.rng.random_range(0..errands.len())]
782069860236atagen2026-10-10 12:44 UTC81
    }
782069860236atagen2026-10-10 12:44 UTC82
9683736a2c5eatagen2026-10-10 13:15 UTC83
    /// Mum answers every request on the run until she has been given something she believes.
782069860236atagen2026-10-10 12:44 UTC84
    pub fn answer_door(&mut self, authorization: Option<&str>) -> Result<(), Problem> {
9683736a2c5eatagen2026-10-10 13:15 UTC85
        let Some((errand, nonce)) = self.mum.as_ref().map(|v| (v.errand, v.nonce.clone())) else {
782069860236atagen2026-10-10 12:44 UTC86
            return Ok(());
782069860236atagen2026-10-10 12:44 UTC87
        };
9683736a2c5eatagen2026-10-10 13:15 UTC88
        let digest = self.floor == Floor::Nans;
782069860236atagen2026-10-10 12:44 UTC89
        let challenge_value = if digest {
782069860236atagen2026-10-10 12:44 UTC90
            format!("Digest realm=\"household\", nonce=\"{nonce}\"")
782069860236atagen2026-10-10 12:44 UTC91
        } else {
782069860236atagen2026-10-10 12:44 UTC92
            "Excuse realm=\"household\"".to_owned()
782069860236atagen2026-10-10 12:44 UTC93
        };
782069860236atagen2026-10-10 12:44 UTC94
        let challenge = |p: Problem| p.header(header::WWW_AUTHENTICATE, &challenge_value);
782069860236atagen2026-10-10 12:44 UTC95
782069860236atagen2026-10-10 12:44 UTC96
        let Some(authorization) = authorization else {
0d604ee39751atagen2026-10-10 14:15 UTC97
            let knocked_before = self.mum.as_ref().is_some_and(|visit| visit.knocked);
0d604ee39751atagen2026-10-10 14:15 UTC98
            if let Some(visit) = &mut self.mum {
0d604ee39751atagen2026-10-10 14:15 UTC99
                visit.knocked = true;
0d604ee39751atagen2026-10-10 14:15 UTC100
            }
0d604ee39751atagen2026-10-10 14:15 UTC101
            if !knocked_before {
0d604ee39751atagen2026-10-10 14:15 UTC102
                self.touch();
0d604ee39751atagen2026-10-10 14:15 UTC103
                return Err(challenge(
0d604ee39751atagen2026-10-10 14:15 UTC104
                    Problem::new(
0d604ee39751atagen2026-10-10 14:15 UTC105
                        StatusCode::UNAUTHORIZED,
0d604ee39751atagen2026-10-10 14:15 UTC106
                        "someone-at-the-door",
0d604ee39751atagen2026-10-10 14:15 UTC107
                        format!("Someone is at the door. {errand}"),
0d604ee39751atagen2026-10-10 14:15 UTC108
                    )
0d604ee39751atagen2026-10-10 14:15 UTC109
                    .with("inventory", self.path("/inventory")),
0d604ee39751atagen2026-10-10 14:15 UTC110
                ));
0d604ee39751atagen2026-10-10 14:15 UTC111
            }
0d604ee39751atagen2026-10-10 14:15 UTC112
            return Err(self.strike(
0d604ee39751atagen2026-10-10 14:15 UTC113
                challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC114
                    StatusCode::UNAUTHORIZED,
0d604ee39751atagen2026-10-10 14:15 UTC115
                    "knocking-again",
0d604ee39751atagen2026-10-10 14:15 UTC116
                    format!("Mum is knocking again. {errand}"),
0d604ee39751atagen2026-10-10 14:15 UTC117
                )),
0d604ee39751atagen2026-10-10 14:15 UTC118
                Cause::Opened,
782069860236atagen2026-10-10 12:44 UTC119
            ));
782069860236atagen2026-10-10 12:44 UTC120
        };
782069860236atagen2026-10-10 12:44 UTC121
782069860236atagen2026-10-10 12:44 UTC122
        if TRUTHS.contains(&spoken(authorization).as_str()) {
782069860236atagen2026-10-10 12:44 UTC123
            self.shame += 10;
782069860236atagen2026-10-10 12:44 UTC124
            self.leave_door();
782069860236atagen2026-10-10 12:44 UTC125
            return Ok(());
782069860236atagen2026-10-10 12:44 UTC126
        }
782069860236atagen2026-10-10 12:44 UTC127
        if parse_credentials(authorization, "basic").is_some() {
9683736a2c5eatagen2026-10-10 13:15 UTC128
            return Err(self.mum_strike(challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC129
                StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC130
                "basic",
9683736a2c5eatagen2026-10-10 13:15 UTC131
                "Mum does not accept Basic authentication. Nobody should.",
782069860236atagen2026-10-10 12:44 UTC132
            ))));
782069860236atagen2026-10-10 12:44 UTC133
        }
782069860236atagen2026-10-10 12:44 UTC134
782069860236atagen2026-10-10 12:44 UTC135
        let excuse = if digest {
782069860236atagen2026-10-10 12:44 UTC136
            let Some(params) = digest_params(authorization) else {
9683736a2c5eatagen2026-10-10 13:15 UTC137
                return Err(self.mum_strike(challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC138
                    StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC139
                    "digest-required",
9683736a2c5eatagen2026-10-10 13:15 UTC140
                    "Mum has started using Digest authentication.",
782069860236atagen2026-10-10 12:44 UTC141
                ))));
782069860236atagen2026-10-10 12:44 UTC142
            };
782069860236atagen2026-10-10 12:44 UTC143
            if params.get("nonce") != Some(&nonce) {
9683736a2c5eatagen2026-10-10 13:15 UTC144
                return Err(self.mum_strike(challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC145
                    StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC146
                    "stale-nonce",
782069860236atagen2026-10-10 12:44 UTC147
                    "That was a different conversation.",
782069860236atagen2026-10-10 12:44 UTC148
                ))));
782069860236atagen2026-10-10 12:44 UTC149
            }
782069860236atagen2026-10-10 12:44 UTC150
            params.get("username").cloned().unwrap_or_default()
782069860236atagen2026-10-10 12:44 UTC151
        } else {
782069860236atagen2026-10-10 12:44 UTC152
            parse_credentials(authorization, "excuse")
782069860236atagen2026-10-10 12:44 UTC153
                .unwrap_or_default()
782069860236atagen2026-10-10 12:44 UTC154
                .to_owned()
782069860236atagen2026-10-10 12:44 UTC155
        };
782069860236atagen2026-10-10 12:44 UTC156
        let excuse = excuse.to_ascii_lowercase();
782069860236atagen2026-10-10 12:44 UTC157
782069860236atagen2026-10-10 12:44 UTC158
        if TRUTHS.contains(&excuse.as_str()) {
782069860236atagen2026-10-10 12:44 UTC159
            self.shame += 10;
782069860236atagen2026-10-10 12:44 UTC160
            self.leave_door();
782069860236atagen2026-10-10 12:44 UTC161
            return Ok(());
782069860236atagen2026-10-10 12:44 UTC162
        }
782069860236atagen2026-10-10 12:44 UTC163
        let true_now = self.conditions.pulled_muscle && excuse == TRUE_WHEN_PULLED;
782069860236atagen2026-10-10 12:44 UTC164
        if digest && self.heard.contains(&excuse) && !true_now {
782069860236atagen2026-10-10 12:44 UTC165
            self.shame += 3;
9683736a2c5eatagen2026-10-10 13:15 UTC166
            return Err(self.mum_strike(challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC167
                StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC168
                "excuse-replayed",
9683736a2c5eatagen2026-10-10 13:15 UTC169
                "Mum has heard that one before. She would have preferred the truth.",
782069860236atagen2026-10-10 12:44 UTC170
            ))));
782069860236atagen2026-10-10 12:44 UTC171
        }
782069860236atagen2026-10-10 12:44 UTC172
        if true_now || self.excuses().contains(&excuse.as_str()) {
782069860236atagen2026-10-10 12:44 UTC173
            self.heard.push(excuse);
782069860236atagen2026-10-10 12:44 UTC174
            self.leave_door();
782069860236atagen2026-10-10 12:44 UTC175
            return Ok(());
782069860236atagen2026-10-10 12:44 UTC176
        }
782069860236atagen2026-10-10 12:44 UTC177
        self.shame += 3;
782069860236atagen2026-10-10 12:44 UTC178
        let detail = if digest {
9683736a2c5eatagen2026-10-10 13:15 UTC179
            "Mum has not heard that one before. She does not believe it. She would have preferred the truth."
782069860236atagen2026-10-10 12:44 UTC180
        } else {
9683736a2c5eatagen2026-10-10 13:15 UTC181
            "Mum has not heard that one before. She does not believe it."
782069860236atagen2026-10-10 12:44 UTC182
        };
9683736a2c5eatagen2026-10-10 13:15 UTC183
        Err(self.mum_strike(challenge(Problem::new(
782069860236atagen2026-10-10 12:44 UTC184
            StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC185
            "excuse-rejected",
782069860236atagen2026-10-10 12:44 UTC186
            detail,
782069860236atagen2026-10-10 12:44 UTC187
        ))))
782069860236atagen2026-10-10 12:44 UTC188
    }
782069860236atagen2026-10-10 12:44 UTC189
9683736a2c5eatagen2026-10-10 13:15 UTC190
    fn mum_strike(&mut self, problem: Problem) -> Problem {
0d604ee39751atagen2026-10-10 14:15 UTC191
        self.strike(problem, Cause::Caught)
0d604ee39751atagen2026-10-10 14:15 UTC192
    }
0d604ee39751atagen2026-10-10 14:15 UTC193
0d604ee39751atagen2026-10-10 14:15 UTC194
    fn strike(&mut self, problem: Problem, cause: Cause) -> Problem {
782069860236atagen2026-10-10 12:44 UTC195
        self.touch();
9683736a2c5eatagen2026-10-10 13:15 UTC196
        let Some(visit) = &mut self.mum else {
782069860236atagen2026-10-10 12:44 UTC197
            return problem;
782069860236atagen2026-10-10 12:44 UTC198
        };
782069860236atagen2026-10-10 12:44 UTC199
        visit.strikes += 1;
782069860236atagen2026-10-10 12:44 UTC200
        if visit.strikes < PATIENCE {
782069860236atagen2026-10-10 12:44 UTC201
            return problem;
782069860236atagen2026-10-10 12:44 UTC202
        }
0d604ee39751atagen2026-10-10 14:15 UTC203
        self.end(cause)
782069860236atagen2026-10-10 12:44 UTC204
    }
782069860236atagen2026-10-10 12:44 UTC205
782069860236atagen2026-10-10 12:44 UTC206
    fn leave_door(&mut self) {
9683736a2c5eatagen2026-10-10 13:15 UTC207
        self.mum = None;
782069860236atagen2026-10-10 12:44 UTC208
        self.suspicion = 0;
782069860236atagen2026-10-10 12:44 UTC209
        self.touch();
782069860236atagen2026-10-10 12:44 UTC210
    }
782069860236atagen2026-10-10 12:44 UTC211
}