atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
fcc6cf6d5edcfdaf719c2b9cd40c6b77532340e6
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/checks.rsRust156 lines5.9 KB
1
use super::{
2
    LUBRICANTS, Lube, Run, Technique,
3
    balance::STORAGE_BULK,
4
    lube::LubeRule,
5
    techniques::{Training, Utility},
6
};
7
use crate::problem::Problem;
8
use axum::http::{StatusCode, header};
9
use serde_json::json;
10
11
impl Run {
12
    pub(super) fn ready(&self, technique: Technique, lube: Option<Lube>) -> Result<(), Problem> {
13
        self.access(technique)?;
14
        match technique {
15
            Technique::Training(training) => Self::check_lube(training, lube),
16
            Technique::Utility(_) => Ok(()),
17
        }
18
    }
19
20
    pub(super) fn access(&self, technique: Technique) -> Result<(), Problem> {
21
        if technique == Technique::Training(Training::Uli) && !self.online.uli_taught {
22
            return Err(Problem::new(
23
                StatusCode::FORBIDDEN,
24
                "technique-locked",
25
                "uli is discussed on the forum, in a thread from 2009, by a man who never posted again. You have not found the reply.",
26
            ));
27
        }
28
        if matches!(technique, Technique::Training(_)) {
29
            self.grip_closes(self.body.grip)?;
30
        }
31
        match technique {
32
            Technique::Utility(Utility::Pump) if !self.items.kit.pump_pro => Err(Problem::new(
33
                StatusCode::FAILED_DEPENDENCY,
34
                "no-pump",
35
                "The pump technique depends on a pump.",
36
            )),
37
            Technique::Utility(_) => Ok(()),
38
            Technique::Training(training) => match training.profile().foreskin {
39
                Some((min, max)) => self.check_foreskin(min, max),
40
                None => Ok(()),
41
            },
42
        }
43
    }
44
45
    pub(super) fn check_lube(training: Training, lube: Option<Lube>) -> Result<(), Problem> {
46
        match (training.profile().lube, lube) {
47
            (LubeRule::Required, None) => Err(Problem::new(
48
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
49
                "lubricant-required",
50
                format!(
51
                    "The {} technique requires a lubricant. None was declared.",
52
                    training.name()
53
                ),
54
            )
55
            .with("lubricants", json!(LUBRICANTS))),
56
            (LubeRule::Forbidden, Some(_)) => Err(Problem::new(
57
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
58
                "lubricant-declared",
59
                format!(
60
                    "The {} technique was requested. A lubricant was declared.",
61
                    training.name()
62
                ),
63
            )),
64
            _ => Ok(()),
65
        }
66
    }
67
68
    /// One unit of whatever was declared. Spit is free and the tap is always on.
69
    pub(super) fn lube_available(&self, lube: Option<Lube>) -> Result<(), Problem> {
70
        let Some(bottle) = lube.and_then(Lube::item) else {
71
            return Ok(());
72
        };
73
        if !self.items.has(bottle) {
74
            return Err(Problem::new(
75
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
76
                "lubricant-exhausted",
77
                format!("{} was declared, but the bottle is empty.", bottle.slug()),
78
            ));
79
        }
80
        Ok(())
81
    }
82
83
    pub(super) fn take_lube(&mut self, lube: Option<Lube>) -> Result<(), Problem> {
84
        self.lube_available(lube)?;
85
        if let Some(lube) = lube {
86
            self.items.lubes.take(lube);
87
        }
88
        Ok(())
89
    }
90
91
    pub fn techniques_now(&self) -> impl Iterator<Item = Technique> + '_ {
92
        let blocked = !self.floor.timed() || self.items.kit.cage || self.body.exhausted;
93
        Technique::ALL
94
            .into_iter()
95
            .filter(move |_| !blocked)
96
            .filter(|&technique| match technique {
97
                Technique::Utility(Utility::Warmup) => {
98
                    !self.here.towel_warmed || self.here.kitchen.kettle_boiled
99
                }
100
                Technique::Utility(Utility::Ice) => self.items.stores.ice_packs > 0,
101
                Technique::Utility(Utility::Supplement) => self.items.stores.supplements > 0,
102
                Technique::Utility(Utility::Meditate) => {
103
                    self.online.meditation_taught && !self.here.meditated
104
                }
105
                Technique::Utility(Utility::Energy) => self.items.stores.energy_drinks > 0,
106
                _ => self.access(technique).is_ok() && self.check_storage().is_ok(),
107
            })
108
    }
109
110
    pub(super) fn check_foreskin(&self, min: f64, max: f64) -> Result<(), Problem> {
111
        let Some(f) = self.body.foreskin else {
112
            if min <= 0.0 {
113
                return Ok(());
114
            }
115
            return Err(Problem::new(
116
                StatusCode::GONE,
117
                "foreskin-gone",
118
                "This technique requires a foreskin. The foreskin is gone.",
119
            )
120
            .header(header::CONTENT_LOCATION, self.path("/foreskin")));
121
        };
122
        if f < min {
123
            return Err(Problem::new(
124
                StatusCode::LENGTH_REQUIRED,
125
                "foreskin-insufficient",
126
                format!(
127
                    "The selected technique requires {min:.1} units of foreskin. {f:.2} units are available. The server has noted the shortfall."
128
                ),
129
            )
130
            .with("required", min));
131
        }
132
        if f > max {
133
            return Err(Problem::new(
134
                StatusCode::PAYLOAD_TOO_LARGE,
135
                "foreskin-excessive",
136
                format!(
137
                    "The selected technique tolerates at most {max:.1} units of foreskin. {f:.2} units are present."
138
                ),
139
            )
140
            .with("maximum", max));
141
        }
142
        Ok(())
143
    }
144
145
    pub(super) fn check_storage(&self) -> Result<(), Problem> {
146
        if self.items.kit.extender && self.body.girth + self.current_length() / 4.0 > STORAGE_BULK {
147
            return Err(Problem::new(
148
                StatusCode::INSUFFICIENT_STORAGE,
149
                "jeans-full",
150
                "The jeans are full. The extender is occupying the remaining storage.",
151
            )
152
            .with("extender", self.path("/inventory/extender")));
153
        }
154
        Ok(())
155
    }
156
}