Skip to content

Commit

Permalink
Option returns for out of map room status/terrain (rustyscreeps#467)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden authored Nov 28, 2023
1 parent 83bccf8 commit c96cff2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 22 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<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:

Expand Down
25 changes: 7 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 All @@ -95,6 +95,7 @@ extern "C" {
pub fn timestamp(this: &JsRoomStatusResult) -> Option<f64>;
}

#[derive(Clone, Debug)]
pub struct RoomStatusResult {
status: RoomStatus,
timestamp: Option<f64>,
Expand All @@ -110,15 +111,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 +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<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 c96cff2

Please sign in to comment.