Skip to content

Commit

Permalink
Merge pull request #229 from rustyscreeps/object-id
Browse files Browse the repository at this point in the history
Packed, typed object ids
  • Loading branch information
daboross authored Aug 20, 2019
2 parents 0af0e34 + a7f720b commit 2ff73ea
Show file tree
Hide file tree
Showing 9 changed files with 715 additions and 9 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ script:
- cargo web build --target=wasm32-unknown-unknown --verbose
- cargo web build --target=wasm32-unknown-unknown --all-features --verbose
- cargo test --verbose
- cargo web test --verbose --nodejs
27 changes: 27 additions & 0 deletions javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,30 @@ function pos_from_packed(repr) {
});
return pos;
}

function object_id_from_packed(slice) {
// reconstruct string in JS
let res = "";
for (var i = 0; i < slice.length; i++) {
if (i > 0) {
res += slice[i].toString(16).padStart(8, "0");
} else {
res += slice[i].toString(16);
}
}
return res;
}

function object_id_to_packed(id) {
let packed = [0, 0, 0];
if (id.length > 16) {
packed[0] = parseInt(id.slice(0, -16), 16);
}
if (id.length > 8) {
packed[1] = parseInt(id.slice(-16, -8), 16);
}
if (id.length > 0) {
packed[2] = parseInt(id.slice(-8), 16);
}
return packed;
}
49 changes: 45 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//!
//! [`Game`]: http://docs.screeps.com/api/#Game
use crate::{
local::{ObjectId, RawObjectId},
macros::*,
objects::{HasId, RoomObject, SizedRoomObject},
traits::TryInto,
Expand Down Expand Up @@ -111,12 +112,47 @@ pub fn time() -> u32 {
/// If all you want to assume is that something has an ID, use
/// [`get_object_erased`].
///
/// This uses the typed id type, [`ObjectId`]. Note that if you'd rather store
/// an untyped ID, it's free to convert from [`RawObjectId`] to [`ObjectId`].
///
/// # Example
///
/// ```no_run
/// use screeps::{game, prelude::*, Creep, ObjectId};
///
/// // get your id however
/// let id: ObjectId<Creep> = "aaaa".parse().unwrap();
///
/// let creep = game::get_object_typed(id).unwrap();
/// match creep {
/// Some(creep) => println!("creep with id aaaa has name {}", creep.name()),
/// None => println!("no creep with id aaaa! such a surprise!"),
/// }
/// ```
///
/// Or, using `RawObjectId`,
///
/// ```no_run
/// use screeps::{game, prelude::*, Creep, RawObjectId};
///
/// let id: RawObjectId = "bbbb".parse().unwrap();
///
/// let creep = game::get_object_typed::<Creep>(id.into()).unwrap();
/// if let Some(creep) = creep {
/// println!("creep with id bbbb exists, and has name {}", creep.name());
/// }
/// ```
///
/// [http://docs.screeps.com/api/#Game.getObjectById]: http://docs.screeps.com/api/#Game.getObjectById
pub fn get_object_typed<T>(id: &str) -> Result<Option<T>, ConversionError>
pub fn get_object_typed<T>(id: ObjectId<T>) -> Result<Option<T>, ConversionError>
where
T: HasId + SizedRoomObject,
{
js!(return Game.getObjectById(@{id});).try_into()
let array_view = unsafe { id.unsafe_as_uploaded() };
(js! {
return Game.getObjectById(object_id_from_packed(@{array_view}));
})
.try_into()
}

/// See [http://docs.screeps.com/api/#Game.getObjectById]
Expand All @@ -126,9 +162,14 @@ where
///
/// If a more specific type is expected, [`get_object_typed`] can be used.
///
/// The ID passed in must be either an [`ObjectId`], or a [`RawObjectId`]. Both
/// work, and the type of [`ObjectId`] if passed will be ignored.
///
/// [http://docs.screeps.com/api/#Game.getObjectById]: http://docs.screeps.com/api/#Game.getObjectById
pub fn get_object_erased(id: &str) -> Option<RoomObject> {
js_unwrap_ref!(Game.getObjectById(@{id}))
pub fn get_object_erased(id: impl Into<RawObjectId>) -> Option<RoomObject> {
let id = id.into();
let array_view = unsafe { id.unsafe_as_uploaded() };
js_unwrap_ref!(Game.getObjectById(object_id_from_packed(@{array_view})))
}

pub fn notify(message: &str, group_interval: Option<u32>) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub use stdweb::private::ConversionError;
pub use crate::{
constants::*,
js_collections::JsVec,
local::{Position, RoomName, RoomNameParseError},
local::{ObjectId, Position, RawObjectId, RawObjectIdParseError, RoomName, RoomNameParseError},
objects::*,
traits::{FromExpectedType, IntoExpectedType},
};
Expand Down
3 changes: 2 additions & 1 deletion src/local.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Pure-data structures relating to Screeps.
use std::ops::Range;

mod object_id;
mod room_name;
mod room_position;

Expand All @@ -17,4 +18,4 @@ const HALF_WORLD_SIZE: i32 = 128;
/// Valid room name coordinates.
const VALID_ROOM_NAME_COORDINATES: Range<i32> = (-HALF_WORLD_SIZE..HALF_WORLD_SIZE);

pub use self::{room_name::*, room_position::*};
pub use self::{object_id::*, room_name::*, room_position::*};
Loading

0 comments on commit 2ff73ea

Please sign in to comment.