an http based roguelike rpg about jelqing
Blame: src/game/body.rs
782069860236 | atagen | 2026-10-10 12:44 UTC | 1 | use axum::http::StatusCode; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 2 | use serde::Deserialize; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 3 | use serde_json::{Value, json}; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 4 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 5 | use super::{ |
782069860236 | atagen | 2026-10-10 12:44 UTC | 6 | Grip, Outcome, Run, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 7 | balance::{EXAGGERATION, PUT_GAIN}, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 8 | etag_matches, honest, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 9 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 10 | use crate::problem::Problem; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 11 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 12 | #[derive(Deserialize)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 13 | pub struct PatchOp { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 14 | pub op: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 15 | pub path: String, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 16 | #[serde(default)] |
782069860236 | atagen | 2026-10-10 12:44 UTC | 17 | pub value: Value, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 18 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 19 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 20 | pub(super) fn exaggeration() -> Problem { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 21 | Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 22 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 23 | "exaggeration", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 24 | "The server has measured. Lying is not a technique.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 25 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 26 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 27 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 28 | impl Run { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 29 | pub fn put(&mut self) -> Outcome { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 30 | if self.put_applied { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 31 | return Outcome::empty(StatusCode::NO_CONTENT); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 32 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 33 | self.put_applied = true; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 34 | self.length += PUT_GAIN; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 35 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 36 | Outcome::json(StatusCode::OK, json!({ "detail": "Applied." })) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 37 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 38 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 39 | pub fn patch(&mut self, ops: &[PatchOp]) -> Result<Outcome, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 40 | for op in ops { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 41 | if op.op != "test" { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 42 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 43 | StatusCode::UNPROCESSABLE_ENTITY, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 44 | "wishful-thinking", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 45 | "The server accepts only the truth.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 46 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 47 | .with("op", op.op.clone())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 48 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 49 | let actual = match op.path.as_str() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 50 | "/length" => self.current_length(), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 51 | "/girth" => self.girth, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 52 | "/foreskin" => self.foreskin.unwrap_or(0.0), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 53 | _ => { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 54 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 55 | StatusCode::UNPROCESSABLE_ENTITY, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 56 | "no-such-measurement", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 57 | "There is no such measurement.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 58 | ) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 59 | .with("path", op.path.clone())); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 60 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 61 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 62 | let Some(claimed) = op.value.as_f64() else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 63 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 64 | StatusCode::BAD_REQUEST, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 65 | "malformed-patch", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 66 | "A test requires a numeric value.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 67 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 68 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 69 | if op.path == "/length" && claimed >= actual + EXAGGERATION { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 70 | self.shame += 2; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 71 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 72 | return Err(exaggeration()); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 73 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 74 | if !honest(claimed, actual) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 75 | self.shame += 1; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 76 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 77 | return Err( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 78 | Problem::new(StatusCode::CONFLICT, "test-failed", "The test failed.") |
782069860236 | atagen | 2026-10-10 12:44 UTC | 79 | .with("path", op.path.clone()), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 80 | ); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 81 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 82 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 83 | Ok(Outcome::json( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 84 | StatusCode::OK, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 85 | json!({ "detail": "Correct." }), |
782069860236 | atagen | 2026-10-10 12:44 UTC | 86 | )) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 87 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 88 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 89 | pub fn delete_foreskin(&mut self, if_match: Option<&str>) -> Result<Outcome, Problem> { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 90 | if self.foreskin.is_none() { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 91 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 92 | StatusCode::GONE, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 93 | "foreskin-gone", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 94 | "The foreskin is gone. It will not be returning.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 95 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 96 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 97 | let Some(if_match) = if_match else { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 98 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 99 | StatusCode::PRECONDITION_REQUIRED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 100 | "confirmation-required", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 101 | "The server requires you to be sure. Send If-Match.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 102 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 103 | }; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 104 | if !etag_matches(if_match, &self.etag()) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 105 | return Err(Problem::new( |
782069860236 | atagen | 2026-10-10 12:44 UTC | 106 | StatusCode::PRECONDITION_FAILED, |
782069860236 | atagen | 2026-10-10 12:44 UTC | 107 | "stale-foreskin", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 108 | "The foreskin has changed since you last looked at it.", |
782069860236 | atagen | 2026-10-10 12:44 UTC | 109 | )); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 110 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 111 | self.foreskin = None; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 112 | self.shame += 20; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 113 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 114 | Ok(Outcome::empty(StatusCode::NO_CONTENT)) |
782069860236 | atagen | 2026-10-10 12:44 UTC | 115 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 116 | |
782069860236 | atagen | 2026-10-10 12:44 UTC | 117 | pub fn set_grip(&mut self, grip: Grip) { |
782069860236 | atagen | 2026-10-10 12:44 UTC | 118 | self.grip = grip; |
782069860236 | atagen | 2026-10-10 12:44 UTC | 119 | self.touch(); |
782069860236 | atagen | 2026-10-10 12:44 UTC | 120 | } |
782069860236 | atagen | 2026-10-10 12:44 UTC | 121 | } |