diff --git a/CHANGELOG.md b/CHANGELOG.md index 1870fa5a..ebfc3ff0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,19 +1,6 @@ Unreleased ========== -### Breaking: - -- Replace a number of conversions from `JsString` to `String` that are happening internally - within the crate, allowing either explicit conversion in user code or keeping the string in - JavaScript if preferred - - All functions in `game` and `game::market` modules returning `JsHashMap` now return - `JsHashMap` - - `raw_memory::segments` now returns `JsHashMap` - - `game::shard` getters returning `String` now return `JsString` - - `SharedCreepProperties::name` now returns `JsString` instead of `String` - - `StructurePortal::shard` now returns `JsString` instead of `String` - - `Owner`, `Reservation`, and `Sign` getters returning `String` now return `JsString` - 0.20.1 (2024-01-09) =================== diff --git a/src/game.rs b/src/game.rs index 5ba9132a..a6e97d52 100644 --- a/src/game.rs +++ b/src/game.rs @@ -83,6 +83,17 @@ pub fn construction_sites() -> JsHashMap { Game::construction_sites().into() } +/// Get a [`JsHashMap`] with all of your creeps, which has creep +/// names as keys. +/// +/// Note that newly spawned creeps are immediately added when spawned, but will +/// not have an id until the following tick. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#Game.creeps) +pub fn creeps() -> JsHashMap { + Game::creeps().into() +} + /// Get a [`JsHashMap`] with all of your creeps, which has /// creep names as keys. /// @@ -90,23 +101,39 @@ pub fn construction_sites() -> JsHashMap { /// not have an id until the following tick. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.creeps) -pub fn creeps() -> JsHashMap { +pub fn creeps_jsstring() -> JsHashMap { Game::creeps().into() } +/// Get a [`JsHashMap`] with all of your flags, which has flag +/// names as keys. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags) +pub fn flags() -> JsHashMap { + Game::flags().into() +} + /// Get a [`JsHashMap`] with all of your flags, which has flag /// names as keys. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.flags) -pub fn flags() -> JsHashMap { +pub fn flags_jsstring() -> JsHashMap { Game::flags().into() } +/// Get a [`JsHashMap`] with all of your power +/// creeps, which has power creep names as keys. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#Game.powerCreeps) +pub fn power_creeps() -> JsHashMap { + Game::power_creeps().into() +} + /// Get a [`JsHashMap`] with all of your power /// creeps, which has power creep names as keys. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.powerCreeps) -pub fn power_creeps() -> JsHashMap { +pub fn power_creeps_jsstring() -> JsHashMap { Game::power_creeps().into() } @@ -126,11 +153,19 @@ pub fn rooms() -> JsHashMap { Game::rooms().into() } +/// Get a [`JsHashMap`] with all of your spawns, which +/// has spawn names as keys. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns) +pub fn spawns() -> JsHashMap { + Game::spawns().into() +} + /// Get a [`JsHashMap`] with all of your spawns, which /// has spawn names as keys. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns) -pub fn spawns() -> JsHashMap { +pub fn spawns_jsstring() -> JsHashMap { Game::spawns().into() } diff --git a/src/game/market.rs b/src/game/market.rs index b863d2ea..bdaae093 100644 --- a/src/game/market.rs +++ b/src/game/market.rs @@ -82,11 +82,19 @@ pub fn outgoing_transactions() -> Vec { .collect() } +/// An [`Object`] with your current buy and sell orders on the market, with +/// order ID [`String`] keys and [`MyOrder`] values. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#Game.market.orders) +pub fn orders() -> JsHashMap { + Market::orders().into() +} + /// An [`Object`] with your current buy and sell orders on the market, with /// order ID [`JsString`] keys and [`MyOrder`] values. /// /// [Screeps documentation](https://docs.screeps.com/api/#Game.market.orders) -pub fn orders() -> JsHashMap { +pub fn orders_jsstring() -> JsHashMap { Market::orders().into() } diff --git a/src/game/shard.rs b/src/game/shard.rs index b5cd843b..5b3822d9 100644 --- a/src/game/shard.rs +++ b/src/game/shard.rs @@ -9,15 +9,27 @@ extern "C" { #[wasm_bindgen(js_name = "shard")] type Shard; - /// Current shard name. #[wasm_bindgen(js_namespace = ["Game"], js_class = "shard", static_method_of = Shard, getter, js_name = name)] - pub fn name() -> JsString; + fn name() -> JsString; - /// Shard type. Currently always "normal". #[wasm_bindgen(js_namespace = ["Game"], js_class = "shard", static_method_of = Shard, getter, js_name = type)] - pub fn shard_type() -> JsString; + fn shard_type() -> JsString; - /// Flag for if this is a public test server or not. #[wasm_bindgen(js_namespace = ["Game"], js_class = "shard", static_method_of = Shard, getter, js_name = ptr)] - pub fn ptr() -> bool; + fn ptr() -> bool; +} + +/// Current shard name. +pub fn name() -> String { + Shard::name().into() +} + +/// Shard type. Currently always "normal". +pub fn shard_type() -> String { + Shard::shard_type().into() +} + +/// Flag for if this is a public test server or not. +pub fn ptr() -> bool { + Shard::ptr() } diff --git a/src/js_collections.rs b/src/js_collections.rs index 27a828fd..5357ef9c 100644 --- a/src/js_collections.rs +++ b/src/js_collections.rs @@ -283,6 +283,20 @@ impl JsCollectionFromValue for JsString { } } +impl JsCollectionIntoValue for String { + fn into_value(self) -> JsValue { + self.into() + } +} + +impl JsCollectionFromValue for String { + fn from_value(val: JsValue) -> String { + let val: JsString = val.unchecked_into(); + + val.into() + } +} + impl JsCollectionIntoValue for u8 { fn into_value(self) -> JsValue { JsValue::from_f64(self as f64) diff --git a/src/lib.rs b/src/lib.rs index 35a68c5f..82df1c35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,7 +128,7 @@ pub use crate::{constants::*, enums::*, js_collections::*, local::*, objects::*, /// use js_sys::{JsString, Reflect}; /// use screeps::{game, prelude::*, Creep}; /// -/// let c = game::creeps().get("Bob".into()).unwrap(); +/// let c = game::creeps().get(String::from("Bob")).unwrap(); /// /// // `HasId` trait brought in from prelude /// let id = c.try_id().unwrap(); diff --git a/src/objects/impls/creep.rs b/src/objects/impls/creep.rs index f473ca98..e498dae9 100644 --- a/src/objects/impls/creep.rs +++ b/src/objects/impls/creep.rs @@ -51,7 +51,10 @@ extern "C" { fn my_internal(this: &Creep) -> bool; #[wasm_bindgen(structural, method, getter = name)] - fn name_internal(this: &Creep) -> JsString; + fn name_internal(this: &Creep) -> String; + + #[wasm_bindgen(structural, method, getter = name)] + fn name_jsstring_internal(this: &Creep) -> JsString; #[wasm_bindgen(structural, method, getter = owner)] fn owner_internal(this: &Creep) -> Owner; @@ -559,10 +562,14 @@ impl SharedCreepProperties for Creep { self.my() } - fn name(&self) -> JsString { + fn name(&self) -> String { self.name_internal() } + fn name_jsstring(&self) -> JsString { + self.name_jsstring_internal() + } + fn owner(&self) -> Owner { self.owner() } diff --git a/src/objects/impls/owned_structure.rs b/src/objects/impls/owned_structure.rs index d5d0638a..6114fdba 100644 --- a/src/objects/impls/owned_structure.rs +++ b/src/objects/impls/owned_structure.rs @@ -1,4 +1,3 @@ -use js_sys::JsString; use wasm_bindgen::prelude::*; use crate::{ @@ -65,5 +64,5 @@ extern "C" { /// The name of the player that owns this object. #[wasm_bindgen(method, getter = username)] - pub fn username(this: &Owner) -> JsString; + pub fn username(this: &Owner) -> String; } diff --git a/src/objects/impls/power_creep.rs b/src/objects/impls/power_creep.rs index cf6f5e44..0d431007 100644 --- a/src/objects/impls/power_creep.rs +++ b/src/objects/impls/power_creep.rs @@ -51,7 +51,10 @@ extern "C" { fn my_internal(this: &PowerCreep) -> bool; #[wasm_bindgen(method, getter = name)] - fn name_internal(this: &PowerCreep) -> JsString; + fn name_internal(this: &PowerCreep) -> String; + + #[wasm_bindgen(method, getter = name)] + fn name_jsstring_internal(this: &PowerCreep) -> JsString; #[wasm_bindgen(method, getter = owner)] fn owner_internal(this: &PowerCreep) -> Owner; @@ -360,10 +363,14 @@ impl SharedCreepProperties for PowerCreep { self.my() } - fn name(&self) -> JsString { + fn name(&self) -> String { self.name_internal() } + fn name_jsstring(&self) -> JsString { + self.name_jsstring_internal() + } + fn owner(&self) -> Owner { self.owner() } @@ -483,7 +490,10 @@ extern "C" { fn level_internal(this: &AccountPowerCreep) -> u32; #[wasm_bindgen(method, getter = name)] - fn name_internal(this: &AccountPowerCreep) -> JsString; + fn name_internal(this: &AccountPowerCreep) -> String; + + #[wasm_bindgen(method, getter = name)] + fn name_jsstring_internal(this: &AccountPowerCreep) -> JsString; #[wasm_bindgen(method, getter = powers)] fn powers_internal(this: &AccountPowerCreep) -> Object; @@ -533,13 +543,20 @@ impl AccountPowerCreep { self.level_internal() } - /// Name of the power creep. + /// The power creep's name as an owned reference to a [`String`]. /// /// [Screeps documentation](https://docs.screeps.com/api/#PowerCreep.name) - pub fn name(&self) -> JsString { + pub fn name(&self) -> String { self.name_internal() } + /// The power creep's name as a [`JsString`]. + /// + /// [Screeps documentation](https://docs.screeps.com/api/#PowerCreep.name) + pub fn name_jsstring(&self) -> JsString { + self.name_jsstring_internal() + } + /// The levels of this power creep's abilities, with [`PowerType`] keys and /// values containing power level and cooldown. /// diff --git a/src/objects/impls/structure_controller.rs b/src/objects/impls/structure_controller.rs index 8c87f9e7..f7bb9550 100644 --- a/src/objects/impls/structure_controller.rs +++ b/src/objects/impls/structure_controller.rs @@ -1,4 +1,4 @@ -use js_sys::{Date, JsString}; +use js_sys::Date; use wasm_bindgen::prelude::*; use crate::{ @@ -132,7 +132,7 @@ extern "C" { /// The name of the player that has reserved this controller. #[wasm_bindgen(method, getter)] - pub fn username(this: &Reservation) -> JsString; + pub fn username(this: &Reservation) -> String; /// The number of ticks until the reservation expires. #[wasm_bindgen(method, getter = ticksToEnd)] @@ -149,11 +149,11 @@ extern "C" { /// The name of the player that has reserved this controller. #[wasm_bindgen(method, getter)] - pub fn username(this: &Sign) -> JsString; + pub fn username(this: &Sign) -> String; /// The text of the sign on this controller. #[wasm_bindgen(method, getter)] - pub fn text(this: &Sign) -> JsString; + pub fn text(this: &Sign) -> String; /// The tick when this sign was written. #[wasm_bindgen(method, getter)] diff --git a/src/objects/impls/structure_portal.rs b/src/objects/impls/structure_portal.rs index 7b56de32..bf1b9082 100644 --- a/src/objects/impls/structure_portal.rs +++ b/src/objects/impls/structure_portal.rs @@ -62,7 +62,7 @@ extern "C" { fn room_internal(this: &InterShardPortalDestination) -> JsString; #[wasm_bindgen(method, getter)] - pub fn shard(this: &InterShardPortalDestination) -> JsString; + pub fn shard(this: &InterShardPortalDestination) -> String; } impl InterShardPortalDestination { diff --git a/src/raw_memory.rs b/src/raw_memory.rs index 4641d421..12a1c947 100644 --- a/src/raw_memory.rs +++ b/src/raw_memory.rs @@ -46,12 +46,21 @@ extern "C" { fn set_public_segments(segment_ids: &Array); } -/// Get a [`JsHashMap`] with all of the segments requested on +/// Get a [`JsHashMap`] with all of the segments requested on /// the previous tick, with segment numbers as keys and segment data in /// [`JsString`] form as values. /// /// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.segments) -pub fn segments() -> JsHashMap { +pub fn segments() -> JsHashMap { + RawMemory::segments().into() +} + +/// Get a [`JsHashMap`] with all of the segments requested on +/// the previous tick, with segment numbers as keys and segment data in +/// [`JsString`] form as values. +/// +/// [Screeps documentation](https://docs.screeps.com/api/#RawMemory.segments) +pub fn segments_jsstring() -> JsHashMap { RawMemory::segments().into() } diff --git a/src/traits.rs b/src/traits.rs index c735d325..b125a0d9 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -205,10 +205,15 @@ pub trait SharedCreepProperties { /// Whether this creep is owned by the player. fn my(&self) -> bool; + /// The creep's name as an owned reference to a [`String`]. + /// + /// [Screeps documentation](https://docs.screeps.com/api/#Creep.name) + fn name(&self) -> String; + /// The creep's name as a [`JsString`]. /// /// [Screeps documentation](https://docs.screeps.com/api/#Creep.name) - fn name(&self) -> JsString; + fn name_jsstring(&self) -> JsString; /// The [`Owner`] of this creep that contains the owner's username. fn owner(&self) -> Owner;