atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
a8269407e054211fed82e710616e72cfa4f72961
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/clinic.rsRust100 lines3.4 KB
1
use axum::http::{StatusCode, header};
2
use serde_json::json;
3
4
use super::{
5
    Floor, Outcome, Run,
6
    credentials::{Scheme, etag_matches, parse_credentials},
7
    measure::{round2, volume},
8
};
9
use crate::{
10
    headers::{SERVER_TIMING, X_DIAGNOSIS},
11
    problem::Problem,
12
};
13
14
#[derive(Default)]
15
pub struct Clinic {
16
    pub(crate) intake_attempts: usize,
17
    pub(crate) measured: bool,
18
    pub(crate) prescribed: bool,
19
    pub(crate) cured: bool,
20
}
21
22
const INTAKE: [&str; 3] = [
23
    "The clinic does not accept excuses.",
24
    "The clinic accepts one token. You have not been carrying it.",
25
    "Be honest.",
26
];
27
28
impl Run {
29
    pub fn diagnose(
30
        &mut self,
31
        authorization: Option<&str>,
32
        if_none_match: Option<&str>,
33
    ) -> Result<Outcome, Problem> {
34
        if self.floor != Floor::Clinic {
35
            let floors = Floor::Clinic.number() - self.floor.number();
36
            return Err(Problem::new(
37
                StatusCode::TOO_EARLY,
38
                "no-appointment",
39
                format!("The clinic's next available appointment is {floors} floors away."),
40
            ));
41
        }
42
        let intake = Problem::new(
43
            StatusCode::UNAUTHORIZED,
44
            "intake",
45
            "The clinic requires authorisation.",
46
        );
47
        let Some(authorization) = authorization else {
48
            return Err(intake.header(header::WWW_AUTHENTICATE, "Bearer realm=\"clinic\""));
49
        };
50
        let truthful = parse_credentials(authorization, Scheme::Bearer)
51
            .is_some_and(|token| token.eq_ignore_ascii_case("honesty"));
52
        if !truthful {
53
            let hint = INTAKE[self.clinic.intake_attempts.min(INTAKE.len() - 1)];
54
            self.clinic.intake_attempts += 1;
55
            return Err(intake.header(
56
                header::WWW_AUTHENTICATE,
57
                format!(
58
                    "Bearer realm=\"clinic\", error=\"invalid_token\", error_description=\"{hint}\""
59
                ),
60
            ));
61
        }
62
63
        self.body.conditions.peyronie = false;
64
        self.body.conditions.priapism_turns = 0;
65
        let etag = self.origin_etag();
66
        if if_none_match.is_some_and(|inm| etag_matches(inm, &etag)) {
67
            self.clinic.cured = true;
68
            return Ok(Outcome::empty(StatusCode::NOT_MODIFIED)
69
                .header(header::ETAG, etag)
70
                .header(X_DIAGNOSIS, "normal"));
71
        }
72
        if !self.clinic.measured {
73
            self.clinic.measured = true;
74
            if !self.items.kit.ruler {
75
                self.standing.shame += 15;
76
            }
77
        }
78
        let volume = volume(self.body.length, self.body.girth);
79
        let foreskin = self.body.foreskin.unwrap_or(0.0);
80
        let timing = format!(
81
            "length;dur={:.2}, girth;dur={:.2}, foreskin;dur={foreskin:.2}, volume;desc=\"cubic inches\";dur={volume:.2}",
82
            self.body.length, self.body.girth
83
        );
84
        self.clinic.prescribed = true;
85
        self.touch();
86
        Ok(Outcome::json(
87
            StatusCode::OK,
88
            json!({
89
                "diagnosis": "within normal range",
90
                "detail": "Congratulations. You have been cured of a condition you never had. The run is over.",
91
                "measured_length": round2(self.body.length),
92
                "measured_girth": round2(self.body.girth),
93
                "volume": round2(volume),
94
                "prescription": "Please stop.",
95
            }),
96
        )
97
        .header(header::ETAG, etag)
98
        .header(SERVER_TIMING, timing))
99
    }
100
}