Skip to content

Commit

Permalink
fix: work-around game creating creeps with id of type number (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfrank authored Mar 25, 2024
1 parent f225811 commit 3971c8c
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/objects/impls/game_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ extern "C" {

/// The unique ID of this object that you can use in
/// /game/utils::getObjectById
#[wasm_bindgen(method, getter)]
pub fn id(this: &GameObject) -> JsString;
#[wasm_bindgen(method, getter, js_name = "id")]
pub fn id_internal(this: &GameObject) -> JsValue;

/// The X coordinate in the room.
#[wasm_bindgen(method, getter)]
Expand Down Expand Up @@ -59,6 +59,28 @@ extern "C" {
pub fn get_range_to(this: &GameObject, pos: &Object) -> u8;
}

impl GameObject {
/* Although Creep.id is documented as a string, in practice it is sometimes
* of type number.
* This seems to happen in swamp & spawn but not in ctf.
* This function returns a JsString always, converting if necessary.
*/
pub fn id(&self) -> JsString {
let i = self.id_internal();
let as_str = i.dyn_into::<JsString>();
match as_str {
Ok(s) => s,
Err(i) => {
let as_f64 = i.as_f64();
match as_f64 {
Some(f) => JsString::from(format!("{f}")),
None => i.unchecked_into::<JsString>(), // this will probably crash
}
}
}
}
}

impl<T> GameObjectProperties for T
where
T: AsRef<GameObject>,
Expand Down

0 comments on commit 3971c8c

Please sign in to comment.