add: user coins and transactions
This commit is contained in:
parent
81a7628861
commit
0a3ce3e9fe
8 changed files with 143 additions and 2 deletions
36
crates/core/src/model/economy.rs
Normal file
36
crates/core/src/model/economy.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use serde::{Serialize, Deserialize};
|
||||
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
|
||||
|
||||
use super::auth::User;
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct CoinTransfer {
|
||||
pub id: usize,
|
||||
pub created: usize,
|
||||
pub sender: usize,
|
||||
pub receiver: usize,
|
||||
pub amount: i32,
|
||||
}
|
||||
|
||||
impl CoinTransfer {
|
||||
/// Create a new [`CoinTransfer`].
|
||||
pub fn new(sender: usize, receiver: usize, amount: i32) -> Self {
|
||||
Self {
|
||||
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
|
||||
created: unix_epoch_timestamp(),
|
||||
sender,
|
||||
receiver,
|
||||
amount,
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply the effects of this transaction onto the sender and receiver balances.
|
||||
///
|
||||
/// # Returns
|
||||
/// `(sender bankrupt, receiver bankrupt)`
|
||||
pub fn apply(&self, sender: &mut User, receiver: &mut User) -> (bool, bool) {
|
||||
sender.coins -= self.amount;
|
||||
receiver.coins += self.amount;
|
||||
(sender.coins < 0, receiver.coins < 0)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue