add: user coins and transactions

This commit is contained in:
trisua 2025-08-06 23:35:13 -04:00
parent 81a7628861
commit 0a3ce3e9fe
8 changed files with 143 additions and 2 deletions

View file

@ -92,6 +92,9 @@ pub struct User {
/// The time at which the user's ban will automatically expire.
#[serde(default)]
pub ban_expire: usize,
/// The number of coins the user has.
#[serde(default)]
pub coins: i32,
}
pub type UserConnections =
@ -397,6 +400,7 @@ impl User {
channel_mutes: Vec::new(),
is_deactivated: false,
ban_expire: 0,
coins: 0,
}
}

View 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)
}
}

View file

@ -5,6 +5,7 @@ pub mod carp;
pub mod channels;
pub mod communities;
pub mod communities_permissions;
pub mod economy;
pub mod journals;
pub mod littleweb;
pub mod mail;