atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/game/clinic.rs

782069860236atagen2026-10-10 12:44 UTC1
use axum::http::{StatusCode, header};
782069860236atagen2026-10-10 12:44 UTC2
use serde_json::json;
782069860236atagen2026-10-10 12:44 UTC3
8b8e83329a1datagen2026-10-12 12:01 UTC4
use super::{
8b8e83329a1datagen2026-10-12 12:01 UTC5
    Floor, Outcome, Run,
8b8e83329a1datagen2026-10-12 12:01 UTC6
    credentials::{Scheme, etag_matches, parse_credentials},
8b8e83329a1datagen2026-10-12 12:01 UTC7
    measure::{round2, volume},
8b8e83329a1datagen2026-10-12 12:01 UTC8
};
058650ec79d6atagen2026-10-12 10:49 UTC9
use crate::{
058650ec79d6atagen2026-10-12 10:49 UTC10
    headers::{SERVER_TIMING, X_DIAGNOSIS},
058650ec79d6atagen2026-10-12 10:49 UTC11
    problem::Problem,
058650ec79d6atagen2026-10-12 10:49 UTC12
};
782069860236atagen2026-10-10 12:44 UTC13
8b8e83329a1datagen2026-10-12 12:01 UTC14
#[derive(Default)]
8b8e83329a1datagen2026-10-12 12:01 UTC15
pub struct Clinic {
8b8e83329a1datagen2026-10-12 12:01 UTC16
    pub(crate) intake_attempts: usize,
8b8e83329a1datagen2026-10-12 12:01 UTC17
    pub(crate) measured: bool,
8b8e83329a1datagen2026-10-12 12:01 UTC18
    pub(crate) prescribed: bool,
8b8e83329a1datagen2026-10-12 12:01 UTC19
    pub(crate) cured: bool,
8b8e83329a1datagen2026-10-12 12:01 UTC20
}
8b8e83329a1datagen2026-10-12 12:01 UTC21
782069860236atagen2026-10-10 12:44 UTC22
const INTAKE: [&str; 3] = [
782069860236atagen2026-10-10 12:44 UTC23
    "The clinic does not accept excuses.",
782069860236atagen2026-10-10 12:44 UTC24
    "The clinic accepts one token. You have not been carrying it.",
782069860236atagen2026-10-10 12:44 UTC25
    "Be honest.",
782069860236atagen2026-10-10 12:44 UTC26
];
782069860236atagen2026-10-10 12:44 UTC27
782069860236atagen2026-10-10 12:44 UTC28
impl Run {
782069860236atagen2026-10-10 12:44 UTC29
    pub fn diagnose(
782069860236atagen2026-10-10 12:44 UTC30
        &mut self,
782069860236atagen2026-10-10 12:44 UTC31
        authorization: Option<&str>,
782069860236atagen2026-10-10 12:44 UTC32
        if_none_match: Option<&str>,
782069860236atagen2026-10-10 12:44 UTC33
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC34
        if self.floor != Floor::Clinic {
782069860236atagen2026-10-10 12:44 UTC35
            let floors = Floor::Clinic.number() - self.floor.number();
782069860236atagen2026-10-10 12:44 UTC36
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC37
                StatusCode::TOO_EARLY,
782069860236atagen2026-10-10 12:44 UTC38
                "no-appointment",
782069860236atagen2026-10-10 12:44 UTC39
                format!("The clinic's next available appointment is {floors} floors away."),
782069860236atagen2026-10-10 12:44 UTC40
            ));
782069860236atagen2026-10-10 12:44 UTC41
        }
782069860236atagen2026-10-10 12:44 UTC42
        let intake = Problem::new(
782069860236atagen2026-10-10 12:44 UTC43
            StatusCode::UNAUTHORIZED,
782069860236atagen2026-10-10 12:44 UTC44
            "intake",
9683736a2c5eatagen2026-10-10 13:15 UTC45
            "The clinic requires authorisation.",
782069860236atagen2026-10-10 12:44 UTC46
        );
782069860236atagen2026-10-10 12:44 UTC47
        let Some(authorization) = authorization else {
782069860236atagen2026-10-10 12:44 UTC48
            return Err(intake.header(header::WWW_AUTHENTICATE, "Bearer realm=\"clinic\""));
782069860236atagen2026-10-10 12:44 UTC49
        };
8b8e83329a1datagen2026-10-12 12:01 UTC50
        let truthful = parse_credentials(authorization, Scheme::Bearer)
782069860236atagen2026-10-10 12:44 UTC51
            .is_some_and(|token| token.eq_ignore_ascii_case("honesty"));
782069860236atagen2026-10-10 12:44 UTC52
        if !truthful {
782069860236atagen2026-10-10 12:44 UTC53
            let hint = INTAKE[self.clinic.intake_attempts.min(INTAKE.len() - 1)];
782069860236atagen2026-10-10 12:44 UTC54
            self.clinic.intake_attempts += 1;
782069860236atagen2026-10-10 12:44 UTC55
            return Err(intake.header(
782069860236atagen2026-10-10 12:44 UTC56
                header::WWW_AUTHENTICATE,
782069860236atagen2026-10-10 12:44 UTC57
                format!(
782069860236atagen2026-10-10 12:44 UTC58
                    "Bearer realm=\"clinic\", error=\"invalid_token\", error_description=\"{hint}\""
782069860236atagen2026-10-10 12:44 UTC59
                ),
782069860236atagen2026-10-10 12:44 UTC60
            ));
782069860236atagen2026-10-10 12:44 UTC61
        }
782069860236atagen2026-10-10 12:44 UTC62
8b8e83329a1datagen2026-10-12 12:01 UTC63
        self.body.conditions.peyronie = false;
8b8e83329a1datagen2026-10-12 12:01 UTC64
        self.body.conditions.priapism_turns = 0;
782069860236atagen2026-10-10 12:44 UTC65
        let etag = self.origin_etag();
782069860236atagen2026-10-10 12:44 UTC66
        if if_none_match.is_some_and(|inm| etag_matches(inm, &etag)) {
782069860236atagen2026-10-10 12:44 UTC67
            self.clinic.cured = true;
782069860236atagen2026-10-10 12:44 UTC68
            return Ok(Outcome::empty(StatusCode::NOT_MODIFIED)
782069860236atagen2026-10-10 12:44 UTC69
                .header(header::ETAG, etag)
782069860236atagen2026-10-10 12:44 UTC70
                .header(X_DIAGNOSIS, "normal"));
782069860236atagen2026-10-10 12:44 UTC71
        }
782069860236atagen2026-10-10 12:44 UTC72
        if !self.clinic.measured {
782069860236atagen2026-10-10 12:44 UTC73
            self.clinic.measured = true;
8b8e83329a1datagen2026-10-12 12:01 UTC74
            if !self.items.kit.ruler {
8b8e83329a1datagen2026-10-12 12:01 UTC75
                self.standing.shame += 15;
782069860236atagen2026-10-10 12:44 UTC76
            }
782069860236atagen2026-10-10 12:44 UTC77
        }
8b8e83329a1datagen2026-10-12 12:01 UTC78
        let volume = volume(self.body.length, self.body.girth);
8b8e83329a1datagen2026-10-12 12:01 UTC79
        let foreskin = self.body.foreskin.unwrap_or(0.0);
058650ec79d6atagen2026-10-12 10:49 UTC80
        let timing = format!(
058650ec79d6atagen2026-10-12 10:49 UTC81
            "length;dur={:.2}, girth;dur={:.2}, foreskin;dur={foreskin:.2}, volume;desc=\"cubic inches\";dur={volume:.2}",
8b8e83329a1datagen2026-10-12 12:01 UTC82
            self.body.length, self.body.girth
058650ec79d6atagen2026-10-12 10:49 UTC83
        );
c4d70621d377atagen2026-10-12 11:56 UTC84
        self.clinic.prescribed = true;
c4d70621d377atagen2026-10-12 11:56 UTC85
        self.touch();
782069860236atagen2026-10-10 12:44 UTC86
        Ok(Outcome::json(
782069860236atagen2026-10-10 12:44 UTC87
            StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC88
            json!({
782069860236atagen2026-10-10 12:44 UTC89
                "diagnosis": "within normal range",
c4d70621d377atagen2026-10-12 11:56 UTC90
                "detail": "Congratulations. You have been cured of a condition you never had. The run is over.",
8b8e83329a1datagen2026-10-12 12:01 UTC91
                "measured_length": round2(self.body.length),
8b8e83329a1datagen2026-10-12 12:01 UTC92
                "measured_girth": round2(self.body.girth),
058650ec79d6atagen2026-10-12 10:49 UTC93
                "volume": round2(volume),
782069860236atagen2026-10-10 12:44 UTC94
                "prescription": "Please stop.",
782069860236atagen2026-10-10 12:44 UTC95
            }),
782069860236atagen2026-10-10 12:44 UTC96
        )
058650ec79d6atagen2026-10-12 10:49 UTC97
        .header(header::ETAG, etag)
058650ec79d6atagen2026-10-12 10:49 UTC98
        .header(SERVER_TIMING, timing))
782069860236atagen2026-10-10 12:44 UTC99
    }
782069860236atagen2026-10-10 12:44 UTC100
}