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#[derive(Clone, Debug, Deref, DerefMut, From, Into, Deserialize, Serialize)]
17#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
18#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
19#[cfg_attr(feature = "typescript", ts(export))]
20pub struct CheatGetCitiesResponse(pub Vec<CheatGetCityResponse>);
21
22#[derive(Clone, Debug, Deserialize, Serialize)]
23#[serde(rename_all = "camelCase")]
24#[cfg_attr(feature = "axum", derive(IntoJsonResponse))]
25#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
26#[cfg_attr(feature = "typescript", ts(export))]
27pub struct CheatGetCityResponse {
28  pub city: City,
29  pub score: Option<Score>,
30}
31
32#[bon::bon]
33impl CheatGetCityResponse {
34  #[builder]
35  pub fn new(
36    #[builder(start_fn)] world: &World,
37    #[builder(start_fn)] coord: Coord,
38    #[builder(default)] score: bool,
39  ) -> Result<Self, CoreError> {
40    let city = cheat::get_city(world, coord)?;
41    let score = score
42      .then(|| city.score(&world.stats().infrastructure()))
43      .transpose()?;
44
45    Ok(Self { city: city.clone(), score })
46  }
47}
48
49impl From<GetCityResponse> for CheatGetCityResponse {
50  fn from(value: GetCityResponse) -> Self {
51    Self { city: value.city, score: value.score }
52  }
53}