atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
a6caa2f9978665374ee51870e78151e4324328d0
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/turn.rsRust763 lines26.9 KB
1
use std::{fmt::Write as _, ops::ControlFlow};
2
3
use axum::http::{StatusCode, header};
4
use serde_json::json;
5
6
use super::{
7
    Floor, Grip, LUBRICANTS, Lube, Outcome, Payload, Run, Story, Technique,
8
    balance::{
9
        ARRIVAL_BONUS, BAND_BONUS, BRANCH_TURN, CAT_TAKES_TOWEL, CHANGING_ROOM_CHILL, DANGER_EQ,
10
        EQ_BAND, EQ_WOBBLE, EXTENDER_GAIN, FRICTION_BURN, FRICTION_BURN_AT, FRICTION_DRY,
11
        FRICTION_GRIP, FRICTION_HOLD, FRICTION_MAX, FRICTION_MEDITATE, FRICTION_SPIT, GREED_HOT,
12
        GREED_WARM, HOLD_EQ, HOT_EQ, MEDITATION_EQ, OBSERVED, PRIAPISM_EQ, PRIAPISM_TURNS,
13
        PULLED_MUSCLE, PULLED_MUSCLE_INTENSITY, REFLECTION, RING_PRIAPISM, RING_RUPTURE_LINE,
14
        RUPTURE_HOLD, RUPTURE_LINE, STORAGE_GIRTH, SUSPICION_LIMIT,
15
    },
16
    not_extended, round3,
17
    state::{MumVisit, Observer, Pending},
18
    techniques::{LubeRule, Profile, Training, Utility},
19
};
20
use crate::{headers::SERVER_TIMING, problem::Problem};
21
22
impl Run {
23
    pub fn jelq(
24
        &mut self,
25
        techniques: &[Technique],
26
        intensity: i32,
27
        lube: Option<Lube>,
28
    ) -> Result<Outcome, Problem> {
29
        self.check_exhausted()?;
30
        if self.story == Story::Pending {
31
            return Ok(self.branch_offer());
32
        }
33
        if self.floor == Floor::Forum {
34
            return Ok(Outcome::json(
35
                StatusCode::SEE_OTHER,
36
                json!({ "detail": "Refer to the forum." }),
37
            )
38
            .header(header::LOCATION, super::FORUM_THREAD));
39
        }
40
        if self.items.cage {
41
            return Err(Problem::new(
42
                StatusCode::LOCKED,
43
                "caged",
44
                "The cage is locked. The key is not in the box.",
45
            ));
46
        }
47
        match *techniques {
48
            [Technique::Utility(utility)] => self.utility(utility, lube),
49
            [Technique::Training(training)] => self.single(training, intensity, lube),
50
            [Technique::Training(a), Technique::Training(b)] => self.combo(a, b, intensity, lube),
51
            [_, _] => Err(Problem::new(
52
                StatusCode::BAD_REQUEST,
53
                "combo-invalid",
54
                "Only training techniques can be combined.",
55
            )),
56
            _ => Err(Problem::new(
57
                StatusCode::BAD_REQUEST,
58
                "too-many-techniques",
59
                "Two techniques at most. The server has limits even if you do not.",
60
            )),
61
        }
62
    }
63
64
    fn utility(&mut self, utility: Utility, lube: Option<Lube>) -> Result<Outcome, Problem> {
65
        match utility {
66
            Utility::Warmup => return self.warm_up(),
67
            Utility::Ice => return self.ice(),
68
            Utility::Supplement => return self.supplement(),
69
            Utility::Meditate => return self.meditate(),
70
            Utility::Energy => return self.energy(),
71
            Utility::Edge | Utility::Pump => {}
72
        }
73
        self.ready(Technique::Utility(utility), lube)?;
74
        self.check_storage()?;
75
        self.floor_enemy()?;
76
        match utility {
77
            Utility::Edge => self.edge(),
78
            _ => self.pump(),
79
        }
80
    }
81
82
    fn single(
83
        &mut self,
84
        training: Training,
85
        intensity: i32,
86
        lube: Option<Lube>,
87
    ) -> Result<Outcome, Problem> {
88
        self.ready(Technique::Training(training), lube)?;
89
        self.check_storage()?;
90
        self.lube_available(lube)?;
91
        self.floor_enemy()?;
92
        self.take_lube(lube)?;
93
        self.chafe(lube, intensity);
94
        self.commit(intensity, lube, 1);
95
        if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
96
            return Ok(interrupted);
97
        }
98
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
99
            return Ok(interrupted);
100
        }
101
        let outcome = self.roll(training, intensity, lube)?;
102
        Ok(self.gossip(outcome))
103
    }
104
105
    fn combo(
106
        &mut self,
107
        a: Training,
108
        b: Training,
109
        intensity: i32,
110
        lube: Option<Lube>,
111
    ) -> Result<Outcome, Problem> {
112
        if a == b {
113
            return Err(Problem::new(
114
                StatusCode::BAD_REQUEST,
115
                "combo-invalid",
116
                "A technique cannot be combined with itself. The server has checked.",
117
            ));
118
        }
119
        self.ready(Technique::Training(a), lube)?;
120
        self.ready(Technique::Training(b), lube)?;
121
        self.check_storage()?;
122
        self.lube_available(lube)?;
123
        self.floor_enemy()?;
124
        self.take_lube(lube)?;
125
        self.chafe(lube, intensity);
126
        self.commit(intensity, lube, 2);
127
        for training in [a, b] {
128
            if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
129
                return Ok(interrupted);
130
            }
131
        }
132
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
133
            return Ok(interrupted);
134
        }
135
136
        let mut statuses = Vec::new();
137
        for training in [a, b] {
138
            let result = self.roll(training, intensity, lube);
139
            if self.ended.is_some() {
140
                return result;
141
            }
142
            let status = match result {
143
                Ok(outcome) => self.gossip(outcome).status,
144
                Err(problem) => problem.status(),
145
            };
146
            statuses.push((training, status));
147
        }
148
149
        let mut xml = String::from(
150
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n",
151
        );
152
        for (training, status) in statuses {
153
            let _ = write!(
154
                xml,
155
                "  <D:response>\n    <D:href>{}?technique={}</D:href>\n    <D:status>HTTP/1.1 {} {}</D:status>\n  </D:response>\n",
156
                self.path(""),
157
                training.name(),
158
                status.as_u16(),
159
                crate::problem::reason(status),
160
            );
161
        }
162
        xml.push_str(
163
            "  <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n",
164
        );
165
        Ok(Outcome::raw(
166
            StatusCode::MULTI_STATUS,
167
            "application/xml; charset=utf-8",
168
            xml,
169
        ))
170
    }
171
172
    fn ready(&self, technique: Technique, lube: Option<Lube>) -> Result<(), Problem> {
173
        self.access(technique)?;
174
        match technique {
175
            Technique::Training(training) => Self::check_lube(training, lube),
176
            Technique::Utility(_) => Ok(()),
177
        }
178
    }
179
180
    /// Whether the run could attempt the technique now, ignoring the lubricant.
181
    fn access(&self, technique: Technique) -> Result<(), Problem> {
182
        if technique == Technique::Training(Training::Uli) && !self.uli_taught {
183
            return Err(Problem::new(
184
                StatusCode::FORBIDDEN,
185
                "technique-locked",
186
                "uli is discussed on the forum, in a thread from 2009, by a man who never posted again. You have not found the reply.",
187
            ));
188
        }
189
        if !self.warmed_up {
190
            return Err(Problem::new(
191
                StatusCode::PRECONDITION_FAILED,
192
                "cold-start",
193
                "Warm-up was skipped. The server has noted this.",
194
            )
195
            .with("technique", Utility::Warmup.name()));
196
        }
197
        match technique {
198
            Technique::Utility(Utility::Pump) if !self.items.pump_pro => Err(Problem::new(
199
                StatusCode::FAILED_DEPENDENCY,
200
                "no-pump",
201
                "The pump technique depends on a pump.",
202
            )),
203
            Technique::Utility(_) => Ok(()),
204
            Technique::Training(training) => match training.profile().foreskin {
205
                Some((min, max)) => self.check_foreskin(min, max),
206
                None => Ok(()),
207
            },
208
        }
209
    }
210
211
    fn check_lube(training: Training, lube: Option<Lube>) -> Result<(), Problem> {
212
        match (training.profile().lube, lube) {
213
            (LubeRule::Required, None) => Err(Problem::new(
214
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
215
                "lubricant-required",
216
                format!(
217
                    "The {} technique requires a lubricant. None was declared.",
218
                    training.name()
219
                ),
220
            )
221
            .with("lubricants", json!(LUBRICANTS))),
222
            (LubeRule::Forbidden, Some(_)) => Err(Problem::new(
223
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
224
                "lubricant-declared",
225
                format!(
226
                    "The {} technique was requested. A lubricant was declared.",
227
                    training.name()
228
                ),
229
            )),
230
            _ => Ok(()),
231
        }
232
    }
233
234
    /// One unit of whatever was declared. Spit is free and the tap is always on.
235
    fn lube_available(&self, lube: Option<Lube>) -> Result<(), Problem> {
236
        let (held, name) = match lube {
237
            None | Some(Lube::Spit) => return Ok(()),
238
            Some(Lube::BabyOil) => (&self.items.baby_oil, "baby-oil"),
239
            Some(Lube::CoconutOil) => (&self.items.coconut_oil, "coconut-oil"),
240
            Some(Lube::Lotion) => (&self.items.lotion, "lotion"),
241
            Some(Lube::Vaseline) => (&self.items.vaseline, "vaseline"),
242
        };
243
        if *held == 0 {
244
            return Err(Problem::new(
245
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
246
                "lubricant-exhausted",
247
                format!("{name} was declared, but the bottle is empty."),
248
            ));
249
        }
250
        Ok(())
251
    }
252
253
    fn take_lube(&mut self, lube: Option<Lube>) -> Result<(), Problem> {
254
        self.lube_available(lube)?;
255
        let held = match lube {
256
            None | Some(Lube::Spit) => return Ok(()),
257
            Some(Lube::BabyOil) => &mut self.items.baby_oil,
258
            Some(Lube::CoconutOil) => &mut self.items.coconut_oil,
259
            Some(Lube::Lotion) => &mut self.items.lotion,
260
            Some(Lube::Vaseline) => &mut self.items.vaseline,
261
        };
262
        *held -= 1;
263
        Ok(())
264
    }
265
266
    /// The techniques a JELQ could use right now without being refused before the roll.
267
    pub fn techniques_now(&self) -> Vec<Technique> {
268
        let blocked = !self.floor.timed() || self.items.cage || self.exhausted;
269
        if blocked {
270
            return Vec::new();
271
        }
272
        Technique::ALL
273
            .into_iter()
274
            .filter(|&technique| match technique {
275
                Technique::Utility(Utility::Warmup) => true,
276
                Technique::Utility(Utility::Ice) => self.items.ice_packs > 0,
277
                Technique::Utility(Utility::Supplement) => self.items.supplements > 0,
278
                Technique::Utility(Utility::Meditate) => !self.here.meditated,
279
                Technique::Utility(Utility::Energy) => self.items.energy_drinks > 0,
280
                _ => self.access(technique).is_ok() && self.check_storage().is_ok(),
281
            })
282
            .collect()
283
    }
284
285
    fn check_foreskin(&self, min: f64, max: f64) -> Result<(), Problem> {
286
        let Some(f) = self.foreskin else {
287
            if min <= 0.0 {
288
                return Ok(());
289
            }
290
            return Err(Problem::new(
291
                StatusCode::GONE,
292
                "foreskin-gone",
293
                "This technique requires a foreskin. The foreskin is gone.",
294
            )
295
            .header(header::CONTENT_LOCATION, self.path("/foreskin")));
296
        };
297
        if f < min {
298
            return Err(Problem::new(
299
                StatusCode::LENGTH_REQUIRED,
300
                "foreskin-insufficient",
301
                format!(
302
                    "The selected technique requires {min:.1} units of foreskin. {f:.2} units are available. The server has noted the shortfall."
303
                ),
304
            )
305
            .with("required", min));
306
        }
307
        if f > max {
308
            return Err(Problem::new(
309
                StatusCode::PAYLOAD_TOO_LARGE,
310
                "foreskin-excessive",
311
                format!(
312
                    "The selected technique tolerates at most {max:.1} units of foreskin. {f:.2} units are present."
313
                ),
314
            )
315
            .with("maximum", max));
316
        }
317
        Ok(())
318
    }
319
320
    fn check_storage(&self) -> Result<(), Problem> {
321
        if self.items.extender && self.girth > STORAGE_GIRTH {
322
            return Err(Problem::new(
323
                StatusCode::INSUFFICIENT_STORAGE,
324
                "jeans-full",
325
                "The jeans are full. The extender is occupying the remaining storage.",
326
            )
327
            .with("extender", self.path("/inventory/extender")));
328
        }
329
        Ok(())
330
    }
331
332
    pub(super) fn chafe(&mut self, lube: Option<Lube>, intensity: i32) {
333
        let (base, slip) = match lube {
334
            None => (FRICTION_DRY, 0),
335
            Some(Lube::Spit) => (FRICTION_SPIT, 0),
336
            Some(Lube::Lotion) => (0, 0),
337
            Some(Lube::BabyOil | Lube::CoconutOil) => (0, 1),
338
            Some(Lube::Vaseline) => (0, 2),
339
        };
340
        let vigour = match intensity {
341
            ..=5 => 0,
342
            6..=8 => 1,
343
            _ => 2,
344
        };
345
        let mut friction = (base + vigour - slip).max(0);
346
        if friction > 0 && self.jelq_grip.is_some_and(|grip| grip != self.grip) {
347
            friction -= FRICTION_GRIP;
348
        }
349
        self.jelq_grip = Some(self.grip);
350
        self.friction = (self.friction + friction).min(FRICTION_MAX);
351
        if self.friction >= FRICTION_BURN_AT
352
            && !self.conditions.friction_burn
353
            && self.chance(
354
                self.decision(
355
                    "friction-burn",
356
                    "With the skin this raw, has it finally burned?",
357
                ),
358
                FRICTION_BURN,
359
            )
360
        {
361
            self.conditions.friction_burn = true;
362
        }
363
    }
364
365
    pub(super) fn commit(&mut self, intensity: i32, lube: Option<Lube>, cost: i32) {
366
        self.touch();
367
        self.turn += 1;
368
        self.here.turns += 1;
369
        self.step_away();
370
        self.realise();
371
        if self.items.extender {
372
            self.length += EXTENDER_GAIN;
373
        }
374
        self.stamina -= cost + i32::from(lube == Some(Lube::Lotion));
375
        if self.stamina <= 0 {
376
            self.exhaust();
377
        }
378
        let chill = if self.floor == Floor::ChangingRoom {
379
            CHANGING_ROOM_CHILL
380
        } else {
381
            0
382
        };
383
        let wobble = self.oracle.span_int(
384
            self.decision(
385
                "wobble",
386
                "How does the count drift on this stroke, up or down?",
387
            ),
388
            -EQ_WOBBLE,
389
            EQ_WOBBLE,
390
        );
391
        self.eq = (self.eq + intensity * 2 - 6 - chill + wobble).clamp(0, 100);
392
393
        let mut suspicion = intensity / 5 + i32::from(lube == Some(Lube::Vaseline));
394
        if matches!(self.floor, Floor::ChangingRoom | Floor::Nans) {
395
            suspicion *= 2;
396
        }
397
        if self.floor == Floor::Nans && lube == Some(Lube::CoconutOil) {
398
            suspicion += 2;
399
        }
400
        let was_below = self.suspicion < SUSPICION_LIMIT;
401
        self.suspicion =
402
            u8::try_from((i32::from(self.suspicion) + suspicion).min(i32::from(SUSPICION_LIMIT)))
403
                .unwrap_or(SUSPICION_LIMIT);
404
405
        if intensity >= PULLED_MUSCLE_INTENSITY
406
            && !self.conditions.pulled_muscle
407
            && self.oracle.chance_not(
408
                self.decision("pulled-muscle", "Do the muscles hold?"),
409
                PULLED_MUSCLE,
410
            )
411
        {
412
            self.conditions.pulled_muscle = true;
413
            self.stamina = self.stamina.min(self.max_stamina());
414
        }
415
        self.settle(was_below);
416
    }
417
418
    /// The part of a turn the house takes part in, whatever the action was.
419
    pub(super) fn settle(&mut self, was_below: bool) {
420
        self.maybe_summon_mum(was_below);
421
        if let Some(observer) = self.floor.observer()
422
            && self.chance(
423
                self.decision("observed", "Has someone quietly noticed what is going on?"),
424
                OBSERVED,
425
            )
426
        {
427
            self.observed = Some(observer);
428
            self.shame += 1;
429
            if observer == Observer::Cat
430
                && self.chance(
431
                    self.decision("cat-takes-towel", "Has the cat taken the towel?"),
432
                    CAT_TAKES_TOWEL,
433
                )
434
            {
435
                self.here.towel_stolen = true;
436
            }
437
        }
438
        if self.items.ring
439
            && self.eq > HOT_EQ
440
            && self.chance(
441
                self.decision(
442
                    "ring-priapism",
443
                    "With the ring still on and everything this warm, will it not go down?",
444
                ),
445
                RING_PRIAPISM,
446
            )
447
        {
448
            self.conditions.priapism_turns = PRIAPISM_TURNS;
449
        }
450
        if self.conditions.priapism_turns > 0 {
451
            self.conditions.priapism_turns -= 1;
452
            self.eq = self.eq.max(PRIAPISM_EQ);
453
        }
454
        if self.turn == BRANCH_TURN && self.story == Story::Unopened {
455
            self.story = Story::Pending;
456
            self.notices
457
                .push("Somewhere, a forum thread from 2009 still has replies.");
458
        }
459
    }
460
461
    /// HOLD. A pause between strokes, offered when the band has been left or the stamina is gone.
462
    pub fn can_hold(&self) -> bool {
463
        self.eq > DANGER_EQ || self.stamina <= 2
464
    }
465
466
    pub fn hold(&mut self) -> Result<Outcome, Problem> {
467
        if self.story == Story::Pending {
468
            return Ok(self.branch_offer());
469
        }
470
        if self.items.cage {
471
            return Err(Problem::new(
472
                StatusCode::LOCKED,
473
                "caged",
474
                "The cage is locked. The key is not in the box.",
475
            ));
476
        }
477
        self.touch();
478
        self.turn += 1;
479
        self.here.turns += 1;
480
        self.step_away();
481
        self.realise();
482
        if self.oracle.chance_not(
483
            self.decision("rupture", "The pause is uneventful"),
484
            RUPTURE_HOLD,
485
        ) {
486
            return Err(self.rupture());
487
        }
488
        let was_below = self.suspicion < SUSPICION_LIMIT;
489
        if self.exhausted {
490
            self.recover();
491
            self.shed(FRICTION_HOLD);
492
            self.settle(was_below);
493
            return Ok(Outcome::json(
494
                StatusCode::OK,
495
                json!({ "detail": "The count went well past ten. Stamina is back. Everything else has gone quiet, and you are cold again." }),
496
            ));
497
        }
498
        if self.chance(
499
            self.decision(
500
                "reflection",
501
                "In the quiet afterwards, have you caught your own eye in the mirror?",
502
            ),
503
            REFLECTION,
504
        ) {
505
            self.eq = 0;
506
            self.shame += 1;
507
            self.notices
508
                .push("the count reached ten, and then the reflection started");
509
        } else {
510
            self.eq = (self.eq - HOLD_EQ).clamp(0, 100);
511
        }
512
        self.stamina = (self.stamina + 1).min(self.max_stamina());
513
        self.shed(FRICTION_HOLD);
514
        self.settle(was_below);
515
        Ok(Outcome::empty(StatusCode::NO_CONTENT))
516
    }
517
518
    /// The breathing technique from the forum's replies. It spends no stamina and the turn is
519
    /// not the floor's, but each floor allows it once.
520
    fn meditate(&mut self) -> Result<Outcome, Problem> {
521
        if self.here.meditated {
522
            return Err(Problem::new(
523
                StatusCode::CONFLICT,
524
                "already-meditated",
525
                "This floor has had its meditation.",
526
            ));
527
        }
528
        self.here.meditated = true;
529
        self.step_away();
530
        self.reset_eq(self.eq - MEDITATION_EQ);
531
        self.shed(FRICTION_MEDITATE);
532
        self.touch();
533
        Ok(Outcome::json(
534
            StatusCode::OK,
535
            json!({
536
                "detail": "In through the nose, out through the mouth. EQ falls by ten.",
537
            }),
538
        ))
539
    }
540
541
    pub(super) fn maybe_summon_mum(&mut self, was_below: bool) {
542
        if !self.floor.has_mum() || self.mum.is_some() {
543
            return;
544
        }
545
        let summoned = was_below && self.suspicion == SUSPICION_LIMIT;
546
        let mut visits = u32::from(self.suspicion) + 1;
547
        if self.here.brother_told {
548
            visits *= 2;
549
        }
550
        if summoned
551
            || self.oracle.ratio(
552
                self.decision(
553
                    "mum-summon",
554
                    "With this much noise in the house, is Mum on her way up?",
555
                ),
556
                visits,
557
                super::balance::MUM_VISIT,
558
            )
559
        {
560
            let errand = self.errand(summoned);
561
            let nonce = self.fresh_nonce();
562
            self.mum = Some(MumVisit {
563
                errand,
564
                nonce,
565
                strikes: 0,
566
                knocked: false,
567
            });
568
        }
569
    }
570
571
    fn roll(
572
        &mut self,
573
        training: Training,
574
        intensity: i32,
575
        lube: Option<Lube>,
576
    ) -> Result<Outcome, Problem> {
577
        let profile = training.profile();
578
        let natural: i32 = self.oracle.span_int(
579
            self.decision(
580
                "natural",
581
                "What does the twenty-sided die show for this technique?",
582
            ),
583
            1,
584
            20,
585
        );
586
        let band = if EQ_BAND.contains(&self.eq) {
587
            BAND_BONUS
588
        } else {
589
            0
590
        };
591
        let grip = match (self.grip, training) {
592
            (Grip::Underhand, Training::Stretch) => 2,
593
            (Grip::Underhand, Training::Wet) => -1,
594
            _ => 0,
595
        };
596
        let arrival = if self.here.warm_arrival {
597
            self.here.warm_arrival = false;
598
            ARRIVAL_BONUS
599
        } else {
600
            0
601
        };
602
        let modifier = lube.map_or(0, Lube::modifier) + band + grip + arrival;
603
        let dc = profile.dc + intensity / 3;
604
        let total = natural + modifier;
605
        let timing = format!("roll;desc=\"d20{modifier:+} vs {dc}\";dur={natural}");
606
        let timed = |p: Problem| p.header(SERVER_TIMING, &timing);
607
608
        let (length, girth) = self.scaled_gains(&profile, intensity);
609
        let burn = self.burn();
610
        let rupture_line = if self.items.ring {
611
            RING_RUPTURE_LINE
612
        } else {
613
            RUPTURE_LINE
614
        };
615
616
        if natural == 1 {
617
            return Err(timed(self.critical_failure(training, &profile)));
618
        }
619
        if natural == 2 && self.eq > DANGER_EQ - burn {
620
            return Err(timed(self.rupture()));
621
        }
622
        if self.eq > rupture_line - burn && total < dc {
623
            return Err(timed(self.rupture()));
624
        }
625
        if natural == 20 {
626
            self.gain(length * 2.0, girth * 2.0, profile.foreskin_growth);
627
            return Ok(Outcome::json(
628
                StatusCode::PERMANENT_REDIRECT,
629
                json!({
630
                    "detail": "Your length has moved permanently.",
631
                    "length": round3(length * 2.0),
632
                    "girth": round3(girth * 2.0),
633
                }),
634
            )
635
            .header(header::LOCATION, self.path("/length"))
636
            .header(SERVER_TIMING, timing));
637
        }
638
        if total < dc - 3 {
639
            return Err(timed(not_extended()));
640
        }
641
        if total < dc {
642
            return Ok(Outcome::empty(StatusCode::NO_CONTENT).header(SERVER_TIMING, timing));
643
        }
644
645
        let outcome = if self.eq < *EQ_BAND.start() {
646
            self.gain(length / 2.0, girth / 2.0, profile.foreskin_growth);
647
            Outcome::json(
648
                StatusCode::PARTIAL_CONTENT,
649
                json!({
650
                    "detail": "Partial gains were applied. EQ was below the band.",
651
                    "length": round3(length / 2.0),
652
                    "girth": round3(girth / 2.0),
653
                }),
654
            )
655
        } else if total >= dc + 5 {
656
            self.gain(length, girth, profile.foreskin_growth);
657
            Outcome::json(
658
                StatusCode::OK,
659
                json!({
660
                    "detail": "Gains were applied.",
661
                    "length": round3(length),
662
                    "girth": round3(girth),
663
                }),
664
            )
665
        } else {
666
            let eta = self.oracle.span_int(
667
                self.decision("eta", "In how many turns will the queued gains arrive?"),
668
                1,
669
                4,
670
            ) as u32;
671
            self.pending.push(Pending {
672
                due: self.turn + eta,
673
                length,
674
                girth,
675
            });
676
            self.gain(0.0, 0.0, profile.foreskin_growth);
677
            Outcome::json(
678
                StatusCode::ACCEPTED,
679
                json!({
680
                    "detail": "Gains have been queued. They may arrive.",
681
                    "eta_turns": eta,
682
                }),
683
            )
684
        };
685
        Ok(outcome.header(SERVER_TIMING, timing))
686
    }
687
688
    fn scaled_gains(&self, profile: &Profile, intensity: i32) -> (f64, f64) {
689
        let scale = f64::from(intensity);
690
        let (mut length, mut girth) = (profile.length * scale, profile.girth * scale);
691
        let greed = if self.eq > HOT_EQ {
692
            GREED_HOT
693
        } else if self.eq > DANGER_EQ {
694
            GREED_WARM
695
        } else {
696
            1.0
697
        };
698
        length *= greed;
699
        girth *= greed;
700
        if self.here.teapot {
701
            length *= 0.5;
702
            girth *= 1.5;
703
        }
704
        if self.items.ring {
705
            girth *= 2.0;
706
        }
707
        if self.conditions.friction_burn {
708
            length *= 0.5;
709
            girth *= 0.5;
710
        }
711
        (length, girth)
712
    }
713
714
    fn critical_failure(&mut self, training: Training, profile: &Profile) -> Problem {
715
        let uses_foreskin = profile.foreskin.is_some_and(|(min, _)| min > 0.0);
716
        if uses_foreskin && self.foreskin.is_some() {
717
            self.foreskin = None;
718
            self.shame += 25;
719
            return Problem::new(
720
                StatusCode::GONE,
721
                "foreskin-lost",
722
                "The technique has removed the foreskin. It will not be returning.",
723
            )
724
            .header(header::CONTENT_LOCATION, self.path("/foreskin"));
725
        }
726
        if self.eq > DANGER_EQ - self.burn() {
727
            return self.rupture();
728
        }
729
        if training == Training::Stretch && !self.here.teapot {
730
            self.here.teapot = true;
731
            self.items.teapot_spout = true;
732
            self.shame += 5;
733
            return Problem::new(
734
                StatusCode::IM_A_TEAPOT,
735
                "teapot",
736
                "The subject is now short and stout. Length gains are halved for the rest of the floor.",
737
            );
738
        }
739
        if training == Training::Uli && !self.conditions.peyronie {
740
            self.conditions.peyronie = true;
741
            self.shame += 5;
742
            return Problem::new(
743
                StatusCode::MISDIRECTED_REQUEST,
744
                "curvature",
745
                "A curvature has developed. Techniques will land slightly to the left until a professional is consulted.",
746
            );
747
        }
748
        not_extended()
749
    }
750
751
    fn gossip(&mut self, mut outcome: Outcome) -> Outcome {
752
        let reportable = matches!(outcome.status.as_u16(), 200 | 202 | 206);
753
        if !self.here.brother_told || !reportable {
754
            return outcome;
755
        }
756
        self.here.brother_told = false;
757
        outcome.status = StatusCode::ALREADY_REPORTED;
758
        if let Payload::Json(body) = &mut outcome.payload {
759
            body["detail"] = json!("Your brother has already told Mum.");
760
        }
761
        outcome
762
    }
763
}