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
use js_sys::JsString;
use wasm_bindgen::{prelude::*, JsCast};

use crate::{
    constants::{ErrorCode, StructureType},
    objects::{Owner, RoomObject},
    prelude::*,
};

#[wasm_bindgen]
extern "C" {
    /// A [`ConstructionSite`] which is an object representing a structure under
    /// construction.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite)
    #[wasm_bindgen(extends = RoomObject)]
    #[derive(Clone, Debug)]
    pub type ConstructionSite;

    #[wasm_bindgen(method, getter = id)]
    fn id_internal(this: &ConstructionSite) -> Option<JsString>;

    #[wasm_bindgen(method, getter = my)]
    fn my_internal(this: &ConstructionSite) -> bool;

    #[wasm_bindgen(method, getter = owner)]
    fn owner_internal(this: &ConstructionSite) -> Owner;

    #[wasm_bindgen(method, getter = progress)]
    fn progress_internal(this: &ConstructionSite) -> u32;

    #[wasm_bindgen(method, getter = progressTotal)]
    fn progress_total_internal(this: &ConstructionSite) -> u32;

    #[wasm_bindgen(method, getter = structureType)]
    fn structure_type_internal(this: &ConstructionSite) -> StructureType;

    #[wasm_bindgen(method, js_name = remove)]
    fn remove_internal(this: &ConstructionSite) -> i8;
}

impl ConstructionSite {
    /// Whether you own the [`ConstructionSite`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.my)
    pub fn my(&self) -> bool {
        self.my_internal()
    }

    /// The [`Owner`] of this construction site, which contains the owner's
    /// username.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.owner)
    pub fn owner(&self) -> Owner {
        self.owner_internal()
    }

    /// The current progress toward completion of the structure being built.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.progress)
    pub fn progress(&self) -> u32 {
        self.progress_internal()
    }

    /// The total progess toward constuction progress needed for the structure
    /// to be completed.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.progressTotal)
    pub fn progress_total(&self) -> u32 {
        self.progress_total_internal()
    }

    /// The type of structure being constructed.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#Structure.structureType)
    pub fn structure_type(&self) -> StructureType {
        self.structure_type_internal()
    }

    /// Remove the [`ConstructionSite`].
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.remove)
    pub fn remove(&self) -> Result<(), ErrorCode> {
        ErrorCode::result_from_i8(self.remove_internal())
    }
}

impl MaybeHasNativeId for ConstructionSite {
    /// The Object ID of the [`ConstructionSite`], or `None` if it was created
    /// this tick.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#ConstructionSite.id)
    fn try_native_id(&self) -> Option<JsString> {
        self.id_internal()
    }
}

impl JsCollectionFromValue for ConstructionSite {
    fn from_value(val: JsValue) -> Self {
        val.unchecked_into()
    }
}