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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//! The main interface to objects in the Screeps game world.
//!
//! This contains all functionality from the `Game` object in Screeps. That
//! generally means all state which is true this tick throughout the world.
//!
//! # Safety
//!
//! All returned game objects must be used only during the tick they are
//! retreived or resolved. They are considered "stale" on subsequent ticks, and
//! the behavior of stale game objects is undefined.
//!
//! [Screeps documentation](http://docs.screeps.com/api/#Game)
use js_sys::{JsString, Object};
use wasm_bindgen::prelude::*;

use crate::{
    constants::IntershardResourceType,
    enums::StructureObject,
    js_collections::{JsHashMap, JsObjectId},
    local::{ObjectId, RawObjectId, RoomName},
    objects::{AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, StructureSpawn},
    traits::MaybeHasId,
};

pub mod cpu;
pub mod gcl;
pub mod gpl;
pub mod map;
pub mod market;
pub mod shard;

#[wasm_bindgen]
extern "C" {
    type Game;

    #[wasm_bindgen(static_method_of = Game, getter = constructionSites)]
    fn construction_sites() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = creeps)]
    fn creeps() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = flags)]
    fn flags() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = powerCreeps)]
    fn power_creeps() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = resources)]
    fn resources() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = rooms)]
    fn rooms() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = spawns)]
    fn spawns() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = structures)]
    fn structures() -> Object;

    #[wasm_bindgen(static_method_of = Game, getter = time)]
    fn time() -> u32;

    #[cfg(feature = "seasonal-season-2")]
    #[wasm_bindgen(static_method_of = Game, getter = score)]
    fn score() -> u32;

    #[cfg(feature = "seasonal-season-2")]
    #[wasm_bindgen(static_method_of = Game, getter = symbols)]
    fn symbols() -> Object;

    #[wasm_bindgen(static_method_of = Game, js_name = getObjectById)]
    fn get_object_by_id(id: &JsString) -> Option<RoomObject>;

    #[wasm_bindgen(static_method_of = Game, js_name = notify)]
    fn notify(message: &JsString, group_interval: Option<u32>);
}

/// Get a [`JsHashMap<RawObjectId, ConstructionSite>`] with all of your
/// construction sites.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.constructionSites)
pub fn construction_sites() -> JsHashMap<RawObjectId, ConstructionSite> {
    Game::construction_sites().into()
}

/// Get a [`JsHashMap<String, Creep>`] with all of your creeps, which has creep
/// names as keys.
///
/// Note that newly spawned creeps are immediately added when spawned, but will
/// not have an id until the following tick.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.creeps)
pub fn creeps() -> JsHashMap<String, Creep> {
    Game::creeps().into()
}

/// Get a [`JsHashMap<String, Flag>`] with all of your flags, which has flag
/// names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags)
pub fn flags() -> JsHashMap<String, Flag> {
    Game::flags().into()
}

/// Get a [`JsHashMap<String, AccountPowerCreep>`] with all of your power
/// creeps, which has power creep names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.powerCreeps)
pub fn power_creeps() -> JsHashMap<String, AccountPowerCreep> {
    Game::power_creeps().into()
}

/// Get a [`JsHashMap<IntershardResourceType, u32>`] with all of your account
/// resources.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.resources)
pub fn resources() -> JsHashMap<IntershardResourceType, u32> {
    Game::resources().into()
}

/// Get a [`JsHashMap<RoomName, Room>`] with the rooms visible for the current
/// tick.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.rooms)
pub fn rooms() -> JsHashMap<RoomName, Room> {
    Game::rooms().into()
}

/// Get a [`JsHashMap<String, StructureSpawn>`] with all of your spawns, which
/// has spawn names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
pub fn spawns() -> JsHashMap<String, StructureSpawn> {
    Game::spawns().into()
}

/// Get a [`JsHashMap<RawObjectId, StructureObject>`] with all of your owned
/// structures.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
pub fn structures() -> JsHashMap<RawObjectId, StructureObject> {
    Game::structures().into()
}

/// Get the current time, the number of ticks the game has been running.
///
/// [Screeps documentation](http://docs.screeps.com/api/#Game.time)
pub fn time() -> u32 {
    Game::time()
}

/// Your current score, as determined by the symbols you have decoded.
///
/// [Screeps documentation](https://docs-season.screeps.com/api/#Game.score)
#[cfg(feature = "seasonal-season-2")]
pub fn score() -> u32 {
    Game::score()
}

/// The symbols you've decoded after multiplier adjustments, used to
/// determine your score.
///
/// [Screeps documentation](https://docs-season.screeps.com/api/#Game.symbols)
#[cfg(feature = "seasonal-season-2")]
pub fn symbols() -> JsHashMap<crate::ResourceType, u32> {
    Game::symbols().into()
}

/// Get the typed object represented by a given [`JsObjectId`], if it's
/// still alive and visible.
///
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_js_id_typed<T>(id: &JsObjectId<T>) -> Option<T>
where
    T: MaybeHasId + JsCast,
{
    Game::get_object_by_id(&id.raw).map(JsCast::unchecked_into)
}

/// Get the typed object represented by a given [`ObjectId`], if it's still
/// alive and visible.
///
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_id_typed<T>(id: &ObjectId<T>) -> Option<T>
where
    T: MaybeHasId + JsCast,
{
    // construct a reference to a javascript string using the id data
    let js_str = JsString::from(id.to_string());

    Game::get_object_by_id(&js_str).map(JsCast::unchecked_into)
}

/// Get the [`RoomObject`] represented by a given [`RawObjectId`], if it's
/// still alive and visible.
///
/// [Screeps documentation](http://docs.screeps.com/api/#Game.getObjectById)
pub fn get_object_by_id_erased(id: &RawObjectId) -> Option<RoomObject> {
    // construct a reference to a javascript string using the id data
    let js_str = JsString::from(id.to_string());

    Game::get_object_by_id(&js_str)
}

/// Send an email message to yourself with a given message.
///
/// Set a `group_interval` with a limit, in minutes, on how frequently emails
/// are allowed to be sent. Message will be truncated to [`NOTIFY_MAX_LENGTH`]
/// characters.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.notify)
///
/// [`NOTIFY_MAX_LENGTH`]: crate::constants::NOTIFY_MAX_LENGTH
pub fn notify(message: &str, group_interval: Option<u32>) {
    let message: JsString = message.into();

    Game::notify(&message, group_interval)
}