Skip to content

Commit

Permalink
Merge branch 'main' into more_position_math
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Mar 16, 2024
2 parents 4efc86a + 2b49b64 commit b172198
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 10 deletions.
5 changes: 4 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 @@ -30,11 +32,12 @@ Unreleased
which returns a `Result<&'static [ResourceType], StoreObjectConversionError>`
- Add missing `StoreObject::Reactor` to the `seasonal-season-5` feature
- Implement `Serialize` and `Deserialize` for `RoomStatus`
- Add function `JsHashMap::entries`
- Add `Position::get_manhattan_range_to`

### 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
25 changes: 25 additions & 0 deletions src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ where
}
}

impl<K, V> JsHashMap<K, V>
where
K: JsCollectionFromValue,
V: JsCollectionFromValue,
{
pub fn entries(&self) -> impl Iterator<Item = (K, V)> {
let array = Object::entries(self.map.unchecked_ref());

OwnedArrayIter::new(array)
}
}

impl<K, V> JsHashMap<K, V>
where
K: JsCollectionIntoValue,
Expand Down Expand Up @@ -307,3 +319,16 @@ impl JsCollectionFromValue for u8 {
}
}
}

impl<K, V> JsCollectionFromValue for (K, V)
where
K: JsCollectionFromValue,
V: JsCollectionFromValue,
{
fn from_value(val: JsValue) -> Self {
let val: &Array = val.dyn_ref().expect("expected tuple of length 2");
let k = K::from_value(val.get(0));
let v = V::from_value(val.get(1));
(k, v)
}
}
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")
}
}

0 comments on commit b172198

Please sign in to comment.