atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
7d3fb63208bf5064f04782aac0c0273305f8f35f
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/http/run.rsRust278 lines9.8 KB
1
//! Plumbing shared by every resource under `/runs/{id}`.
2
3
use std::time::Instant;
4
5
use axum::{
6
    http::{HeaderName, HeaderValue, StatusCode, header},
7
    response::{IntoResponse, Response},
8
};
9
use hyper_util::rt::TokioIo;
10
use tokio::io::AsyncWriteExt;
11
12
use super::{AppState, ctx::Ctx, meta, middleware::Compress, resource::Resource, runs};
13
use crate::{
14
    game::{Elder, FORUM_THREAD, Floor, Run, SHOP, Story, balance::SUSPICION_LIMIT},
15
    headers::{
16
        X_CASH, X_EQ, X_FLOOR, X_FORESKIN, X_GIRTH, X_LENGTH, X_OBSERVED_BY,
17
        X_SELF_REPORTED_LENGTH, X_SHAME, X_STAMINA, X_SUSPICION,
18
    },
19
    locale::Locale,
20
    problem::{PROBLEM_BASE, Problem},
21
    secret::Secret,
22
};
23
24
pub const REL_BASE: &str = "https://jelq.dev/rels/";
25
const COOKIE_ATTRIBUTES: &str = "Max-Age=31536000; Path=/; HttpOnly; SameSite=Strict";
26
/// Which household hazards stand between a request and the run.
27
#[derive(Clone, Copy, PartialEq, Eq)]
28
pub enum Scope {
29
    Home,
30
    Pockets,
31
}
32
33
pub struct RunResource {
34
    pub scope: Scope,
35
    pub resource: Resource<Run>,
36
}
37
38
/// What the server writes over the connection after a `101`.
39
#[derive(Clone)]
40
pub struct Greeting(pub String);
41
42
pub fn serve(state: &AppState, mut ctx: Ctx, target: &RunResource) -> Response {
43
    let mut runs = state.runs();
44
    let Some(run) = runs.get_mut(ctx.param("id")) else {
45
        return Problem::new(
46
            StatusCode::NOT_FOUND,
47
            "no-such-run",
48
            "There is no such run. There may never have been.",
49
        )
50
        .into_response();
51
    };
52
    if let Some(ending) = &run.ended {
53
        return Problem::new(StatusCode::GONE, "run-ended", "This run has ended.")
54
            .with("cause", format!("{PROBLEM_BASE}{}", ending.cause.slug()))
55
            .with("turn", ending.turn)
56
            .with("graveyard", "/graveyard")
57
            .into_response();
58
    }
59
    run.last_seen = ctx.now;
60
    run.recover(ctx.now);
61
    run.observed = None;
62
    let response =
63
        gate(run, &ctx, target.scope).unwrap_or_else(|| target.resource.dispatch(run, &ctx));
64
    let response = finish(run, &ctx, &state.config.secret, response);
65
    drop(runs);
66
    greet(response, &mut ctx)
67
}
68
69
fn gate(run: &mut Run, ctx: &Ctx, scope: Scope) -> Option<Response> {
70
    if scope == Scope::Pockets {
71
        return None;
72
    }
73
    if run.floor == Floor::Hotel && !run.here.wifi_accepted {
74
        let portal = run.path("/portal");
75
        return Some(
76
            Problem::new(
77
                StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
78
                "captive-portal",
79
                "The network requires authentication. The hotel would like you to agree to some terms.",
80
            )
81
            .with("portal", portal.as_str())
82
            .header(header::LINK, format!("<{portal}>; rel=\"{REL_BASE}portal\""))
83
            .into_response(),
84
        );
85
    }
86
    let authorization = ctx
87
        .header(header::AUTHORIZATION)
88
        .map(|text| Locale::negotiate(ctx.header(header::ACCEPT_LANGUAGE)).canonicalise(text));
89
    run.answer_door(authorization.as_deref())
90
        .err()
91
        .map(IntoResponse::into_response)
92
}
93
94
/// Every resource the run can reach right now, then every technique a JELQ could use. The `allow`
95
/// and `lubricant` extension attributes (RFC 8288 §3) say what each accepts, so a client never guesses.
96
fn links(run: &Run, now: Instant) -> String {
97
    let linked: [(&str, &RunResource, bool); 13] = [
98
        ("", &runs::RUN, true),
99
        ("floor", &runs::FLOOR, true),
100
        ("length", &runs::LENGTH, true),
101
        ("foreskin", &runs::FORESKIN, run.foreskin.is_some()),
102
        ("inventory", &runs::INVENTORY, true),
103
        ("techniques", &runs::TECHNIQUES, true),
104
        ("drawer", &runs::DRAWER, run.drawer_worth_searching()),
105
        ("grip", &runs::GRIP, true),
106
        ("shop", &runs::SHOP, run.floor.shop_open()),
107
        (
108
            "portal",
109
            &runs::PORTAL,
110
            run.floor == Floor::Hotel && !run.here.wifi_accepted,
111
        ),
112
        (
113
            "elder",
114
            &runs::ELDER,
115
            run.floor == Floor::Forum && run.elder != Elder::Beaten,
116
        ),
117
        ("footage", &runs::FOOTAGE, run.filmed_at.is_some()),
118
        ("diagnosis", &runs::DIAGNOSIS, run.floor == Floor::Clinic),
119
    ];
120
    linked
121
        .into_iter()
122
        .filter(|(_, _, relevant)| *relevant)
123
        .map(|(rel, target, _)| {
124
            let (path, name) = if rel.is_empty() {
125
                (run.path(""), "run")
126
            } else {
127
                (run.path(&format!("/{rel}")), rel)
128
            };
129
            let allow = target.resource.allow(run);
130
            format!("<{path}>; rel=\"{REL_BASE}{name}\"; allow=\"{allow}\"")
131
        })
132
        .chain(
133
            (run.forum_read() || run.floor.number() >= Floor::Forum.number()).then(|| {
134
                format!(
135
                    "<{FORUM_THREAD}>; rel=\"{REL_BASE}thread\"; allow=\"{}\"",
136
                    meta::THREAD.allow_any()
137
                )
138
            }),
139
        )
140
        .chain((run.story == Story::Pending).then(|| {
141
            let choice = |slug: &str, title: &str| {
142
                format!(
143
                    "<{}>; rel=\"{REL_BASE}branch\"; title=\"{title}\"; allow=\"POST\"",
144
                    run.path(&format!("/branches/{slug}"))
145
                )
146
            };
147
            [choice("forum", "Read the forum"), choice("close-tab", "Close the tab")].join(", ")
148
        }))
149
        .chain(run.techniques_now(now).into_iter().map(|technique| {
150
            let name = technique.name();
151
            let lubricant = technique
152
                .lubricant()
153
                .map_or(String::new(), |rule| format!("; lubricant=\"{rule}\""));
154
            format!(
155
                "<{}?technique={name}>; rel=\"{REL_BASE}technique\"; title=\"{name}\"; allow=\"JELQ\"{lubricant}",
156
                run.path("")
157
            )
158
        }))
159
        .chain(run.here.shopping.then(|| {
160
            SHOP.iter()
161
                .filter_map(|item| {
162
                    let price = item.price()?;
163
                    Some(format!(
164
                        "<{}>; rel=\"{REL_BASE}shop-item\"; title=\"{} for {price}\"; allow=\"POST\"",
165
                        run.path(&format!("/shop/{}", item.slug())),
166
                        item.slug()
167
                    ))
168
                })
169
                .collect::<Vec<_>>()
170
                .join(", ")
171
        }))
172
        .collect::<Vec<_>>()
173
        .join(", ")
174
}
175
176
/// Shame lives in a signed cookie so it follows the player across runs. Each response adds this
177
/// run's new shame to whatever total the client presents, so two runs at once both count.
178
fn settle_shame(run: &mut Run, ctx: &Ctx, secret: &Secret) -> u32 {
179
    let presented = ctx
180
        .cookie("shame")
181
        .and_then(|signed| secret.verify("shame", signed))
182
        .and_then(|total| total.parse::<u32>().ok());
183
    let total = presented.unwrap_or(run.shame_total) + (run.shame - run.shame_reported);
184
    run.shame_reported = run.shame;
185
    run.shame_total = total;
186
    total
187
}
188
189
pub fn finish(run: &mut Run, ctx: &Ctx, secret: &Secret, mut response: Response) -> Response {
190
    let status = response.status();
191
    if status == StatusCode::NOT_MODIFIED {
192
        return response;
193
    }
194
    let shame = settle_shame(run, ctx, secret);
195
    let mut stats: Vec<(HeaderName, String)> = vec![
196
        if run.items.ruler {
197
            (X_LENGTH, format!("{:.2}", run.current_length()))
198
        } else {
199
            (
200
                X_SELF_REPORTED_LENGTH,
201
                format!("{:.2}", run.self_reported()),
202
            )
203
        },
204
        (X_GIRTH, format!("{:.2}", run.girth)),
205
        (
206
            X_FORESKIN,
207
            run.foreskin.map_or("gone".into(), |f| format!("{f:.2}")),
208
        ),
209
        (X_EQ, run.eq.to_string()),
210
        (X_STAMINA, format!("{}/{}", run.stamina, run.max_stamina())),
211
        (X_SUSPICION, format!("{}/{SUSPICION_LIMIT}", run.suspicion)),
212
        (X_SHAME, shame.to_string()),
213
        (X_CASH, run.cash.to_string()),
214
        (X_FLOOR, run.floor.number().to_string()),
215
        (header::LINK, links(run, ctx.now)),
216
    ];
217
    if let Some(observer) = run.observed {
218
        stats.push((X_OBSERVED_BY, observer.name().into()));
219
    }
220
    if run.mum.is_some() {
221
        stats.push((header::VIA, "1.1 mum".into()));
222
        stats.push((header::VARY, "Mum".into()));
223
    }
224
    // RFC 9111 obsoleted Warning, which is why nobody heeds it.
225
    if run.conditions.pulled_muscle {
226
        stats.push((header::WARNING, "199 - \"a muscle has been pulled\"".into()));
227
    }
228
    for notice in run.notices.drain(..) {
229
        stats.push((header::WARNING, format!("199 - \"{notice}\"")));
230
    }
231
    if run.eq > 70 && run.ended.is_none() {
232
        stats.push((header::WARNING, "199 - \"it is turning purple\"".into()));
233
    }
234
    stats.push((
235
        header::SET_COOKIE,
236
        format!(
237
            "shame={}; {COOKIE_ATTRIBUTES}",
238
            secret.sign("shame", &shame.to_string())
239
        ),
240
    ));
241
    if run.items.ruler {
242
        stats.push((
243
            header::SET_COOKIE,
244
            format!("ruler={}; {COOKIE_ATTRIBUTES}", secret.sign("ruler", "1")),
245
        ));
246
    }
247
    let headers = response.headers_mut();
248
    for (name, value) in stats {
249
        if let Ok(value) = HeaderValue::from_str(&value) {
250
            headers.append(name, value);
251
        }
252
    }
253
    let bodiless = status.is_informational() || status == StatusCode::NO_CONTENT;
254
    if run.items.ring && !bodiless {
255
        response.extensions_mut().insert(Compress);
256
    }
257
    response
258
}
259
260
fn greet(response: Response, ctx: &mut Ctx) -> Response {
261
    if response.status() != StatusCode::SWITCHING_PROTOCOLS {
262
        return response;
263
    }
264
    let (Some(Greeting(text)), Some(upgrade)) = (
265
        response.extensions().get::<Greeting>().cloned(),
266
        ctx.upgrade.take(),
267
    ) else {
268
        return response;
269
    };
270
    tokio::spawn(async move {
271
        if let Ok(upgraded) = upgrade.await {
272
            let mut io = TokioIo::new(upgraded);
273
            let _ = io.write_all(text.as_bytes()).await;
274
            let _ = io.shutdown().await;
275
        }
276
    });
277
    response
278
}