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/floors.rsRust213 lines7.5 KB
1
use axum::http::{StatusCode, header};
2
use serde_json::{Value, json};
3
4
use super::{
5
    Cause, Elder, Floor, FloorState, Item, Outcome, Run,
6
    balance::{
7
        FLOOR_ALLOWANCE, FLOOR_TURNS, KEY_IN_DRAWER, RULER_IN_DRAWER, STARTING_EQ, SUSPICION_LIMIT,
8
        ZIP,
9
    },
10
    round2,
11
};
12
use crate::problem::Problem;
13
14
impl Run {
15
    pub(super) fn next_floor(&mut self, paid: bool) {
16
        let Some(next) = self.floor.next() else {
17
            return;
18
        };
19
        self.floor = next;
20
        self.here = FloorState::default();
21
        self.warmed_up = false;
22
        self.eq = STARTING_EQ;
23
        self.suspicion = 0;
24
        self.mum = None;
25
        if paid {
26
            self.cash += FLOOR_ALLOWANCE;
27
        }
28
        self.touch();
29
    }
30
31
    pub fn advance(&mut self) -> Result<Outcome, Problem> {
32
        match self.floor {
33
            Floor::Clinic => {
34
                return Err(Problem::new(
35
                    StatusCode::CONFLICT,
36
                    "no-further-floor",
37
                    "There is no further floor. There is only the diagnosis.",
38
                ));
39
            }
40
            Floor::Forum if self.elder != Elder::Beaten => {
41
                return Err(Problem::new(
42
                    StatusCode::CONFLICT,
43
                    "elder-unaddressed",
44
                    "The Elder has not been addressed.",
45
                )
46
                .with("elder", self.path("/elder")));
47
            }
48
            floor if floor.timed() && self.here.turns < FLOOR_TURNS => {
49
                return Err(Problem::new(
50
                    StatusCode::TOO_EARLY,
51
                    "floor-not-over",
52
                    format!(
53
                        "The floor is not over. {} turns remain.",
54
                        FLOOR_TURNS - self.here.turns
55
                    ),
56
                ));
57
            }
58
            _ => {}
59
        }
60
        if self.chance(ZIP) {
61
            return Err(self.end(Cause::Zip));
62
        }
63
        let reverted = self.here.temp_length;
64
        self.next_floor(true);
65
        let mut body = json!({
66
            "detail": format!("You have arrived at {}.", self.floor.name()),
67
            "note": self.floor.note(),
68
        });
69
        if reverted > 0.0 {
70
            body["reverted"] = json!(round2(reverted));
71
        }
72
        Ok(
73
            Outcome::json(StatusCode::SEE_OTHER, body)
74
                .header(header::LOCATION, self.path("/floor")),
75
        )
76
    }
77
78
    pub fn floor_view(&self) -> Value {
79
        let mut view = json!({
80
            "name": self.floor.name(),
81
            "note": self.floor.note(),
82
            "turns": self.here.turns,
83
            "turns_required": self.floor.timed().then_some(FLOOR_TURNS),
84
            "exit": { "href": self.path("/floor"), "method": "POST" },
85
        });
86
        match self.floor {
87
            Floor::ChangingRoom => {
88
                view["others"] = json!([
89
                    { "name": "Greg", "self_reported_length": 9.5 },
90
                    { "name": "Dmitri", "self_reported_length": 8.75 },
91
                    { "name": "a man in a towel", "self_reported_length": 11.0 },
92
                ]);
93
            }
94
            Floor::Hotel => view["portal"] = json!(self.path("/portal")),
95
            Floor::Forum => {
96
                view["thread"] = json!(super::FORUM_THREAD);
97
                view["elder"] = json!(self.path("/elder"));
98
            }
99
            Floor::Clinic => view["diagnosis"] = json!(self.path("/diagnosis")),
100
            _ => {}
101
        }
102
        if self.floor.shop_open() {
103
            view["shop"] = json!(self.path("/shop"));
104
        }
105
        view
106
    }
107
108
    fn found(&self, item: Item, detail: &str) -> Outcome {
109
        Outcome::json(StatusCode::FOUND, json!({ "detail": detail })).header(
110
            header::LOCATION,
111
            self.path(&format!("/inventory/{}", item.slug())),
112
        )
113
    }
114
115
    pub fn search_drawer(&mut self) -> Result<Outcome, Problem> {
116
        self.here.drawer_searches += 1;
117
        self.touch();
118
        let first = self.here.drawer_searches == 1;
119
        if self.items.cage {
120
            return Ok(self.rummage_for_key());
121
        }
122
        let ruler_here = self.floor == Floor::Bedroom && self.chance(RULER_IN_DRAWER);
123
        match self.floor {
124
            Floor::Bedroom if first && !self.items.ruler && ruler_here => {
125
                self.items.grant(Item::Ruler);
126
                self.shame += 10;
127
                Ok(self.found(
128
                    Item::Ruler,
129
                    "You now know. This is the first honest measurement of the run.",
130
                ))
131
            }
132
            Floor::Bathroom if first => {
133
                self.items.grant(Item::DoorLock);
134
                Ok(self.found(Item::DoorLock, "A door lock. It works once."))
135
            }
136
            Floor::ChangingRoom => {
137
                self.shame += 3;
138
                Err(Problem::new(
139
                    StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS,
140
                    "not-your-locker",
141
                    "That locker belongs to Greg. The server cannot serve this request for legal reasons.",
142
                ))
143
            }
144
            Floor::Nans if first => {
145
                self.items.grant(Item::Cage);
146
                self.shame += 5;
147
                Ok(self.found(
148
                    Item::Cage,
149
                    "A small box. Inside was a cage. It is now on. The key is not in the box.",
150
                ))
151
            }
152
            Floor::Hotel if first => {
153
                self.items.grant(Item::DndSign);
154
                self.items.grant(Item::IcePack);
155
                self.items.grant(Item::IcePack);
156
                Ok(self.found(
157
                    Item::DndSign,
158
                    "A Do Not Disturb sign, and two ice packs from the machine down the hall.",
159
                ))
160
            }
161
            Floor::Forum if first => {
162
                self.items.grant(Item::Spreadsheet);
163
                Ok(self.found(
164
                    Item::Spreadsheet,
165
                    "Your own spreadsheet. Every line is flat.",
166
                ))
167
            }
168
            Floor::Clinic => Err(Problem::new(
169
                StatusCode::FORBIDDEN,
170
                "doctors-drawer",
171
                "That is the doctor's drawer.",
172
            )),
173
            _ => Ok(Outcome::empty(StatusCode::NO_CONTENT)),
174
        }
175
    }
176
177
    pub fn accept_terms(&mut self) -> Result<Outcome, Problem> {
178
        if self.floor != Floor::Hotel {
179
            return Err(Problem::new(
180
                StatusCode::NOT_FOUND,
181
                "no-portal",
182
                "There is no captive portal here. There is only the regular kind of captivity.",
183
            ));
184
        }
185
        self.here.wifi_accepted = true;
186
        self.touch();
187
        Ok(Outcome::json(
188
            StatusCode::SEE_OTHER,
189
            json!({ "detail": "Connected. The hotel does not want to know." }),
190
        )
191
        .header(header::LOCATION, self.path("")))
192
    }
193
194
    /// Searching while caged is noisy, and the key only turns up some of the time.
195
    fn rummage_for_key(&mut self) -> Outcome {
196
        let was_below = self.suspicion < SUSPICION_LIMIT;
197
        self.suspicion = (self.suspicion + 1).min(SUSPICION_LIMIT);
198
        self.maybe_summon_mum(was_below);
199
        if !self.chance(KEY_IN_DRAWER) {
200
            return Outcome::empty(StatusCode::NO_CONTENT);
201
        }
202
        self.items.grant(Item::CageKey);
203
        Outcome::json(
204
            StatusCode::OK,
205
            json!({ "detail": "The key was in the drawer. The cage is off." }),
206
        )
207
    }
208
209
    /// Whether searching the drawer on this floor would turn anything up.
210
    pub fn drawer_worth_searching(&self) -> bool {
211
        self.here.drawer_searches == 0 || self.items.cage
212
    }
213
}