summaryrefslogtreecommitdiff
path: root/rust_backend/src/market/order.rs
blob: 6dbbcf65237dcf937640d99672ed8dff0ee046c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::error::Error;
use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::eve_service::EveService;


#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Order {
    duration: u64,
    is_buy_order: bool,
    issued: DateTime<Utc>,
    location_id: u64,
    min_volume: u64,
    order_id: u64,
    price: f64,
    range: String,
    system_id: u64,
    type_id: u64,
    volume_remain: u64,
    volume_total: u64
}

const REGIONS: [u64; 1] = [10000033];

#[derive(Clone, Serialize)]
pub struct Orders {
    region_orders: HashMap<u64, Vec<Order>>,
    expiry_times: HashMap<u64, DateTime<Utc>>, // region_id to expiration time
}


impl Orders {
    pub fn new() -> Self {
        Orders {
            region_orders: HashMap::new(),
            expiry_times: HashMap::new(),
        }
    }

    pub fn insert(&mut self, region: u64, orders: Vec<Order>, expiry: DateTime<Utc>) {
        self.region_orders.insert(region, orders);
        self.expiry_times.insert(region, expiry);
    }

    pub fn expired_regions(&self) -> Vec<u64> {
        let current_time = Utc::now();

        println!("expiry: {:?}, time now: {:?}", &self.expiry_times, &current_time);

        let mut expired: Vec<u64> = self.expiry_times.iter()
            .filter(|&(_region_id, &expiry_time)| expiry_time < current_time)
            .map(|(&region_id, _)| region_id)
            .collect();

        for &region in REGIONS.iter() {
            if !self.expiry_times.contains_key(&region) {
                expired.push(region);
            }
        }
    

        expired

    }

    pub fn update(&mut self, updated_orders: Orders) {
        for (key, value) in updated_orders.region_orders {
            self.region_orders.insert(key, value);

            if let Some(expiry_time) = updated_orders.expiry_times.get(&key) {
                self.expiry_times.insert(key, *expiry_time);
            }
        }
    }


    pub fn orders(&self) -> Vec<Order> {
        self.region_orders.values().flat_map(|orders| orders.clone()).collect()
    }
}