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

use crate::{
    constants::{ErrorCode, ResourceType},
    local::RoomName,
    objects::{OwnedStructure, RoomObject, Store, Structure},
    prelude::*,
};

#[wasm_bindgen]
extern "C" {
    /// An object representing a [`StructureTerminal`], which can send resources
    /// to distant rooms and participate in the market.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal)
    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
    #[derive(Clone, Debug)]
    pub type StructureTerminal;

    /// The number of ticks until the [`StructureTerminal`] can use
    /// [`StructureTerminal::send`] or be used in a market transaction again.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.cooldown)
    #[wasm_bindgen(method, getter)]
    pub fn cooldown(this: &StructureTerminal) -> u32;

    /// The [`Store`] of the terminal, which contains information about what
    /// resources it is it holding.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.store)
    #[wasm_bindgen(method, getter)]
    pub fn store(this: &StructureTerminal) -> Store;

    #[wasm_bindgen(method, js_name = send)]
    fn send_internal(
        this: &StructureTerminal,
        resource_type: ResourceType,
        amount: u32,
        destination: &JsString,
        description: Option<&JsString>,
    ) -> i8;
}

impl StructureTerminal {
    /// Send resources to another room's terminal.
    ///
    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.send)
    pub fn send(
        &self,
        resource_type: ResourceType,
        amount: u32,
        destination: RoomName,
        description: Option<&str>,
    ) -> Result<(), ErrorCode> {
        let desination = destination.into();
        let description = description.map(JsString::from);

        ErrorCode::result_from_i8(self.send_internal(
            resource_type,
            amount,
            &desination,
            description.as_ref(),
        ))
    }
}

impl HasCooldown for StructureTerminal {
    fn cooldown(&self) -> u32 {
        Self::cooldown(self)
    }
}

impl HasStore for StructureTerminal {
    fn store(&self) -> Store {
        Self::store(self)
    }
}