Skip to content

Commit

Permalink
Option returns for out of map room status/terrain
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Nov 28, 2023
1 parent be38e3f commit adf885a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
Unreleased
==========

### Breaking:

- Changed `RoomTerrain::new` and `game::map::get_room_terrain` return type to
`Option<RoomTerrain>`, returning `None` when the specified room is outside the server's
map
- Changed `game::map::get_room_status` return type to `Option<RoomStatusResult>`, returning
`None` instead of the previous behavior of returning an artificial 'normal' status for rooms
outside the server's map

### Bugfixes:

- Implement `JsCollectionIntoValue` for `Direction`, making the `JsHashMap` returned by
Expand Down
24 changes: 6 additions & 18 deletions src/game/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomTerrain, JsValue>;

#[wasm_bindgen(js_namespace = ["Game"], js_class = "map", static_method_of = Map, js_name = getWorldSize)]
fn get_world_size() -> u32;
Expand Down Expand Up @@ -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<RoomTerrain> {
let name = room_name.into();

Map::get_room_terrain(&name)
Map::get_room_terrain(&name).ok()
}

/// Get the size of the world map.
Expand Down Expand Up @@ -110,15 +110,6 @@ impl RoomStatusResult {
}
}

impl Default for RoomStatusResult {
fn default() -> Self {
RoomStatusResult {
status: RoomStatus::Normal,
timestamp: None,
}
}
}

impl From<JsRoomStatusResult> for RoomStatusResult {
fn from(val: JsRoomStatusResult) -> Self {
RoomStatusResult {
Expand All @@ -141,13 +132,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<RoomStatusResult> {
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]
Expand Down
8 changes: 4 additions & 4 deletions src/objects/impls/room_terrain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomTerrain, JsValue>;

/// Get the type of terrain at given coordinates.
///
Expand All @@ -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<RoomTerrain> {
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
Expand Down

0 comments on commit adf885a

Please sign in to comment.