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