atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
ec6fd4bb5b6028e1fe0ec1bc286e57748b9728dc
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git

Blame: src/game/body.rs

782069860236atagen2026-10-10 12:44 UTC1
use axum::http::StatusCode;
782069860236atagen2026-10-10 12:44 UTC2
use serde::Deserialize;
782069860236atagen2026-10-10 12:44 UTC3
use serde_json::{Value, json};
782069860236atagen2026-10-10 12:44 UTC4
782069860236atagen2026-10-10 12:44 UTC5
use super::{
782069860236atagen2026-10-10 12:44 UTC6
    Grip, Outcome, Run,
782069860236atagen2026-10-10 12:44 UTC7
    balance::{EXAGGERATION, PUT_GAIN},
782069860236atagen2026-10-10 12:44 UTC8
    etag_matches, honest,
782069860236atagen2026-10-10 12:44 UTC9
};
782069860236atagen2026-10-10 12:44 UTC10
use crate::problem::Problem;
782069860236atagen2026-10-10 12:44 UTC11
782069860236atagen2026-10-10 12:44 UTC12
#[derive(Deserialize)]
782069860236atagen2026-10-10 12:44 UTC13
pub struct PatchOp {
782069860236atagen2026-10-10 12:44 UTC14
    pub op: String,
782069860236atagen2026-10-10 12:44 UTC15
    pub path: String,
782069860236atagen2026-10-10 12:44 UTC16
    #[serde(default)]
782069860236atagen2026-10-10 12:44 UTC17
    pub value: Value,
782069860236atagen2026-10-10 12:44 UTC18
}
782069860236atagen2026-10-10 12:44 UTC19
782069860236atagen2026-10-10 12:44 UTC20
pub(super) fn exaggeration() -> Problem {
782069860236atagen2026-10-10 12:44 UTC21
    Problem::new(
782069860236atagen2026-10-10 12:44 UTC22
        StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC23
        "exaggeration",
782069860236atagen2026-10-10 12:44 UTC24
        "The server has measured. Lying is not a technique.",
782069860236atagen2026-10-10 12:44 UTC25
    )
782069860236atagen2026-10-10 12:44 UTC26
}
782069860236atagen2026-10-10 12:44 UTC27
782069860236atagen2026-10-10 12:44 UTC28
impl Run {
782069860236atagen2026-10-10 12:44 UTC29
    pub fn put(&mut self) -> Outcome {
782069860236atagen2026-10-10 12:44 UTC30
        if self.put_applied {
782069860236atagen2026-10-10 12:44 UTC31
            return Outcome::empty(StatusCode::NO_CONTENT);
782069860236atagen2026-10-10 12:44 UTC32
        }
782069860236atagen2026-10-10 12:44 UTC33
        self.put_applied = true;
782069860236atagen2026-10-10 12:44 UTC34
        self.length += PUT_GAIN;
782069860236atagen2026-10-10 12:44 UTC35
        self.touch();
782069860236atagen2026-10-10 12:44 UTC36
        Outcome::json(StatusCode::OK, json!({ "detail": "Applied." }))
782069860236atagen2026-10-10 12:44 UTC37
    }
782069860236atagen2026-10-10 12:44 UTC38
782069860236atagen2026-10-10 12:44 UTC39
    pub fn patch(&mut self, ops: &[PatchOp]) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC40
        for op in ops {
782069860236atagen2026-10-10 12:44 UTC41
            if op.op != "test" {
782069860236atagen2026-10-10 12:44 UTC42
                return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC43
                    StatusCode::UNPROCESSABLE_ENTITY,
782069860236atagen2026-10-10 12:44 UTC44
                    "wishful-thinking",
782069860236atagen2026-10-10 12:44 UTC45
                    "The server accepts only the truth.",
782069860236atagen2026-10-10 12:44 UTC46
                )
782069860236atagen2026-10-10 12:44 UTC47
                .with("op", op.op.clone()));
782069860236atagen2026-10-10 12:44 UTC48
            }
782069860236atagen2026-10-10 12:44 UTC49
            let actual = match op.path.as_str() {
782069860236atagen2026-10-10 12:44 UTC50
                "/length" => self.current_length(),
782069860236atagen2026-10-10 12:44 UTC51
                "/girth" => self.girth,
782069860236atagen2026-10-10 12:44 UTC52
                "/foreskin" => self.foreskin.unwrap_or(0.0),
782069860236atagen2026-10-10 12:44 UTC53
                _ => {
782069860236atagen2026-10-10 12:44 UTC54
                    return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC55
                        StatusCode::UNPROCESSABLE_ENTITY,
782069860236atagen2026-10-10 12:44 UTC56
                        "no-such-measurement",
782069860236atagen2026-10-10 12:44 UTC57
                        "There is no such measurement.",
782069860236atagen2026-10-10 12:44 UTC58
                    )
782069860236atagen2026-10-10 12:44 UTC59
                    .with("path", op.path.clone()));
782069860236atagen2026-10-10 12:44 UTC60
                }
782069860236atagen2026-10-10 12:44 UTC61
            };
782069860236atagen2026-10-10 12:44 UTC62
            let Some(claimed) = op.value.as_f64() else {
782069860236atagen2026-10-10 12:44 UTC63
                return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC64
                    StatusCode::BAD_REQUEST,
782069860236atagen2026-10-10 12:44 UTC65
                    "malformed-patch",
782069860236atagen2026-10-10 12:44 UTC66
                    "A test requires a numeric value.",
782069860236atagen2026-10-10 12:44 UTC67
                ));
782069860236atagen2026-10-10 12:44 UTC68
            };
782069860236atagen2026-10-10 12:44 UTC69
            if op.path == "/length" && claimed >= actual + EXAGGERATION {
782069860236atagen2026-10-10 12:44 UTC70
                self.shame += 2;
782069860236atagen2026-10-10 12:44 UTC71
                self.touch();
782069860236atagen2026-10-10 12:44 UTC72
                return Err(exaggeration());
782069860236atagen2026-10-10 12:44 UTC73
            }
782069860236atagen2026-10-10 12:44 UTC74
            if !honest(claimed, actual) {
782069860236atagen2026-10-10 12:44 UTC75
                self.shame += 1;
782069860236atagen2026-10-10 12:44 UTC76
                self.touch();
782069860236atagen2026-10-10 12:44 UTC77
                return Err(
782069860236atagen2026-10-10 12:44 UTC78
                    Problem::new(StatusCode::CONFLICT, "test-failed", "The test failed.")
782069860236atagen2026-10-10 12:44 UTC79
                        .with("path", op.path.clone()),
782069860236atagen2026-10-10 12:44 UTC80
                );
782069860236atagen2026-10-10 12:44 UTC81
            }
782069860236atagen2026-10-10 12:44 UTC82
        }
782069860236atagen2026-10-10 12:44 UTC83
        Ok(Outcome::json(
782069860236atagen2026-10-10 12:44 UTC84
            StatusCode::OK,
782069860236atagen2026-10-10 12:44 UTC85
            json!({ "detail": "Correct." }),
782069860236atagen2026-10-10 12:44 UTC86
        ))
782069860236atagen2026-10-10 12:44 UTC87
    }
782069860236atagen2026-10-10 12:44 UTC88
782069860236atagen2026-10-10 12:44 UTC89
    pub fn delete_foreskin(&mut self, if_match: Option<&str>) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC90
        if self.foreskin.is_none() {
782069860236atagen2026-10-10 12:44 UTC91
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC92
                StatusCode::GONE,
782069860236atagen2026-10-10 12:44 UTC93
                "foreskin-gone",
782069860236atagen2026-10-10 12:44 UTC94
                "The foreskin is gone. It will not be returning.",
782069860236atagen2026-10-10 12:44 UTC95
            ));
782069860236atagen2026-10-10 12:44 UTC96
        }
782069860236atagen2026-10-10 12:44 UTC97
        let Some(if_match) = if_match else {
782069860236atagen2026-10-10 12:44 UTC98
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC99
                StatusCode::PRECONDITION_REQUIRED,
782069860236atagen2026-10-10 12:44 UTC100
                "confirmation-required",
782069860236atagen2026-10-10 12:44 UTC101
                "The server requires you to be sure. Send If-Match.",
782069860236atagen2026-10-10 12:44 UTC102
            ));
782069860236atagen2026-10-10 12:44 UTC103
        };
782069860236atagen2026-10-10 12:44 UTC104
        if !etag_matches(if_match, &self.etag()) {
782069860236atagen2026-10-10 12:44 UTC105
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC106
                StatusCode::PRECONDITION_FAILED,
782069860236atagen2026-10-10 12:44 UTC107
                "stale-foreskin",
782069860236atagen2026-10-10 12:44 UTC108
                "The foreskin has changed since you last looked at it.",
782069860236atagen2026-10-10 12:44 UTC109
            ));
782069860236atagen2026-10-10 12:44 UTC110
        }
782069860236atagen2026-10-10 12:44 UTC111
        self.foreskin = None;
782069860236atagen2026-10-10 12:44 UTC112
        self.shame += 20;
782069860236atagen2026-10-10 12:44 UTC113
        self.touch();
782069860236atagen2026-10-10 12:44 UTC114
        Ok(Outcome::empty(StatusCode::NO_CONTENT))
782069860236atagen2026-10-10 12:44 UTC115
    }
782069860236atagen2026-10-10 12:44 UTC116
782069860236atagen2026-10-10 12:44 UTC117
    pub fn set_grip(&mut self, grip: Grip) {
782069860236atagen2026-10-10 12:44 UTC118
        self.grip = grip;
782069860236atagen2026-10-10 12:44 UTC119
        self.touch();
782069860236atagen2026-10-10 12:44 UTC120
    }
782069860236atagen2026-10-10 12:44 UTC121
}