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 unusable keys in game::structures/construction_sites #510

Merged
merged 2 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<ObjectId<_>, _>`
instead of `JsHashMap<RawObjectId, _>`

### Additions:

Expand All @@ -33,7 +35,7 @@ Unreleased

### Bugfixes:

- Implement `JsCollectionFromValue` for `Direction`
- Implement `JsCollectionFromValue` for `Direction`, `ObjectId<_>`
- Implement `Debug` for `RouteStep`

### Misc:
Expand Down
17 changes: 10 additions & 7 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -75,11 +78,11 @@ extern "C" {
fn notify(message: &JsString, group_interval: Option<u32>);
}

/// Get a [`JsHashMap<RawObjectId, ConstructionSite>`] with all of your
/// construction sites.
/// Get a [`JsHashMap<ObjectId<ConstructionSite>, ConstructionSite>`] with all
/// of your construction sites.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.constructionSites)
pub fn construction_sites() -> JsHashMap<RawObjectId, ConstructionSite> {
pub fn construction_sites() -> JsHashMap<ObjectId<ConstructionSite>, ConstructionSite> {
Game::construction_sites().into()
}

Expand Down Expand Up @@ -169,11 +172,11 @@ pub fn spawns_jsstring() -> JsHashMap<JsString, StructureSpawn> {
Game::spawns().into()
}

/// Get a [`JsHashMap<RawObjectId, StructureObject>`] with all of your owned
/// structures.
/// Get a [`JsHashMap<ObjectId<Structure>, StructureObject>`] with all of your
/// owned structures.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
pub fn structures() -> JsHashMap<RawObjectId, StructureObject> {
pub fn structures() -> JsHashMap<ObjectId<Structure>, StructureObject> {
Game::structures().into()
}

Expand Down
13 changes: 11 additions & 2 deletions src/local/object_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -262,3 +263,11 @@ impl<T> From<u128> for ObjectId<T> {
Self::from_packed(packed)
}
}

impl<T> JsCollectionFromValue for ObjectId<T> {
fn from_value(val: JsValue) -> Self {
let val: JsString = val.unchecked_into();
let val: String = val.into();
val.parse().expect("valid id string")
}
}
Loading