an http based roguelike rpg about jelqing
| 1 | use super::{ |
| 2 | Floor, Lube, Outcome, Run, Story, Technique, |
| 3 | techniques::{Training, Utility}, |
| 4 | }; |
| 5 | use crate::problem::Problem; |
| 6 | use axum::http::{StatusCode, header}; |
| 7 | use serde_json::{Value, json}; |
| 8 | use std::{fmt::Write as _, ops::ControlFlow}; |
| 9 | |
| 10 | impl Run { |
| 11 | pub fn jelq( |
| 12 | &mut self, |
| 13 | techniques: &[Technique], |
| 14 | intensity: i32, |
| 15 | lube: Option<Lube>, |
| 16 | ) -> Result<Outcome, Problem> { |
| 17 | if techniques != [Technique::Utility(Utility::Energy)] { |
| 18 | self.check_exhausted()?; |
| 19 | } |
| 20 | if self.online.story == Story::Pending { |
| 21 | return Ok(self.branch_offer()); |
| 22 | } |
| 23 | if self.floor == Floor::Forum { |
| 24 | return Ok(Outcome::json( |
| 25 | StatusCode::SEE_OTHER, |
| 26 | json!({ "detail": "Refer to the forum." }), |
| 27 | ) |
| 28 | .header(header::LOCATION, self.path("/thread"))); |
| 29 | } |
| 30 | if self.items.kit.cage { |
| 31 | return Err(Problem::new( |
| 32 | StatusCode::LOCKED, |
| 33 | "caged", |
| 34 | "The cage is locked. The key is not in the box.", |
| 35 | )); |
| 36 | } |
| 37 | match *techniques { |
| 38 | [Technique::Utility(utility)] => self.utility(utility, lube), |
| 39 | [Technique::Training(training)] => self.single(training, intensity, lube), |
| 40 | [Technique::Training(a), Technique::Training(b)] => self.combo(a, b, intensity, lube), |
| 41 | [_, _] => Err(Problem::new( |
| 42 | StatusCode::BAD_REQUEST, |
| 43 | "combo-invalid", |
| 44 | "Only training techniques can be combined.", |
| 45 | )), |
| 46 | _ => Err(Problem::new( |
| 47 | StatusCode::BAD_REQUEST, |
| 48 | "too-many-techniques", |
| 49 | "Two techniques at most. The server has limits even if you do not.", |
| 50 | )), |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | pub(super) fn utility( |
| 55 | &mut self, |
| 56 | utility: Utility, |
| 57 | lube: Option<Lube>, |
| 58 | ) -> Result<Outcome, Problem> { |
| 59 | match utility { |
| 60 | Utility::Warmup => return self.warm_up(), |
| 61 | Utility::Ice => return self.ice(), |
| 62 | Utility::Supplement => return self.supplement(), |
| 63 | Utility::Meditate => return self.meditate(), |
| 64 | Utility::Energy => return self.energy(), |
| 65 | Utility::Edge | Utility::Pump => {} |
| 66 | } |
| 67 | self.ready(Technique::Utility(utility), lube)?; |
| 68 | self.check_storage()?; |
| 69 | self.check_clock()?; |
| 70 | self.floor_enemy()?; |
| 71 | match utility { |
| 72 | Utility::Edge => self.edge(), |
| 73 | _ => self.pump(), |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | pub(super) fn single( |
| 78 | &mut self, |
| 79 | training: Training, |
| 80 | intensity: i32, |
| 81 | lube: Option<Lube>, |
| 82 | ) -> Result<Outcome, Problem> { |
| 83 | self.ready(Technique::Training(training), lube)?; |
| 84 | self.check_storage()?; |
| 85 | self.lube_available(lube)?; |
| 86 | self.check_clock()?; |
| 87 | self.floor_enemy()?; |
| 88 | self.take_lube(lube)?; |
| 89 | let cold = self.cold(); |
| 90 | self.chafe(lube, intensity, cold); |
| 91 | self.commit(intensity, lube, 1)?; |
| 92 | if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? { |
| 93 | return Ok(interrupted); |
| 94 | } |
| 95 | if let ControlFlow::Break(interrupted) = self.hazards(intensity)? { |
| 96 | return Ok(interrupted); |
| 97 | } |
| 98 | let outcome = self.roll(training, intensity, lube)?; |
| 99 | Ok(self.gossip(outcome)) |
| 100 | } |
| 101 | |
| 102 | pub(super) fn combo( |
| 103 | &mut self, |
| 104 | a: Training, |
| 105 | b: Training, |
| 106 | intensity: i32, |
| 107 | lube: Option<Lube>, |
| 108 | ) -> Result<Outcome, Problem> { |
| 109 | if a == b { |
| 110 | return Err(Problem::new( |
| 111 | StatusCode::BAD_REQUEST, |
| 112 | "combo-invalid", |
| 113 | "A technique cannot be combined with itself. The server has checked.", |
| 114 | )); |
| 115 | } |
| 116 | self.ready(Technique::Training(a), lube)?; |
| 117 | self.ready(Technique::Training(b), lube)?; |
| 118 | self.check_storage()?; |
| 119 | self.lube_available(lube)?; |
| 120 | self.check_clock()?; |
| 121 | self.floor_enemy()?; |
| 122 | self.take_lube(lube)?; |
| 123 | let cold = self.cold(); |
| 124 | self.chafe(lube, intensity, cold); |
| 125 | self.chafe(lube, intensity, cold); |
| 126 | self.commit(intensity, lube, 2)?; |
| 127 | for training in [a, b] { |
| 128 | if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? { |
| 129 | return Ok(interrupted); |
| 130 | } |
| 131 | } |
| 132 | if let ControlFlow::Break(interrupted) = self.hazards(intensity)? { |
| 133 | return Ok(interrupted); |
| 134 | } |
| 135 | |
| 136 | let mut reports = Vec::new(); |
| 137 | for training in [a, b] { |
| 138 | let result = self.roll(training, intensity, lube); |
| 139 | if self.ended.is_some() { |
| 140 | return result; |
| 141 | } |
| 142 | reports.push(( |
| 143 | training, |
| 144 | Report::of(result.map(|outcome| self.gossip(outcome))), |
| 145 | )); |
| 146 | } |
| 147 | |
| 148 | let mut xml = String::from( |
| 149 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n", |
| 150 | ); |
| 151 | for (training, report) in reports { |
| 152 | let _ = write!( |
| 153 | xml, |
| 154 | " <D:response>\n <D:href>{}?technique={}</D:href>\n <D:status>HTTP/1.1 {} {}</D:status>\n", |
| 155 | self.path(""), |
| 156 | training.name(), |
| 157 | report.status.as_u16(), |
| 158 | crate::problem::reason(report.status), |
| 159 | ); |
| 160 | if let Some(location) = report.location { |
| 161 | let _ = writeln!( |
| 162 | xml, |
| 163 | " <D:location><D:href>{}</D:href></D:location>", |
| 164 | escape(&location) |
| 165 | ); |
| 166 | } |
| 167 | if let Some(text) = report.text { |
| 168 | let _ = writeln!( |
| 169 | xml, |
| 170 | " <D:responsedescription>{}</D:responsedescription>", |
| 171 | escape(&text) |
| 172 | ); |
| 173 | } |
| 174 | xml.push_str(" </D:response>\n"); |
| 175 | } |
| 176 | xml.push_str( |
| 177 | " <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n", |
| 178 | ); |
| 179 | Ok(Outcome::raw( |
| 180 | StatusCode::MULTI_STATUS, |
| 181 | "application/xml; charset=utf-8", |
| 182 | xml, |
| 183 | )) |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /// What one technique of a combo would have answered on its own. |
| 188 | struct Report { |
| 189 | status: StatusCode, |
| 190 | location: Option<String>, |
| 191 | text: Option<String>, |
| 192 | } |
| 193 | |
| 194 | impl Report { |
| 195 | fn of(result: Result<Outcome, Problem>) -> Self { |
| 196 | match result { |
| 197 | Ok(outcome) => Self { |
| 198 | status: outcome.status(), |
| 199 | location: outcome.header_value(&header::LOCATION).map(str::to_owned), |
| 200 | text: outcome.body().map(Value::to_string), |
| 201 | }, |
| 202 | Err(problem) => Self { |
| 203 | status: problem.status(), |
| 204 | location: None, |
| 205 | text: Some(Value::Object(problem.document()).to_string()), |
| 206 | }, |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | fn escape(text: &str) -> String { |
| 212 | text.replace('&', "&") |
| 213 | .replace('<', "<") |
| 214 | .replace('>', ">") |
| 215 | } |