Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: work-around game creating creeps with id of type number #9

Merged
merged 2 commits into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading