atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/game/forum.rs

a6caa2f99786atagen2026-10-12 10:38 UTC1
use std::time::SystemTime;
a6caa2f99786atagen2026-10-12 10:38 UTC2
782069860236atagen2026-10-10 12:44 UTC3
use axum::http::{StatusCode, header};
782069860236atagen2026-10-10 12:44 UTC4
use serde_json::{Value, json};
782069860236atagen2026-10-10 12:44 UTC5
782069860236atagen2026-10-10 12:44 UTC6
use super::{
782069860236atagen2026-10-10 12:44 UTC7
    Cause, Elder, Floor, Outcome, Run, Story,
a6caa2f99786atagen2026-10-12 10:38 UTC8
    balance::{EXAGGERATION, PATIENCE, ULI_ODDS, ULI_READS},
782069860236atagen2026-10-10 12:44 UTC9
    body::exaggeration,
782069860236atagen2026-10-10 12:44 UTC10
    honest, parse_credentials,
782069860236atagen2026-10-10 12:44 UTC11
};
782069860236atagen2026-10-10 12:44 UTC12
use crate::{headers::X_FORUM_NOTE, problem::Problem};
782069860236atagen2026-10-10 12:44 UTC13
782069860236atagen2026-10-10 12:44 UTC14
pub const FORUM_THREAD: &str = "/forum/threads/2009";
782069860236atagen2026-10-10 12:44 UTC15
pub const ELDER_LAST_MODIFIED: &str = "Sun, 14 Jun 2009 03:12:00 GMT";
782069860236atagen2026-10-10 12:44 UTC16
782069860236atagen2026-10-10 12:44 UTC17
const SPREADSHEET: &str = "\
782069860236atagen2026-10-10 12:44 UTC18
date,length,girth,technique,notes,honest?
782069860236atagen2026-10-10 12:44 UTC19
2007-01-03,7.50,5.50,wet,baseline,no
782069860236atagen2026-10-10 12:44 UTC20
2007-06-11,7.50,5.50,wet,feeling fuller,no
782069860236atagen2026-10-10 12:44 UTC21
2008-01-02,7.50,5.50,dry,new routine,no
782069860236atagen2026-10-10 12:44 UTC22
2008-09-19,7.50,5.50,uli,PUT is idempotent. do not use PUT.,no
782069860236atagen2026-10-10 12:44 UTC23
2009-06-14,7.50,5.50,wet,progress is slow but real,no
782069860236atagen2026-10-10 12:44 UTC24
";
782069860236atagen2026-10-10 12:44 UTC25
a6caa2f99786atagen2026-10-12 10:38 UTC26
fn nothing_new() -> Outcome {
a6caa2f99786atagen2026-10-12 10:38 UTC27
    Outcome::empty(StatusCode::NOT_MODIFIED).header(
a6caa2f99786atagen2026-10-12 10:38 UTC28
        header::WARNING,
a6caa2f99786atagen2026-10-12 10:38 UTC29
        r#"199 - "Nothing new has been posted since 2009.""#,
a6caa2f99786atagen2026-10-12 10:38 UTC30
    )
a6caa2f99786atagen2026-10-12 10:38 UTC31
}
a6caa2f99786atagen2026-10-12 10:38 UTC32
782069860236atagen2026-10-10 12:44 UTC33
pub fn forum_thread() -> Value {
782069860236atagen2026-10-10 12:44 UTC34
    json!({
782069860236atagen2026-10-10 12:44 UTC35
        "title": "The definitive jelqing FAQ (updated 2009)",
782069860236atagen2026-10-10 12:44 UTC36
        "posts": [
782069860236atagen2026-10-10 12:44 UTC37
            { "author": "TheElder", "body": "Warm up. Always warm up. A cold jelq is a 412 waiting to happen." },
782069860236atagen2026-10-10 12:44 UTC38
            { "author": "TheElder", "body": "Keep EQ between 50 and 70. Above 70 you are gambling. Above 90 you are a cautionary tale." },
0fdc47364c8catagen2026-10-11 04:45 UTC39
            { "author": "newbie_2009", "body": "everyone keeps saying uli. who is uli" },
0fdc47364c8catagen2026-10-11 04:45 UTC40
            { "author": "TheElder", "body": "uli was a member here. He posted the technique in 2001 and never posted again. Some say he ascended. The search function says his account is gone." },
0fdc47364c8catagen2026-10-11 04:45 UTC41
            { "author": "TheElder", "body": "We do not say his name in vain. We say it in girth." },
782069860236atagen2026-10-10 12:44 UTC42
            { "author": "newbie_2009", "body": "I have been sending PUT for three weeks and nothing has changed." },
782069860236atagen2026-10-10 12:44 UTC43
            { "author": "TheElder", "body": "PUT is idempotent. Read the spec." },
782069860236atagen2026-10-10 12:44 UTC44
            { "author": "TheElder", "body": "Members verify with a Ruler. Proxy-Authorization, inches, no rounding up. We can tell." },
782069860236atagen2026-10-10 12:44 UTC45
            { "author": "throwaway_77", "body": "my doctor said i was normal. i didnt believe him. i still dont know what he meant by bearer" },
782069860236atagen2026-10-10 12:44 UTC46
            { "author": "TheElder", "body": "My numbers are in my spreadsheet. They have not changed since 2009. Nothing about me has changed since 2009." },
782069860236atagen2026-10-10 12:44 UTC47
        ],
782069860236atagen2026-10-10 12:44 UTC48
    })
782069860236atagen2026-10-10 12:44 UTC49
}
782069860236atagen2026-10-10 12:44 UTC50
782069860236atagen2026-10-10 12:44 UTC51
fn verification(detail: &str) -> Problem {
782069860236atagen2026-10-10 12:44 UTC52
    Problem::new(
782069860236atagen2026-10-10 12:44 UTC53
        StatusCode::PROXY_AUTHENTICATION_REQUIRED,
782069860236atagen2026-10-10 12:44 UTC54
        "verification-required",
782069860236atagen2026-10-10 12:44 UTC55
        detail,
782069860236atagen2026-10-10 12:44 UTC56
    )
782069860236atagen2026-10-10 12:44 UTC57
    .header(header::PROXY_AUTHENTICATE, "Ruler realm=\"forum\"")
782069860236atagen2026-10-10 12:44 UTC58
}
782069860236atagen2026-10-10 12:44 UTC59
782069860236atagen2026-10-10 12:44 UTC60
impl Run {
782069860236atagen2026-10-10 12:44 UTC61
    pub(super) fn branch_offer(&self) -> Outcome {
782069860236atagen2026-10-10 12:44 UTC62
        let forum = self.path("/branches/forum");
782069860236atagen2026-10-10 12:44 UTC63
        let close = self.path("/branches/close-tab");
782069860236atagen2026-10-10 12:44 UTC64
        Outcome::json(
782069860236atagen2026-10-10 12:44 UTC65
            StatusCode::MULTIPLE_CHOICES,
782069860236atagen2026-10-10 12:44 UTC66
            json!({
782069860236atagen2026-10-10 12:44 UTC67
                "detail": "A browser tab is open. It has been open for some time.",
782069860236atagen2026-10-10 12:44 UTC68
                "choices": [
782069860236atagen2026-10-10 12:44 UTC69
                    { "href": forum, "method": "POST", "title": "Read the forum" },
782069860236atagen2026-10-10 12:44 UTC70
                    { "href": close, "method": "POST", "title": "Close the tab" },
782069860236atagen2026-10-10 12:44 UTC71
                ],
782069860236atagen2026-10-10 12:44 UTC72
            }),
782069860236atagen2026-10-10 12:44 UTC73
        )
782069860236atagen2026-10-10 12:44 UTC74
        .header(
782069860236atagen2026-10-10 12:44 UTC75
            header::LINK,
782069860236atagen2026-10-10 12:44 UTC76
            format!(
d46f9d0b953aatagen2026-10-10 14:41 UTC77
                "<{forum}>; rel=\"alternate\"; title=\"Read the forum\"; allow=\"POST\", <{close}>; rel=\"alternate\"; title=\"Close the tab\"; allow=\"POST\""
782069860236atagen2026-10-10 12:44 UTC78
            ),
782069860236atagen2026-10-10 12:44 UTC79
        )
782069860236atagen2026-10-10 12:44 UTC80
    }
782069860236atagen2026-10-10 12:44 UTC81
782069860236atagen2026-10-10 12:44 UTC82
    pub fn choose(&mut self, choice: &str) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC83
        if self.story != Story::Pending {
782069860236atagen2026-10-10 12:44 UTC84
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC85
                StatusCode::CONFLICT,
782069860236atagen2026-10-10 12:44 UTC86
                "no-decision-pending",
782069860236atagen2026-10-10 12:44 UTC87
                "There is no decision pending.",
782069860236atagen2026-10-10 12:44 UTC88
            ));
782069860236atagen2026-10-10 12:44 UTC89
        }
782069860236atagen2026-10-10 12:44 UTC90
        let (story, location, detail) = match choice {
782069860236atagen2026-10-10 12:44 UTC91
            "forum" => {
782069860236atagen2026-10-10 12:44 UTC92
                self.shame += 3;
b85a0d4536c3atagen2026-10-11 08:50 UTC93
                let detail =
b85a0d4536c3atagen2026-10-11 08:50 UTC94
                    "The thread is from 2009. A technique called uli is discussed at length.";
0fdc47364c8catagen2026-10-11 04:45 UTC95
                (Story::ReadForum, self.path("/techniques"), detail)
782069860236atagen2026-10-10 12:44 UTC96
            }
782069860236atagen2026-10-10 12:44 UTC97
            "close-tab" => {
782069860236atagen2026-10-10 12:44 UTC98
                self.suspicion = 0;
782069860236atagen2026-10-10 12:44 UTC99
                (
782069860236atagen2026-10-10 12:44 UTC100
                    Story::ClosedTab,
782069860236atagen2026-10-10 12:44 UTC101
                    self.path(""),
782069860236atagen2026-10-10 12:44 UTC102
                    "The tab is closed. Nobody saw it.",
782069860236atagen2026-10-10 12:44 UTC103
                )
782069860236atagen2026-10-10 12:44 UTC104
            }
782069860236atagen2026-10-10 12:44 UTC105
            _ => {
782069860236atagen2026-10-10 12:44 UTC106
                return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC107
                    StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC108
                    "no-such-choice",
782069860236atagen2026-10-10 12:44 UTC109
                    "That was not one of the choices.",
782069860236atagen2026-10-10 12:44 UTC110
                ));
782069860236atagen2026-10-10 12:44 UTC111
            }
782069860236atagen2026-10-10 12:44 UTC112
        };
782069860236atagen2026-10-10 12:44 UTC113
        self.story = story;
782069860236atagen2026-10-10 12:44 UTC114
        self.touch();
782069860236atagen2026-10-10 12:44 UTC115
        Ok(
782069860236atagen2026-10-10 12:44 UTC116
            Outcome::json(StatusCode::SEE_OTHER, json!({ "detail": detail }))
782069860236atagen2026-10-10 12:44 UTC117
                .header(header::LOCATION, location),
782069860236atagen2026-10-10 12:44 UTC118
        )
782069860236atagen2026-10-10 12:44 UTC119
    }
782069860236atagen2026-10-10 12:44 UTC120
a6caa2f99786atagen2026-10-12 10:38 UTC121
    /// Re-reading is free. Each floor gets a couple of live reads that may surface the reply,
a6caa2f99786atagen2026-10-12 10:38 UTC122
    /// after which the forum is down and the browser serves what it kept.
b85a0d4536c3atagen2026-10-11 08:50 UTC123
    pub fn read_thread(&mut self) -> Result<Outcome, Problem> {
b85a0d4536c3atagen2026-10-11 08:50 UTC124
        if self.story == Story::Pending {
b85a0d4536c3atagen2026-10-11 08:50 UTC125
            return Ok(self.branch_offer());
b85a0d4536c3atagen2026-10-11 08:50 UTC126
        }
e807266c323batagen2026-10-12 09:46 UTC127
        if !self.thread_reachable() {
e807266c323batagen2026-10-12 09:46 UTC128
            return Err(Problem::new(
e807266c323batagen2026-10-12 09:46 UTC129
                StatusCode::NOT_FOUND,
e807266c323batagen2026-10-12 09:46 UTC130
                "no-thread",
e807266c323batagen2026-10-12 09:46 UTC131
                "There is no thread to re-read. Nobody has mentioned a forum yet.",
e807266c323batagen2026-10-12 09:46 UTC132
            ));
e807266c323batagen2026-10-12 09:46 UTC133
        }
5cc6ab966f30atagen2026-10-11 11:23 UTC134
        self.end_kitchen_visit();
a6caa2f99786atagen2026-10-12 10:38 UTC135
        if self.uli_taught {
a6caa2f99786atagen2026-10-12 10:38 UTC136
            return Ok(Outcome::json(StatusCode::OK, forum_thread()));
b85a0d4536c3atagen2026-10-11 08:50 UTC137
        }
a6caa2f99786atagen2026-10-12 10:38 UTC138
        if self.here.thread_rolls >= ULI_READS {
a6caa2f99786atagen2026-10-12 10:38 UTC139
            let age = httpdate::parse_http_date(ELDER_LAST_MODIFIED)
a6caa2f99786atagen2026-10-12 10:38 UTC140
                .ok()
a6caa2f99786atagen2026-10-12 10:38 UTC141
                .and_then(|posted| SystemTime::now().duration_since(posted).ok())
a6caa2f99786atagen2026-10-12 10:38 UTC142
                .map_or(0, |age| age.as_secs());
a6caa2f99786atagen2026-10-12 10:38 UTC143
            return Ok(Outcome::json(StatusCode::OK, forum_thread())
a6caa2f99786atagen2026-10-12 10:38 UTC144
                .header(header::AGE, age.to_string())
a6caa2f99786atagen2026-10-12 10:38 UTC145
                .header(header::WARNING, r#"112 - "Disconnected operation""#));
a6caa2f99786atagen2026-10-12 10:38 UTC146
        }
a6caa2f99786atagen2026-10-12 10:38 UTC147
        self.here.thread_rolls += 1;
a6caa2f99786atagen2026-10-12 10:38 UTC148
        self.touch();
3f9091557ec3atagen2026-10-11 11:43 UTC149
        if self.chance(
3f9091557ec3atagen2026-10-11 11:43 UTC150
            self.decision(
3f9091557ec3atagen2026-10-11 11:43 UTC151
                "teach-uli",
3f9091557ec3atagen2026-10-11 11:43 UTC152
                "Does this reread finally surface the 2009 reply that teaches uli?",
3f9091557ec3atagen2026-10-11 11:43 UTC153
            ),
3f9091557ec3atagen2026-10-11 11:43 UTC154
            ULI_ODDS,
3f9091557ec3atagen2026-10-11 11:43 UTC155
        ) {
b85a0d4536c3atagen2026-10-11 08:50 UTC156
            self.uli_taught = true;
b85a0d4536c3atagen2026-10-11 08:50 UTC157
            return Ok(Outcome::json(
b85a0d4536c3atagen2026-10-11 08:50 UTC158
                StatusCode::OK,
b85a0d4536c3atagen2026-10-11 08:50 UTC159
                json!({ "detail": "Past the necroposting, a reply from TheElder lays out the technique. Uli. The full routine. You write it down before the tab can lose it." }),
b85a0d4536c3atagen2026-10-11 08:50 UTC160
            )
b85a0d4536c3atagen2026-10-11 08:50 UTC161
            .header(header::LOCATION, self.path("/techniques")));
b85a0d4536c3atagen2026-10-11 08:50 UTC162
        }
a6caa2f99786atagen2026-10-12 10:38 UTC163
        Ok(nothing_new())
b85a0d4536c3atagen2026-10-11 08:50 UTC164
    }
b85a0d4536c3atagen2026-10-11 08:50 UTC165
782069860236atagen2026-10-10 12:44 UTC166
    fn elder_strike(&mut self, problem: Problem) -> Problem {
782069860236atagen2026-10-10 12:44 UTC167
        self.touch();
782069860236atagen2026-10-10 12:44 UTC168
        let Elder::Unverified { strikes } = &mut self.elder else {
782069860236atagen2026-10-10 12:44 UTC169
            return problem;
782069860236atagen2026-10-10 12:44 UTC170
        };
782069860236atagen2026-10-10 12:44 UTC171
        *strikes += 1;
782069860236atagen2026-10-10 12:44 UTC172
        if *strikes < PATIENCE {
782069860236atagen2026-10-10 12:44 UTC173
            return problem;
782069860236atagen2026-10-10 12:44 UTC174
        }
782069860236atagen2026-10-10 12:44 UTC175
        self.end(Cause::Banned)
782069860236atagen2026-10-10 12:44 UTC176
    }
782069860236atagen2026-10-10 12:44 UTC177
782069860236atagen2026-10-10 12:44 UTC178
    /// Failed verification counts toward a ban even on GET, the way repeated bad credentials lock an account.
782069860236atagen2026-10-10 12:44 UTC179
    fn verify_member(&mut self, proxy_authorization: Option<&str>) -> Result<(), Problem> {
782069860236atagen2026-10-10 12:44 UTC180
        if !matches!(self.elder, Elder::Unverified { .. }) {
782069860236atagen2026-10-10 12:44 UTC181
            return Ok(());
782069860236atagen2026-10-10 12:44 UTC182
        }
782069860236atagen2026-10-10 12:44 UTC183
        let Some(credentials) = proxy_authorization else {
782069860236atagen2026-10-10 12:44 UTC184
            return Err(verification("The forum requires verification."));
782069860236atagen2026-10-10 12:44 UTC185
        };
782069860236atagen2026-10-10 12:44 UTC186
        let Some(claimed) =
782069860236atagen2026-10-10 12:44 UTC187
            parse_credentials(credentials, "ruler").and_then(|v| v.parse::<f64>().ok())
782069860236atagen2026-10-10 12:44 UTC188
        else {
782069860236atagen2026-10-10 12:44 UTC189
            return Err(verification("The forum requires verification, in inches."));
782069860236atagen2026-10-10 12:44 UTC190
        };
782069860236atagen2026-10-10 12:44 UTC191
        let actual = self.current_length();
782069860236atagen2026-10-10 12:44 UTC192
        if claimed >= actual + EXAGGERATION {
782069860236atagen2026-10-10 12:44 UTC193
            self.shame += 2;
782069860236atagen2026-10-10 12:44 UTC194
            return Err(self.elder_strike(exaggeration()));
782069860236atagen2026-10-10 12:44 UTC195
        }
782069860236atagen2026-10-10 12:44 UTC196
        if !honest(claimed, actual) {
782069860236atagen2026-10-10 12:44 UTC197
            self.shame += 1;
058650ec79d6atagen2026-10-12 10:49 UTC198
            let failed = verification("Verification failed. The forum can tell.");
058650ec79d6atagen2026-10-12 10:49 UTC199
            if self.girth_respected() {
058650ec79d6atagen2026-10-12 10:49 UTC200
                return Err(failed.header(
058650ec79d6atagen2026-10-12 10:49 UTC201
                    X_FORUM_NOTE,
058650ec79d6atagen2026-10-12 10:49 UTC202
                    "The mods have let this one go. Girth is also a dimension.",
058650ec79d6atagen2026-10-12 10:49 UTC203
                ));
058650ec79d6atagen2026-10-12 10:49 UTC204
            }
058650ec79d6atagen2026-10-12 10:49 UTC205
            return Err(self.elder_strike(failed));
782069860236atagen2026-10-10 12:44 UTC206
        }
782069860236atagen2026-10-10 12:44 UTC207
        self.elder = Elder::Verified;
782069860236atagen2026-10-10 12:44 UTC208
        self.touch();
782069860236atagen2026-10-10 12:44 UTC209
        Ok(())
782069860236atagen2026-10-10 12:44 UTC210
    }
782069860236atagen2026-10-10 12:44 UTC211
782069860236atagen2026-10-10 12:44 UTC212
    pub fn elder(
782069860236atagen2026-10-10 12:44 UTC213
        &mut self,
782069860236atagen2026-10-10 12:44 UTC214
        proxy_authorization: Option<&str>,
782069860236atagen2026-10-10 12:44 UTC215
        if_modified_since: Option<&str>,
782069860236atagen2026-10-10 12:44 UTC216
    ) -> Result<Outcome, Problem> {
782069860236atagen2026-10-10 12:44 UTC217
        if self.floor != Floor::Forum {
782069860236atagen2026-10-10 12:44 UTC218
            return Err(Problem::new(
782069860236atagen2026-10-10 12:44 UTC219
                StatusCode::NOT_FOUND,
782069860236atagen2026-10-10 12:44 UTC220
                "elder-absent",
782069860236atagen2026-10-10 12:44 UTC221
                "The Elder only posts on the forum.",
782069860236atagen2026-10-10 12:44 UTC222
            ));
782069860236atagen2026-10-10 12:44 UTC223
        }
782069860236atagen2026-10-10 12:44 UTC224
        self.verify_member(proxy_authorization)?;
782069860236atagen2026-10-10 12:44 UTC225
782069860236atagen2026-10-10 12:44 UTC226
        let last_modified =
782069860236atagen2026-10-10 12:44 UTC227
            httpdate::parse_http_date(ELDER_LAST_MODIFIED).expect("valid constant date");
782069860236atagen2026-10-10 12:44 UTC228
        let current = if_modified_since
782069860236atagen2026-10-10 12:44 UTC229
            .and_then(|d| httpdate::parse_http_date(d).ok())
782069860236atagen2026-10-10 12:44 UTC230
            .is_some_and(|since| since >= last_modified);
782069860236atagen2026-10-10 12:44 UTC231
        if current {
782069860236atagen2026-10-10 12:44 UTC232
            if self.elder != Elder::Beaten {
782069860236atagen2026-10-10 12:44 UTC233
                self.elder = Elder::Beaten;
782069860236atagen2026-10-10 12:44 UTC234
                self.touch();
782069860236atagen2026-10-10 12:44 UTC235
            }
782069860236atagen2026-10-10 12:44 UTC236
            return Ok(Outcome::empty(StatusCode::NOT_MODIFIED)
782069860236atagen2026-10-10 12:44 UTC237
                .header(header::LAST_MODIFIED, ELDER_LAST_MODIFIED));
782069860236atagen2026-10-10 12:44 UTC238
        }
782069860236atagen2026-10-10 12:44 UTC239
        let spreadsheet = Outcome::raw(StatusCode::OK, "text/csv; header=present", SPREADSHEET)
782069860236atagen2026-10-10 12:44 UTC240
            .header(header::LAST_MODIFIED, ELDER_LAST_MODIFIED);
782069860236atagen2026-10-10 12:44 UTC241
        if self.filmed_at.is_some() {
782069860236atagen2026-10-10 12:44 UTC242
            return Ok(spreadsheet.header(X_FORUM_NOTE, "We have seen the footage."));
782069860236atagen2026-10-10 12:44 UTC243
        }
782069860236atagen2026-10-10 12:44 UTC244
        Ok(spreadsheet)
782069860236atagen2026-10-10 12:44 UTC245
    }
782069860236atagen2026-10-10 12:44 UTC246
}