summaryrefslogtreecommitdiff
path: root/src/modules/construction.rs
blob: 091ebc619d2560371316c464aaf419b76365cffb (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::collections::HashMap;
use std::time::SystemTime;

use crate::constants;
use crate::item::ItemType;
use crate::mass::Mass;
use crate::math::Vector;
use crate::modules::types::ModuleType;
use crate::storage::Storage;

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Construction {
    pub status: Status,
    construction: Option<ModuleType>,
    time: u64,
    start: Option<SystemTime>,
}

impl Construction {
    pub fn new() -> Construction {
        Construction {
            status: Status::None,
            construction: None,
            time: constants::SHIP_CONSTRUCTION_TIME,
            start: None,
        }
    }

    pub fn process(
        &mut self,
        ship_velocity: Vector,
        ship_position: Vector,
        masses: &mut HashMap<String, Mass>,
        storage: &mut Storage,
    ) {
        if storage.item_count(ItemType::Iron) < constants::SHIP_CONSTRUCTION_IRON_COST {
            self.off();
        }
        if let Some(timer) = self.start {
            if timer.elapsed().unwrap().as_secs() > self.time {
                self.start = Some(SystemTime::now());
                self.status = Status::Constructed;
            }
        }
        if self.status == Status::Constructed {
            for _ in 0..constants::SHIP_CONSTRUCTION_IRON_COST {
                storage.take_item(ItemType::Iron).unwrap();
            }

            masses.insert(
                "Station".to_string(),
                Mass::new_station(
                    ModuleType::Refinery,
                    ship_position.clone(),
                    ship_velocity.clone(),
                ),
            );
            self.constructed();
        }
    }

    pub fn get_client_data(&self, storage: &Storage) -> String {
        let client_data = ClientData {
            has_enough: storage.item_count(ItemType::Iron)
                >= constants::SHIP_CONSTRUCTION_IRON_COST,
            status: self.status.clone(),
        };
        serde_json::to_string(&client_data).unwrap() + "\n"
    }

    pub fn give_received_data(&mut self, recv: String) {
        if let "c" = recv.as_str() {
            self.toggle()
        }
    }

    fn toggle(&mut self) {
        match self.status {
            Status::None => self.on(),
            _ => self.off(),
        };
    }

    fn on(&mut self) {
        self.start = Some(SystemTime::now());
        self.status = Status::Constructing;
    }

    fn off(&mut self) {
        self.start = None;
        self.status = Status::None;
    }

    fn constructed(&mut self) {
        self.off()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientData {
    pub status: Status,
    pub has_enough: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Status {
    None,
    Constructing,
    Constructed,
}

impl Default for Status {
    fn default() -> Self {
        Status::None
    }
}