diff --git a/CHANGELOG.md b/CHANGELOG.md index 3db5dcd6..aecad2c1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ Unreleased - Change `TOWER_OPTIMAL_RANGE` and `TOWER_FALLOFF_RANGE` types to `u8` and `TOWER_FALLOFF` type to `f64` +- Changed `RoomTerrain::new` and `game::map::get_room_terrain` return type to + `Option`, returning `None` when the specified room is outside the server's + map +- Changed `game::map::get_room_status` return type to `Option`, returning + `None` instead of the previous behavior of returning an artificial 'normal' status for rooms + outside the server's map ### Bugfixes: diff --git a/src/game/map.rs b/src/game/map.rs index 6d0a8aa4..167b0870 100644 --- a/src/game/map.rs +++ b/src/game/map.rs @@ -33,8 +33,8 @@ extern "C" { #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomLinearDistance)] fn get_room_linear_distance(room_1: &JsString, room_2: &JsString, continuous: bool) -> u32; - #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomTerrain)] - fn get_room_terrain(room_name: &JsString) -> RoomTerrain; + #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getRoomTerrain, catch)] + fn get_room_terrain(room_name: &JsString) -> Result; #[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getWorldSize)] fn get_world_size() -> u32; @@ -70,10 +70,10 @@ pub fn get_room_linear_distance(from_room: RoomName, to_room: RoomName, continuo /// vision in. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.map.getRoomTerrain) -pub fn get_room_terrain(room_name: RoomName) -> RoomTerrain { +pub fn get_room_terrain(room_name: RoomName) -> Option { let name = room_name.into(); - Map::get_room_terrain(&name) + Map::get_room_terrain(&name).ok() } /// Get the size of the world map. @@ -95,6 +95,7 @@ extern "C" { pub fn timestamp(this: &JsRoomStatusResult) -> Option; } +#[derive(Clone, Debug)] pub struct RoomStatusResult { status: RoomStatus, timestamp: Option, @@ -110,15 +111,6 @@ impl RoomStatusResult { } } -impl Default for RoomStatusResult { - fn default() -> Self { - RoomStatusResult { - status: RoomStatus::Normal, - timestamp: None, - } - } -} - impl From for RoomStatusResult { fn from(val: JsRoomStatusResult) -> Self { RoomStatusResult { @@ -141,13 +133,10 @@ pub enum RoomStatus { /// area or currently inaccessible. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.map.getRoomStatus) -pub fn get_room_status(room_name: RoomName) -> RoomStatusResult { +pub fn get_room_status(room_name: RoomName) -> Option { let name = room_name.into(); - Map::get_room_status(&name) - .ok() - .map(RoomStatusResult::from) - .unwrap_or_default() + Map::get_room_status(&name).ok().map(RoomStatusResult::from) } #[wasm_bindgen] diff --git a/src/objects/impls/room_terrain.rs b/src/objects/impls/room_terrain.rs index ed37062f..4dfe77e6 100644 --- a/src/objects/impls/room_terrain.rs +++ b/src/objects/impls/room_terrain.rs @@ -15,8 +15,8 @@ extern "C" { #[wasm_bindgen(js_namespace = Room, js_name = Terrain)] pub type RoomTerrain; - #[wasm_bindgen(constructor, js_namespace = Room, js_class = Terrain)] - fn new_internal(room_name: &JsString) -> RoomTerrain; + #[wasm_bindgen(constructor, js_namespace = Room, js_class = Terrain, catch)] + fn new_internal(room_name: &JsString) -> Result; /// Get the type of terrain at given coordinates. /// @@ -38,10 +38,10 @@ impl RoomTerrain { /// of the room. /// /// [Screeps documentation](https://docs.screeps.com/api/#Room.Terrain.constructor) - pub fn new(room_name: RoomName) -> RoomTerrain { + pub fn new(room_name: RoomName) -> Option { let name = room_name.into(); - Self::new_internal(&name) + Self::new_internal(&name).ok() } /// Get a copy of the underlying Uint8Array with the data about the room's