atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
f39b10e20a0b672c83264ded097f223a248730bd
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/turn.rsRust547 lines19.3 KB
1
use std::{fmt::Write as _, ops::ControlFlow, time::Instant};
2
3
use axum::http::{StatusCode, header};
4
use rand::Rng;
5
use serde_json::json;
6
7
use super::{
8
    Floor, Grip, LUBRICANTS, Lube, Outcome, Payload, Run, Story, Technique,
9
    balance::{
10
        BAND_BONUS, BRANCH_TURN, CAT_TAKES_TOWEL, CHANGING_ROOM_CHILL, DANGER_EQ, EQ_BAND,
11
        EQ_WOBBLE, EXTENDER_GAIN, HOT_EQ, OBSERVED, PRIAPISM_EQ, PRIAPISM_TURNS, PULLED_MUSCLE,
12
        PULLED_MUSCLE_INTENSITY, RING_PRIAPISM, RING_RUPTURE_LINE, RUPTURE_LINE, STORAGE_GIRTH,
13
        SUSPICION_LIMIT,
14
    },
15
    not_extended, round3,
16
    state::{MumVisit, Observer, Pending},
17
    techniques::{LubeRule, Profile, Training, Utility},
18
};
19
use crate::{headers::SERVER_TIMING, problem::Problem};
20
21
impl Run {
22
    pub fn jelq(
23
        &mut self,
24
        techniques: &[Technique],
25
        intensity: i32,
26
        lube: Option<Lube>,
27
        now: Instant,
28
    ) -> Result<Outcome, Problem> {
29
        self.check_refractory(now)?;
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, now),
49
            [Technique::Training(training)] => self.single(training, intensity, lube, now),
50
            [Technique::Training(a), Technique::Training(b)] => {
51
                self.combo(a, b, intensity, lube, now)
52
            }
53
            [_, _] => Err(Problem::new(
54
                StatusCode::BAD_REQUEST,
55
                "combo-invalid",
56
                "Only training techniques can be combined.",
57
            )),
58
            _ => Err(Problem::new(
59
                StatusCode::BAD_REQUEST,
60
                "too-many-techniques",
61
                "Two techniques at most. The server has limits even if you do not.",
62
            )),
63
        }
64
    }
65
66
    fn utility(
67
        &mut self,
68
        utility: Utility,
69
        lube: Option<Lube>,
70
        now: Instant,
71
    ) -> Result<Outcome, Problem> {
72
        match utility {
73
            Utility::Warmup => return self.warm_up(),
74
            Utility::Ice => return self.ice(),
75
            Utility::Supplement => return self.supplement(),
76
            Utility::Edge | Utility::Pump => {}
77
        }
78
        if utility == Utility::Pump {
79
            self.here.edges = 0;
80
        }
81
        self.ready(Technique::Utility(utility), lube)?;
82
        self.check_storage()?;
83
        self.floor_enemy()?;
84
        match utility {
85
            Utility::Edge => self.edge(now),
86
            _ => self.pump(now),
87
        }
88
    }
89
90
    fn single(
91
        &mut self,
92
        training: Training,
93
        intensity: i32,
94
        lube: Option<Lube>,
95
        now: Instant,
96
    ) -> Result<Outcome, Problem> {
97
        self.here.edges = 0;
98
        self.ready(Technique::Training(training), lube)?;
99
        self.check_storage()?;
100
        self.floor_enemy()?;
101
        self.commit(intensity, lube, now, 1);
102
        if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
103
            return Ok(interrupted);
104
        }
105
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
106
            return Ok(interrupted);
107
        }
108
        let outcome = self.roll(training, intensity, lube)?;
109
        Ok(self.gossip(outcome))
110
    }
111
112
    fn combo(
113
        &mut self,
114
        a: Training,
115
        b: Training,
116
        intensity: i32,
117
        lube: Option<Lube>,
118
        now: Instant,
119
    ) -> Result<Outcome, Problem> {
120
        if a == b {
121
            return Err(Problem::new(
122
                StatusCode::BAD_REQUEST,
123
                "combo-invalid",
124
                "A technique cannot be combined with itself. The server has checked.",
125
            ));
126
        }
127
        self.here.edges = 0;
128
        self.ready(Technique::Training(a), lube)?;
129
        self.ready(Technique::Training(b), lube)?;
130
        self.check_storage()?;
131
        self.floor_enemy()?;
132
        self.commit(intensity, lube, now, 2);
133
        for training in [a, b] {
134
            if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity)? {
135
                return Ok(interrupted);
136
            }
137
        }
138
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
139
            return Ok(interrupted);
140
        }
141
142
        let mut statuses = Vec::new();
143
        for training in [a, b] {
144
            let result = self.roll(training, intensity, lube);
145
            if self.ended.is_some() {
146
                return result;
147
            }
148
            let status = match result {
149
                Ok(outcome) => self.gossip(outcome).status,
150
                Err(problem) => problem.status(),
151
            };
152
            statuses.push((training, status));
153
        }
154
155
        let mut xml = String::from(
156
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n",
157
        );
158
        for (training, status) in statuses {
159
            let _ = write!(
160
                xml,
161
                "  <D:response>\n    <D:href>{}?technique={}</D:href>\n    <D:status>HTTP/1.1 {} {}</D:status>\n  </D:response>\n",
162
                self.path(""),
163
                training.name(),
164
                status.as_u16(),
165
                crate::problem::reason(status),
166
            );
167
        }
168
        xml.push_str(
169
            "  <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n",
170
        );
171
        Ok(Outcome::raw(
172
            StatusCode::MULTI_STATUS,
173
            "application/xml; charset=utf-8",
174
            xml,
175
        ))
176
    }
177
178
    fn ready(&self, technique: Technique, lube: Option<Lube>) -> Result<(), Problem> {
179
        self.access(technique)?;
180
        match technique {
181
            Technique::Training(training) => Self::check_lube(training, lube),
182
            Technique::Utility(_) => Ok(()),
183
        }
184
    }
185
186
    /// Whether the run could attempt the technique now, ignoring the lubricant.
187
    fn access(&self, technique: Technique) -> Result<(), Problem> {
188
        if technique == Technique::Training(Training::Uli) && !self.forum_read() {
189
            return Err(Problem::new(
190
                StatusCode::FORBIDDEN,
191
                "technique-locked",
192
                "This technique is discussed on the forum. You have not been to the forum.",
193
            ));
194
        }
195
        if !self.warmed_up {
196
            return Err(Problem::new(
197
                StatusCode::PRECONDITION_FAILED,
198
                "cold-start",
199
                "Warm-up was skipped. The server has noted this.",
200
            )
201
            .with("technique", Utility::Warmup.name()));
202
        }
203
        match technique {
204
            Technique::Utility(Utility::Pump) if !self.items.pump_pro => Err(Problem::new(
205
                StatusCode::FAILED_DEPENDENCY,
206
                "no-pump",
207
                "The pump technique depends on a pump.",
208
            )),
209
            Technique::Utility(_) => Ok(()),
210
            Technique::Training(training) => match training.profile().foreskin {
211
                Some((min, max)) => self.check_foreskin(min, max),
212
                None => Ok(()),
213
            },
214
        }
215
    }
216
217
    fn check_lube(training: Training, lube: Option<Lube>) -> Result<(), Problem> {
218
        match (training.profile().lube, lube) {
219
            (LubeRule::Required, None) => Err(Problem::new(
220
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
221
                "lubricant-required",
222
                format!(
223
                    "The {} technique requires a lubricant. None was declared.",
224
                    training.name()
225
                ),
226
            )
227
            .with("lubricants", json!(LUBRICANTS))),
228
            (LubeRule::Forbidden, Some(_)) => Err(Problem::new(
229
                StatusCode::UNSUPPORTED_MEDIA_TYPE,
230
                "lubricant-declared",
231
                format!(
232
                    "The {} technique was requested. A lubricant was declared.",
233
                    training.name()
234
                ),
235
            )),
236
            _ => Ok(()),
237
        }
238
    }
239
240
    /// The techniques a JELQ could use right now without being refused before the roll.
241
    pub fn techniques_now(&self, now: Instant) -> Vec<Technique> {
242
        let blocked = !self.floor.timed()
243
            || self.items.cage
244
            || self.story == Story::Pending
245
            || self.refractory_until.is_some_and(|until| now < until);
246
        if blocked {
247
            return Vec::new();
248
        }
249
        Technique::ALL
250
            .into_iter()
251
            .filter(|&technique| match technique {
252
                Technique::Utility(Utility::Warmup) => true,
253
                Technique::Utility(Utility::Ice) => self.items.ice_packs > 0,
254
                Technique::Utility(Utility::Supplement) => self.items.supplements > 0,
255
                _ => self.access(technique).is_ok() && self.check_storage().is_ok(),
256
            })
257
            .collect()
258
    }
259
260
    fn check_foreskin(&self, min: f64, max: f64) -> Result<(), Problem> {
261
        let Some(f) = self.foreskin else {
262
            if min <= 0.0 {
263
                return Ok(());
264
            }
265
            return Err(Problem::new(
266
                StatusCode::GONE,
267
                "foreskin-gone",
268
                "This technique requires a foreskin. The foreskin is gone.",
269
            )
270
            .header(header::CONTENT_LOCATION, self.path("/foreskin")));
271
        };
272
        if f < min {
273
            return Err(Problem::new(
274
                StatusCode::LENGTH_REQUIRED,
275
                "foreskin-insufficient",
276
                format!(
277
                    "The selected technique requires {min:.1} units of foreskin. {f:.2} units are available. The server has noted the shortfall."
278
                ),
279
            )
280
            .with("required", min));
281
        }
282
        if f > max {
283
            return Err(Problem::new(
284
                StatusCode::PAYLOAD_TOO_LARGE,
285
                "foreskin-excessive",
286
                format!(
287
                    "The selected technique tolerates at most {max:.1} units of foreskin. {f:.2} units are present."
288
                ),
289
            )
290
            .with("maximum", max));
291
        }
292
        Ok(())
293
    }
294
295
    fn check_storage(&self) -> Result<(), Problem> {
296
        if self.items.extender && self.girth > STORAGE_GIRTH {
297
            return Err(Problem::new(
298
                StatusCode::INSUFFICIENT_STORAGE,
299
                "jeans-full",
300
                "The jeans are full. The extender is occupying the remaining storage.",
301
            )
302
            .with("extender", self.path("/inventory/extender")));
303
        }
304
        Ok(())
305
    }
306
307
    pub(super) fn commit(&mut self, intensity: i32, lube: Option<Lube>, now: Instant, cost: i32) {
308
        self.touch();
309
        self.turn += 1;
310
        self.here.turns += 1;
311
        self.realise();
312
        if self.items.extender {
313
            self.length += EXTENDER_GAIN;
314
        }
315
        self.stamina -= cost + i32::from(lube == Some(Lube::Lotion));
316
        if self.stamina <= 0 {
317
            self.exhaust(now);
318
        }
319
        let chill = if self.floor == Floor::ChangingRoom {
320
            CHANGING_ROOM_CHILL
321
        } else {
322
            0
323
        };
324
        let wobble = self.rng.random_range(-EQ_WOBBLE..=EQ_WOBBLE);
325
        self.eq = (self.eq + intensity * 2 - 6 - chill + wobble).clamp(0, 100);
326
327
        let mut suspicion = intensity / 5 + i32::from(lube == Some(Lube::Vaseline));
328
        if matches!(self.floor, Floor::ChangingRoom | Floor::Nans) {
329
            suspicion *= 2;
330
        }
331
        if self.floor == Floor::Nans && lube == Some(Lube::CoconutOil) {
332
            suspicion += 2;
333
        }
334
        let was_below = self.suspicion < SUSPICION_LIMIT;
335
        self.suspicion =
336
            u8::try_from((i32::from(self.suspicion) + suspicion).min(i32::from(SUSPICION_LIMIT)))
337
                .unwrap_or(SUSPICION_LIMIT);
338
        self.maybe_summon_mum(was_below);
339
340
        if let Some(observer) = self.floor.observer()
341
            && self.chance(OBSERVED)
342
        {
343
            self.observed = Some(observer);
344
            self.shame += 1;
345
            if observer == Observer::Cat && self.chance(CAT_TAKES_TOWEL) {
346
                self.here.towel_stolen = true;
347
            }
348
        }
349
        if intensity >= PULLED_MUSCLE_INTENSITY
350
            && !self.conditions.pulled_muscle
351
            && self.chance(PULLED_MUSCLE)
352
        {
353
            self.conditions.pulled_muscle = true;
354
            self.stamina = self.stamina.min(self.max_stamina());
355
        }
356
        if self.items.ring && self.eq > HOT_EQ && self.chance(RING_PRIAPISM) {
357
            self.conditions.priapism_turns = PRIAPISM_TURNS;
358
        }
359
        if self.conditions.priapism_turns > 0 {
360
            self.conditions.priapism_turns -= 1;
361
            self.eq = self.eq.max(PRIAPISM_EQ);
362
        }
363
        if self.turn == BRANCH_TURN && self.story == Story::Unopened {
364
            self.story = Story::Pending;
365
        }
366
    }
367
368
    pub(super) fn maybe_summon_mum(&mut self, was_below: bool) {
369
        if !self.floor.has_mum() || self.mum.is_some() {
370
            return;
371
        }
372
        let summoned = was_below && self.suspicion == SUSPICION_LIMIT;
373
        let visits = u32::from(self.suspicion) + 1;
374
        if summoned || self.rng.random_ratio(visits, super::balance::MUM_VISIT) {
375
            let errand = self.errand(summoned);
376
            let nonce = self.fresh_nonce();
377
            self.mum = Some(MumVisit {
378
                errand,
379
                nonce,
380
                strikes: 0,
381
                knocked: false,
382
            });
383
        }
384
    }
385
386
    fn roll(
387
        &mut self,
388
        training: Training,
389
        intensity: i32,
390
        lube: Option<Lube>,
391
    ) -> Result<Outcome, Problem> {
392
        let profile = training.profile();
393
        let natural: i32 = self.rng.random_range(1..=20);
394
        let band = if EQ_BAND.contains(&self.eq) {
395
            BAND_BONUS
396
        } else {
397
            0
398
        };
399
        let grip = match (self.grip, training) {
400
            (Grip::Underhand, Training::Stretch) => 2,
401
            (Grip::Underhand, Training::Wet) => -1,
402
            _ => 0,
403
        };
404
        let modifier = lube.map_or(0, Lube::modifier) + band + grip;
405
        let dc = profile.dc + intensity / 3;
406
        let total = natural + modifier;
407
        let timing = format!("roll;desc=\"d20{modifier:+} vs {dc}\";dur={natural}");
408
        let timed = |p: Problem| p.header(SERVER_TIMING, &timing);
409
410
        let (length, girth) = self.scaled_gains(&profile, intensity);
411
        let rupture_line = if self.items.ring {
412
            RING_RUPTURE_LINE
413
        } else {
414
            RUPTURE_LINE
415
        };
416
417
        if natural == 1 {
418
            return Err(timed(self.critical_failure(training, &profile)));
419
        }
420
        if natural == 2 && self.eq > DANGER_EQ {
421
            return Err(timed(self.rupture()));
422
        }
423
        if self.eq > rupture_line && total < dc {
424
            return Err(timed(self.rupture()));
425
        }
426
        if natural == 20 {
427
            self.gain(length * 2.0, girth * 2.0, profile.foreskin_growth);
428
            return Ok(Outcome::json(
429
                StatusCode::PERMANENT_REDIRECT,
430
                json!({
431
                    "detail": "Your length has moved permanently.",
432
                    "length": round3(length * 2.0),
433
                    "girth": round3(girth * 2.0),
434
                }),
435
            )
436
            .header(header::LOCATION, self.path("/length"))
437
            .header(SERVER_TIMING, timing));
438
        }
439
        if total < dc - 3 {
440
            return Err(timed(not_extended()));
441
        }
442
        if total < dc {
443
            return Ok(Outcome::empty(StatusCode::NO_CONTENT).header(SERVER_TIMING, timing));
444
        }
445
446
        let outcome = if self.eq < *EQ_BAND.start() {
447
            self.gain(length / 2.0, girth / 2.0, profile.foreskin_growth);
448
            Outcome::json(
449
                StatusCode::PARTIAL_CONTENT,
450
                json!({
451
                    "detail": "Partial gains were applied. EQ was below the band.",
452
                    "length": round3(length / 2.0),
453
                    "girth": round3(girth / 2.0),
454
                }),
455
            )
456
        } else if total >= dc + 5 {
457
            self.gain(length, girth, profile.foreskin_growth);
458
            Outcome::json(
459
                StatusCode::OK,
460
                json!({
461
                    "detail": "Gains were applied.",
462
                    "length": round3(length),
463
                    "girth": round3(girth),
464
                }),
465
            )
466
        } else {
467
            let eta = self.rng.random_range(1..=4);
468
            self.pending.push(Pending {
469
                due: self.turn + eta,
470
                length,
471
                girth,
472
            });
473
            self.gain(0.0, 0.0, profile.foreskin_growth);
474
            Outcome::json(
475
                StatusCode::ACCEPTED,
476
                json!({
477
                    "detail": "Gains have been queued. They may arrive.",
478
                    "eta_turns": eta,
479
                }),
480
            )
481
        };
482
        Ok(outcome.header(SERVER_TIMING, timing))
483
    }
484
485
    fn scaled_gains(&self, profile: &Profile, intensity: i32) -> (f64, f64) {
486
        let scale = f64::from(intensity);
487
        let (mut length, mut girth) = (profile.length * scale, profile.girth * scale);
488
        if self.here.teapot {
489
            length *= 0.5;
490
            girth *= 1.5;
491
        }
492
        if self.items.ring {
493
            girth *= 2.0;
494
        }
495
        (length, girth)
496
    }
497
498
    fn critical_failure(&mut self, training: Training, profile: &Profile) -> Problem {
499
        let uses_foreskin = profile.foreskin.is_some_and(|(min, _)| min > 0.0);
500
        if uses_foreskin && self.foreskin.is_some() {
501
            self.foreskin = None;
502
            self.shame += 25;
503
            return Problem::new(
504
                StatusCode::GONE,
505
                "foreskin-lost",
506
                "The technique has removed the foreskin. It will not be returning.",
507
            )
508
            .header(header::CONTENT_LOCATION, self.path("/foreskin"));
509
        }
510
        if self.eq > DANGER_EQ {
511
            return self.rupture();
512
        }
513
        if training == Training::Stretch && !self.here.teapot {
514
            self.here.teapot = true;
515
            self.items.teapot_spout = true;
516
            self.shame += 5;
517
            return Problem::new(
518
                StatusCode::IM_A_TEAPOT,
519
                "teapot",
520
                "The subject is now short and stout. Length gains are halved for the rest of the floor.",
521
            );
522
        }
523
        if training == Training::Uli && !self.conditions.peyronie {
524
            self.conditions.peyronie = true;
525
            self.shame += 5;
526
            return Problem::new(
527
                StatusCode::MISDIRECTED_REQUEST,
528
                "curvature",
529
                "A curvature has developed. Techniques will land slightly to the left until a professional is consulted.",
530
            );
531
        }
532
        not_extended()
533
    }
534
535
    fn gossip(&mut self, mut outcome: Outcome) -> Outcome {
536
        let reportable = matches!(outcome.status.as_u16(), 200 | 202 | 206);
537
        if !self.here.flatmate_told || !reportable {
538
            return outcome;
539
        }
540
        self.here.flatmate_told = false;
541
        outcome.status = StatusCode::ALREADY_REPORTED;
542
        if let Payload::Json(body) = &mut outcome.payload {
543
            body["detail"] = json!("The flatmate has already told everyone.");
544
        }
545
        outcome
546
    }
547
}