screeps/objects/impls/
owned_structure.rs

1use wasm_bindgen::prelude::*;
2
3use crate::{
4    objects::{RoomObject, Structure},
5    prelude::*,
6};
7
8#[wasm_bindgen]
9extern "C" {
10    /// Parent class for all [`Structure`] objects types which are (or can be)
11    /// owned by a specific player.
12    ///
13    /// [Screeps documentation](https://docs.screeps.com/api/#OwnedStructure)
14    #[wasm_bindgen(extends = RoomObject, extends = Structure)]
15    #[derive(Clone, Debug)]
16    pub type OwnedStructure;
17
18    // For controllers (possibly other structures?) user can be set to null, even
19    // though it's meant to always be owned. This internal method is used to map
20    // that case to false.
21    #[wasm_bindgen(method, getter = my)]
22    fn my_internal(this: &OwnedStructure) -> Option<bool>;
23
24    /// The [`Owner`] of this structure that contains the owner's username, or
25    /// `None` if it's an ownable structure currently not under a player's
26    /// control.
27    ///
28    /// [Screeps documentation](https://docs.screeps.com/api/#OwnedStructure.owner)
29    #[wasm_bindgen(method, getter)]
30    pub fn owner(this: &OwnedStructure) -> Option<Owner>;
31}
32
33impl OwnedStructure {
34    /// Whether this structure is owned by the player.
35    ///
36    /// [Screeps documentation](https://docs.screeps.com/api/#OwnedStructure.my)
37    pub fn my(&self) -> bool {
38        // If there is no user assigned, like in unowned controllers, `my` returns
39        // undefined. That should be `false`, since that's not owned by the caller.
40        self.my_internal().unwrap_or(false)
41    }
42}
43
44impl Attackable for OwnedStructure {}
45
46impl<T> OwnedStructureProperties for T
47where
48    T: AsRef<OwnedStructure>,
49{
50    fn my(&self) -> bool {
51        OwnedStructure::my(self.as_ref())
52    }
53
54    fn owner(&self) -> Option<Owner> {
55        OwnedStructure::owner(self.as_ref())
56    }
57}
58
59#[wasm_bindgen]
60extern "C" {
61    /// Object with owner info for an owned game object.
62    #[wasm_bindgen]
63    pub type Owner;
64
65    /// The name of the player that owns this object.
66    #[wasm_bindgen(method, getter)]
67    pub fn username(this: &Owner) -> String;
68}