an http based roguelike rpg about jelqing
| 1 | use axum::http::{StatusCode, header}; |
| 2 | use serde_json::json; |
| 3 | |
| 4 | use super::{ |
| 5 | Cause, Floor, Item, Outcome, Run, Story, |
| 6 | balance::{ |
| 7 | CAUGHT_MELONED, COCONUT_ODDS, ENERGY_ODDS, KITCHEN_ICE_ODDS, KITCHEN_TELLINGS, |
| 8 | KITCHEN_TOLD, MELON_SHAME, RUPTURE_LINE, SUSPICION_LIMIT, WATERMELON, |
| 9 | }, |
| 10 | }; |
| 11 | use crate::problem::Problem; |
| 12 | use rand::Rng; |
| 13 | |
| 14 | impl Run { |
| 15 | /// Raid the fridge. It costs the floor turn like anything else, and the household keeps count. |
| 16 | pub fn raid_kitchen(&mut self) -> Result<Outcome, Problem> { |
| 17 | if !self.floor.has_kitchen() { |
| 18 | return Err(Problem::new( |
| 19 | StatusCode::NOT_FOUND, |
| 20 | "no-kitchen", |
| 21 | "There is no kitchen on this floor.", |
| 22 | )); |
| 23 | } |
| 24 | if self.kitchen_tellings >= KITCHEN_TELLINGS { |
| 25 | return Err(Problem::new( |
| 26 | StatusCode::GONE, |
| 27 | "kitchen-off-limits", |
| 28 | "The kitchen is off limits. You have been told twice.", |
| 29 | )); |
| 30 | } |
| 31 | if self.story == Story::Pending { |
| 32 | return Ok(self.branch_offer()); |
| 33 | } |
| 34 | self.touch(); |
| 35 | self.turn += 1; |
| 36 | self.here.turns += 1; |
| 37 | self.realise(); |
| 38 | let was_below = self.suspicion < SUSPICION_LIMIT; |
| 39 | let outcome = if self.eq > RUPTURE_LINE && self.rng.random_ratio(WATERMELON.0, WATERMELON.1) |
| 40 | { |
| 41 | self.here.melon_offered = true; |
| 42 | self.melon_offer() |
| 43 | } else if self.chance(COCONUT_ODDS) { |
| 44 | self.items.grant(Item::CoconutOil); |
| 45 | Ok(Outcome::json( |
| 46 | StatusCode::OK, |
| 47 | json!({ "detail": "A jar of coconut oil, half of it already gone." }), |
| 48 | )) |
| 49 | } else if self.chance(ENERGY_ODDS) { |
| 50 | self.items.grant(Item::EnergyDrink); |
| 51 | Ok(Outcome::json( |
| 52 | StatusCode::OK, |
| 53 | json!({ "detail": "An energy drink, cold and faintly alarming." }), |
| 54 | )) |
| 55 | } else if self.chance(KITCHEN_ICE_ODDS) { |
| 56 | self.items.grant(Item::IcePack); |
| 57 | Ok(Outcome::json( |
| 58 | StatusCode::OK, |
| 59 | json!({ "detail": "An ice pack, buried under the peas." }), |
| 60 | )) |
| 61 | } else { |
| 62 | Ok(Outcome::empty(StatusCode::NO_CONTENT)) |
| 63 | }; |
| 64 | self.settle(was_below); |
| 65 | let outcome = outcome?; |
| 66 | if self.chance(KITCHEN_TOLD) { |
| 67 | self.kitchen_tellings += 1; |
| 68 | if self.kitchen_tellings >= KITCHEN_TELLINGS { |
| 69 | return Err(Problem::new( |
| 70 | StatusCode::GONE, |
| 71 | "kitchen-off-limits", |
| 72 | "The kitchen is off limits. You have been told twice.", |
| 73 | )); |
| 74 | } |
| 75 | self.notices.push(if self.floor == Floor::Nans { |
| 76 | "Stop raiding the fridge." |
| 77 | } else { |
| 78 | "Stop raiding the kitchen." |
| 79 | }); |
| 80 | } |
| 81 | Ok(outcome) |
| 82 | } |
| 83 | |
| 84 | fn melon_offer(&self) -> Result<Outcome, Problem> { |
| 85 | let commit = self.path("/kitchen/watermelon"); |
| 86 | Ok(Outcome::json( |
| 87 | StatusCode::MULTIPLE_CHOICES, |
| 88 | json!({ |
| 89 | "detail": "The fridge holds a watermelon. It is exactly the right shape.", |
| 90 | "choices": [ |
| 91 | { "href": commit, "method": "POST", "title": "Commit to the watermelon" }, |
| 92 | { "href": self.path(""), "method": "GET", "title": "Close the fridge" }, |
| 93 | ], |
| 94 | }), |
| 95 | ) |
| 96 | .header( |
| 97 | header::LINK, |
| 98 | format!( |
| 99 | "<{commit}>; rel=\"alternate\"; title=\"Commit to the watermelon\"; allow=\"POST\"" |
| 100 | ), |
| 101 | )) |
| 102 | } |
| 103 | |
| 104 | /// The watermelon. It empties EQ, keeps the gains, costs shame, and the household may notice. |
| 105 | pub fn commit_melon(&mut self) -> Result<Outcome, Problem> { |
| 106 | if !self.here.melon_offered { |
| 107 | return Err(Problem::new( |
| 108 | StatusCode::CONFLICT, |
| 109 | "no-melon-on-offer", |
| 110 | "There is no watermelon on offer.", |
| 111 | )); |
| 112 | } |
| 113 | self.here.melon_offered = false; |
| 114 | self.eq = 0; |
| 115 | self.shame += MELON_SHAME; |
| 116 | self.touch(); |
| 117 | if self.chance(CAUGHT_MELONED) { |
| 118 | return Err(self.end(Cause::Meloned)); |
| 119 | } |
| 120 | Ok(Outcome::json( |
| 121 | StatusCode::OK, |
| 122 | json!({ |
| 123 | "detail": "The watermelon did not survive, and neither did the mood. EQ is gone, the gains are kept, and the shame is considerable.", |
| 124 | }), |
| 125 | )) |
| 126 | } |
| 127 | } |