atagen

jelq

public

an http based roguelike rpg about jelqing

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

Blame: src/http/run.rs

782069860236atagen2026-10-10 12:44 UTC1
//! Plumbing shared by every resource under `/runs/{id}`.
782069860236atagen2026-10-10 12:44 UTC2
782069860236atagen2026-10-10 12:44 UTC3
use axum::{
782069860236atagen2026-10-10 12:44 UTC4
    http::{HeaderName, HeaderValue, StatusCode, header},
782069860236atagen2026-10-10 12:44 UTC5
    response::{IntoResponse, Response},
782069860236atagen2026-10-10 12:44 UTC6
};
782069860236atagen2026-10-10 12:44 UTC7
use hyper_util::rt::TokioIo;
782069860236atagen2026-10-10 12:44 UTC8
use tokio::io::AsyncWriteExt;
782069860236atagen2026-10-10 12:44 UTC9
159f0998263batagen2026-10-12 11:01 UTC10
use super::{
159f0998263batagen2026-10-12 11:01 UTC11
    AppState, ctx::Ctx, meta, middleware::Compress, registry::hold, resource::Resource, runs,
159f0998263batagen2026-10-12 11:01 UTC12
};
782069860236atagen2026-10-10 12:44 UTC13
use crate::{
0fdc47364c8catagen2026-10-11 04:45 UTC14
    game::{
0fdc47364c8catagen2026-10-11 04:45 UTC15
        Elder, FORUM_THREAD, Floor, Run, SHOP, Story,
e807266c323batagen2026-10-12 09:46 UTC16
        balance::{FRICTION_MAX, KITCHEN_TELLINGS, SUSPICION_LIMIT},
0fdc47364c8catagen2026-10-11 04:45 UTC17
    },
782069860236atagen2026-10-10 12:44 UTC18
    headers::{
5dfebcc0ac43atagen2026-10-11 05:28 UTC19
        X_CASH, X_EQ, X_FLOOR, X_FORESKIN, X_FRICTION, X_GIRTH, X_LENGTH, X_OBSERVED_BY,
f39b10e20a0batagen2026-10-10 14:33 UTC20
        X_SELF_REPORTED_LENGTH, X_SHAME, X_STAMINA, X_SUSPICION,
782069860236atagen2026-10-10 12:44 UTC21
    },
ef7d6dee5976atagen2026-10-10 13:23 UTC22
    locale::Locale,
782069860236atagen2026-10-10 12:44 UTC23
    problem::{PROBLEM_BASE, Problem},
159f0998263batagen2026-10-12 11:01 UTC24
    records::Record,
782069860236atagen2026-10-10 12:44 UTC25
    secret::Secret,
782069860236atagen2026-10-10 12:44 UTC26
};
782069860236atagen2026-10-10 12:44 UTC27
782069860236atagen2026-10-10 12:44 UTC28
pub const REL_BASE: &str = "https://jelq.dev/rels/";
782069860236atagen2026-10-10 12:44 UTC29
const COOKIE_ATTRIBUTES: &str = "Max-Age=31536000; Path=/; HttpOnly; SameSite=Strict";
782069860236atagen2026-10-10 12:44 UTC30
/// Which household hazards stand between a request and the run.
782069860236atagen2026-10-10 12:44 UTC31
#[derive(Clone, Copy, PartialEq, Eq)]
782069860236atagen2026-10-10 12:44 UTC32
pub enum Scope {
782069860236atagen2026-10-10 12:44 UTC33
    Home,
782069860236atagen2026-10-10 12:44 UTC34
    Pockets,
782069860236atagen2026-10-10 12:44 UTC35
}
782069860236atagen2026-10-10 12:44 UTC36
782069860236atagen2026-10-10 12:44 UTC37
pub struct RunResource {
782069860236atagen2026-10-10 12:44 UTC38
    pub scope: Scope,
782069860236atagen2026-10-10 12:44 UTC39
    pub resource: Resource<Run>,
782069860236atagen2026-10-10 12:44 UTC40
}
782069860236atagen2026-10-10 12:44 UTC41
782069860236atagen2026-10-10 12:44 UTC42
/// What the server writes over the connection after a `101`.
782069860236atagen2026-10-10 12:44 UTC43
#[derive(Clone)]
782069860236atagen2026-10-10 12:44 UTC44
pub struct Greeting(pub String);
782069860236atagen2026-10-10 12:44 UTC45
782069860236atagen2026-10-10 12:44 UTC46
pub fn serve(state: &AppState, mut ctx: Ctx, target: &RunResource) -> Response {
159f0998263batagen2026-10-12 11:01 UTC47
    let id = ctx.param("id").to_owned();
159f0998263batagen2026-10-12 11:01 UTC48
    let Some(slot) = state.runs.get(&id) else {
159f0998263batagen2026-10-12 11:01 UTC49
        return match state.records.find(&id) {
159f0998263batagen2026-10-12 11:01 UTC50
            Some(Record {
159f0998263batagen2026-10-12 11:01 UTC51
                ending: Some(ending),
159f0998263batagen2026-10-12 11:01 UTC52
                turn,
159f0998263batagen2026-10-12 11:01 UTC53
                ..
159f0998263batagen2026-10-12 11:01 UTC54
            }) => gone(&ending.cause, turn),
159f0998263batagen2026-10-12 11:01 UTC55
            _ => Problem::new(
159f0998263batagen2026-10-12 11:01 UTC56
                StatusCode::NOT_FOUND,
159f0998263batagen2026-10-12 11:01 UTC57
                "no-such-run",
159f0998263batagen2026-10-12 11:01 UTC58
                "There is no such run. There may never have been.",
159f0998263batagen2026-10-12 11:01 UTC59
            )
159f0998263batagen2026-10-12 11:01 UTC60
            .into_response(),
159f0998263batagen2026-10-12 11:01 UTC61
        };
782069860236atagen2026-10-10 12:44 UTC62
    };
159f0998263batagen2026-10-12 11:01 UTC63
    let mut guard = hold(&slot);
159f0998263batagen2026-10-12 11:01 UTC64
    let run = &mut *guard;
782069860236atagen2026-10-10 12:44 UTC65
    if let Some(ending) = &run.ended {
159f0998263batagen2026-10-12 11:01 UTC66
        return gone(ending.cause.slug(), ending.turn);
782069860236atagen2026-10-10 12:44 UTC67
    }
782069860236atagen2026-10-10 12:44 UTC68
    run.last_seen = ctx.now;
782069860236atagen2026-10-10 12:44 UTC69
    run.observed = None;
782069860236atagen2026-10-10 12:44 UTC70
    let response =
782069860236atagen2026-10-10 12:44 UTC71
        gate(run, &ctx, target.scope).unwrap_or_else(|| target.resource.dispatch(run, &ctx));
782069860236atagen2026-10-10 12:44 UTC72
    let response = finish(run, &ctx, &state.config.secret, response);
159f0998263batagen2026-10-12 11:01 UTC73
    if run.ended.is_some() || run.clinic.cured {
159f0998263batagen2026-10-12 11:01 UTC74
        state.records.save(&Record::of(run));
159f0998263batagen2026-10-12 11:01 UTC75
    }
159f0998263batagen2026-10-12 11:01 UTC76
    let ended = run.ended.is_some();
159f0998263batagen2026-10-12 11:01 UTC77
    drop(guard);
159f0998263batagen2026-10-12 11:01 UTC78
    if ended {
159f0998263batagen2026-10-12 11:01 UTC79
        state.runs.remove(&id);
159f0998263batagen2026-10-12 11:01 UTC80
    }
782069860236atagen2026-10-10 12:44 UTC81
    greet(response, &mut ctx)
782069860236atagen2026-10-10 12:44 UTC82
}
782069860236atagen2026-10-10 12:44 UTC83
159f0998263batagen2026-10-12 11:01 UTC84
fn gone(cause: &str, turn: u32) -> Response {
159f0998263batagen2026-10-12 11:01 UTC85
    Problem::new(StatusCode::GONE, "run-ended", "This run has ended.")
159f0998263batagen2026-10-12 11:01 UTC86
        .with("cause", format!("{PROBLEM_BASE}{cause}"))
159f0998263batagen2026-10-12 11:01 UTC87
        .with("turn", turn)
159f0998263batagen2026-10-12 11:01 UTC88
        .with("graveyard", "/graveyard")
159f0998263batagen2026-10-12 11:01 UTC89
        .into_response()
159f0998263batagen2026-10-12 11:01 UTC90
}
159f0998263batagen2026-10-12 11:01 UTC91
782069860236atagen2026-10-10 12:44 UTC92
fn gate(run: &mut Run, ctx: &Ctx, scope: Scope) -> Option<Response> {
782069860236atagen2026-10-10 12:44 UTC93
    if scope == Scope::Pockets {
782069860236atagen2026-10-10 12:44 UTC94
        return None;
782069860236atagen2026-10-10 12:44 UTC95
    }
782069860236atagen2026-10-10 12:44 UTC96
    if run.floor == Floor::Hotel && !run.here.wifi_accepted {
782069860236atagen2026-10-10 12:44 UTC97
        let portal = run.path("/portal");
782069860236atagen2026-10-10 12:44 UTC98
        return Some(
782069860236atagen2026-10-10 12:44 UTC99
            Problem::new(
782069860236atagen2026-10-10 12:44 UTC100
                StatusCode::NETWORK_AUTHENTICATION_REQUIRED,
782069860236atagen2026-10-10 12:44 UTC101
                "captive-portal",
782069860236atagen2026-10-10 12:44 UTC102
                "The network requires authentication. The hotel would like you to agree to some terms.",
782069860236atagen2026-10-10 12:44 UTC103
            )
782069860236atagen2026-10-10 12:44 UTC104
            .with("portal", portal.as_str())
782069860236atagen2026-10-10 12:44 UTC105
            .header(header::LINK, format!("<{portal}>; rel=\"{REL_BASE}portal\""))
782069860236atagen2026-10-10 12:44 UTC106
            .into_response(),
782069860236atagen2026-10-10 12:44 UTC107
        );
782069860236atagen2026-10-10 12:44 UTC108
    }
ef7d6dee5976atagen2026-10-10 13:23 UTC109
    let authorization = ctx
ef7d6dee5976atagen2026-10-10 13:23 UTC110
        .header(header::AUTHORIZATION)
ef7d6dee5976atagen2026-10-10 13:23 UTC111
        .map(|text| Locale::negotiate(ctx.header(header::ACCEPT_LANGUAGE)).canonicalise(text));
ef7d6dee5976atagen2026-10-10 13:23 UTC112
    run.answer_door(authorization.as_deref())
782069860236atagen2026-10-10 12:44 UTC113
        .err()
782069860236atagen2026-10-10 12:44 UTC114
        .map(IntoResponse::into_response)
782069860236atagen2026-10-10 12:44 UTC115
}
782069860236atagen2026-10-10 12:44 UTC116
58e1fb3943e6atagen2026-10-10 13:30 UTC117
/// Every resource the run can reach right now, then every technique a JELQ could use. The `allow`
58e1fb3943e6atagen2026-10-10 13:30 UTC118
/// and `lubricant` extension attributes (RFC 8288 §3) say what each accepts, so a client never guesses.
a6caa2f99786atagen2026-10-12 10:38 UTC119
fn links(run: &Run) -> String {
e807266c323batagen2026-10-12 09:46 UTC120
    let thread_reachable = run.thread_reachable();
b85a0d4536c3atagen2026-10-11 08:50 UTC121
    let linked: [(&str, &RunResource, bool); 15] = [
185851447cebatagen2026-10-10 12:58 UTC122
        ("", &runs::RUN, true),
185851447cebatagen2026-10-10 12:58 UTC123
        ("floor", &runs::FLOOR, true),
185851447cebatagen2026-10-10 12:58 UTC124
        ("length", &runs::LENGTH, true),
185851447cebatagen2026-10-10 12:58 UTC125
        ("foreskin", &runs::FORESKIN, run.foreskin.is_some()),
185851447cebatagen2026-10-10 12:58 UTC126
        ("inventory", &runs::INVENTORY, true),
185851447cebatagen2026-10-10 12:58 UTC127
        ("techniques", &runs::TECHNIQUES, true),
d604371299fbatagen2026-10-10 13:54 UTC128
        ("drawer", &runs::DRAWER, run.drawer_worth_searching()),
0a0bfee8329eatagen2026-10-11 05:11 UTC129
        ("kitchen", &runs::KITCHEN, {
0a0bfee8329eatagen2026-10-11 05:11 UTC130
            run.floor.has_kitchen() && run.kitchen_tellings < KITCHEN_TELLINGS
0a0bfee8329eatagen2026-10-11 05:11 UTC131
        }),
b85a0d4536c3atagen2026-10-11 08:50 UTC132
        ("re-read", &runs::REREAD, thread_reachable),
185851447cebatagen2026-10-10 12:58 UTC133
        ("grip", &runs::GRIP, true),
185851447cebatagen2026-10-10 12:58 UTC134
        ("shop", &runs::SHOP, run.floor.shop_open()),
d604371299fbatagen2026-10-10 13:54 UTC135
        (
d604371299fbatagen2026-10-10 13:54 UTC136
            "portal",
d604371299fbatagen2026-10-10 13:54 UTC137
            &runs::PORTAL,
d604371299fbatagen2026-10-10 13:54 UTC138
            run.floor == Floor::Hotel && !run.here.wifi_accepted,
d604371299fbatagen2026-10-10 13:54 UTC139
        ),
d604371299fbatagen2026-10-10 13:54 UTC140
        (
d604371299fbatagen2026-10-10 13:54 UTC141
            "elder",
d604371299fbatagen2026-10-10 13:54 UTC142
            &runs::ELDER,
d604371299fbatagen2026-10-10 13:54 UTC143
            run.floor == Floor::Forum && run.elder != Elder::Beaten,
d604371299fbatagen2026-10-10 13:54 UTC144
        ),
185851447cebatagen2026-10-10 12:58 UTC145
        ("footage", &runs::FOOTAGE, run.filmed_at.is_some()),
d604371299fbatagen2026-10-10 13:54 UTC146
        ("diagnosis", &runs::DIAGNOSIS, run.floor == Floor::Clinic),
185851447cebatagen2026-10-10 12:58 UTC147
    ];
185851447cebatagen2026-10-10 12:58 UTC148
    linked
185851447cebatagen2026-10-10 12:58 UTC149
        .into_iter()
185851447cebatagen2026-10-10 12:58 UTC150
        .filter(|(_, _, relevant)| *relevant)
185851447cebatagen2026-10-10 12:58 UTC151
        .map(|(rel, target, _)| {
185851447cebatagen2026-10-10 12:58 UTC152
            let (path, name) = if rel.is_empty() {
185851447cebatagen2026-10-10 12:58 UTC153
                (run.path(""), "run")
185851447cebatagen2026-10-10 12:58 UTC154
            } else {
185851447cebatagen2026-10-10 12:58 UTC155
                (run.path(&format!("/{rel}")), rel)
185851447cebatagen2026-10-10 12:58 UTC156
            };
185851447cebatagen2026-10-10 12:58 UTC157
            let allow = target.resource.allow(run);
185851447cebatagen2026-10-10 12:58 UTC158
            format!("<{path}>; rel=\"{REL_BASE}{name}\"; allow=\"{allow}\"")
185851447cebatagen2026-10-10 12:58 UTC159
        })
b85a0d4536c3atagen2026-10-11 08:50 UTC160
        .chain(thread_reachable.then(|| {
0fdc47364c8catagen2026-10-11 04:45 UTC161
            format!(
0fdc47364c8catagen2026-10-11 04:45 UTC162
                "<{FORUM_THREAD}>; rel=\"{REL_BASE}thread\"; allow=\"{}\"",
0fdc47364c8catagen2026-10-11 04:45 UTC163
                meta::THREAD.allow_any()
0fdc47364c8catagen2026-10-11 04:45 UTC164
            )
0fdc47364c8catagen2026-10-11 04:45 UTC165
        }),)
d46f9d0b953aatagen2026-10-10 14:41 UTC166
        .chain((run.story == Story::Pending).then(|| {
d46f9d0b953aatagen2026-10-10 14:41 UTC167
            let choice = |slug: &str, title: &str| {
d46f9d0b953aatagen2026-10-10 14:41 UTC168
                format!(
d46f9d0b953aatagen2026-10-10 14:41 UTC169
                    "<{}>; rel=\"{REL_BASE}branch\"; title=\"{title}\"; allow=\"POST\"",
d46f9d0b953aatagen2026-10-10 14:41 UTC170
                    run.path(&format!("/branches/{slug}"))
d46f9d0b953aatagen2026-10-10 14:41 UTC171
                )
d46f9d0b953aatagen2026-10-10 14:41 UTC172
            };
d46f9d0b953aatagen2026-10-10 14:41 UTC173
            [choice("forum", "Read the forum"), choice("close-tab", "Close the tab")].join(", ")
d46f9d0b953aatagen2026-10-10 14:41 UTC174
        }))
a6caa2f99786atagen2026-10-12 10:38 UTC175
        .chain(run.techniques_now().into_iter().map(|technique| {
58e1fb3943e6atagen2026-10-10 13:30 UTC176
            let name = technique.name();
58e1fb3943e6atagen2026-10-10 13:30 UTC177
            let lubricant = technique
58e1fb3943e6atagen2026-10-10 13:30 UTC178
                .lubricant()
58e1fb3943e6atagen2026-10-10 13:30 UTC179
                .map_or(String::new(), |rule| format!("; lubricant=\"{rule}\""));
58e1fb3943e6atagen2026-10-10 13:30 UTC180
            format!(
58e1fb3943e6atagen2026-10-10 13:30 UTC181
                "<{}?technique={name}>; rel=\"{REL_BASE}technique\"; title=\"{name}\"; allow=\"JELQ\"{lubricant}",
58e1fb3943e6atagen2026-10-10 13:30 UTC182
                run.path("")
58e1fb3943e6atagen2026-10-10 13:30 UTC183
            )
58e1fb3943e6atagen2026-10-10 13:30 UTC184
        }))
7d3fb63208bfatagen2026-10-11 04:38 UTC185
        .chain(run.here.shopping.then(|| {
7d3fb63208bfatagen2026-10-11 04:38 UTC186
            SHOP.iter()
7d3fb63208bfatagen2026-10-11 04:38 UTC187
                .filter_map(|item| {
7d3fb63208bfatagen2026-10-11 04:38 UTC188
                    let price = item.price()?;
7d3fb63208bfatagen2026-10-11 04:38 UTC189
                    Some(format!(
7d3fb63208bfatagen2026-10-11 04:38 UTC190
                        "<{}>; rel=\"{REL_BASE}shop-item\"; title=\"{} for {price}\"; allow=\"POST\"",
7d3fb63208bfatagen2026-10-11 04:38 UTC191
                        run.path(&format!("/shop/{}", item.slug())),
7d3fb63208bfatagen2026-10-11 04:38 UTC192
                        item.slug()
7d3fb63208bfatagen2026-10-11 04:38 UTC193
                    ))
7d3fb63208bfatagen2026-10-11 04:38 UTC194
                })
7d3fb63208bfatagen2026-10-11 04:38 UTC195
                .collect::<Vec<_>>()
7d3fb63208bfatagen2026-10-11 04:38 UTC196
                .join(", ")
7d3fb63208bfatagen2026-10-11 04:38 UTC197
        }))
185851447cebatagen2026-10-10 12:58 UTC198
        .collect::<Vec<_>>()
185851447cebatagen2026-10-10 12:58 UTC199
        .join(", ")
782069860236atagen2026-10-10 12:44 UTC200
}
782069860236atagen2026-10-10 12:44 UTC201
782069860236atagen2026-10-10 12:44 UTC202
/// Shame lives in a signed cookie so it follows the player across runs. Each response adds this
782069860236atagen2026-10-10 12:44 UTC203
/// run's new shame to whatever total the client presents, so two runs at once both count.
782069860236atagen2026-10-10 12:44 UTC204
fn settle_shame(run: &mut Run, ctx: &Ctx, secret: &Secret) -> u32 {
782069860236atagen2026-10-10 12:44 UTC205
    let presented = ctx
782069860236atagen2026-10-10 12:44 UTC206
        .cookie("shame")
782069860236atagen2026-10-10 12:44 UTC207
        .and_then(|signed| secret.verify("shame", signed))
782069860236atagen2026-10-10 12:44 UTC208
        .and_then(|total| total.parse::<u32>().ok());
782069860236atagen2026-10-10 12:44 UTC209
    let total = presented.unwrap_or(run.shame_total) + (run.shame - run.shame_reported);
782069860236atagen2026-10-10 12:44 UTC210
    run.shame_reported = run.shame;
782069860236atagen2026-10-10 12:44 UTC211
    run.shame_total = total;
782069860236atagen2026-10-10 12:44 UTC212
    total
782069860236atagen2026-10-10 12:44 UTC213
}
782069860236atagen2026-10-10 12:44 UTC214
782069860236atagen2026-10-10 12:44 UTC215
pub fn finish(run: &mut Run, ctx: &Ctx, secret: &Secret, mut response: Response) -> Response {
782069860236atagen2026-10-10 12:44 UTC216
    let status = response.status();
782069860236atagen2026-10-10 12:44 UTC217
    if status == StatusCode::NOT_MODIFIED {
782069860236atagen2026-10-10 12:44 UTC218
        return response;
782069860236atagen2026-10-10 12:44 UTC219
    }
782069860236atagen2026-10-10 12:44 UTC220
    let shame = settle_shame(run, ctx, secret);
782069860236atagen2026-10-10 12:44 UTC221
    let mut stats: Vec<(HeaderName, String)> = vec![
f39b10e20a0batagen2026-10-10 14:33 UTC222
        if run.items.ruler {
d46f9d0b953aatagen2026-10-10 14:41 UTC223
            (X_LENGTH, format!("{:.2}", run.current_length()))
f39b10e20a0batagen2026-10-10 14:33 UTC224
        } else {
f39b10e20a0batagen2026-10-10 14:33 UTC225
            (
f39b10e20a0batagen2026-10-10 14:33 UTC226
                X_SELF_REPORTED_LENGTH,
f39b10e20a0batagen2026-10-10 14:33 UTC227
                format!("{:.2}", run.self_reported()),
f39b10e20a0batagen2026-10-10 14:33 UTC228
            )
f39b10e20a0batagen2026-10-10 14:33 UTC229
        },
782069860236atagen2026-10-10 12:44 UTC230
        (X_GIRTH, format!("{:.2}", run.girth)),
782069860236atagen2026-10-10 12:44 UTC231
        (
782069860236atagen2026-10-10 12:44 UTC232
            X_FORESKIN,
782069860236atagen2026-10-10 12:44 UTC233
            run.foreskin.map_or("gone".into(), |f| format!("{f:.2}")),
782069860236atagen2026-10-10 12:44 UTC234
        ),
5dfebcc0ac43atagen2026-10-11 05:28 UTC235
        (X_FRICTION, format!("{}/{}", run.friction, FRICTION_MAX)),
782069860236atagen2026-10-10 12:44 UTC236
        (X_EQ, run.eq.to_string()),
782069860236atagen2026-10-10 12:44 UTC237
        (X_STAMINA, format!("{}/{}", run.stamina, run.max_stamina())),
782069860236atagen2026-10-10 12:44 UTC238
        (X_SUSPICION, format!("{}/{SUSPICION_LIMIT}", run.suspicion)),
782069860236atagen2026-10-10 12:44 UTC239
        (X_SHAME, shame.to_string()),
782069860236atagen2026-10-10 12:44 UTC240
        (X_CASH, run.cash.to_string()),
782069860236atagen2026-10-10 12:44 UTC241
        (X_FLOOR, run.floor.number().to_string()),
a6caa2f99786atagen2026-10-12 10:38 UTC242
        (header::LINK, links(run)),
782069860236atagen2026-10-10 12:44 UTC243
    ];
782069860236atagen2026-10-10 12:44 UTC244
    if let Some(observer) = run.observed {
782069860236atagen2026-10-10 12:44 UTC245
        stats.push((X_OBSERVED_BY, observer.name().into()));
782069860236atagen2026-10-10 12:44 UTC246
    }
9683736a2c5eatagen2026-10-10 13:15 UTC247
    if run.mum.is_some() {
9683736a2c5eatagen2026-10-10 13:15 UTC248
        stats.push((header::VIA, "1.1 mum".into()));
9683736a2c5eatagen2026-10-10 13:15 UTC249
        stats.push((header::VARY, "Mum".into()));
782069860236atagen2026-10-10 12:44 UTC250
    }
782069860236atagen2026-10-10 12:44 UTC251
    // RFC 9111 obsoleted Warning, which is why nobody heeds it.
782069860236atagen2026-10-10 12:44 UTC252
    if run.conditions.pulled_muscle {
782069860236atagen2026-10-10 12:44 UTC253
        stats.push((header::WARNING, "199 - \"a muscle has been pulled\"".into()));
782069860236atagen2026-10-10 12:44 UTC254
    }
5dfebcc0ac43atagen2026-10-11 05:28 UTC255
    if run.conditions.friction_burn {
5dfebcc0ac43atagen2026-10-11 05:28 UTC256
        stats.push((header::WARNING, "199 - \"friction burn\"".into()));
5dfebcc0ac43atagen2026-10-11 05:28 UTC257
    }
d46f9d0b953aatagen2026-10-10 14:41 UTC258
    for notice in run.notices.drain(..) {
d46f9d0b953aatagen2026-10-10 14:41 UTC259
        stats.push((header::WARNING, format!("199 - \"{notice}\"")));
d46f9d0b953aatagen2026-10-10 14:41 UTC260
    }
782069860236atagen2026-10-10 12:44 UTC261
    if run.eq > 70 && run.ended.is_none() {
782069860236atagen2026-10-10 12:44 UTC262
        stats.push((header::WARNING, "199 - \"it is turning purple\"".into()));
782069860236atagen2026-10-10 12:44 UTC263
    }
782069860236atagen2026-10-10 12:44 UTC264
    stats.push((
782069860236atagen2026-10-10 12:44 UTC265
        header::SET_COOKIE,
782069860236atagen2026-10-10 12:44 UTC266
        format!(
782069860236atagen2026-10-10 12:44 UTC267
            "shame={}; {COOKIE_ATTRIBUTES}",
782069860236atagen2026-10-10 12:44 UTC268
            secret.sign("shame", &shame.to_string())
782069860236atagen2026-10-10 12:44 UTC269
        ),
782069860236atagen2026-10-10 12:44 UTC270
    ));
782069860236atagen2026-10-10 12:44 UTC271
    if run.items.ruler {
782069860236atagen2026-10-10 12:44 UTC272
        stats.push((
782069860236atagen2026-10-10 12:44 UTC273
            header::SET_COOKIE,
782069860236atagen2026-10-10 12:44 UTC274
            format!("ruler={}; {COOKIE_ATTRIBUTES}", secret.sign("ruler", "1")),
782069860236atagen2026-10-10 12:44 UTC275
        ));
782069860236atagen2026-10-10 12:44 UTC276
    }
782069860236atagen2026-10-10 12:44 UTC277
    let headers = response.headers_mut();
782069860236atagen2026-10-10 12:44 UTC278
    for (name, value) in stats {
782069860236atagen2026-10-10 12:44 UTC279
        if let Ok(value) = HeaderValue::from_str(&value) {
782069860236atagen2026-10-10 12:44 UTC280
            headers.append(name, value);
782069860236atagen2026-10-10 12:44 UTC281
        }
782069860236atagen2026-10-10 12:44 UTC282
    }
782069860236atagen2026-10-10 12:44 UTC283
    let bodiless = status.is_informational() || status == StatusCode::NO_CONTENT;
782069860236atagen2026-10-10 12:44 UTC284
    if run.items.ring && !bodiless {
782069860236atagen2026-10-10 12:44 UTC285
        response.extensions_mut().insert(Compress);
782069860236atagen2026-10-10 12:44 UTC286
    }
782069860236atagen2026-10-10 12:44 UTC287
    response
782069860236atagen2026-10-10 12:44 UTC288
}
782069860236atagen2026-10-10 12:44 UTC289
782069860236atagen2026-10-10 12:44 UTC290
fn greet(response: Response, ctx: &mut Ctx) -> Response {
782069860236atagen2026-10-10 12:44 UTC291
    if response.status() != StatusCode::SWITCHING_PROTOCOLS {
782069860236atagen2026-10-10 12:44 UTC292
        return response;
782069860236atagen2026-10-10 12:44 UTC293
    }
782069860236atagen2026-10-10 12:44 UTC294
    let (Some(Greeting(text)), Some(upgrade)) = (
782069860236atagen2026-10-10 12:44 UTC295
        response.extensions().get::<Greeting>().cloned(),
782069860236atagen2026-10-10 12:44 UTC296
        ctx.upgrade.take(),
782069860236atagen2026-10-10 12:44 UTC297
    ) else {
782069860236atagen2026-10-10 12:44 UTC298
        return response;
782069860236atagen2026-10-10 12:44 UTC299
    };
782069860236atagen2026-10-10 12:44 UTC300
    tokio::spawn(async move {
782069860236atagen2026-10-10 12:44 UTC301
        if let Ok(upgraded) = upgrade.await {
782069860236atagen2026-10-10 12:44 UTC302
            let mut io = TokioIo::new(upgraded);
782069860236atagen2026-10-10 12:44 UTC303
            let _ = io.write_all(text.as_bytes()).await;
782069860236atagen2026-10-10 12:44 UTC304
            let _ = io.shutdown().await;
782069860236atagen2026-10-10 12:44 UTC305
        }
782069860236atagen2026-10-10 12:44 UTC306
    });
782069860236atagen2026-10-10 12:44 UTC307
    response
782069860236atagen2026-10-10 12:44 UTC308
}