Skip to main content

artifacts/apis/
simulation_api.rs

1use super::{configuration, Error};
2use crate::{apis::ResponseContent, models};
3use reqwest::StatusCode;
4use serde::{de, Deserialize, Deserializer, Serialize};
5
6/// struct for passing parameters to the method [`fight_simulation`]
7#[derive(Clone, Debug)]
8pub struct FightSimulationParams {
9    pub combat_simulation_request_schema: models::CombatSimulationRequestSchema,
10}
11
12impl FightSimulationParams {
13    pub fn new(combat_simulation_request_schema: models::CombatSimulationRequestSchema) -> Self {
14        Self {
15            combat_simulation_request_schema,
16        }
17    }
18}
19
20/// struct for typed errors of method [`fight_simulation`]
21#[derive(Debug, Clone, Serialize)]
22#[serde(untagged)]
23pub enum FightSimulationError {
24    /// Monster or item not found.
25    Status404(models::ErrorResponseSchema),
26    /// Only boss monsters can be fought by multiple characters.
27    Status486(models::ErrorResponseSchema),
28    /// Access denied, you must be a member to do that.
29    Status451(models::ErrorResponseSchema),
30    /// Request could not be processed due to an invalid payload.
31    Status422(models::ErrorResponseSchema),
32}
33
34impl<'de> Deserialize<'de> for FightSimulationError {
35    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
36    where
37        D: Deserializer<'de>,
38    {
39        let raw = models::ErrorResponseSchema::deserialize(deserializer)?;
40        match raw.error.code {
41            404 => Ok(Self::Status404(raw)),
42            486 => Ok(Self::Status486(raw)),
43            451 => Ok(Self::Status451(raw)),
44            422 => Ok(Self::Status422(raw)),
45            _ => Err(de::Error::custom(format!(
46                "Unexpected error code: {}",
47                raw.error.code
48            ))),
49        }
50    }
51}
52
53/// Simulate combat with fake characters against a monster multiple times. Member or founder account required.
54pub async fn fight_simulation(
55    configuration: &configuration::Configuration,
56    params: FightSimulationParams,
57) -> Result<models::CombatSimulationResponseSchema, Error<FightSimulationError>> {
58    let local_var_configuration = configuration;
59
60    // unbox the parameters
61    let combat_simulation_request_schema = params.combat_simulation_request_schema;
62
63    let local_var_client = &local_var_configuration.client;
64
65    let local_var_uri_str = format!(
66        "{}/simulation/fight_simulation",
67        local_var_configuration.base_path
68    );
69    let mut local_var_req_builder =
70        local_var_client.request(reqwest::Method::POST, local_var_uri_str.as_str());
71
72    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
73        local_var_req_builder =
74            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
75    }
76    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
77        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
78    };
79    local_var_req_builder = local_var_req_builder.json(&combat_simulation_request_schema);
80
81    let local_var_req = local_var_req_builder.build()?;
82    let local_var_resp = local_var_client.execute(local_var_req).await?;
83
84    let local_var_status = local_var_resp.status();
85    let local_var_content = local_var_resp.text().await?;
86
87    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
88        serde_json::from_str(&local_var_content).map_err(Error::from)
89    } else {
90        let local_var_entity: Option<FightSimulationError> =
91            serde_json::from_str(&local_var_content).ok();
92        let local_var_error = ResponseContent {
93            status: local_var_status,
94            content: local_var_content,
95            entity: local_var_entity,
96        };
97        Err(Error::ResponseError(local_var_error))
98    }
99}