42 lines
981 B
Rust
42 lines
981 B
Rust
use generator::MarketMultiplier;
|
|
use rust_decimal::Decimal;
|
|
use tokio::sync::{Mutex, MutexGuard};
|
|
|
|
mod generator;
|
|
|
|
pub(crate) struct Market {
|
|
generator: Mutex<MarketMultiplier>,
|
|
}
|
|
|
|
impl Market {
|
|
pub(crate) fn new() -> Self {
|
|
Self {
|
|
generator: Mutex::new(MarketMultiplier::new(
|
|
Decimal::new(1, 1),
|
|
Decimal::new(1, 0),
|
|
)),
|
|
}
|
|
}
|
|
|
|
pub(crate) async fn lock(&self) -> LockedMarket<'_> {
|
|
LockedMarket(self.generator.lock().await, 0)
|
|
}
|
|
}
|
|
|
|
pub(crate) struct LockedMarket<'a>(MutexGuard<'a, MarketMultiplier>, usize);
|
|
|
|
impl<'a> LockedMarket<'a> {
|
|
pub(crate) fn peek_price(&mut self, initial_price: &Decimal, n: usize) -> Decimal {
|
|
let mut price = *initial_price;
|
|
for i in self.0.peek_n(n).iter() {
|
|
price *= i.modifier();
|
|
}
|
|
self.1 = n;
|
|
price.round_dp(2)
|
|
}
|
|
|
|
pub(crate) fn commit(mut self) {
|
|
self.0.next_n(self.1);
|
|
}
|
|
}
|