an http based roguelike rpg about jelqing
| 1 | use std::{sync::LazyLock, time::SystemTime}; |
| 2 | |
| 3 | use axum::http::{StatusCode, header}; |
| 4 | use serde_json::{Value, json}; |
| 5 | |
| 6 | use super::{ |
| 7 | Cause, Floor, Outcome, Run, |
| 8 | balance::{EXAGGERATION, MEDITATION_ODDS, PATIENCE, THREAD_READS, ULI_ODDS}, |
| 9 | credentials::{Scheme, parse_credentials}, |
| 10 | measure::{exaggeration, honest}, |
| 11 | }; |
| 12 | use crate::{headers::X_FORUM_NOTE, problem::Problem}; |
| 13 | |
| 14 | pub const FORUM_THREAD: &str = "/forum/threads/2009"; |
| 15 | pub const ELDER_LAST_MODIFIED: &str = "Sun, 14 Jun 2009 03:12:00 GMT"; |
| 16 | static ELDER_POSTED: LazyLock<SystemTime> = LazyLock::new(|| { |
| 17 | httpdate::parse_http_date(ELDER_LAST_MODIFIED).expect("the Elder's date is a valid HTTP-date") |
| 18 | }); |
| 19 | |
| 20 | const SPREADSHEET: &str = "\ |
| 21 | date,length,girth,technique,notes,honest? |
| 22 | 2007-01-03,7.50,5.50,wet,baseline,no |
| 23 | 2007-06-11,7.50,5.50,wet,feeling fuller,no |
| 24 | 2008-01-02,7.50,5.50,dry,new routine,no |
| 25 | 2008-09-19,7.50,5.50,uli,PUT is idempotent. do not use PUT.,no |
| 26 | 2009-06-14,7.50,5.50,wet,progress is slow but real,no |
| 27 | "; |
| 28 | |
| 29 | fn thread(detail: Option<&str>) -> Outcome { |
| 30 | let mut body = forum_thread(); |
| 31 | if let Some(detail) = detail { |
| 32 | body["detail"] = json!(detail); |
| 33 | } |
| 34 | Outcome::json(StatusCode::OK, body).header(header::LAST_MODIFIED, ELDER_LAST_MODIFIED) |
| 35 | } |
| 36 | |
| 37 | fn nothing_new() -> Outcome { |
| 38 | thread(None).header( |
| 39 | header::WARNING, |
| 40 | r#"199 - "Nothing new has been posted since 2009.""#, |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | pub fn forum_thread() -> Value { |
| 45 | json!({ |
| 46 | "title": "The definitive jelqing FAQ (updated 2009)", |
| 47 | "posts": [ |
| 48 | { "author": "TheElder", "body": "Warm up. Always warm up. A cold jelq is a 412 waiting to happen." }, |
| 49 | { "author": "TheElder", "body": "Keep EQ between 50 and 70. Above 70 you are gambling. Above 90 you are a cautionary tale." }, |
| 50 | { "author": "newbie_2009", "body": "everyone keeps saying uli. who is uli" }, |
| 51 | { "author": "TheElder", "body": "uli was a member here. He posted the technique in 2001 and never posted again. Some say he ascended. The search function says his account is gone." }, |
| 52 | { "author": "TheElder", "body": "We do not say his name in vain. We say it in girth." }, |
| 53 | { "author": "newbie_2009", "body": "I have been sending PUT for three weeks and nothing has changed." }, |
| 54 | { "author": "TheElder", "body": "PUT is idempotent. Read the spec." }, |
| 55 | { "author": "TheElder", "body": "Members verify with a Ruler. Proxy-Authorization, inches, no rounding up. We can tell." }, |
| 56 | { "author": "throwaway_77", "body": "my doctor said i was normal. i didnt believe him. i still dont know what he meant by bearer" }, |
| 57 | { "author": "TheElder", "body": "My numbers are in my spreadsheet. They have not changed since 2009. Nothing about me has changed since 2009." }, |
| 58 | ], |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | fn verification(detail: &str) -> Problem { |
| 63 | Problem::new( |
| 64 | StatusCode::PROXY_AUTHENTICATION_REQUIRED, |
| 65 | "verification-required", |
| 66 | detail, |
| 67 | ) |
| 68 | .header(header::PROXY_AUTHENTICATE, "Ruler realm=\"forum\"") |
| 69 | } |
| 70 | |
| 71 | /// The browser tab that opens on turn 10. |
| 72 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 73 | pub enum Story { |
| 74 | Unopened, |
| 75 | Pending, |
| 76 | ReadForum, |
| 77 | ClosedTab, |
| 78 | } |
| 79 | |
| 80 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 81 | pub enum Branch { |
| 82 | Forum, |
| 83 | CloseTab, |
| 84 | } |
| 85 | |
| 86 | impl Branch { |
| 87 | pub fn slug(self) -> &'static str { |
| 88 | match self { |
| 89 | Self::Forum => "forum", |
| 90 | Self::CloseTab => "close-tab", |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | impl std::str::FromStr for Branch { |
| 96 | type Err = (); |
| 97 | |
| 98 | fn from_str(slug: &str) -> Result<Self, ()> { |
| 99 | [Self::Forum, Self::CloseTab] |
| 100 | .into_iter() |
| 101 | .find(|branch| branch.slug() == slug) |
| 102 | .ok_or(()) |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 107 | pub enum Elder { |
| 108 | Unverified { strikes: u8 }, |
| 109 | Verified, |
| 110 | Beaten, |
| 111 | } |
| 112 | |
| 113 | /// The run's life online, from the tab on turn 10 to the footage the neighbour took. |
| 114 | pub struct Online { |
| 115 | pub(crate) story: Story, |
| 116 | pub(crate) meditation_taught: bool, |
| 117 | pub(crate) uli_taught: bool, |
| 118 | pub(crate) elder: Elder, |
| 119 | pub(crate) filmed_at: Option<SystemTime>, |
| 120 | } |
| 121 | |
| 122 | impl Default for Online { |
| 123 | fn default() -> Self { |
| 124 | Self { |
| 125 | story: Story::Unopened, |
| 126 | meditation_taught: false, |
| 127 | uli_taught: false, |
| 128 | elder: Elder::Unverified { strikes: 0 }, |
| 129 | filmed_at: None, |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | impl Run { |
| 135 | pub(super) fn branch_offer(&self) -> Outcome { |
| 136 | let forum = self.path(&format!("/branches/{}", Branch::Forum.slug())); |
| 137 | let close = self.path(&format!("/branches/{}", Branch::CloseTab.slug())); |
| 138 | Outcome::json( |
| 139 | StatusCode::MULTIPLE_CHOICES, |
| 140 | json!({ |
| 141 | "detail": "A browser tab is open. It has been open for some time.", |
| 142 | "choices": [ |
| 143 | { "href": forum, "method": "POST", "title": "Read the forum" }, |
| 144 | { "href": close, "method": "POST", "title": "Close the tab" }, |
| 145 | ], |
| 146 | }), |
| 147 | ) |
| 148 | .header( |
| 149 | header::LINK, |
| 150 | format!( |
| 151 | "<{forum}>; rel=\"alternate\"; title=\"Read the forum\"; allow=\"POST\", <{close}>; rel=\"alternate\"; title=\"Close the tab\"; allow=\"POST\"" |
| 152 | ), |
| 153 | ) |
| 154 | } |
| 155 | |
| 156 | pub fn choose(&mut self, choice: Branch) -> Result<Outcome, Problem> { |
| 157 | if self.online.story != Story::Pending { |
| 158 | return Err(Problem::new( |
| 159 | StatusCode::CONFLICT, |
| 160 | "no-decision-pending", |
| 161 | "There is no decision pending.", |
| 162 | )); |
| 163 | } |
| 164 | let (story, location, detail) = match choice { |
| 165 | Branch::Forum => { |
| 166 | self.standing.shame += 3; |
| 167 | let detail = if self.learn_meditation() { |
| 168 | "The thread is from 2009. A technique called uli is discussed at length. Further down, someone explains how to breathe, and you write it down." |
| 169 | } else { |
| 170 | "The thread is from 2009. A technique called uli is discussed at length." |
| 171 | }; |
| 172 | (Story::ReadForum, self.path("/techniques"), detail) |
| 173 | } |
| 174 | Branch::CloseTab => { |
| 175 | self.standing.suspicion = 0; |
| 176 | ( |
| 177 | Story::ClosedTab, |
| 178 | self.path(""), |
| 179 | "The tab is closed. Nobody saw it.", |
| 180 | ) |
| 181 | } |
| 182 | }; |
| 183 | self.online.story = story; |
| 184 | self.touch(); |
| 185 | Ok( |
| 186 | Outcome::json(StatusCode::SEE_OTHER, json!({ "detail": detail })) |
| 187 | .header(header::LOCATION, location), |
| 188 | ) |
| 189 | } |
| 190 | |
| 191 | /// Re-reading is free. Each floor gets a couple of live reads that may surface the reply, |
| 192 | /// after which the forum is down and the browser serves what it kept. |
| 193 | pub fn read_thread(&mut self) -> Result<Outcome, Problem> { |
| 194 | if self.online.story == Story::Pending { |
| 195 | return Ok(self.branch_offer()); |
| 196 | } |
| 197 | if !self.thread_reachable() { |
| 198 | return Err(Problem::new( |
| 199 | StatusCode::NOT_FOUND, |
| 200 | "no-thread", |
| 201 | "There is no thread. Nobody has mentioned a forum yet.", |
| 202 | )); |
| 203 | } |
| 204 | self.end_kitchen_visit(); |
| 205 | if self.online.uli_taught { |
| 206 | return Ok(thread(None)); |
| 207 | } |
| 208 | if self.here.thread_rolls >= THREAD_READS { |
| 209 | let age = SystemTime::now() |
| 210 | .duration_since(*ELDER_POSTED) |
| 211 | .map_or(0, |age| age.as_secs()); |
| 212 | return Ok(thread(None) |
| 213 | .header(header::AGE, age.to_string()) |
| 214 | .header(header::WARNING, r#"112 - "Disconnected operation""#)); |
| 215 | } |
| 216 | self.here.thread_rolls += 1; |
| 217 | self.touch(); |
| 218 | let detail = if !self.online.meditation_taught { |
| 219 | if !self.learn_meditation() { |
| 220 | return Ok(nothing_new()); |
| 221 | } |
| 222 | "Between two arguments about pumps, a reply explains how to breathe. In through the nose, out through the mouth. You write it down." |
| 223 | } else { |
| 224 | if !self.chance( |
| 225 | self.decision( |
| 226 | "teach-uli", |
| 227 | "Does this read finally surface the 2009 reply that teaches uli?", |
| 228 | ), |
| 229 | ULI_ODDS, |
| 230 | ) { |
| 231 | return Ok(nothing_new()); |
| 232 | } |
| 233 | self.online.uli_taught = true; |
| 234 | "Past the necroposting, a reply from TheElder lays out the technique. Uli. The full routine. You write it down before the tab can lose it." |
| 235 | }; |
| 236 | Ok(thread(Some(detail))) |
| 237 | } |
| 238 | |
| 239 | fn learn_meditation(&mut self) -> bool { |
| 240 | let taught = self.chance( |
| 241 | self.decision( |
| 242 | "teach-meditation", |
| 243 | "Does the thread surface the reply that teaches the breathing technique?", |
| 244 | ), |
| 245 | MEDITATION_ODDS, |
| 246 | ); |
| 247 | self.online.meditation_taught |= taught; |
| 248 | taught |
| 249 | } |
| 250 | |
| 251 | fn elder_strike(&mut self, problem: Problem) -> Problem { |
| 252 | self.touch(); |
| 253 | let Elder::Unverified { strikes } = &mut self.online.elder else { |
| 254 | return problem; |
| 255 | }; |
| 256 | *strikes += 1; |
| 257 | if *strikes < PATIENCE { |
| 258 | return problem; |
| 259 | } |
| 260 | self.end(Cause::Banned) |
| 261 | } |
| 262 | |
| 263 | /// Failed verification counts toward a ban even on GET, the way repeated bad credentials lock an account. |
| 264 | fn verify_member(&mut self, proxy_authorization: Option<&str>) -> Result<(), Problem> { |
| 265 | if !matches!(self.online.elder, Elder::Unverified { .. }) { |
| 266 | return Ok(()); |
| 267 | } |
| 268 | let Some(credentials) = proxy_authorization else { |
| 269 | return Err(verification("The forum requires verification.")); |
| 270 | }; |
| 271 | let Some(claimed) = |
| 272 | parse_credentials(credentials, Scheme::Ruler).and_then(|v| v.parse::<f64>().ok()) |
| 273 | else { |
| 274 | return Err(verification("The forum requires verification, in inches.")); |
| 275 | }; |
| 276 | let actual = self.current_length(); |
| 277 | if claimed >= actual + EXAGGERATION { |
| 278 | self.standing.shame += 2; |
| 279 | return Err(self.elder_strike(exaggeration())); |
| 280 | } |
| 281 | if !honest(claimed, actual) { |
| 282 | self.standing.shame += 1; |
| 283 | let failed = verification("Verification failed. The forum can tell."); |
| 284 | if self.girth_respected() { |
| 285 | return Err(failed.header( |
| 286 | X_FORUM_NOTE, |
| 287 | "The mods have let this one go. Girth is also a dimension.", |
| 288 | )); |
| 289 | } |
| 290 | return Err(self.elder_strike(failed)); |
| 291 | } |
| 292 | self.online.elder = Elder::Verified; |
| 293 | self.touch(); |
| 294 | Ok(()) |
| 295 | } |
| 296 | |
| 297 | pub fn elder( |
| 298 | &mut self, |
| 299 | proxy_authorization: Option<&str>, |
| 300 | if_modified_since: Option<&str>, |
| 301 | ) -> Result<Outcome, Problem> { |
| 302 | if self.floor != Floor::Forum { |
| 303 | return Err(Problem::new( |
| 304 | StatusCode::NOT_FOUND, |
| 305 | "elder-absent", |
| 306 | "The Elder only posts on the forum.", |
| 307 | )); |
| 308 | } |
| 309 | self.verify_member(proxy_authorization)?; |
| 310 | |
| 311 | let current = if_modified_since |
| 312 | .and_then(|d| httpdate::parse_http_date(d).ok()) |
| 313 | .is_some_and(|since| since >= *ELDER_POSTED); |
| 314 | if current { |
| 315 | if self.online.elder != Elder::Beaten { |
| 316 | self.online.elder = Elder::Beaten; |
| 317 | self.touch(); |
| 318 | } |
| 319 | return Ok(Outcome::empty(StatusCode::NOT_MODIFIED) |
| 320 | .header(header::LAST_MODIFIED, ELDER_LAST_MODIFIED)); |
| 321 | } |
| 322 | let spreadsheet = Outcome::raw(StatusCode::OK, "text/csv; header=present", SPREADSHEET) |
| 323 | .header(header::LAST_MODIFIED, ELDER_LAST_MODIFIED); |
| 324 | if self.online.filmed_at.is_some() { |
| 325 | return Ok(spreadsheet.header(X_FORUM_NOTE, "We have seen the footage.")); |
| 326 | } |
| 327 | Ok(spreadsheet) |
| 328 | } |
| 329 | } |