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::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.floor_enemy()?; |
| 70 | match utility { |
| 71 | Utility::Edge => self.edge(), |
| 72 | _ => self.pump(), |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | pub(super) fn single( |
| 77 | &mut self, |
| 78 | training: Training, |
| 79 | intensity: i32, |
| 80 | lube: Option<Lube>, |
| 81 | ) -> Result<Outcome, Problem> { |
| 82 | self.ready(Technique::Training(training), lube)?; |
| 83 | self.check_storage()?; |
| 84 | self.lube_available(lube)?; |
| 85 | self.floor_enemy()?; |
| 86 | self.take_lube(lube)?; |
| 87 | let cold = self.cold(); |
| 88 | self.chafe(lube, intensity, cold); |
| 89 | self.commit(intensity, lube, 1)?; |
| 90 | if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? { |
| 91 | return Ok(interrupted); |
| 92 | } |
| 93 | if let ControlFlow::Break(interrupted) = self.hazards(intensity)? { |
| 94 | return Ok(interrupted); |
| 95 | } |
| 96 | let outcome = self.roll(training, intensity, lube)?; |
| 97 | Ok(self.gossip(outcome)) |
| 98 | } |
| 99 | |
| 100 | pub(super) fn combo( |
| 101 | &mut self, |
| 102 | a: Training, |
| 103 | b: Training, |
| 104 | intensity: i32, |
| 105 | lube: Option<Lube>, |
| 106 | ) -> Result<Outcome, Problem> { |
| 107 | if a == b { |
| 108 | return Err(Problem::new( |
| 109 | StatusCode::BAD_REQUEST, |
| 110 | "combo-invalid", |
| 111 | "A technique cannot be combined with itself. The server has checked.", |
| 112 | )); |
| 113 | } |
| 114 | self.ready(Technique::Training(a), lube)?; |
| 115 | self.ready(Technique::Training(b), lube)?; |
| 116 | self.check_storage()?; |
| 117 | self.lube_available(lube)?; |
| 118 | self.floor_enemy()?; |
| 119 | self.take_lube(lube)?; |
| 120 | let cold = self.cold(); |
| 121 | self.chafe(lube, intensity, cold); |
| 122 | self.chafe(lube, intensity, cold); |
| 123 | self.commit(intensity, lube, 2)?; |
| 124 | for training in [a, b] { |
| 125 | if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? { |
| 126 | return Ok(interrupted); |
| 127 | } |
| 128 | } |
| 129 | if let ControlFlow::Break(interrupted) = self.hazards(intensity)? { |
| 130 | return Ok(interrupted); |
| 131 | } |
| 132 | |
| 133 | let mut statuses = Vec::new(); |
| 134 | for training in [a, b] { |
| 135 | let result = self.roll(training, intensity, lube); |
| 136 | if self.ended.is_some() { |
| 137 | return result; |
| 138 | } |
| 139 | let status = match result { |
| 140 | Ok(outcome) => self.gossip(outcome).status(), |
| 141 | Err(problem) => problem.status(), |
| 142 | }; |
| 143 | statuses.push((training, status)); |
| 144 | } |
| 145 | |
| 146 | let mut xml = String::from( |
| 147 | "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n", |
| 148 | ); |
| 149 | for (training, status) in statuses { |
| 150 | let _ = write!( |
| 151 | xml, |
| 152 | " <D:response>\n <D:href>{}?technique={}</D:href>\n <D:status>HTTP/1.1 {} {}</D:status>\n </D:response>\n", |
| 153 | self.path(""), |
| 154 | training.name(), |
| 155 | status.as_u16(), |
| 156 | crate::problem::reason(status), |
| 157 | ); |
| 158 | } |
| 159 | xml.push_str( |
| 160 | " <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n", |
| 161 | ); |
| 162 | Ok(Outcome::raw( |
| 163 | StatusCode::MULTI_STATUS, |
| 164 | "application/xml; charset=utf-8", |
| 165 | xml, |
| 166 | )) |
| 167 | } |
| 168 | } |