screeps/objects/impls/
structure_terminal.rs

1use js_sys::JsString;
2use wasm_bindgen::prelude::*;
3
4use crate::{
5    constants::ResourceType,
6    enums::action_error_codes::structure_terminal::*,
7    local::RoomName,
8    objects::{OwnedStructure, RoomObject, Store, Structure},
9    prelude::*,
10};
11
12#[wasm_bindgen]
13extern "C" {
14    /// An object representing a [`StructureTerminal`], which can send resources
15    /// to distant rooms and participate in the market.
16    ///
17    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal)
18    #[wasm_bindgen(extends = RoomObject, extends = Structure, extends = OwnedStructure)]
19    #[derive(Clone, Debug)]
20    pub type StructureTerminal;
21
22    /// The number of ticks until the [`StructureTerminal`] can use
23    /// [`StructureTerminal::send`] or be used in a market transaction again.
24    ///
25    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.cooldown)
26    #[wasm_bindgen(method, getter)]
27    pub fn cooldown(this: &StructureTerminal) -> u32;
28
29    /// The [`Store`] of the terminal, which contains information about what
30    /// resources it is it holding.
31    ///
32    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.store)
33    #[wasm_bindgen(method, getter)]
34    pub fn store(this: &StructureTerminal) -> Store;
35
36    #[wasm_bindgen(method, js_name = send)]
37    fn send_internal(
38        this: &StructureTerminal,
39        resource_type: ResourceType,
40        amount: u32,
41        destination: &JsString,
42        description: Option<&JsString>,
43    ) -> i8;
44}
45
46impl StructureTerminal {
47    /// Send resources to another room's terminal.
48    ///
49    /// [Screeps documentation](https://docs.screeps.com/api/#StructureTerminal.send)
50    pub fn send(
51        &self,
52        resource_type: ResourceType,
53        amount: u32,
54        destination: RoomName,
55        description: Option<&str>,
56    ) -> Result<(), SendErrorCode> {
57        let desination = destination.into();
58        let description = description.map(JsString::from);
59
60        SendErrorCode::result_from_i8(self.send_internal(
61            resource_type,
62            amount,
63            &desination,
64            description.as_ref(),
65        ))
66    }
67}
68
69impl HasCooldown for StructureTerminal {
70    fn cooldown(&self) -> u32 {
71        Self::cooldown(self)
72    }
73}
74
75impl HasStore for StructureTerminal {
76    fn store(&self) -> Store {
77        Self::store(self)
78    }
79}
80
81impl Attackable for StructureTerminal {}
82impl Dismantleable for StructureTerminal {}
83impl Repairable for StructureTerminal {}
84impl Transferable for StructureTerminal {}
85impl Withdrawable for StructureTerminal {}