essences/
bundles.rs

1use super::{abilities, currency, items};
2
3use tsify_next::Tsify;
4
5use crate::prelude::*;
6use strum_macros::{Display, EnumIter, EnumString};
7
8#[declare]
9pub type BundleId = uuid::Uuid;
10
11#[derive(
12    Debug,
13    Clone,
14    Copy,
15    EnumString,
16    Display,
17    Deserialize,
18    Serialize,
19    Hash,
20    Eq,
21    PartialEq,
22    EnumIter,
23    Default,
24    JsonSchema,
25    Tsify,
26)]
27#[tsify(namespace)]
28pub enum BundleStepType {
29    #[default]
30    Currency,
31    Ability,
32    Item,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Tsify, JsonSchema)]
36pub struct BundleRawStep {
37    #[schemars(title = "Тип элемента в бандле'")]
38    pub item_type: BundleStepType,
39
40    #[schemars(title = "Скрипт для элемента", schema_with = "script_schema")]
41    pub script: String,
42
43    #[schemars(title = "Нужно ли показывать попап")]
44    pub has_pop_up: bool,
45}
46
47#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq, Default, JsonSchema, Tsify)]
48#[tsify(namespace)]
49pub enum BundleClaimMode {
50    #[default]
51    Sequential,
52    AllAtOnce,
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Tsify, JsonSchema)]
56pub struct BundleRaw {
57    #[schemars(schema_with = "id_schema")]
58    pub id: BundleId,
59
60    #[schemars(title = "Содержимое бандла")]
61    pub steps: Vec<BundleRawStep>,
62
63    #[schemars(title = "Режим получения наград", default = "default_claim_mode")]
64    #[serde(default)]
65    pub claim_mode: BundleClaimMode,
66}
67
68fn default_claim_mode() -> BundleClaimMode {
69    BundleClaimMode::Sequential
70}
71
72#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Tsify, Default)]
73pub struct BundleAbility {
74    pub template: abilities::AbilityTemplate,
75    pub shards_amount: i64,
76}
77
78#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Tsify)]
79#[tsify(into_wasm_abi)]
80pub enum BundleElement {
81    Currencies(Vec<currency::CurrencyUnit>),
82    Abilities(Vec<BundleAbility>),
83    Items(Vec<items::Item>),
84}
85
86impl Default for BundleElement {
87    fn default() -> Self {
88        BundleElement::Currencies(Vec::new())
89    }
90}
91
92#[derive(
93    Clone, Debug, Serialize, Deserialize, PartialEq, Eq, CustomType, JsonSchema, Tsify, Default,
94)]
95pub struct BundleStep {
96    pub bundle_id: BundleId,
97    pub element: BundleElement,
98    pub has_pop_up: bool,
99}
100
101#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Tsify)]
102#[tsify(into_wasm_abi)]
103pub enum BundleStepGeneric {
104    Sequential(BundleStep),
105    AllAtOnce {
106        bundle_id: BundleId,
107        elements: Vec<BundleElement>,
108        has_pop_up: bool,
109    },
110}
111
112impl Default for BundleStepGeneric {
113    fn default() -> Self {
114        BundleStepGeneric::Sequential(BundleStep::default())
115    }
116}
117
118impl BundleStepGeneric {
119    pub fn has_pop_up(&self) -> bool {
120        match self {
121            BundleStepGeneric::Sequential(step) => step.has_pop_up,
122            BundleStepGeneric::AllAtOnce { has_pop_up, .. } => *has_pop_up,
123        }
124    }
125
126    pub fn bundle_id(&self) -> BundleId {
127        match self {
128            BundleStepGeneric::Sequential(step) => step.bundle_id,
129            BundleStepGeneric::AllAtOnce { bundle_id, .. } => *bundle_id,
130        }
131    }
132}