Skip to main content

nil_payload/response/cheat/
city.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::response::city::GetCityResponse;
5use derive_more::{Deref, DerefMut, From, Into};
6use nil_core::city::City;
7use nil_core::continent::coord::Coord;
8use nil_core::error::Error as CoreError;
9use nil_core::ranking::score::Score;
10use nil_core::world::{World, cheat};
11use serde::{Deserialize, Serialize};
12
13#[cfg(feature = "axum")]
14use nil_payload_macros::IntoJsonResponse;
15
16#[cfg(feature = "typescript")]
17use ts_rs::TS;
18
19#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
20#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
21#[cfg_attr(feature = "typescript", derive(TS))]
22#[cfg_attr(feature = "typescript", ts(export))]
23pub struct CheatGetCitiesResponse(pub Vec<CheatGetCityResponse>);
24
25#[derive(Clone, Debug, Deserialize, Serialize)]
26#[serde(rename_all = "camelCase")]
27#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
28#[cfg_attr(feature = "typescript", derive(TS))]
29#[cfg_attr(feature = "typescript", ts(export))]
30pub struct CheatGetCityResponse {
31  pub city: City,
32  pub score: Option<Score>,
33}
34
35#[bon::bon]
36impl CheatGetCityResponse {
37  #[builder]
38  pub fn new(
39    #[builder(start_fn)] world: &World,
40    #[builder(start_fn)] coord: Coord,
41    #[builder(default)] score: bool,
42  ) -> Result<Self, CoreError> {
43    let city = cheat::get_city(world, coord)?;
44    let score = score
45      .then(|| city.score(&world.stats().infrastructure()))
46      .transpose()?;
47
48    Ok(Self { city: city.clone(), score })
49  }
50}
51
52impl From<GetCityResponse> for CheatGetCityResponse {
53  fn from(value: GetCityResponse) -> Self {
54    Self { city: value.city, score: value.score }
55  }
56}