atagen

jelq

public

an http based roguelike rpg about jelqing

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