From cfef1c2de878ef45c8d48314ea273125219b031e Mon Sep 17 00:00:00 2001 From: Shane Madden Date: Wed, 6 Mar 2024 19:22:13 -0700 Subject: [PATCH] Fix unusable keys in game::structures/construction_sites (#510) --- CHANGELOG.md | 4 +++- src/game.rs | 17 ++++++++++------- src/local/object_id.rs | 13 +++++++++++-- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 337c1712..a7364e26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,8 @@ Unreleased - Remove features `generate-pixel` and `inter-shard-memory`, use the `mmo` feature instead - Place `game::cpu::{shard_limits, unlocked, unlocked_time, set_shard_limits, unlock}` functions behind the `mmo` feature +- Change return type of `game::{construction_sites, structures}` to `JsHashMap, _>` + instead of `JsHashMap` ### Additions: @@ -33,7 +35,7 @@ Unreleased ### Bugfixes: -- Implement `JsCollectionFromValue` for `Direction` +- Implement `JsCollectionFromValue` for `Direction`, `ObjectId<_>` - Implement `Debug` for `RouteStep` ### Misc: diff --git a/src/game.rs b/src/game.rs index a6e97d52..2b797ba1 100644 --- a/src/game.rs +++ b/src/game.rs @@ -18,7 +18,10 @@ use crate::{ enums::StructureObject, js_collections::{JsHashMap, JsObjectId}, local::{ObjectId, RawObjectId, RoomName}, - objects::{AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, StructureSpawn}, + objects::{ + AccountPowerCreep, ConstructionSite, Creep, Flag, Room, RoomObject, Structure, + StructureSpawn, + }, traits::MaybeHasId, }; @@ -75,11 +78,11 @@ extern "C" { fn notify(message: &JsString, group_interval: Option); } -/// Get a [`JsHashMap`] with all of your -/// construction sites. +/// Get a [`JsHashMap, ConstructionSite>`] with all +/// of your construction sites. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.constructionSites) -pub fn construction_sites() -> JsHashMap { +pub fn construction_sites() -> JsHashMap, ConstructionSite> { Game::construction_sites().into() } @@ -169,11 +172,11 @@ pub fn spawns_jsstring() -> JsHashMap { Game::spawns().into() } -/// Get a [`JsHashMap`] with all of your owned -/// structures. +/// Get a [`JsHashMap, StructureObject>`] with all of your +/// owned structures. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns) -pub fn structures() -> JsHashMap { +pub fn structures() -> JsHashMap, StructureObject> { Game::structures().into() } diff --git a/src/local/object_id.rs b/src/local/object_id.rs index 157bfe75..ffc851df 100644 --- a/src/local/object_id.rs +++ b/src/local/object_id.rs @@ -7,10 +7,11 @@ use std::{ }; use arrayvec::ArrayString; +use js_sys::JsString; use serde::{Deserialize, Serialize}; -use wasm_bindgen::JsCast; +use wasm_bindgen::{JsCast, JsValue}; -use crate::{game, objects::RoomObject, traits::MaybeHasId}; +use crate::{game, js_collections::JsCollectionFromValue, objects::RoomObject, traits::MaybeHasId}; mod errors; mod raw; @@ -262,3 +263,11 @@ impl From for ObjectId { Self::from_packed(packed) } } + +impl JsCollectionFromValue for ObjectId { + fn from_value(val: JsValue) -> Self { + let val: JsString = val.unchecked_into(); + let val: String = val.into(); + val.parse().expect("valid id string") + } +}