essences/
vassals.rs

1use chrono::Utc;
2use strum_macros::{Display, EnumString};
3
4use crate::prelude::*;
5
6use crate::currency::CurrencyUnit;
7
8#[derive(
9    Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, CustomType, JsonSchema, Tsify,
10)]
11pub struct Suzerain {
12    pub character_id: Uuid,
13    pub username: String,
14    pub photo_url: Option<String>,
15    pub loyalty: i64,
16    pub last_claimed_reward_at: chrono::DateTime<chrono::Utc>,
17    pub last_claimed_power: i64,
18    pub current_power: i64,
19    pub current_reward: Vec<CurrencyUnit>,
20    pub created_at: chrono::DateTime<chrono::Utc>,
21}
22
23impl Suzerain {
24    pub fn claim_reward(&mut self, claim_time: chrono::DateTime<chrono::Utc>) {
25        self.current_reward = vec![];
26        self.last_claimed_reward_at = claim_time;
27        self.last_claimed_power = self.current_power;
28    }
29
30    pub fn time_as(&self) -> chrono::TimeDelta {
31        ::time::utc_now() - self.created_at
32    }
33}
34
35#[derive(
36    Clone, Debug, Default, Serialize, Deserialize, PartialEq, Eq, CustomType, JsonSchema, Tsify,
37)]
38pub struct Vassal {
39    pub character_id: Uuid,
40    pub username: String,
41    pub photo_url: Option<String>,
42    pub loyalty: i64,
43    pub last_claimed_reward_at: chrono::DateTime<chrono::Utc>,
44    pub last_claimed_power: i64,
45    pub current_power: i64,
46    pub current_reward: Vec<CurrencyUnit>,
47    pub created_at: chrono::DateTime<chrono::Utc>,
48}
49
50impl Vassal {
51    pub fn claim_reward(&mut self, claim_time: chrono::DateTime<chrono::Utc>) {
52        self.current_reward = vec![];
53        self.last_claimed_reward_at = claim_time;
54        self.last_claimed_power = self.current_power;
55    }
56}
57
58#[derive(Default, Serialize, Deserialize)]
59pub struct VassalBuilder {
60    character_id: Uuid,
61    username: String,
62    photo_url: Option<String>,
63    loyalty: i64,
64    last_claimed_reward_at: chrono::DateTime<Utc>,
65    last_claimed_power: i64,
66    current_power: i64,
67    current_reward: Vec<CurrencyUnit>,
68    created_at: chrono::DateTime<Utc>,
69}
70
71impl VassalBuilder {
72    pub fn new() -> Self {
73        Self {
74            character_id: Uuid::now_v7(),
75            username: "".to_owned(),
76            photo_url: None,
77            loyalty: 0,
78            last_claimed_reward_at: ::time::utc_now(),
79            last_claimed_power: 0,
80            current_power: 0,
81            current_reward: Vec::new(),
82            created_at: ::time::utc_now(),
83        }
84    }
85
86    pub fn character_id(mut self, character_id: Uuid) -> Self {
87        self.character_id = character_id;
88        self
89    }
90
91    pub fn username(mut self, username: String) -> Self {
92        self.username = username;
93        self
94    }
95
96    pub fn photo_url(mut self, photo_url: Option<String>) -> Self {
97        self.photo_url = photo_url;
98        self
99    }
100
101    pub fn loyalty(mut self, loyalty: i64) -> Self {
102        self.loyalty = loyalty;
103        self
104    }
105
106    pub fn last_claimed_reward_at(mut self, last_claimed_reward_at: chrono::DateTime<Utc>) -> Self {
107        self.last_claimed_reward_at = last_claimed_reward_at;
108        self
109    }
110
111    pub fn last_claimed_power(mut self, last_claimed_power: i64) -> Self {
112        self.last_claimed_power = last_claimed_power;
113        self
114    }
115
116    pub fn current_power(mut self, current_power: i64) -> Self {
117        self.current_power = current_power;
118        self
119    }
120
121    pub fn current_reward(mut self, current_reward: Vec<CurrencyUnit>) -> Self {
122        self.current_reward = current_reward;
123        self
124    }
125
126    pub fn created_at(mut self, created_at: chrono::DateTime<Utc>) -> Self {
127        self.created_at = created_at;
128        self
129    }
130
131    pub fn build(self) -> Vassal {
132        Vassal {
133            character_id: self.character_id,
134            username: self.username,
135            photo_url: self.photo_url,
136            loyalty: self.loyalty,
137            last_claimed_reward_at: self.last_claimed_reward_at,
138            last_claimed_power: self.last_claimed_power,
139            current_power: self.current_power,
140            current_reward: self.current_reward,
141            created_at: self.created_at,
142        }
143    }
144}
145
146#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, Copy)]
147pub struct VassalLinkLoyalty {
148    pub suzerain_id: Uuid,
149    pub vassal_id: Uuid,
150    pub loyalty: i64,
151    pub loyalty_set_at: chrono::DateTime<Utc>,
152    pub cur_time: chrono::DateTime<Utc>,
153}
154
155/// Task in config
156#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema, Tsify)]
157pub struct VassalTaskTemplate {
158    #[schemars(schema_with = "id_schema")]
159    pub id: Uuid,
160    #[schemars(title = "Название задания")]
161    pub title: i18n::I18nString,
162    #[schemars(title = "Длительность выполнения задания")]
163    pub duration_sec: u64,
164    #[schemars(
165        title = "Скрипт награды за задание",
166        description = "Скрипт высчитывающий награду за задание",
167        schema_with = "script_schema"
168    )]
169    pub reward_script: String,
170    #[schemars(title = "Скрипт лояльности за задание", schema_with = "script_schema")]
171    pub loyalty_script: String,
172}
173
174#[derive(
175    Clone,
176    Debug,
177    Serialize,
178    Default,
179    Deserialize,
180    PartialEq,
181    Eq,
182    JsonSchema,
183    Tsify,
184    EnumString,
185    Display,
186)]
187pub enum VassalTaskStatus {
188    #[default]
189    NotStarted,
190    StartedGood,
191    StartedBad,
192    ChangedBadToGood,
193    FinishedGood,
194    FinishedBad,
195}
196
197impl CustomType for VassalTaskStatus {
198    fn build(mut builder: TypeBuilder<Self>) {
199        builder
200            .with_name("VassalTaskStatus")
201            .with_get("str", |vts: &mut VassalTaskStatus| {
202                VassalTaskStatus::to_string(vts)
203            });
204    }
205}
206
207/// Task in state
208#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Tsify)]
209pub struct VassalTask {
210    pub id: Uuid,
211    pub template_task_id: Uuid,
212    pub suzerain_id: Uuid,
213    pub suzerain_power: i64,
214    pub vassal_id: Uuid,
215    pub vassal_power: i64,
216    pub good_reward: Vec<CurrencyUnit>,
217    pub bad_reward: Vec<CurrencyUnit>,
218    pub good_loyalty: i64,
219    pub bad_loyalty: i64,
220    pub task_status: VassalTaskStatus,
221    pub claimed_by_suzerain: bool,
222    pub claimed_by_vassal: bool,
223    pub started_at: Option<chrono::DateTime<chrono::Utc>>,
224    pub finish_at: Option<chrono::DateTime<chrono::Utc>>,
225}
226
227#[derive(Default, Deserialize, Serialize)]
228pub struct VassalTaskBuilder {
229    pub id: Uuid,
230    pub template_task_id: Uuid,
231    pub suzerain_id: Uuid,
232    pub suzerain_power: i64,
233    pub vassal_id: Uuid,
234    pub vassal_power: i64,
235    pub good_reward: Vec<CurrencyUnit>,
236    pub bad_reward: Vec<CurrencyUnit>,
237    pub good_loyalty: i64,
238    pub bad_loyalty: i64,
239    pub task_status: VassalTaskStatus,
240    pub claimed_by_suzerain: bool,
241    pub claimed_by_vassal: bool,
242    pub started_at: Option<chrono::DateTime<chrono::Utc>>,
243    pub finish_at: Option<chrono::DateTime<chrono::Utc>>,
244}
245
246impl VassalTaskBuilder {
247    pub fn new() -> Self {
248        Self {
249            id: Uuid::now_v7(),
250            template_task_id: Uuid::nil(),
251            suzerain_id: Uuid::now_v7(),
252            suzerain_power: 1,
253            vassal_id: Uuid::now_v7(),
254            vassal_power: 1,
255            good_reward: Vec::new(),
256            bad_reward: Vec::new(),
257            good_loyalty: 0,
258            bad_loyalty: 0,
259            task_status: VassalTaskStatus::NotStarted,
260            claimed_by_suzerain: false,
261            claimed_by_vassal: false,
262            started_at: None,
263            finish_at: None,
264        }
265    }
266
267    pub fn id(mut self, id: Uuid) -> Self {
268        self.id = id;
269        self
270    }
271
272    pub fn template_task_id(mut self, template_task_id: Uuid) -> Self {
273        self.template_task_id = template_task_id;
274        self
275    }
276
277    pub fn suzerain_id(mut self, suzerain_id: Uuid) -> Self {
278        self.suzerain_id = suzerain_id;
279        self
280    }
281
282    pub fn suzerain_power(mut self, suzerain_power: i64) -> Self {
283        self.suzerain_power = suzerain_power;
284        self
285    }
286
287    pub fn vassal_id(mut self, vassal_id: Uuid) -> Self {
288        self.vassal_id = vassal_id;
289        self
290    }
291
292    pub fn vassal_power(mut self, vassal_power: i64) -> Self {
293        self.vassal_power = vassal_power;
294        self
295    }
296
297    pub fn good_reward(mut self, good_reward: Vec<CurrencyUnit>) -> Self {
298        self.good_reward = good_reward;
299        self
300    }
301
302    pub fn bad_reward(mut self, bad_reward: Vec<CurrencyUnit>) -> Self {
303        self.bad_reward = bad_reward;
304        self
305    }
306
307    pub fn good_loyalty(mut self, good_loyalty: i64) -> Self {
308        self.good_loyalty = good_loyalty;
309        self
310    }
311
312    pub fn bad_loyalty(mut self, bad_loyalty: i64) -> Self {
313        self.bad_loyalty = bad_loyalty;
314        self
315    }
316
317    pub fn task_status(mut self, task_status: VassalTaskStatus) -> Self {
318        self.task_status = task_status;
319        self
320    }
321
322    pub fn claimed_by_suzerain(mut self, claimed: bool) -> Self {
323        self.claimed_by_suzerain = claimed;
324        self
325    }
326
327    pub fn claimed_by_vassal(mut self, claimed: bool) -> Self {
328        self.claimed_by_vassal = claimed;
329        self
330    }
331
332    pub fn started_at(mut self, started_at: Option<chrono::DateTime<Utc>>) -> Self {
333        self.started_at = started_at;
334        self
335    }
336
337    pub fn finish_at(mut self, finish_at: Option<chrono::DateTime<Utc>>) -> Self {
338        self.finish_at = finish_at;
339        self
340    }
341
342    pub fn build(self) -> VassalTask {
343        VassalTask {
344            id: self.id,
345            template_task_id: self.template_task_id,
346            suzerain_id: self.suzerain_id,
347            suzerain_power: self.suzerain_power,
348            vassal_id: self.vassal_id,
349            vassal_power: self.vassal_power,
350            good_reward: self.good_reward,
351            bad_reward: self.bad_reward,
352            good_loyalty: self.good_loyalty,
353            bad_loyalty: self.bad_loyalty,
354            task_status: self.task_status,
355            claimed_by_suzerain: self.claimed_by_suzerain,
356            claimed_by_vassal: self.claimed_by_vassal,
357            started_at: self.started_at,
358            finish_at: self.finish_at,
359        }
360    }
361}
362
363impl CustomType for VassalTask {
364    fn build(mut builder: TypeBuilder<Self>) {
365        builder
366            .with_name("VassalTask")
367            .with_get("id", |vassal_task: &mut Self| vassal_task.id)
368            .with_get("template_task_id", |vassal_task: &mut Self| {
369                vassal_task.template_task_id
370            })
371            .with_get("suzerain_id", |vassal_task: &mut Self| {
372                vassal_task.suzerain_id
373            })
374            .with_get("suzerain_power", |vassal_task: &mut Self| {
375                vassal_task.suzerain_power
376            })
377            .with_get("vassal_id", |vassal_task: &mut Self| vassal_task.vassal_id)
378            .with_get("vassal_power", |vassal_task: &mut Self| {
379                vassal_task.vassal_power
380            })
381            .with_get("good_reward", |vassal_task: &mut Self| {
382                vassal_task.good_reward.clone()
383            })
384            .with_get("bad_reward", |vassal_task: &mut Self| {
385                vassal_task.bad_reward.clone()
386            })
387            .with_get("good_loyalty", |vassal_task: &mut Self| {
388                vassal_task.good_loyalty
389            })
390            .with_get("bad_loyalty", |vassal_task: &mut Self| {
391                vassal_task.bad_loyalty
392            })
393            .with_get("task_status", |vassal_task: &mut Self| {
394                VassalTaskStatus::to_string(&vassal_task.task_status)
395            })
396            .with_get("claimed_by_suzerain", |vassal_task: &mut Self| {
397                vassal_task.claimed_by_suzerain
398            })
399            .with_get("claimed_by_vassal", |vassal_task: &mut Self| {
400                vassal_task.claimed_by_vassal
401            })
402            .with_get("started_at", |vassal_task: &mut Self| {
403                vassal_task.started_at
404            })
405            .with_get("finish_at", |vassal_task: &mut Self| vassal_task.finish_at);
406    }
407}