atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/game/mod.rs

782069860236atagen2026-10-10 12:44 UTC1
pub mod balance;
782069860236atagen2026-10-10 12:44 UTC2
mod body;
782069860236atagen2026-10-10 12:44 UTC3
mod clinic;
782069860236atagen2026-10-10 12:44 UTC4
mod floors;
782069860236atagen2026-10-10 12:44 UTC5
mod forum;
782069860236atagen2026-10-10 12:44 UTC6
mod hazards;
782069860236atagen2026-10-10 12:44 UTC7
mod household;
782069860236atagen2026-10-10 12:44 UTC8
mod items;
0a0bfee8329eatagen2026-10-11 05:11 UTC9
mod kitchen;
782069860236atagen2026-10-10 12:44 UTC10
mod outcome;
782069860236atagen2026-10-10 12:44 UTC11
mod shop;
782069860236atagen2026-10-10 12:44 UTC12
mod state;
782069860236atagen2026-10-10 12:44 UTC13
mod techniques;
782069860236atagen2026-10-10 12:44 UTC14
mod turn;
782069860236atagen2026-10-10 12:44 UTC15
mod utility;
782069860236atagen2026-10-10 12:44 UTC16
782069860236atagen2026-10-10 12:44 UTC17
pub use body::PatchOp;
782069860236atagen2026-10-10 12:44 UTC18
pub use forum::{ELDER_LAST_MODIFIED, FORUM_THREAD, forum_thread};
7d3fb63208bfatagen2026-10-11 04:38 UTC19
pub use items::{Item, Items, SHOP};
782069860236atagen2026-10-10 12:44 UTC20
pub use outcome::{Outcome, Payload};
782069860236atagen2026-10-10 12:44 UTC21
pub use state::{
9683736a2c5eatagen2026-10-10 13:15 UTC22
    Cause, Clinic, Conditions, Elder, Ending, Floor, FloorState, Grip, MumVisit, Observer, Story,
782069860236atagen2026-10-10 12:44 UTC23
};
782069860236atagen2026-10-10 12:44 UTC24
pub use techniques::{LUBRICANTS, Lube, Technique};
dfa38cf4cd4batagen2026-10-10 09:17 UTC25
99524cf9f17catagen2026-10-10 10:53 UTC26
use std::{
782069860236atagen2026-10-10 12:44 UTC27
    hash::{DefaultHasher, Hash, Hasher},
c3e0c27cf8fdatagen2026-10-10 11:28 UTC28
    time::{Duration, Instant, SystemTime},
99524cf9f17catagen2026-10-10 10:53 UTC29
};
dfa38cf4cd4batagen2026-10-10 09:17 UTC30
782069860236atagen2026-10-10 12:44 UTC31
use axum::http::{StatusCode, header};
dfa38cf4cd4batagen2026-10-10 09:17 UTC32
use rand::{Rng, SeedableRng, rngs::StdRng};
dfa38cf4cd4batagen2026-10-10 09:17 UTC33
use serde_json::{Value, json};
dfa38cf4cd4batagen2026-10-10 09:17 UTC34
dfa38cf4cd4batagen2026-10-10 09:17 UTC35
use crate::problem::Problem;
782069860236atagen2026-10-10 12:44 UTC36
use balance::{
782069860236atagen2026-10-10 12:44 UTC37
    HONEST_TOLERANCE, MAX_STAMINA, PULLED_STAMINA, SELF_REPORT_INFLATION, STARTING_CASH,
782069860236atagen2026-10-10 12:44 UTC38
    STARTING_EQ,
782069860236atagen2026-10-10 12:44 UTC39
};
782069860236atagen2026-10-10 12:44 UTC40
use state::Pending;
dfa38cf4cd4batagen2026-10-10 09:17 UTC41
dfa38cf4cd4batagen2026-10-10 09:17 UTC42
pub struct Run {
9683736a2c5eatagen2026-10-10 13:15 UTC43
    /// The private id in the run's URL. Knowing it is the only authorisation a run has.
dfa38cf4cd4batagen2026-10-10 09:17 UTC44
    pub id: String,
782069860236atagen2026-10-10 12:44 UTC45
    pub public_id: String,
782069860236atagen2026-10-10 12:44 UTC46
    etag_salt: u64,
dfa38cf4cd4batagen2026-10-10 09:17 UTC47
    pub version: u64,
dfa38cf4cd4batagen2026-10-10 09:17 UTC48
    pub turn: u32,
dfa38cf4cd4batagen2026-10-10 09:17 UTC49
    pub floor: Floor,
782069860236atagen2026-10-10 12:44 UTC50
    pub here: FloorState,
dfa38cf4cd4batagen2026-10-10 09:17 UTC51
    pub length: f64,
dfa38cf4cd4batagen2026-10-10 09:17 UTC52
    pub girth: f64,
dfa38cf4cd4batagen2026-10-10 09:17 UTC53
    pub foreskin: Option<f64>,
dfa38cf4cd4batagen2026-10-10 09:17 UTC54
    pub eq: i32,
dfa38cf4cd4batagen2026-10-10 09:17 UTC55
    pub stamina: i32,
dfa38cf4cd4batagen2026-10-10 09:17 UTC56
    pub suspicion: u8,
dfa38cf4cd4batagen2026-10-10 09:17 UTC57
    pub shame: u32,
782069860236atagen2026-10-10 12:44 UTC58
    pub shame_reported: u32,
782069860236atagen2026-10-10 12:44 UTC59
    pub shame_total: u32,
dfa38cf4cd4batagen2026-10-10 09:17 UTC60
    pub cash: u32,
dfa38cf4cd4batagen2026-10-10 09:17 UTC61
    pub grip: Grip,
dfa38cf4cd4batagen2026-10-10 09:17 UTC62
    pub items: Items,
782069860236atagen2026-10-10 12:44 UTC63
    pub conditions: Conditions,
dfa38cf4cd4batagen2026-10-10 09:17 UTC64
    pub warmed_up: bool,
dfa38cf4cd4batagen2026-10-10 09:17 UTC65
    pub put_applied: bool,
782069860236atagen2026-10-10 12:44 UTC66
    pub story: Story,
0fdc47364c8catagen2026-10-11 04:45 UTC67
    pub meditation_taught: bool,
0a0bfee8329eatagen2026-10-11 05:11 UTC68
    pub kitchen_tellings: u8,
9683736a2c5eatagen2026-10-10 13:15 UTC69
    pub mum: Option<MumVisit>,
782069860236atagen2026-10-10 12:44 UTC70
    pub heard: Vec<String>,
782069860236atagen2026-10-10 12:44 UTC71
    pub elder: Elder,
782069860236atagen2026-10-10 12:44 UTC72
    pub clinic: Clinic,
99524cf9f17catagen2026-10-10 10:53 UTC73
    pub seeded: bool,
c3e0c27cf8fdatagen2026-10-10 11:28 UTC74
    pub filmed_at: Option<SystemTime>,
782069860236atagen2026-10-10 12:44 UTC75
    pub observed: Option<Observer>,
782069860236atagen2026-10-10 12:44 UTC76
    pub refractory: Duration,
dfa38cf4cd4batagen2026-10-10 09:17 UTC77
    pub refractory_until: Option<Instant>,
dfa38cf4cd4batagen2026-10-10 09:17 UTC78
    pub pending: Vec<Pending>,
d46f9d0b953aatagen2026-10-10 14:41 UTC79
    pub notices: Vec<&'static str>,
dfa38cf4cd4batagen2026-10-10 09:17 UTC80
    pub ended: Option<Ending>,
782069860236atagen2026-10-10 12:44 UTC81
    pub last_seen: Instant,
dfa38cf4cd4batagen2026-10-10 09:17 UTC82
    rng: StdRng,
dfa38cf4cd4batagen2026-10-10 09:17 UTC83
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC84
dfa38cf4cd4batagen2026-10-10 09:17 UTC85
pub fn round2(x: f64) -> f64 {
dfa38cf4cd4batagen2026-10-10 09:17 UTC86
    (x * 100.0).round() / 100.0
dfa38cf4cd4batagen2026-10-10 09:17 UTC87
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC88
dfa38cf4cd4batagen2026-10-10 09:17 UTC89
fn round3(x: f64) -> f64 {
dfa38cf4cd4batagen2026-10-10 09:17 UTC90
    (x * 1000.0).round() / 1000.0
dfa38cf4cd4batagen2026-10-10 09:17 UTC91
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC92
177535ac8abfatagen2026-10-10 10:39 UTC93
fn honest(claimed: f64, actual: f64) -> bool {
782069860236atagen2026-10-10 12:44 UTC94
    (claimed - actual).abs() < HONEST_TOLERANCE
dfa38cf4cd4batagen2026-10-10 09:17 UTC95
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC96
dfa38cf4cd4batagen2026-10-10 09:17 UTC97
fn not_extended() -> Problem {
dfa38cf4cd4batagen2026-10-10 09:17 UTC98
    Problem::new(
dfa38cf4cd4batagen2026-10-10 09:17 UTC99
        StatusCode::NOT_EXTENDED,
dfa38cf4cd4batagen2026-10-10 09:17 UTC100
        "not-extended",
dfa38cf4cd4batagen2026-10-10 09:17 UTC101
        "The requested extension was not applied. The server has recorded the attempt.",
dfa38cf4cd4batagen2026-10-10 09:17 UTC102
    )
dfa38cf4cd4batagen2026-10-10 09:17 UTC103
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC104
dfa38cf4cd4batagen2026-10-10 09:17 UTC105
pub fn parse_credentials<'a>(authorization: &'a str, scheme: &str) -> Option<&'a str> {
dfa38cf4cd4batagen2026-10-10 09:17 UTC106
    let (given, rest) = authorization.trim().split_once(' ')?;
dfa38cf4cd4batagen2026-10-10 09:17 UTC107
    given
dfa38cf4cd4batagen2026-10-10 09:17 UTC108
        .eq_ignore_ascii_case(scheme)
dfa38cf4cd4batagen2026-10-10 09:17 UTC109
        .then(|| rest.trim().trim_matches('"'))
dfa38cf4cd4batagen2026-10-10 09:17 UTC110
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC111
dfa38cf4cd4batagen2026-10-10 09:17 UTC112
pub fn etag_matches(condition: &str, etag: &str) -> bool {
dfa38cf4cd4batagen2026-10-10 09:17 UTC113
    condition
dfa38cf4cd4batagen2026-10-10 09:17 UTC114
        .split(',')
dfa38cf4cd4batagen2026-10-10 09:17 UTC115
        .map(str::trim)
dfa38cf4cd4batagen2026-10-10 09:17 UTC116
        .any(|tag| tag == "*" || tag.trim_start_matches("W/") == etag)
dfa38cf4cd4batagen2026-10-10 09:17 UTC117
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC118
dfa38cf4cd4batagen2026-10-10 09:17 UTC119
pub fn shaft(mm: usize) -> String {
dfa38cf4cd4batagen2026-10-10 09:17 UTC120
    format!("8{}D", "=".repeat(mm.saturating_sub(2)))
dfa38cf4cd4batagen2026-10-10 09:17 UTC121
}
dfa38cf4cd4batagen2026-10-10 09:17 UTC122
dfa38cf4cd4batagen2026-10-10 09:17 UTC123
impl Run {
782069860236atagen2026-10-10 12:44 UTC124
    pub fn new(id: String, public_id: String, seed: u64) -> Self {
dfa38cf4cd4batagen2026-10-10 09:17 UTC125
        let mut rng = StdRng::seed_from_u64(seed);
dfa38cf4cd4batagen2026-10-10 09:17 UTC126
        let length = round2(rng.random_range(4.5..6.5));
dfa38cf4cd4batagen2026-10-10 09:17 UTC127
        let girth = round2(rng.random_range(4.0..5.0));
dfa38cf4cd4batagen2026-10-10 09:17 UTC128
        let foreskin = round2(rng.random_range(0.4..2.4));
782069860236atagen2026-10-10 12:44 UTC129
        let etag_salt = rng.random();
782069860236atagen2026-10-10 12:44 UTC130
        Self {
dfa38cf4cd4batagen2026-10-10 09:17 UTC131
            id,
782069860236atagen2026-10-10 12:44 UTC132
            public_id,
782069860236atagen2026-10-10 12:44 UTC133
            etag_salt,
dfa38cf4cd4batagen2026-10-10 09:17 UTC134
            version: 0,
dfa38cf4cd4batagen2026-10-10 09:17 UTC135
            turn: 0,
dfa38cf4cd4batagen2026-10-10 09:17 UTC136
            floor: Floor::Bedroom,
782069860236atagen2026-10-10 12:44 UTC137
            here: FloorState::default(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC138
            length,
dfa38cf4cd4batagen2026-10-10 09:17 UTC139
            girth,
dfa38cf4cd4batagen2026-10-10 09:17 UTC140
            foreskin: Some(foreskin),
782069860236atagen2026-10-10 12:44 UTC141
            eq: STARTING_EQ,
dfa38cf4cd4batagen2026-10-10 09:17 UTC142
            stamina: MAX_STAMINA,
dfa38cf4cd4batagen2026-10-10 09:17 UTC143
            suspicion: 0,
dfa38cf4cd4batagen2026-10-10 09:17 UTC144
            shame: 0,
782069860236atagen2026-10-10 12:44 UTC145
            shame_reported: 0,
782069860236atagen2026-10-10 12:44 UTC146
            shame_total: 0,
dfa38cf4cd4batagen2026-10-10 09:17 UTC147
            cash: STARTING_CASH,
dfa38cf4cd4batagen2026-10-10 09:17 UTC148
            grip: Grip::Overhand,
782069860236atagen2026-10-10 12:44 UTC149
            items: Items::default(),
782069860236atagen2026-10-10 12:44 UTC150
            conditions: Conditions::default(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC151
            warmed_up: false,
dfa38cf4cd4batagen2026-10-10 09:17 UTC152
            put_applied: false,
782069860236atagen2026-10-10 12:44 UTC153
            story: Story::Unopened,
0fdc47364c8catagen2026-10-11 04:45 UTC154
            meditation_taught: false,
0a0bfee8329eatagen2026-10-11 05:11 UTC155
            kitchen_tellings: 0,
9683736a2c5eatagen2026-10-10 13:15 UTC156
            mum: None,
782069860236atagen2026-10-10 12:44 UTC157
            heard: Vec::new(),
d46f9d0b953aatagen2026-10-10 14:41 UTC158
            notices: Vec::new(),
782069860236atagen2026-10-10 12:44 UTC159
            elder: Elder::Unverified { strikes: 0 },
782069860236atagen2026-10-10 12:44 UTC160
            clinic: Clinic::default(),
99524cf9f17catagen2026-10-10 10:53 UTC161
            seeded: false,
c3e0c27cf8fdatagen2026-10-10 11:28 UTC162
            filmed_at: None,
dfa38cf4cd4batagen2026-10-10 09:17 UTC163
            observed: None,
782069860236atagen2026-10-10 12:44 UTC164
            refractory: Duration::from_secs(30),
dfa38cf4cd4batagen2026-10-10 09:17 UTC165
            refractory_until: None,
dfa38cf4cd4batagen2026-10-10 09:17 UTC166
            pending: Vec::new(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC167
            ended: None,
782069860236atagen2026-10-10 12:44 UTC168
            last_seen: Instant::now(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC169
            rng,
782069860236atagen2026-10-10 12:44 UTC170
        }
782069860236atagen2026-10-10 12:44 UTC171
    }
782069860236atagen2026-10-10 12:44 UTC172
782069860236atagen2026-10-10 12:44 UTC173
    fn etag_at(&self, version: u64) -> String {
782069860236atagen2026-10-10 12:44 UTC174
        let mut hasher = DefaultHasher::new();
782069860236atagen2026-10-10 12:44 UTC175
        (self.etag_salt, version).hash(&mut hasher);
782069860236atagen2026-10-10 12:44 UTC176
        format!("\"{:016x}\"", hasher.finish())
dfa38cf4cd4batagen2026-10-10 09:17 UTC177
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC178
dfa38cf4cd4batagen2026-10-10 09:17 UTC179
    pub fn etag(&self) -> String {
782069860236atagen2026-10-10 12:44 UTC180
        self.etag_at(self.version)
782069860236atagen2026-10-10 12:44 UTC181
    }
782069860236atagen2026-10-10 12:44 UTC182
782069860236atagen2026-10-10 12:44 UTC183
    /// The ETag the run had when it was created, which the true ending asks for.
782069860236atagen2026-10-10 12:44 UTC184
    pub fn origin_etag(&self) -> String {
782069860236atagen2026-10-10 12:44 UTC185
        self.etag_at(0)
dfa38cf4cd4batagen2026-10-10 09:17 UTC186
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC187
dfa38cf4cd4batagen2026-10-10 09:17 UTC188
    pub fn touch(&mut self) {
dfa38cf4cd4batagen2026-10-10 09:17 UTC189
        self.version += 1;
dfa38cf4cd4batagen2026-10-10 09:17 UTC190
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC191
dfa38cf4cd4batagen2026-10-10 09:17 UTC192
    fn fresh_nonce(&mut self) -> String {
dfa38cf4cd4batagen2026-10-10 09:17 UTC193
        format!("{:016x}", self.rng.random::<u64>())
dfa38cf4cd4batagen2026-10-10 09:17 UTC194
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC195
782069860236atagen2026-10-10 12:44 UTC196
    fn chance(&mut self, odds: u32) -> bool {
782069860236atagen2026-10-10 12:44 UTC197
        self.rng.random_ratio(1, odds)
782069860236atagen2026-10-10 12:44 UTC198
    }
782069860236atagen2026-10-10 12:44 UTC199
782069860236atagen2026-10-10 12:44 UTC200
    pub fn path(&self, sub: &str) -> String {
782069860236atagen2026-10-10 12:44 UTC201
        format!("/runs/{}{sub}", self.id)
782069860236atagen2026-10-10 12:44 UTC202
    }
782069860236atagen2026-10-10 12:44 UTC203
dfa38cf4cd4batagen2026-10-10 09:17 UTC204
    pub fn current_length(&self) -> f64 {
782069860236atagen2026-10-10 12:44 UTC205
        self.length + self.here.temp_length
dfa38cf4cd4batagen2026-10-10 09:17 UTC206
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC207
dfa38cf4cd4batagen2026-10-10 09:17 UTC208
    pub fn self_reported(&self) -> f64 {
dfa38cf4cd4batagen2026-10-10 09:17 UTC209
        self.current_length() + SELF_REPORT_INFLATION
dfa38cf4cd4batagen2026-10-10 09:17 UTC210
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC211
c3e0c27cf8fdatagen2026-10-10 11:28 UTC212
    pub fn max_stamina(&self) -> i32 {
782069860236atagen2026-10-10 12:44 UTC213
        if self.conditions.pulled_muscle {
782069860236atagen2026-10-10 12:44 UTC214
            PULLED_STAMINA
782069860236atagen2026-10-10 12:44 UTC215
        } else {
782069860236atagen2026-10-10 12:44 UTC216
            MAX_STAMINA
782069860236atagen2026-10-10 12:44 UTC217
        }
782069860236atagen2026-10-10 12:44 UTC218
    }
782069860236atagen2026-10-10 12:44 UTC219
782069860236atagen2026-10-10 12:44 UTC220
    pub fn forum_read(&self) -> bool {
782069860236atagen2026-10-10 12:44 UTC221
        self.story == Story::ReadForum
c3e0c27cf8fdatagen2026-10-10 11:28 UTC222
    }
c3e0c27cf8fdatagen2026-10-10 11:28 UTC223
c3e0c27cf8fdatagen2026-10-10 11:28 UTC224
    pub fn footage(&self) -> Option<(SystemTime, u64)> {
c3e0c27cf8fdatagen2026-10-10 11:28 UTC225
        let filmed = self.filmed_at?;
c3e0c27cf8fdatagen2026-10-10 11:28 UTC226
        let age = filmed.elapsed().map_or(0, |d| d.as_secs());
c3e0c27cf8fdatagen2026-10-10 11:28 UTC227
        Some((filmed, age))
c3e0c27cf8fdatagen2026-10-10 11:28 UTC228
    }
c3e0c27cf8fdatagen2026-10-10 11:28 UTC229
dfa38cf4cd4batagen2026-10-10 09:17 UTC230
    pub fn status(&self) -> &'static str {
782069860236atagen2026-10-10 12:44 UTC231
        match (&self.ended, self.clinic.cured) {
dfa38cf4cd4batagen2026-10-10 09:17 UTC232
            (Some(_), _) => "gone",
dfa38cf4cd4batagen2026-10-10 09:17 UTC233
            (None, true) => "normal",
dfa38cf4cd4batagen2026-10-10 09:17 UTC234
            (None, false) => "alive",
dfa38cf4cd4batagen2026-10-10 09:17 UTC235
        }
dfa38cf4cd4batagen2026-10-10 09:17 UTC236
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC237
782069860236atagen2026-10-10 12:44 UTC238
    /// The parts of the run that its response headers do not already carry.
dfa38cf4cd4batagen2026-10-10 09:17 UTC239
    pub fn view(&self) -> Value {
dfa38cf4cd4batagen2026-10-10 09:17 UTC240
        let conditions: Vec<&str> = [
782069860236atagen2026-10-10 12:44 UTC241
            (self.here.teapot, "short and stout"),
782069860236atagen2026-10-10 12:44 UTC242
            (self.conditions.peyronie, "curved"),
dfa38cf4cd4batagen2026-10-10 09:17 UTC243
            (self.items.cage, "caged"),
782069860236atagen2026-10-10 12:44 UTC244
            (self.conditions.priapism_turns > 0, "priapism"),
782069860236atagen2026-10-10 12:44 UTC245
            (self.conditions.pulled_muscle, "pulled muscle"),
c3e0c27cf8fdatagen2026-10-10 11:28 UTC246
            (self.filmed_at.is_some(), "viral"),
dfa38cf4cd4batagen2026-10-10 09:17 UTC247
        ]
dfa38cf4cd4batagen2026-10-10 09:17 UTC248
        .into_iter()
dfa38cf4cd4batagen2026-10-10 09:17 UTC249
        .filter_map(|(active, name)| active.then_some(name))
dfa38cf4cd4batagen2026-10-10 09:17 UTC250
        .collect();
782069860236atagen2026-10-10 12:44 UTC251
        json!({
782069860236atagen2026-10-10 12:44 UTC252
            "public_id": self.public_id,
dfa38cf4cd4batagen2026-10-10 09:17 UTC253
            "status": self.status(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC254
            "turn": self.turn,
782069860236atagen2026-10-10 12:44 UTC255
            "run_shame": self.shame,
dfa38cf4cd4batagen2026-10-10 09:17 UTC256
            "grip": self.grip.name(),
dfa38cf4cd4batagen2026-10-10 09:17 UTC257
            "warmed_up": self.warmed_up,
99524cf9f17catagen2026-10-10 10:53 UTC258
            "ranked": !self.seeded,
dfa38cf4cd4batagen2026-10-10 09:17 UTC259
            "conditions": conditions,
782069860236atagen2026-10-10 12:44 UTC260
        })
dfa38cf4cd4batagen2026-10-10 09:17 UTC261
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC262
782069860236atagen2026-10-10 12:44 UTC263
    pub fn end(&mut self, cause: Cause) -> Problem {
dfa38cf4cd4batagen2026-10-10 09:17 UTC264
        self.ended = Some(Ending {
dfa38cf4cd4batagen2026-10-10 09:17 UTC265
            cause,
dfa38cf4cd4batagen2026-10-10 09:17 UTC266
            turn: self.turn,
dfa38cf4cd4batagen2026-10-10 09:17 UTC267
        });
782069860236atagen2026-10-10 12:44 UTC268
        cause.problem().with("turn", self.turn)
dfa38cf4cd4batagen2026-10-10 09:17 UTC269
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC270
e76ab808e7adatagen2026-10-10 13:46 UTC271
    /// Ends the refractory period once it has run out, whatever the request.
e76ab808e7adatagen2026-10-10 13:46 UTC272
    pub fn recover(&mut self, now: Instant) {
e76ab808e7adatagen2026-10-10 13:46 UTC273
        if self.refractory_until.is_some_and(|until| now >= until) {
e76ab808e7adatagen2026-10-10 13:46 UTC274
            self.refractory_until = None;
e76ab808e7adatagen2026-10-10 13:46 UTC275
            self.stamina = self.max_stamina();
e76ab808e7adatagen2026-10-10 13:46 UTC276
            self.eq = STARTING_EQ;
e76ab808e7adatagen2026-10-10 13:46 UTC277
            self.warmed_up = false;
e76ab808e7adatagen2026-10-10 13:46 UTC278
            self.touch();
e76ab808e7adatagen2026-10-10 13:46 UTC279
        }
e76ab808e7adatagen2026-10-10 13:46 UTC280
    }
e76ab808e7adatagen2026-10-10 13:46 UTC281
dfa38cf4cd4batagen2026-10-10 09:17 UTC282
    fn check_refractory(&mut self, now: Instant) -> Result<(), Problem> {
e76ab808e7adatagen2026-10-10 13:46 UTC283
        self.recover(now);
dfa38cf4cd4batagen2026-10-10 09:17 UTC284
        let Some(until) = self.refractory_until else {
dfa38cf4cd4batagen2026-10-10 09:17 UTC285
            return Ok(());
dfa38cf4cd4batagen2026-10-10 09:17 UTC286
        };
e76ab808e7adatagen2026-10-10 13:46 UTC287
        let seconds = until.saturating_duration_since(now).as_secs() + 1;
e76ab808e7adatagen2026-10-10 13:46 UTC288
        Err(Problem::new(
e76ab808e7adatagen2026-10-10 13:46 UTC289
            StatusCode::TOO_MANY_REQUESTS,
e76ab808e7adatagen2026-10-10 13:46 UTC290
            "overtrained",
e76ab808e7adatagen2026-10-10 13:46 UTC291
            "Stamina is exhausted. The refractory period is in effect.",
e76ab808e7adatagen2026-10-10 13:46 UTC292
        )
e76ab808e7adatagen2026-10-10 13:46 UTC293
        .header(header::RETRY_AFTER, seconds.to_string()))
dfa38cf4cd4batagen2026-10-10 09:17 UTC294
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC295
782069860236atagen2026-10-10 12:44 UTC296
    fn exhaust(&mut self, now: Instant) {
782069860236atagen2026-10-10 12:44 UTC297
        self.stamina = 0;
782069860236atagen2026-10-10 12:44 UTC298
        self.refractory_until = Some(now + self.refractory);
782069860236atagen2026-10-10 12:44 UTC299
    }
782069860236atagen2026-10-10 12:44 UTC300
9683736a2c5eatagen2026-10-10 13:15 UTC301
    fn realise(&mut self) {
dfa38cf4cd4batagen2026-10-10 09:17 UTC302
        let (due, waiting): (Vec<_>, Vec<_>) = std::mem::take(&mut self.pending)
dfa38cf4cd4batagen2026-10-10 09:17 UTC303
            .into_iter()
dfa38cf4cd4batagen2026-10-10 09:17 UTC304
            .partition(|p| p.due <= self.turn);
dfa38cf4cd4batagen2026-10-10 09:17 UTC305
        self.pending = waiting;
dfa38cf4cd4batagen2026-10-10 09:17 UTC306
        for p in due {
dfa38cf4cd4batagen2026-10-10 09:17 UTC307
            self.length += p.length;
dfa38cf4cd4batagen2026-10-10 09:17 UTC308
            self.girth += p.girth;
dfa38cf4cd4batagen2026-10-10 09:17 UTC309
        }
dfa38cf4cd4batagen2026-10-10 09:17 UTC310
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC311
dfa38cf4cd4batagen2026-10-10 09:17 UTC312
    fn gain(&mut self, length: f64, girth: f64, foreskin: f64) {
dfa38cf4cd4batagen2026-10-10 09:17 UTC313
        self.length += length;
dfa38cf4cd4batagen2026-10-10 09:17 UTC314
        self.girth += girth;
dfa38cf4cd4batagen2026-10-10 09:17 UTC315
        if let Some(f) = &mut self.foreskin {
dfa38cf4cd4batagen2026-10-10 09:17 UTC316
            *f += foreskin;
dfa38cf4cd4batagen2026-10-10 09:17 UTC317
        }
dfa38cf4cd4batagen2026-10-10 09:17 UTC318
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC319
dfa38cf4cd4batagen2026-10-10 09:17 UTC320
    fn rupture(&mut self) -> Problem {
782069860236atagen2026-10-10 12:44 UTC321
        let cause = Cause::Ruptured {
782069860236atagen2026-10-10 12:44 UTC322
            ice_unused: self.items.ice_packs > 0,
dfa38cf4cd4batagen2026-10-10 09:17 UTC323
        };
782069860236atagen2026-10-10 12:44 UTC324
        self.end(cause)
dfa38cf4cd4batagen2026-10-10 09:17 UTC325
    }
dfa38cf4cd4batagen2026-10-10 09:17 UTC326
}