an http based roguelike rpg about jelqing
Blame: src/http/inventory.rs
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 1 | use axum::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 2 | Json, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 3 | http::StatusCode, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 4 | response::{IntoResponse, Response}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 5 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 6 | use serde_json::json; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 7 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 8 | use super::{ctx::Ctx, response::respond}; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 9 | use crate::{ |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 10 | game::{Item, Run}, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 11 | problem::Problem, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 12 | }; |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 13 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 14 | pub(super) fn get_inventory(run: &mut Run, _: &Ctx) -> Response { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 15 | Json(run.inventory_view()).into_response() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 16 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 17 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 18 | pub(super) fn owned_item(run: &Run, ctx: &Ctx) -> Result<Item, Problem> { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 19 | Item::parse(ctx.param("item")) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 20 | .filter(|&item| run.holds(item)) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 21 | .ok_or_else(|| { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 22 | Problem::new( |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 23 | StatusCode::NOT_FOUND, |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 24 | "no-such-item", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 25 | "You do not have that.", |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 26 | ) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 27 | }) |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 28 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 29 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 30 | pub(super) fn get_item(run: &mut Run, ctx: &Ctx) -> Response { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 31 | match owned_item(run, ctx) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 32 | Ok(item) => { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 33 | Json(json!({ "name": item.slug(), "description": item.description() })).into_response() |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 34 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 35 | Err(problem) => problem.into_response(), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 36 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 37 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 38 | |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 39 | pub(super) fn delete_item(run: &mut Run, ctx: &Ctx) -> Response { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 40 | match owned_item(run, ctx) { |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 41 | Ok(item) => respond(run.drop_item(item)), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 42 | Err(problem) => problem.into_response(), |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 43 | } |
8b8e83329a1d | atagen | 2026-10-12 12:01 UTC | 44 | } |