atagen

jelq

public

an http based roguelike rpg about jelqing

Star0
Watch0
6619c458c23edaf053ae371f8aa0df1e866febb6
1
ssh://brewery@pls.lobotomise.me:33/atagen/jelq.git
src/game/dispatch.rsRust171 lines5.7 KB
1
use super::{
2
    Floor, Lube, Outcome, Run, Story, Technique,
3
    techniques::{Training, Utility},
4
};
5
use crate::problem::Problem;
6
use axum::http::{StatusCode, header};
7
use serde_json::json;
8
use std::{fmt::Write as _, ops::ControlFlow};
9
10
impl Run {
11
    pub fn jelq(
12
        &mut self,
13
        techniques: &[Technique],
14
        intensity: i32,
15
        lube: Option<Lube>,
16
    ) -> Result<Outcome, Problem> {
17
        if techniques != [Technique::Utility(Utility::Energy)] {
18
            self.check_exhausted()?;
19
        }
20
        if self.online.story == Story::Pending {
21
            return Ok(self.branch_offer());
22
        }
23
        if self.floor == Floor::Forum {
24
            return Ok(Outcome::json(
25
                StatusCode::SEE_OTHER,
26
                json!({ "detail": "Refer to the forum." }),
27
            )
28
            .header(header::LOCATION, self.path("/thread")));
29
        }
30
        if self.items.kit.cage {
31
            return Err(Problem::new(
32
                StatusCode::LOCKED,
33
                "caged",
34
                "The cage is locked. The key is not in the box.",
35
            ));
36
        }
37
        match *techniques {
38
            [Technique::Utility(utility)] => self.utility(utility, lube),
39
            [Technique::Training(training)] => self.single(training, intensity, lube),
40
            [Technique::Training(a), Technique::Training(b)] => self.combo(a, b, intensity, lube),
41
            [_, _] => Err(Problem::new(
42
                StatusCode::BAD_REQUEST,
43
                "combo-invalid",
44
                "Only training techniques can be combined.",
45
            )),
46
            _ => Err(Problem::new(
47
                StatusCode::BAD_REQUEST,
48
                "too-many-techniques",
49
                "Two techniques at most. The server has limits even if you do not.",
50
            )),
51
        }
52
    }
53
54
    pub(super) fn utility(
55
        &mut self,
56
        utility: Utility,
57
        lube: Option<Lube>,
58
    ) -> Result<Outcome, Problem> {
59
        match utility {
60
            Utility::Warmup => return self.warm_up(),
61
            Utility::Ice => return self.ice(),
62
            Utility::Supplement => return self.supplement(),
63
            Utility::Meditate => return self.meditate(),
64
            Utility::Energy => return self.energy(),
65
            Utility::Edge | Utility::Pump => {}
66
        }
67
        self.ready(Technique::Utility(utility), lube)?;
68
        self.check_storage()?;
69
        self.check_clock()?;
70
        self.floor_enemy()?;
71
        match utility {
72
            Utility::Edge => self.edge(),
73
            _ => self.pump(),
74
        }
75
    }
76
77
    pub(super) fn single(
78
        &mut self,
79
        training: Training,
80
        intensity: i32,
81
        lube: Option<Lube>,
82
    ) -> Result<Outcome, Problem> {
83
        self.ready(Technique::Training(training), lube)?;
84
        self.check_storage()?;
85
        self.lube_available(lube)?;
86
        self.check_clock()?;
87
        self.floor_enemy()?;
88
        self.take_lube(lube)?;
89
        let cold = self.cold();
90
        self.chafe(lube, intensity, cold);
91
        self.commit(intensity, lube, 1)?;
92
        if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? {
93
            return Ok(interrupted);
94
        }
95
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
96
            return Ok(interrupted);
97
        }
98
        let outcome = self.roll(training, intensity, lube)?;
99
        Ok(self.gossip(outcome))
100
    }
101
102
    pub(super) fn combo(
103
        &mut self,
104
        a: Training,
105
        b: Training,
106
        intensity: i32,
107
        lube: Option<Lube>,
108
    ) -> Result<Outcome, Problem> {
109
        if a == b {
110
            return Err(Problem::new(
111
                StatusCode::BAD_REQUEST,
112
                "combo-invalid",
113
                "A technique cannot be combined with itself. The server has checked.",
114
            ));
115
        }
116
        self.ready(Technique::Training(a), lube)?;
117
        self.ready(Technique::Training(b), lube)?;
118
        self.check_storage()?;
119
        self.lube_available(lube)?;
120
        self.check_clock()?;
121
        self.floor_enemy()?;
122
        self.take_lube(lube)?;
123
        let cold = self.cold();
124
        self.chafe(lube, intensity, cold);
125
        self.chafe(lube, intensity, cold);
126
        self.commit(intensity, lube, 2)?;
127
        for training in [a, b] {
128
            if let ControlFlow::Break(interrupted) = self.mishaps(training, intensity, cold)? {
129
                return Ok(interrupted);
130
            }
131
        }
132
        if let ControlFlow::Break(interrupted) = self.hazards(intensity)? {
133
            return Ok(interrupted);
134
        }
135
136
        let mut statuses = Vec::new();
137
        for training in [a, b] {
138
            let result = self.roll(training, intensity, lube);
139
            if self.ended.is_some() {
140
                return result;
141
            }
142
            let status = match result {
143
                Ok(outcome) => self.gossip(outcome).status(),
144
                Err(problem) => problem.status(),
145
            };
146
            statuses.push((training, status));
147
        }
148
149
        let mut xml = String::from(
150
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<D:multistatus xmlns:D=\"DAV:\">\n",
151
        );
152
        for (training, status) in statuses {
153
            let _ = write!(
154
                xml,
155
                "  <D:response>\n    <D:href>{}?technique={}</D:href>\n    <D:status>HTTP/1.1 {} {}</D:status>\n  </D:response>\n",
156
                self.path(""),
157
                training.name(),
158
                status.as_u16(),
159
                crate::problem::reason(status),
160
            );
161
        }
162
        xml.push_str(
163
            "  <D:responsedescription>Each technique has been reported separately.</D:responsedescription>\n</D:multistatus>\n",
164
        );
165
        Ok(Outcome::raw(
166
            StatusCode::MULTI_STATUS,
167
            "application/xml; charset=utf-8",
168
            xml,
169
        ))
170
    }
171
}