an http based roguelike rpg about jelqing
| 1 | use super::{ |
| 2 | LUBRICANTS, Lube, Run, Technique, |
| 3 | balance::STORAGE_BULK, |
| 4 | lube::LubeRule, |
| 5 | techniques::{Training, Utility}, |
| 6 | }; |
| 7 | use crate::problem::Problem; |
| 8 | use axum::http::{StatusCode, header}; |
| 9 | use serde_json::json; |
| 10 | |
| 11 | impl Run { |
| 12 | pub(super) fn ready(&self, technique: Technique, lube: Option<Lube>) -> Result<(), Problem> { |
| 13 | self.access(technique)?; |
| 14 | match technique { |
| 15 | Technique::Training(training) => Self::check_lube(training, lube), |
| 16 | Technique::Utility(_) => Ok(()), |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | pub(super) fn access(&self, technique: Technique) -> Result<(), Problem> { |
| 21 | if technique == Technique::Training(Training::Uli) && !self.online.uli_taught { |
| 22 | return Err(Problem::new( |
| 23 | StatusCode::FORBIDDEN, |
| 24 | "technique-locked", |
| 25 | "uli is discussed on the forum, in a thread from 2009, by a man who never posted again. You have not found the reply.", |
| 26 | )); |
| 27 | } |
| 28 | if matches!(technique, Technique::Training(_)) { |
| 29 | self.grip_closes(self.body.grip)?; |
| 30 | } |
| 31 | match technique { |
| 32 | Technique::Utility(Utility::Pump) if !self.items.kit.pump_pro => Err(Problem::new( |
| 33 | StatusCode::FAILED_DEPENDENCY, |
| 34 | "no-pump", |
| 35 | "The pump technique depends on a pump.", |
| 36 | )), |
| 37 | Technique::Utility(_) => Ok(()), |
| 38 | Technique::Training(training) => match training.profile().foreskin { |
| 39 | Some((min, max)) => self.check_foreskin(min, max), |
| 40 | None => Ok(()), |
| 41 | }, |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub(super) fn check_lube(training: Training, lube: Option<Lube>) -> Result<(), Problem> { |
| 46 | match (training.profile().lube, lube) { |
| 47 | (LubeRule::Required, None) => Err(Problem::new( |
| 48 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
| 49 | "lubricant-required", |
| 50 | format!( |
| 51 | "The {} technique requires a lubricant. None was declared.", |
| 52 | training.name() |
| 53 | ), |
| 54 | ) |
| 55 | .with("lubricants", json!(LUBRICANTS))), |
| 56 | (LubeRule::Forbidden, Some(_)) => Err(Problem::new( |
| 57 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
| 58 | "lubricant-declared", |
| 59 | format!( |
| 60 | "The {} technique was requested. A lubricant was declared.", |
| 61 | training.name() |
| 62 | ), |
| 63 | )), |
| 64 | _ => Ok(()), |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// One unit of whatever was declared. Spit is free and the tap is always on. |
| 69 | pub(super) fn lube_available(&self, lube: Option<Lube>) -> Result<(), Problem> { |
| 70 | let Some(bottle) = lube.and_then(Lube::item) else { |
| 71 | return Ok(()); |
| 72 | }; |
| 73 | if !self.items.has(bottle) { |
| 74 | return Err(Problem::new( |
| 75 | StatusCode::UNSUPPORTED_MEDIA_TYPE, |
| 76 | "lubricant-exhausted", |
| 77 | format!("{} was declared, but the bottle is empty.", bottle.slug()), |
| 78 | )); |
| 79 | } |
| 80 | Ok(()) |
| 81 | } |
| 82 | |
| 83 | pub(super) fn take_lube(&mut self, lube: Option<Lube>) -> Result<(), Problem> { |
| 84 | self.lube_available(lube)?; |
| 85 | if let Some(lube) = lube { |
| 86 | self.items.lubes.take(lube); |
| 87 | } |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
| 91 | pub fn techniques_now(&self) -> impl Iterator<Item = Technique> + '_ { |
| 92 | let blocked = !self.floor.timed() || self.items.kit.cage; |
| 93 | Technique::ALL |
| 94 | .into_iter() |
| 95 | .filter(move |_| !blocked) |
| 96 | .filter(|&technique| { |
| 97 | !self.body.exhausted || technique == Technique::Utility(Utility::Energy) |
| 98 | }) |
| 99 | .filter(|&technique| match technique { |
| 100 | Technique::Utility(Utility::Warmup) => { |
| 101 | !self.here.towel_warmed || self.here.kitchen.kettle_boiled |
| 102 | } |
| 103 | Technique::Utility(Utility::Ice) => self.items.stores.ice_packs > 0, |
| 104 | Technique::Utility(Utility::Supplement) => self.items.stores.supplements > 0, |
| 105 | Technique::Utility(Utility::Meditate) => { |
| 106 | self.online.meditation_taught && !self.here.meditated |
| 107 | } |
| 108 | Technique::Utility(Utility::Energy) => self.items.stores.energy_drinks > 0, |
| 109 | _ => self.access(technique).is_ok() && self.check_storage().is_ok(), |
| 110 | }) |
| 111 | } |
| 112 | |
| 113 | pub(super) fn check_foreskin(&self, min: f64, max: f64) -> Result<(), Problem> { |
| 114 | let Some(f) = self.body.foreskin else { |
| 115 | if min <= 0.0 { |
| 116 | return Ok(()); |
| 117 | } |
| 118 | return Err(Problem::new( |
| 119 | StatusCode::GONE, |
| 120 | "foreskin-gone", |
| 121 | "This technique requires a foreskin. The foreskin is gone.", |
| 122 | ) |
| 123 | .header(header::CONTENT_LOCATION, self.path("/foreskin"))); |
| 124 | }; |
| 125 | if f < min { |
| 126 | return Err(Problem::new( |
| 127 | StatusCode::LENGTH_REQUIRED, |
| 128 | "foreskin-insufficient", |
| 129 | format!( |
| 130 | "The selected technique requires {min:.1} units of foreskin. {f:.2} units are available. The server has noted the shortfall." |
| 131 | ), |
| 132 | ) |
| 133 | .with("required", min)); |
| 134 | } |
| 135 | if f > max { |
| 136 | return Err(Problem::new( |
| 137 | StatusCode::PAYLOAD_TOO_LARGE, |
| 138 | "foreskin-excessive", |
| 139 | format!( |
| 140 | "The selected technique tolerates at most {max:.1} units of foreskin. {f:.2} units are present." |
| 141 | ), |
| 142 | ) |
| 143 | .with("maximum", max)); |
| 144 | } |
| 145 | Ok(()) |
| 146 | } |
| 147 | |
| 148 | pub(super) fn check_storage(&self) -> Result<(), Problem> { |
| 149 | if self.items.kit.extender && self.body.girth + self.current_length() / 4.0 > STORAGE_BULK { |
| 150 | return Err(Problem::new( |
| 151 | StatusCode::INSUFFICIENT_STORAGE, |
| 152 | "jeans-full", |
| 153 | "The jeans are full. The extender is occupying the remaining storage.", |
| 154 | ) |
| 155 | .with("extender", self.path("/inventory/extender"))); |
| 156 | } |
| 157 | Ok(()) |
| 158 | } |
| 159 | } |