atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/game/turn.rs

782069860236atagen2026-10-10 12:44 UTC1
use std::{fmt::Write as _, ops::ControlFlow, time::Instant};
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
use serde_json::json;
782069860236atagen2026-10-10 12:44 UTC6
782069860236atagen2026-10-10 12:44 UTC7
use super::{
782069860236atagen2026-10-10 12:44 UTC8
    Floor, Grip, LUBRICANTS, Lube, Outcome, Payload, Run, Story, Technique,
782069860236atagen2026-10-10 12:44 UTC9
    balance::{
9683736a2c5eatagen2026-10-10 13:15 UTC10
        BAND_BONUS, BRANCH_TURN, CAT_TAKES_TOWEL, CHANGING_ROOM_CHILL, DANGER_EQ, EQ_BAND,
9683736a2c5eatagen2026-10-10 13:15 UTC11
        EQ_WOBBLE, EXTENDER_GAIN, HOT_EQ, OBSERVED, PRIAPISM_EQ, PRIAPISM_TURNS, PULLED_MUSCLE,
782069860236atagen2026-10-10 12:44 UTC12
        PULLED_MUSCLE_INTENSITY, RING_PRIAPISM, RING_RUPTURE_LINE, RUPTURE_LINE, STORAGE_GIRTH,
782069860236atagen2026-10-10 12:44 UTC13
        SUSPICION_LIMIT,
782069860236atagen2026-10-10 12:44 UTC14
    },
782069860236atagen2026-10-10 12:44 UTC15
    not_extended, round3,
9683736a2c5eatagen2026-10-10 13:15 UTC16
    state::{MumVisit, Observer, Pending},
782069860236atagen2026-10-10 12:44 UTC17
    techniques::{LubeRule, Profile, Training, Utility},
782069860236atagen2026-10-10 12:44 UTC18
};
782069860236atagen2026-10-10 12:44 UTC19
use crate::{headers::SERVER_TIMING, problem::Problem};
782069860236atagen2026-10-10 12:44 UTC20
782069860236atagen2026-10-10 12:44 UTC21
impl Run {
782069860236atagen2026-10-10 12:44 UTC22
    pub fn jelq(
782069860236atagen2026-10-10 12:44 UTC23
        &mut self,
782069860236atagen2026-10-10 12:44 UTC24
        techniques: &[Technique],
782069860236atagen2026-10-10 12:44 UTC25
        intensity: i32,
782069860236atagen2026-10-10 12:44 UTC26
        lube: Option<Lube>,
782069860236atagen2026-10-10 12:44 UTC27
        now: Instant,
782069860236atagen2026-10-10 12:44 UTC28
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC29
        self.check_refractory(now)?;
782069860236atagen2026-10-10 12:44 UTC30
        if self.story == Story::Pending {
782069860236atagen2026-10-10 12:44 UTC31
            return Ok(self.branch_offer());
782069860236atagen2026-10-10 12:44 UTC32
        }
782069860236atagen2026-10-10 12:44 UTC33
        if self.floor == Floor::Forum {
782069860236atagen2026-10-10 12:44 UTC34
            return Ok(Outcome::json(
782069860236atagen2026-10-10 12:44 UTC35
                StatusCode::SEE_OTHER,
782069860236atagen2026-10-10 12:44 UTC36
                json!({ "detail": "Refer to the forum." }),
782069860236atagen2026-10-10 12:44 UTC37
            )
782069860236atagen2026-10-10 12:44 UTC38
            .header(header::LOCATION, super::FORUM_THREAD));
782069860236atagen2026-10-10 12:44 UTC39
        }
782069860236atagen2026-10-10 12:44 UTC40
        if self.items.cage {
782069860236atagen2026-10-10 12:44 UTC41
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC42
                StatusCode::LOCKED,
782069860236atagen2026-10-10 12:44 UTC43
                "caged",
782069860236atagen2026-10-10 12:44 UTC44
                "The cage is locked. The key is not in the box.",
782069860236atagen2026-10-10 12:44 UTC45
            ));
782069860236atagen2026-10-10 12:44 UTC46
        }
782069860236atagen2026-10-10 12:44 UTC47
        match *techniques {
782069860236atagen2026-10-10 12:44 UTC48
            [Technique::Utility(utility)] => self.utility(utility, lube, now),
782069860236atagen2026-10-10 12:44 UTC49
            [Technique::Training(training)] => self.single(training, intensity, lube, now),
782069860236atagen2026-10-10 12:44 UTC50
            [Technique::Training(a), Technique::Training(b)] => {
782069860236atagen2026-10-10 12:44 UTC51
                self.combo(a, b, intensity, lube, now)
782069860236atagen2026-10-10 12:44 UTC52
            }
782069860236atagen2026-10-10 12:44 UTC53
            [_, _] => Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC54
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC55
                "combo-invalid",
782069860236atagen2026-10-10 12:44 UTC56
                "Only training techniques can be combined.",
782069860236atagen2026-10-10 12:44 UTC57
            )),
782069860236atagen2026-10-10 12:44 UTC58
            _ => Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC59
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC60
                "too-many-techniques",
782069860236atagen2026-10-10 12:44 UTC61
                "Two techniques at most. The server has limits even if you do not.",
782069860236atagen2026-10-10 12:44 UTC62
            )),
782069860236atagen2026-10-10 12:44 UTC63
        }
782069860236atagen2026-10-10 12:44 UTC64
    }
782069860236atagen2026-10-10 12:44 UTC65
782069860236atagen2026-10-10 12:44 UTC66
    fn utility(
782069860236atagen2026-10-10 12:44 UTC67
        &mut self,
782069860236atagen2026-10-10 12:44 UTC68
        utility: Utility,
782069860236atagen2026-10-10 12:44 UTC69
        lube: Option<Lube>,
782069860236atagen2026-10-10 12:44 UTC70
        now: Instant,
782069860236atagen2026-10-10 12:44 UTC71
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC72
        match utility {
782069860236atagen2026-10-10 12:44 UTC73
            Utility::Warmup => return self.warm_up(),
782069860236atagen2026-10-10 12:44 UTC74
            Utility::Ice => return self.ice(),
782069860236atagen2026-10-10 12:44 UTC75
            Utility::Supplement => return self.supplement(),
782069860236atagen2026-10-10 12:44 UTC76
            Utility::Edge | Utility::Pump => {}
782069860236atagen2026-10-10 12:44 UTC77
        }
782069860236atagen2026-10-10 12:44 UTC78
        if utility == Utility::Pump {
782069860236atagen2026-10-10 12:44 UTC79
            self.here.edges = 0;
782069860236atagen2026-10-10 12:44 UTC80
        }
782069860236atagen2026-10-10 12:44 UTC81
        self.ready(Technique::Utility(utility), lube)?;
782069860236atagen2026-10-10 12:44 UTC82
        self.check_storage()?;
782069860236atagen2026-10-10 12:44 UTC83
        self.floor_enemy()?;
782069860236atagen2026-10-10 12:44 UTC84
        match utility {
782069860236atagen2026-10-10 12:44 UTC85
            Utility::Edge => self.edge(now),
782069860236atagen2026-10-10 12:44 UTC86
            _ => self.pump(now),
782069860236atagen2026-10-10 12:44 UTC87
        }
782069860236atagen2026-10-10 12:44 UTC88
    }
782069860236atagen2026-10-10 12:44 UTC89
782069860236atagen2026-10-10 12:44 UTC90
    fn single(
782069860236atagen2026-10-10 12:44 UTC91
        &mut self,
782069860236atagen2026-10-10 12:44 UTC92
        training: Training,
782069860236atagen2026-10-10 12:44 UTC93
        intensity: i32,
782069860236atagen2026-10-10 12:44 UTC94
        lube: Option<Lube>,
782069860236atagen2026-10-10 12:44 UTC95
        now: Instant,
782069860236atagen2026-10-10 12:44 UTC96
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC97
        self.here.edges = 0;
782069860236atagen2026-10-10 12:44 UTC98
        self.ready(Technique::Training(training), lube)?;
782069860236atagen2026-10-10 12:44 UTC99
        self.check_storage()?;
782069860236atagen2026-10-10 12:44 UTC100
        self.floor_enemy()?;
782069860236atagen2026-10-10 12:44 UTC101
        self.commit(intensity, lube, now, 1);
782069860236atagen2026-10-10 12:44 UTC102
        if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
782069860236atagen2026-10-10 12:44 UTC103
            return Ok(interrupted);
782069860236atagen2026-10-10 12:44 UTC104
        }
782069860236atagen2026-10-10 12:44 UTC105
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
782069860236atagen2026-10-10 12:44 UTC106
            return Ok(interrupted);
782069860236atagen2026-10-10 12:44 UTC107
        }
782069860236atagen2026-10-10 12:44 UTC108
        let outcome = self.roll(training, intensity, lube)?;
782069860236atagen2026-10-10 12:44 UTC109
        Ok(self.gossip(outcome))
782069860236atagen2026-10-10 12:44 UTC110
    }
782069860236atagen2026-10-10 12:44 UTC111
782069860236atagen2026-10-10 12:44 UTC112
    fn combo(
782069860236atagen2026-10-10 12:44 UTC113
        &mut self,
782069860236atagen2026-10-10 12:44 UTC114
        a: Training,
782069860236atagen2026-10-10 12:44 UTC115
        b: Training,
782069860236atagen2026-10-10 12:44 UTC116
        intensity: i32,
782069860236atagen2026-10-10 12:44 UTC117
        lube: Option<Lube>,
782069860236atagen2026-10-10 12:44 UTC118
        now: Instant,
782069860236atagen2026-10-10 12:44 UTC119
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC120
        if a == b {
782069860236atagen2026-10-10 12:44 UTC121
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC122
                StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC123
                "combo-invalid",
782069860236atagen2026-10-10 12:44 UTC124
                "A technique cannot be combined with itself. The server has checked.",
782069860236atagen2026-10-10 12:44 UTC125
            ));
782069860236atagen2026-10-10 12:44 UTC126
        }
782069860236atagen2026-10-10 12:44 UTC127
        self.here.edges = 0;
782069860236atagen2026-10-10 12:44 UTC128
        self.ready(Technique::Training(a), lube)?;
782069860236atagen2026-10-10 12:44 UTC129
        self.ready(Technique::Training(b), lube)?;
782069860236atagen2026-10-10 12:44 UTC130
        self.check_storage()?;
782069860236atagen2026-10-10 12:44 UTC131
        self.floor_enemy()?;
782069860236atagen2026-10-10 12:44 UTC132
        self.commit(intensity, lube, now, 2);
782069860236atagen2026-10-10 12:44 UTC133
        for training in [a, b] {
782069860236atagen2026-10-10 12:44 UTC134
            if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
782069860236atagen2026-10-10 12:44 UTC135
                return Ok(interrupted);
782069860236atagen2026-10-10 12:44 UTC136
            }
782069860236atagen2026-10-10 12:44 UTC137
        }
782069860236atagen2026-10-10 12:44 UTC138
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
782069860236atagen2026-10-10 12:44 UTC139
            return Ok(interrupted);
782069860236atagen2026-10-10 12:44 UTC140
        }
782069860236atagen2026-10-10 12:44 UTC141
782069860236atagen2026-10-10 12:44 UTC142
        let mut statuses = Vec::new();
782069860236atagen2026-10-10 12:44 UTC143
        for training in [a, b] {
782069860236atagen2026-10-10 12:44 UTC144
            let result = self.roll(training, intensity, lube);
782069860236atagen2026-10-10 12:44 UTC145
            if self.ended.is_some() {
782069860236atagen2026-10-10 12:44 UTC146
                return result;
782069860236atagen2026-10-10 12:44 UTC147
            }
782069860236atagen2026-10-10 12:44 UTC148
            let status = match result {
782069860236atagen2026-10-10 12:44 UTC149
                Ok(outcome) => self.gossip(outcome).status,
782069860236atagen2026-10-10 12:44 UTC150
                Err(problem) => problem.status(),
782069860236atagen2026-10-10 12:44 UTC151
            };
782069860236atagen2026-10-10 12:44 UTC152
            statuses.push((training, status));
782069860236atagen2026-10-10 12:44 UTC153
        }
782069860236atagen2026-10-10 12:44 UTC154
782069860236atagen2026-10-10 12:44 UTC155
        let mut xml = String::from(
782069860236atagen2026-10-10 12:44 UTC156
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n",
782069860236atagen2026-10-10 12:44 UTC157
        );
782069860236atagen2026-10-10 12:44 UTC158
        for (training, status) in statuses {
782069860236atagen2026-10-10 12:44 UTC159
            let _ = write!(
782069860236atagen2026-10-10 12:44 UTC160
                xml,
782069860236atagen2026-10-10 12:44 UTC161
                "  <D:response>\n    <D:href>{}?technique={}</D:href>\n    <D:status>HTTP/1.1 {} {}</D:status>\n  </D:response>\n",
782069860236atagen2026-10-10 12:44 UTC162
                self.path(""),
782069860236atagen2026-10-10 12:44 UTC163
                training.name(),
782069860236atagen2026-10-10 12:44 UTC164
                status.as_u16(),
782069860236atagen2026-10-10 12:44 UTC165
                crate::problem::reason(status),
782069860236atagen2026-10-10 12:44 UTC166
            );
782069860236atagen2026-10-10 12:44 UTC167
        }
782069860236atagen2026-10-10 12:44 UTC168
        xml.push_str(
782069860236atagen2026-10-10 12:44 UTC169
            "  <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n",
782069860236atagen2026-10-10 12:44 UTC170
        );
782069860236atagen2026-10-10 12:44 UTC171
        Ok(Outcome::raw(
782069860236atagen2026-10-10 12:44 UTC172
            StatusCode::MULTI_STATUS,
782069860236atagen2026-10-10 12:44 UTC173
            "application/xml; charset=utf-8",
782069860236atagen2026-10-10 12:44 UTC174
            xml,
782069860236atagen2026-10-10 12:44 UTC175
        ))
782069860236atagen2026-10-10 12:44 UTC176
    }
782069860236atagen2026-10-10 12:44 UTC177
782069860236atagen2026-10-10 12:44 UTC178
    fn ready(&self, technique: Technique, lube: Option<Lube>) -> Result<(), Problem> {
58e1fb3943e6atagen2026-10-10 13:30 UTC179
        self.access(technique)?;
58e1fb3943e6atagen2026-10-10 13:30 UTC180
        match technique {
58e1fb3943e6atagen2026-10-10 13:30 UTC181
            Technique::Training(training) => Self::check_lube(training, lube),
58e1fb3943e6atagen2026-10-10 13:30 UTC182
            Technique::Utility(_) => Ok(()),
58e1fb3943e6atagen2026-10-10 13:30 UTC183
        }
58e1fb3943e6atagen2026-10-10 13:30 UTC184
    }
58e1fb3943e6atagen2026-10-10 13:30 UTC185
58e1fb3943e6atagen2026-10-10 13:30 UTC186
    /// Whether the run could attempt the technique now, ignoring the lubricant.
58e1fb3943e6atagen2026-10-10 13:30 UTC187
    fn access(&self, technique: Technique) -> Result<(), Problem> {
782069860236atagen2026-10-10 12:44 UTC188
        if technique == Technique::Training(Training::Uli) && !self.forum_read() {
782069860236atagen2026-10-10 12:44 UTC189
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC190
                StatusCode::FORBIDDEN,
782069860236atagen2026-10-10 12:44 UTC191
                "technique-locked",
782069860236atagen2026-10-10 12:44 UTC192
                "This technique is discussed on the forum. You have not been to the forum.",
782069860236atagen2026-10-10 12:44 UTC193
            ));
782069860236atagen2026-10-10 12:44 UTC194
        }
782069860236atagen2026-10-10 12:44 UTC195
        if !self.warmed_up {
782069860236atagen2026-10-10 12:44 UTC196
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC197
                StatusCode::PRECONDITION_FAILED,
782069860236atagen2026-10-10 12:44 UTC198
                "cold-start",
782069860236atagen2026-10-10 12:44 UTC199
                "Warm-up was skipped. The server has noted this.",
782069860236atagen2026-10-10 12:44 UTC200
            )
782069860236atagen2026-10-10 12:44 UTC201
            .with("technique", Utility::Warmup.name()));
782069860236atagen2026-10-10 12:44 UTC202
        }
58e1fb3943e6atagen2026-10-10 13:30 UTC203
        match technique {
58e1fb3943e6atagen2026-10-10 13:30 UTC204
            Technique::Utility(Utility::Pump) if !self.items.pump_pro => Err(Problem::new(
58e1fb3943e6atagen2026-10-10 13:30 UTC205
                StatusCode::FAILED_DEPENDENCY,
58e1fb3943e6atagen2026-10-10 13:30 UTC206
                "no-pump",
58e1fb3943e6atagen2026-10-10 13:30 UTC207
                "The pump technique depends on a pump.",
58e1fb3943e6atagen2026-10-10 13:30 UTC208
            )),
58e1fb3943e6atagen2026-10-10 13:30 UTC209
            Technique::Utility(_) => Ok(()),
58e1fb3943e6atagen2026-10-10 13:30 UTC210
            Technique::Training(training) => match training.profile().foreskin {
58e1fb3943e6atagen2026-10-10 13:30 UTC211
                Some((min, max)) => self.check_foreskin(min, max),
58e1fb3943e6atagen2026-10-10 13:30 UTC212
                None => Ok(()),
58e1fb3943e6atagen2026-10-10 13:30 UTC213
            },
782069860236atagen2026-10-10 12:44 UTC214
        }
58e1fb3943e6atagen2026-10-10 13:30 UTC215
    }
58e1fb3943e6atagen2026-10-10 13:30 UTC216
58e1fb3943e6atagen2026-10-10 13:30 UTC217
    fn check_lube(training: Training, lube: Option<Lube>) -> Result<(), Problem> {
58e1fb3943e6atagen2026-10-10 13:30 UTC218
        match (training.profile().lube, lube) {
58e1fb3943e6atagen2026-10-10 13:30 UTC219
            (LubeRule::Required, None) => Err(Problem::new(
58e1fb3943e6atagen2026-10-10 13:30 UTC220
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
58e1fb3943e6atagen2026-10-10 13:30 UTC221
                "lubricant-required",
58e1fb3943e6atagen2026-10-10 13:30 UTC222
                format!(
58e1fb3943e6atagen2026-10-10 13:30 UTC223
                    "The {} technique requires a lubricant. None was declared.",
58e1fb3943e6atagen2026-10-10 13:30 UTC224
                    training.name()
58e1fb3943e6atagen2026-10-10 13:30 UTC225
                ),
58e1fb3943e6atagen2026-10-10 13:30 UTC226
            )
58e1fb3943e6atagen2026-10-10 13:30 UTC227
            .with("lubricants", json!(LUBRICANTS))),
58e1fb3943e6atagen2026-10-10 13:30 UTC228
            (LubeRule::Forbidden, Some(_)) => Err(Problem::new(
58e1fb3943e6atagen2026-10-10 13:30 UTC229
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
58e1fb3943e6atagen2026-10-10 13:30 UTC230
                "lubricant-declared",
58e1fb3943e6atagen2026-10-10 13:30 UTC231
                format!(
58e1fb3943e6atagen2026-10-10 13:30 UTC232
                    "The {} technique was requested. A lubricant was declared.",
58e1fb3943e6atagen2026-10-10 13:30 UTC233
                    training.name()
58e1fb3943e6atagen2026-10-10 13:30 UTC234
                ),
58e1fb3943e6atagen2026-10-10 13:30 UTC235
            )),
58e1fb3943e6atagen2026-10-10 13:30 UTC236
            _ => Ok(()),
58e1fb3943e6atagen2026-10-10 13:30 UTC237
        }
58e1fb3943e6atagen2026-10-10 13:30 UTC238
    }
58e1fb3943e6atagen2026-10-10 13:30 UTC239
58e1fb3943e6atagen2026-10-10 13:30 UTC240
    /// The techniques a JELQ could use right now without being refused before the roll.
58e1fb3943e6atagen2026-10-10 13:30 UTC241
    pub fn techniques_now(&self, now: Instant) -> Vec<Technique> {
58e1fb3943e6atagen2026-10-10 13:30 UTC242
        let blocked = !self.floor.timed()
58e1fb3943e6atagen2026-10-10 13:30 UTC243
            || self.items.cage
58e1fb3943e6atagen2026-10-10 13:30 UTC244
            || self.story == Story::Pending
58e1fb3943e6atagen2026-10-10 13:30 UTC245
            || self.refractory_until.is_some_and(|until| now < until);
58e1fb3943e6atagen2026-10-10 13:30 UTC246
        if blocked {
58e1fb3943e6atagen2026-10-10 13:30 UTC247
            return Vec::new();
782069860236atagen2026-10-10 12:44 UTC248
        }
58e1fb3943e6atagen2026-10-10 13:30 UTC249
        Technique::ALL
58e1fb3943e6atagen2026-10-10 13:30 UTC250
            .into_iter()
58e1fb3943e6atagen2026-10-10 13:30 UTC251
            .filter(|&technique| match technique {
58e1fb3943e6atagen2026-10-10 13:30 UTC252
                Technique::Utility(Utility::Warmup) => true,
58e1fb3943e6atagen2026-10-10 13:30 UTC253
                Technique::Utility(Utility::Ice) => self.items.ice_packs > 0,
58e1fb3943e6atagen2026-10-10 13:30 UTC254
                Technique::Utility(Utility::Supplement) => self.items.supplements > 0,
58e1fb3943e6atagen2026-10-10 13:30 UTC255
                _ => self.access(technique).is_ok() && self.check_storage().is_ok(),
58e1fb3943e6atagen2026-10-10 13:30 UTC256
            })
58e1fb3943e6atagen2026-10-10 13:30 UTC257
            .collect()
782069860236atagen2026-10-10 12:44 UTC258
    }
782069860236atagen2026-10-10 12:44 UTC259
782069860236atagen2026-10-10 12:44 UTC260
    fn check_foreskin(&self, min: f64, max: f64) -> Result<(), Problem> {
782069860236atagen2026-10-10 12:44 UTC261
        let Some(f) = self.foreskin else {
782069860236atagen2026-10-10 12:44 UTC262
            if min <= 0.0 {
782069860236atagen2026-10-10 12:44 UTC263
                return Ok(());
782069860236atagen2026-10-10 12:44 UTC264
            }
782069860236atagen2026-10-10 12:44 UTC265
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC266
                StatusCode::GONE,
782069860236atagen2026-10-10 12:44 UTC267
                "foreskin-gone",
782069860236atagen2026-10-10 12:44 UTC268
                "This technique requires a foreskin. The foreskin is gone.",
782069860236atagen2026-10-10 12:44 UTC269
            )
782069860236atagen2026-10-10 12:44 UTC270
            .header(header::CONTENT_LOCATION, self.path("/foreskin")));
782069860236atagen2026-10-10 12:44 UTC271
        };
782069860236atagen2026-10-10 12:44 UTC272
        if f < min {
782069860236atagen2026-10-10 12:44 UTC273
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC274
                StatusCode::LENGTH_REQUIRED,
782069860236atagen2026-10-10 12:44 UTC275
                "foreskin-insufficient",
782069860236atagen2026-10-10 12:44 UTC276
                format!(
782069860236atagen2026-10-10 12:44 UTC277
                    "The selected technique requires {min:.1} units of foreskin. {f:.2} units are available. The server has noted the shortfall."
782069860236atagen2026-10-10 12:44 UTC278
                ),
782069860236atagen2026-10-10 12:44 UTC279
            )
782069860236atagen2026-10-10 12:44 UTC280
            .with("required", min));
782069860236atagen2026-10-10 12:44 UTC281
        }
782069860236atagen2026-10-10 12:44 UTC282
        if f > max {
782069860236atagen2026-10-10 12:44 UTC283
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC284
                StatusCode::PAYLOAD_TOO_LARGE,
782069860236atagen2026-10-10 12:44 UTC285
                "foreskin-excessive",
782069860236atagen2026-10-10 12:44 UTC286
                format!(
782069860236atagen2026-10-10 12:44 UTC287
                    "The selected technique tolerates at most {max:.1} units of foreskin. {f:.2} units are present."
782069860236atagen2026-10-10 12:44 UTC288
                ),
782069860236atagen2026-10-10 12:44 UTC289
            )
782069860236atagen2026-10-10 12:44 UTC290
            .with("maximum", max));
782069860236atagen2026-10-10 12:44 UTC291
        }
782069860236atagen2026-10-10 12:44 UTC292
        Ok(())
782069860236atagen2026-10-10 12:44 UTC293
    }
782069860236atagen2026-10-10 12:44 UTC294
782069860236atagen2026-10-10 12:44 UTC295
    fn check_storage(&self) -> Result<(), Problem> {
782069860236atagen2026-10-10 12:44 UTC296
        if self.items.extender && self.girth > STORAGE_GIRTH {
782069860236atagen2026-10-10 12:44 UTC297
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC298
                StatusCode::INSUFFICIENT_STORAGE,
782069860236atagen2026-10-10 12:44 UTC299
                "jeans-full",
782069860236atagen2026-10-10 12:44 UTC300
                "The jeans are full. The extender is occupying the remaining storage.",
782069860236atagen2026-10-10 12:44 UTC301
            )
782069860236atagen2026-10-10 12:44 UTC302
            .with("extender", self.path("/inventory/extender")));
782069860236atagen2026-10-10 12:44 UTC303
        }
782069860236atagen2026-10-10 12:44 UTC304
        Ok(())
782069860236atagen2026-10-10 12:44 UTC305
    }
782069860236atagen2026-10-10 12:44 UTC306
782069860236atagen2026-10-10 12:44 UTC307
    pub(super) fn commit(&mut self, intensity: i32, lube: Option<Lube>, now: Instant, cost: i32) {
782069860236atagen2026-10-10 12:44 UTC308
        self.touch();
782069860236atagen2026-10-10 12:44 UTC309
        self.turn += 1;
782069860236atagen2026-10-10 12:44 UTC310
        self.here.turns += 1;
9683736a2c5eatagen2026-10-10 13:15 UTC311
        self.realise();
782069860236atagen2026-10-10 12:44 UTC312
        if self.items.extender {
782069860236atagen2026-10-10 12:44 UTC313
            self.length += EXTENDER_GAIN;
782069860236atagen2026-10-10 12:44 UTC314
        }
782069860236atagen2026-10-10 12:44 UTC315
        self.stamina -= cost + i32::from(lube == Some(Lube::Lotion));
782069860236atagen2026-10-10 12:44 UTC316
        if self.stamina <= 0 {
782069860236atagen2026-10-10 12:44 UTC317
            self.exhaust(now);
782069860236atagen2026-10-10 12:44 UTC318
        }
9683736a2c5eatagen2026-10-10 13:15 UTC319
        let chill = if self.floor == Floor::ChangingRoom {
9683736a2c5eatagen2026-10-10 13:15 UTC320
            CHANGING_ROOM_CHILL
782069860236atagen2026-10-10 12:44 UTC321
        } else {
782069860236atagen2026-10-10 12:44 UTC322
            0
782069860236atagen2026-10-10 12:44 UTC323
        };
782069860236atagen2026-10-10 12:44 UTC324
        let wobble = self.rng.random_range(-EQ_WOBBLE..=EQ_WOBBLE);
782069860236atagen2026-10-10 12:44 UTC325
        self.eq = (self.eq + intensity * 2 - 6 - chill + wobble).clamp(0, 100);
782069860236atagen2026-10-10 12:44 UTC326
782069860236atagen2026-10-10 12:44 UTC327
        let mut suspicion = intensity / 5 + i32::from(lube == Some(Lube::Vaseline));
9683736a2c5eatagen2026-10-10 13:15 UTC328
        if matches!(self.floor, Floor::ChangingRoom | Floor::Nans) {
782069860236atagen2026-10-10 12:44 UTC329
            suspicion *= 2;
782069860236atagen2026-10-10 12:44 UTC330
        }
9683736a2c5eatagen2026-10-10 13:15 UTC331
        if self.floor == Floor::Nans && lube == Some(Lube::CoconutOil) {
782069860236atagen2026-10-10 12:44 UTC332
            suspicion += 2;
782069860236atagen2026-10-10 12:44 UTC333
        }
782069860236atagen2026-10-10 12:44 UTC334
        let was_below = self.suspicion < SUSPICION_LIMIT;
782069860236atagen2026-10-10 12:44 UTC335
        self.suspicion =
782069860236atagen2026-10-10 12:44 UTC336
            u8::try_from((i32::from(self.suspicion) + suspicion).min(i32::from(SUSPICION_LIMIT)))
782069860236atagen2026-10-10 12:44 UTC337
                .unwrap_or(SUSPICION_LIMIT);
9683736a2c5eatagen2026-10-10 13:15 UTC338
        self.maybe_summon_mum(was_below);
782069860236atagen2026-10-10 12:44 UTC339
782069860236atagen2026-10-10 12:44 UTC340
        if let Some(observer) = self.floor.observer()
782069860236atagen2026-10-10 12:44 UTC341
            && self.chance(OBSERVED)
782069860236atagen2026-10-10 12:44 UTC342
        {
782069860236atagen2026-10-10 12:44 UTC343
            self.observed = Some(observer);
782069860236atagen2026-10-10 12:44 UTC344
            self.shame += 1;
782069860236atagen2026-10-10 12:44 UTC345
            if observer == Observer::Cat && self.chance(CAT_TAKES_TOWEL) {
782069860236atagen2026-10-10 12:44 UTC346
                self.here.towel_stolen = true;
782069860236atagen2026-10-10 12:44 UTC347
            }
782069860236atagen2026-10-10 12:44 UTC348
        }
782069860236atagen2026-10-10 12:44 UTC349
        if intensity >= PULLED_MUSCLE_INTENSITY
782069860236atagen2026-10-10 12:44 UTC350
            && !self.conditions.pulled_muscle
782069860236atagen2026-10-10 12:44 UTC351
            && self.chance(PULLED_MUSCLE)
782069860236atagen2026-10-10 12:44 UTC352
        {
782069860236atagen2026-10-10 12:44 UTC353
            self.conditions.pulled_muscle = true;
782069860236atagen2026-10-10 12:44 UTC354
            self.stamina = self.stamina.min(self.max_stamina());
782069860236atagen2026-10-10 12:44 UTC355
        }
782069860236atagen2026-10-10 12:44 UTC356
        if self.items.ring && self.eq > HOT_EQ && self.chance(RING_PRIAPISM) {
782069860236atagen2026-10-10 12:44 UTC357
            self.conditions.priapism_turns = PRIAPISM_TURNS;
782069860236atagen2026-10-10 12:44 UTC358
        }
782069860236atagen2026-10-10 12:44 UTC359
        if self.conditions.priapism_turns > 0 {
782069860236atagen2026-10-10 12:44 UTC360
            self.conditions.priapism_turns -= 1;
782069860236atagen2026-10-10 12:44 UTC361
            self.eq = self.eq.max(PRIAPISM_EQ);
782069860236atagen2026-10-10 12:44 UTC362
        }
782069860236atagen2026-10-10 12:44 UTC363
        if self.turn == BRANCH_TURN && self.story == Story::Unopened {
782069860236atagen2026-10-10 12:44 UTC364
            self.story = Story::Pending;
782069860236atagen2026-10-10 12:44 UTC365
        }
782069860236atagen2026-10-10 12:44 UTC366
    }
782069860236atagen2026-10-10 12:44 UTC367
9683736a2c5eatagen2026-10-10 13:15 UTC368
    fn maybe_summon_mum(&mut self, was_below: bool) {
9683736a2c5eatagen2026-10-10 13:15 UTC369
        if !self.floor.has_mum() || self.mum.is_some() {
782069860236atagen2026-10-10 12:44 UTC370
            return;
782069860236atagen2026-10-10 12:44 UTC371
        }
782069860236atagen2026-10-10 12:44 UTC372
        let summoned = was_below && self.suspicion == SUSPICION_LIMIT;
782069860236atagen2026-10-10 12:44 UTC373
        let visits = u32::from(self.suspicion) + 1;
9683736a2c5eatagen2026-10-10 13:15 UTC374
        if summoned || self.rng.random_ratio(visits, super::balance::MUM_VISIT) {
782069860236atagen2026-10-10 12:44 UTC375
            let errand = self.errand(summoned);
782069860236atagen2026-10-10 12:44 UTC376
            let nonce = self.fresh_nonce();
9683736a2c5eatagen2026-10-10 13:15 UTC377
            self.mum = Some(MumVisit {
782069860236atagen2026-10-10 12:44 UTC378
                errand,
782069860236atagen2026-10-10 12:44 UTC379
                nonce,
782069860236atagen2026-10-10 12:44 UTC380
                strikes: 0,
782069860236atagen2026-10-10 12:44 UTC381
            });
782069860236atagen2026-10-10 12:44 UTC382
        }
782069860236atagen2026-10-10 12:44 UTC383
    }
782069860236atagen2026-10-10 12:44 UTC384
782069860236atagen2026-10-10 12:44 UTC385
    fn roll(
782069860236atagen2026-10-10 12:44 UTC386
        &mut self,
782069860236atagen2026-10-10 12:44 UTC387
        training: Training,
782069860236atagen2026-10-10 12:44 UTC388
        intensity: i32,
782069860236atagen2026-10-10 12:44 UTC389
        lube: Option<Lube>,
782069860236atagen2026-10-10 12:44 UTC390
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC391
        let profile = training.profile();
782069860236atagen2026-10-10 12:44 UTC392
        let natural: i32 = self.rng.random_range(1..=20);
782069860236atagen2026-10-10 12:44 UTC393
        let band = if EQ_BAND.contains(&self.eq) {
782069860236atagen2026-10-10 12:44 UTC394
            BAND_BONUS
782069860236atagen2026-10-10 12:44 UTC395
        } else {
782069860236atagen2026-10-10 12:44 UTC396
            0
782069860236atagen2026-10-10 12:44 UTC397
        };
782069860236atagen2026-10-10 12:44 UTC398
        let grip = match (self.grip, training) {
782069860236atagen2026-10-10 12:44 UTC399
            (Grip::Underhand, Training::Stretch) => 2,
782069860236atagen2026-10-10 12:44 UTC400
            (Grip::Underhand, Training::Wet) => -1,
782069860236atagen2026-10-10 12:44 UTC401
            _ => 0,
782069860236atagen2026-10-10 12:44 UTC402
        };
782069860236atagen2026-10-10 12:44 UTC403
        let modifier = lube.map_or(0, Lube::modifier) + band + grip;
782069860236atagen2026-10-10 12:44 UTC404
        let dc = profile.dc + intensity / 3;
782069860236atagen2026-10-10 12:44 UTC405
        let total = natural + modifier;
782069860236atagen2026-10-10 12:44 UTC406
        let timing = format!("roll;desc=\"d20{modifier:+} vs {dc}\";dur={natural}");
782069860236atagen2026-10-10 12:44 UTC407
        let timed = |p: Problem| p.header(SERVER_TIMING, &timing);
782069860236atagen2026-10-10 12:44 UTC408
782069860236atagen2026-10-10 12:44 UTC409
        let (length, girth) = self.scaled_gains(&profile, intensity);
782069860236atagen2026-10-10 12:44 UTC410
        let rupture_line = if self.items.ring {
782069860236atagen2026-10-10 12:44 UTC411
            RING_RUPTURE_LINE
782069860236atagen2026-10-10 12:44 UTC412
        } else {
782069860236atagen2026-10-10 12:44 UTC413
            RUPTURE_LINE
782069860236atagen2026-10-10 12:44 UTC414
        };
782069860236atagen2026-10-10 12:44 UTC415
782069860236atagen2026-10-10 12:44 UTC416
        if natural == 1 {
782069860236atagen2026-10-10 12:44 UTC417
            return Err(timed(self.critical_failure(training, &profile)));
782069860236atagen2026-10-10 12:44 UTC418
        }
782069860236atagen2026-10-10 12:44 UTC419
        if natural == 2 && self.eq > DANGER_EQ {
782069860236atagen2026-10-10 12:44 UTC420
            return Err(timed(self.rupture()));
782069860236atagen2026-10-10 12:44 UTC421
        }
782069860236atagen2026-10-10 12:44 UTC422
        if self.eq > rupture_line && total < dc {
782069860236atagen2026-10-10 12:44 UTC423
            return Err(timed(self.rupture()));
782069860236atagen2026-10-10 12:44 UTC424
        }
782069860236atagen2026-10-10 12:44 UTC425
        if natural == 20 {
782069860236atagen2026-10-10 12:44 UTC426
            self.gain(length * 2.0, girth * 2.0, profile.foreskin_growth);
782069860236atagen2026-10-10 12:44 UTC427
            return Ok(Outcome::json(
782069860236atagen2026-10-10 12:44 UTC428
                StatusCode::PERMANENT_REDIRECT,
782069860236atagen2026-10-10 12:44 UTC429
                json!({
782069860236atagen2026-10-10 12:44 UTC430
                    "detail": "Your length has moved permanently.",
782069860236atagen2026-10-10 12:44 UTC431
                    "length": round3(length * 2.0),
782069860236atagen2026-10-10 12:44 UTC432
                    "girth": round3(girth * 2.0),
782069860236atagen2026-10-10 12:44 UTC433
                }),
782069860236atagen2026-10-10 12:44 UTC434
            )
782069860236atagen2026-10-10 12:44 UTC435
            .header(header::LOCATION, self.path("/length"))
782069860236atagen2026-10-10 12:44 UTC436
            .header(SERVER_TIMING, timing));
782069860236atagen2026-10-10 12:44 UTC437
        }
782069860236atagen2026-10-10 12:44 UTC438
        if total < dc - 3 {
782069860236atagen2026-10-10 12:44 UTC439
            return Err(timed(not_extended()));
782069860236atagen2026-10-10 12:44 UTC440
        }
782069860236atagen2026-10-10 12:44 UTC441
        if total < dc {
782069860236atagen2026-10-10 12:44 UTC442
            return Ok(Outcome::empty(StatusCode::NO_CONTENT).header(SERVER_TIMING, timing));
782069860236atagen2026-10-10 12:44 UTC443
        }
782069860236atagen2026-10-10 12:44 UTC444
782069860236atagen2026-10-10 12:44 UTC445
        let outcome = if self.eq < *EQ_BAND.start() {
782069860236atagen2026-10-10 12:44 UTC446
            self.gain(length / 2.0, girth / 2.0, profile.foreskin_growth);
782069860236atagen2026-10-10 12:44 UTC447
            Outcome::json(
782069860236atagen2026-10-10 12:44 UTC448
                StatusCode::PARTIAL_CONTENT,
782069860236atagen2026-10-10 12:44 UTC449
                json!({
782069860236atagen2026-10-10 12:44 UTC450
                    "detail": "Partial gains were applied. EQ was below the band.",
782069860236atagen2026-10-10 12:44 UTC451
                    "length": round3(length / 2.0),
782069860236atagen2026-10-10 12:44 UTC452
                    "girth": round3(girth / 2.0),
782069860236atagen2026-10-10 12:44 UTC453
                }),
782069860236atagen2026-10-10 12:44 UTC454
            )
782069860236atagen2026-10-10 12:44 UTC455
        } else if total >= dc + 5 {
782069860236atagen2026-10-10 12:44 UTC456
            self.gain(length, girth, profile.foreskin_growth);
782069860236atagen2026-10-10 12:44 UTC457
            Outcome::json(
782069860236atagen2026-10-10 12:44 UTC458
                StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC459
                json!({
782069860236atagen2026-10-10 12:44 UTC460
                    "detail": "Gains were applied.",
782069860236atagen2026-10-10 12:44 UTC461
                    "length": round3(length),
782069860236atagen2026-10-10 12:44 UTC462
                    "girth": round3(girth),
782069860236atagen2026-10-10 12:44 UTC463
                }),
782069860236atagen2026-10-10 12:44 UTC464
            )
782069860236atagen2026-10-10 12:44 UTC465
        } else {
782069860236atagen2026-10-10 12:44 UTC466
            let eta = self.rng.random_range(1..=4);
782069860236atagen2026-10-10 12:44 UTC467
            self.pending.push(Pending {
782069860236atagen2026-10-10 12:44 UTC468
                due: self.turn + eta,
782069860236atagen2026-10-10 12:44 UTC469
                length,
782069860236atagen2026-10-10 12:44 UTC470
                girth,
782069860236atagen2026-10-10 12:44 UTC471
            });
782069860236atagen2026-10-10 12:44 UTC472
            self.gain(0.0, 0.0, profile.foreskin_growth);
782069860236atagen2026-10-10 12:44 UTC473
            Outcome::json(
782069860236atagen2026-10-10 12:44 UTC474
                StatusCode::ACCEPTED,
782069860236atagen2026-10-10 12:44 UTC475
                json!({
782069860236atagen2026-10-10 12:44 UTC476
                    "detail": "Gains have been queued. They may arrive.",
782069860236atagen2026-10-10 12:44 UTC477
                    "eta_turns": eta,
782069860236atagen2026-10-10 12:44 UTC478
                }),
782069860236atagen2026-10-10 12:44 UTC479
            )
782069860236atagen2026-10-10 12:44 UTC480
        };
782069860236atagen2026-10-10 12:44 UTC481
        Ok(outcome.header(SERVER_TIMING, timing))
782069860236atagen2026-10-10 12:44 UTC482
    }
782069860236atagen2026-10-10 12:44 UTC483
782069860236atagen2026-10-10 12:44 UTC484
    fn scaled_gains(&self, profile: &Profile, intensity: i32) -> (f64, f64) {
782069860236atagen2026-10-10 12:44 UTC485
        let scale = f64::from(intensity);
782069860236atagen2026-10-10 12:44 UTC486
        let (mut length, mut girth) = (profile.length * scale, profile.girth * scale);
782069860236atagen2026-10-10 12:44 UTC487
        if self.here.teapot {
782069860236atagen2026-10-10 12:44 UTC488
            length *= 0.5;
782069860236atagen2026-10-10 12:44 UTC489
            girth *= 1.5;
782069860236atagen2026-10-10 12:44 UTC490
        }
782069860236atagen2026-10-10 12:44 UTC491
        if self.items.ring {
782069860236atagen2026-10-10 12:44 UTC492
            girth *= 2.0;
782069860236atagen2026-10-10 12:44 UTC493
        }
782069860236atagen2026-10-10 12:44 UTC494
        (length, girth)
782069860236atagen2026-10-10 12:44 UTC495
    }
782069860236atagen2026-10-10 12:44 UTC496
782069860236atagen2026-10-10 12:44 UTC497
    fn critical_failure(&mut self, training: Training, profile: &Profile) -> Problem {
782069860236atagen2026-10-10 12:44 UTC498
        let uses_foreskin = profile.foreskin.is_some_and(|(min, _)| min > 0.0);
782069860236atagen2026-10-10 12:44 UTC499
        if uses_foreskin && self.foreskin.is_some() {
782069860236atagen2026-10-10 12:44 UTC500
            self.foreskin = None;
782069860236atagen2026-10-10 12:44 UTC501
            self.shame += 25;
782069860236atagen2026-10-10 12:44 UTC502
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC503
                StatusCode::GONE,
782069860236atagen2026-10-10 12:44 UTC504
                "foreskin-lost",
782069860236atagen2026-10-10 12:44 UTC505
                "The technique has removed the foreskin. It will not be returning.",
782069860236atagen2026-10-10 12:44 UTC506
            )
782069860236atagen2026-10-10 12:44 UTC507
            .header(header::CONTENT_LOCATION, self.path("/foreskin"));
782069860236atagen2026-10-10 12:44 UTC508
        }
782069860236atagen2026-10-10 12:44 UTC509
        if self.eq > DANGER_EQ {
782069860236atagen2026-10-10 12:44 UTC510
            return self.rupture();
782069860236atagen2026-10-10 12:44 UTC511
        }
782069860236atagen2026-10-10 12:44 UTC512
        if training == Training::Stretch && !self.here.teapot {
782069860236atagen2026-10-10 12:44 UTC513
            self.here.teapot = true;
782069860236atagen2026-10-10 12:44 UTC514
            self.items.teapot_spout = true;
782069860236atagen2026-10-10 12:44 UTC515
            self.shame += 5;
782069860236atagen2026-10-10 12:44 UTC516
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC517
                StatusCode::IM_A_TEAPOT,
782069860236atagen2026-10-10 12:44 UTC518
                "teapot",
782069860236atagen2026-10-10 12:44 UTC519
                "The subject is now short and stout. Length gains are halved for the rest of the floor.",
782069860236atagen2026-10-10 12:44 UTC520
            );
782069860236atagen2026-10-10 12:44 UTC521
        }
782069860236atagen2026-10-10 12:44 UTC522
        if training == Training::Uli && !self.conditions.peyronie {
782069860236atagen2026-10-10 12:44 UTC523
            self.conditions.peyronie = true;
782069860236atagen2026-10-10 12:44 UTC524
            self.shame += 5;
782069860236atagen2026-10-10 12:44 UTC525
            return Problem::new(
782069860236atagen2026-10-10 12:44 UTC526
                StatusCode::MISDIRECTED_REQUEST,
782069860236atagen2026-10-10 12:44 UTC527
                "curvature",
782069860236atagen2026-10-10 12:44 UTC528
                "A curvature has developed. Techniques will land slightly to the left until a professional is consulted.",
782069860236atagen2026-10-10 12:44 UTC529
            );
782069860236atagen2026-10-10 12:44 UTC530
        }
782069860236atagen2026-10-10 12:44 UTC531
        not_extended()
782069860236atagen2026-10-10 12:44 UTC532
    }
782069860236atagen2026-10-10 12:44 UTC533
782069860236atagen2026-10-10 12:44 UTC534
    fn gossip(&mut self, mut outcome: Outcome) -> Outcome {
782069860236atagen2026-10-10 12:44 UTC535
        let reportable = matches!(outcome.status.as_u16(), 200 | 202 | 206);
9683736a2c5eatagen2026-10-10 13:15 UTC536
        if !self.here.flatmate_told || !reportable {
782069860236atagen2026-10-10 12:44 UTC537
            return outcome;
782069860236atagen2026-10-10 12:44 UTC538
        }
9683736a2c5eatagen2026-10-10 13:15 UTC539
        self.here.flatmate_told = false;
782069860236atagen2026-10-10 12:44 UTC540
        outcome.status = StatusCode::ALREADY_REPORTED;
782069860236atagen2026-10-10 12:44 UTC541
        if let Payload::Json(body) = &mut outcome.payload {
9683736a2c5eatagen2026-10-10 13:15 UTC542
            body["detail"] = json!("The flatmate has already told everyone.");
782069860236atagen2026-10-10 12:44 UTC543
        }
782069860236atagen2026-10-10 12:44 UTC544
        outcome
782069860236atagen2026-10-10 12:44 UTC545
    }
782069860236atagen2026-10-10 12:44 UTC546
}