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, Structure,
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>);
}
pub fn construction_sites() -> JsHashMap<ObjectId<ConstructionSite>, ConstructionSite> {
Game::construction_sites().into()
}
pub fn creeps() -> JsHashMap<String, Creep> {
Game::creeps().into()
}
pub fn creeps_jsstring() -> JsHashMap<JsString, Creep> {
Game::creeps().into()
}
pub fn flags() -> JsHashMap<String, Flag> {
Game::flags().into()
}
pub fn flags_jsstring() -> JsHashMap<JsString, Flag> {
Game::flags().into()
}
pub fn power_creeps() -> JsHashMap<String, AccountPowerCreep> {
Game::power_creeps().into()
}
pub fn power_creeps_jsstring() -> JsHashMap<JsString, AccountPowerCreep> {
Game::power_creeps().into()
}
pub fn resources() -> JsHashMap<IntershardResourceType, u32> {
Game::resources().into()
}
pub fn rooms() -> JsHashMap<RoomName, Room> {
Game::rooms().into()
}
pub fn spawns() -> JsHashMap<String, StructureSpawn> {
Game::spawns().into()
}
pub fn spawns_jsstring() -> JsHashMap<JsString, StructureSpawn> {
Game::spawns().into()
}
pub fn structures() -> JsHashMap<ObjectId<Structure>, StructureObject> {
Game::structures().into()
}
pub fn time() -> u32 {
Game::time()
}
#[cfg(feature = "seasonal-season-2")]
pub fn score() -> u32 {
Game::score()
}
#[cfg(feature = "seasonal-season-2")]
pub fn symbols() -> JsHashMap<crate::ResourceType, u32> {
Game::symbols().into()
}
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)
}
pub fn get_object_by_id_typed<T>(id: &ObjectId<T>) -> Option<T>
where
T: MaybeHasId + JsCast,
{
let js_str = JsString::from(id.to_string());
Game::get_object_by_id(&js_str).map(JsCast::unchecked_into)
}
pub fn get_object_by_id_erased(id: &RawObjectId) -> Option<RoomObject> {
let js_str = JsString::from(id.to_string());
Game::get_object_by_id(&js_str)
}
pub fn notify(message: &str, group_interval: Option<u32>) {
let message: JsString = message.into();
Game::notify(&message, group_interval)
}