an http based roguelike rpg about jelqing
| 1 | use std::time::Instant; |
| 2 | |
| 3 | use serde_json::{Value, json}; |
| 4 | |
| 5 | use super::{ |
| 6 | Arousal, Arrival, Danger, Elder, Excuse, Floor, Grip, Item, Observer, Run, Story, Technique, |
| 7 | balance::{DANGER_EQ, FLOOR_LIMIT}, |
| 8 | techniques::{Training, Utility}, |
| 9 | }; |
| 10 | |
| 11 | pub enum Shown { |
| 12 | Measured(f64), |
| 13 | SelfReported(f64), |
| 14 | } |
| 15 | |
| 16 | pub struct Gauges { |
| 17 | pub(crate) length: Shown, |
| 18 | pub(crate) girth: f64, |
| 19 | pub(crate) foreskin: Option<f64>, |
| 20 | pub(crate) friction: i32, |
| 21 | pub(crate) arousal: Arousal, |
| 22 | pub(crate) stamina: i32, |
| 23 | pub(crate) max_stamina: i32, |
| 24 | pub(crate) suspicion: u8, |
| 25 | pub(crate) cash: u32, |
| 26 | pub(crate) floor: u8, |
| 27 | pub(crate) turn: u32, |
| 28 | pub(crate) floor_turns: u32, |
| 29 | pub(crate) floor_limit: Option<u32>, |
| 30 | pub(crate) dangers: Vec<(&'static str, Danger)>, |
| 31 | pub(crate) observed: Option<Observer>, |
| 32 | pub(crate) mum_challenge: Option<String>, |
| 33 | pub(crate) warnings: Vec<&'static str>, |
| 34 | pub(crate) arrived: Option<Arrival>, |
| 35 | pub(crate) ruler: bool, |
| 36 | pub(crate) ring: bool, |
| 37 | } |
| 38 | |
| 39 | pub struct Reach { |
| 40 | pub(crate) foreskin: bool, |
| 41 | pub(crate) drawer: bool, |
| 42 | pub(crate) kitchen: bool, |
| 43 | pub(crate) thread: bool, |
| 44 | pub(crate) shop: bool, |
| 45 | pub(crate) portal: bool, |
| 46 | pub(crate) elder: bool, |
| 47 | pub(crate) footage: bool, |
| 48 | pub(crate) diagnosis: bool, |
| 49 | pub(crate) branch_pending: bool, |
| 50 | pub(crate) shopping: bool, |
| 51 | } |
| 52 | |
| 53 | impl Run { |
| 54 | /// Drains the notices, so call it once per response. |
| 55 | pub fn gauges(&mut self) -> Gauges { |
| 56 | let conditions = &self.body.conditions; |
| 57 | let mut warnings = Vec::new(); |
| 58 | if conditions.pulled_muscle { |
| 59 | warnings.push("a muscle has been pulled"); |
| 60 | } |
| 61 | if conditions.friction_burn { |
| 62 | warnings.push("friction burn"); |
| 63 | } |
| 64 | warnings.extend(self.notices.drain(..).map(|notice| notice.text())); |
| 65 | let arrived = self.body.arrived.take(); |
| 66 | if arrived.is_some() { |
| 67 | warnings.push("the queued gains have arrived"); |
| 68 | } |
| 69 | if self.hotter_than(DANGER_EQ) && self.ended.is_none() { |
| 70 | warnings.push("it is turning purple"); |
| 71 | } |
| 72 | Gauges { |
| 73 | length: if self.items.kit.ruler { |
| 74 | Shown::Measured(self.current_length()) |
| 75 | } else { |
| 76 | Shown::SelfReported(self.self_reported()) |
| 77 | }, |
| 78 | girth: self.body.girth, |
| 79 | foreskin: self.body.foreskin, |
| 80 | friction: self.body.friction, |
| 81 | arousal: self.arousal(), |
| 82 | stamina: self.body.stamina, |
| 83 | max_stamina: self.max_stamina(), |
| 84 | suspicion: self.standing.suspicion, |
| 85 | cash: self.standing.cash, |
| 86 | floor: self.floor.number(), |
| 87 | turn: self.turn, |
| 88 | floor_turns: self.here.turns, |
| 89 | floor_limit: self.floor.timed().then_some(FLOOR_LIMIT), |
| 90 | dangers: self.dangers(), |
| 91 | observed: self.observed, |
| 92 | mum_challenge: self.mum_challenge(), |
| 93 | warnings, |
| 94 | arrived, |
| 95 | ruler: self.items.kit.ruler, |
| 96 | ring: self.items.kit.ring, |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | pub fn reach(&self) -> Reach { |
| 101 | Reach { |
| 102 | foreskin: self.body.foreskin.is_some(), |
| 103 | drawer: self.drawer_worth_searching(), |
| 104 | kitchen: self.floor.has_kitchen() && !self.here.kitchen.closed, |
| 105 | thread: self.thread_reachable(), |
| 106 | shop: self.floor.shop_open(), |
| 107 | portal: self.needs_portal(), |
| 108 | elder: self.floor == Floor::Forum && self.online.elder != Elder::Beaten, |
| 109 | footage: self.online.filmed_at.is_some(), |
| 110 | diagnosis: self.floor == Floor::Clinic, |
| 111 | branch_pending: self.online.story == Story::Pending, |
| 112 | shopping: self.here.shopping, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | pub fn needs_portal(&self) -> bool { |
| 117 | self.floor == Floor::Hotel && !self.here.wifi_accepted |
| 118 | } |
| 119 | |
| 120 | /// Shame follows the player across runs in a signed cookie. Each response adds this run's new |
| 121 | /// shame to whatever total the client presents, so two runs at once both count. |
| 122 | pub fn settle_shame(&mut self, presented: Option<u32>) -> u32 { |
| 123 | let standing = &mut self.standing; |
| 124 | let total = |
| 125 | presented.unwrap_or(standing.shame_total) + (standing.shame - standing.shame_reported); |
| 126 | standing.shame_reported = standing.shame; |
| 127 | standing.shame_total = total; |
| 128 | total |
| 129 | } |
| 130 | |
| 131 | pub fn trace(&mut self) { |
| 132 | self.standing.shame += 1; |
| 133 | } |
| 134 | |
| 135 | pub fn measured(&self) -> bool { |
| 136 | self.items.kit.ruler |
| 137 | } |
| 138 | |
| 139 | pub fn id(&self) -> &str { |
| 140 | &self.ident.id |
| 141 | } |
| 142 | |
| 143 | pub fn last_seen(&self) -> Instant { |
| 144 | self.ident.last_seen |
| 145 | } |
| 146 | |
| 147 | /// A request has arrived. Whoever was watching last time has looked away. |
| 148 | pub fn seen(&mut self, now: Instant) { |
| 149 | self.ident.last_seen = now; |
| 150 | self.observed = None; |
| 151 | } |
| 152 | |
| 153 | pub fn foreskin(&self) -> Option<f64> { |
| 154 | self.body.foreskin |
| 155 | } |
| 156 | |
| 157 | pub fn grip(&self) -> Grip { |
| 158 | self.body.grip |
| 159 | } |
| 160 | |
| 161 | pub fn knows(&self, technique: Technique) -> bool { |
| 162 | match technique { |
| 163 | Technique::Training(Training::Uli) => self.online.uli_taught, |
| 164 | Technique::Utility(Utility::Meditate) => self.online.meditation_taught, |
| 165 | _ => true, |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | pub fn holds(&self, item: Item) -> bool { |
| 170 | self.items.has(item) |
| 171 | } |
| 172 | |
| 173 | pub fn inventory_view(&self) -> Value { |
| 174 | let items: Vec<Value> = self |
| 175 | .items |
| 176 | .owned() |
| 177 | .into_iter() |
| 178 | .map(|item| { |
| 179 | json!({ |
| 180 | "name": item.slug(), |
| 181 | "href": self.path(&format!("/inventory/{}", item.slug())), |
| 182 | }) |
| 183 | }) |
| 184 | .collect(); |
| 185 | json!({ |
| 186 | "items": items, |
| 187 | "ice_packs": self.items.stores.ice_packs, |
| 188 | "supplements": self.items.stores.supplements, |
| 189 | "excuses": self.excuses().into_iter().map(Excuse::phrase).collect::<Vec<_>>(), |
| 190 | }) |
| 191 | } |
| 192 | } |