Skip to content

Commit

Permalink
first swing at a happy medium (and not being a breaking change)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanemadden committed Jan 13, 2024
1 parent 142341d commit 066e5fb
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 42 deletions.
13 changes: 0 additions & 13 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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<String, _>` now return
`JsHashMap<JsString, _>`
- `raw_memory::segments` now returns `JsHashMap<u8, JsString>`
- `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)
===================

Expand Down
43 changes: 39 additions & 4 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,30 +83,57 @@ pub fn construction_sites() -> JsHashMap<RawObjectId, ConstructionSite> {
Game::construction_sites().into()
}

/// Get a [`JsHashMap<String, Creep>`] 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<String, Creep> {
Game::creeps().into()
}

/// Get a [`JsHashMap<JsString, Creep>`] 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<JsString, Creep> {
pub fn creeps_jsstring() -> JsHashMap<JsString, Creep> {
Game::creeps().into()
}

/// Get a [`JsHashMap<String, Flag>`] with all of your flags, which has flag
/// names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags)
pub fn flags() -> JsHashMap<String, Flag> {
Game::flags().into()
}

/// Get a [`JsHashMap<JsString, Flag>`] with all of your flags, which has flag
/// names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.flags)
pub fn flags() -> JsHashMap<JsString, Flag> {
pub fn flags_jsstring() -> JsHashMap<JsString, Flag> {
Game::flags().into()
}

/// Get a [`JsHashMap<String, AccountPowerCreep>`] 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<String, AccountPowerCreep> {
Game::power_creeps().into()
}

/// Get a [`JsHashMap<JsString, AccountPowerCreep>`] 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<JsString, AccountPowerCreep> {
pub fn power_creeps_jsstring() -> JsHashMap<JsString, AccountPowerCreep> {
Game::power_creeps().into()
}

Expand All @@ -126,11 +153,19 @@ pub fn rooms() -> JsHashMap<RoomName, Room> {
Game::rooms().into()
}

/// Get a [`JsHashMap<String, StructureSpawn>`] with all of your spawns, which
/// has spawn names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
pub fn spawns() -> JsHashMap<String, StructureSpawn> {
Game::spawns().into()
}

/// Get a [`JsHashMap<JsString, StructureSpawn>`] with all of your spawns, which
/// has spawn names as keys.
///
/// [Screeps documentation](https://docs.screeps.com/api/#Game.spawns)
pub fn spawns() -> JsHashMap<JsString, StructureSpawn> {
pub fn spawns_jsstring() -> JsHashMap<JsString, StructureSpawn> {
Game::spawns().into()
}

Expand Down
10 changes: 9 additions & 1 deletion src/game/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,19 @@ pub fn outgoing_transactions() -> Vec<Transaction> {
.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<String, MyOrder> {
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<JsString, MyOrder> {
pub fn orders_jsstring() -> JsHashMap<JsString, MyOrder> {
Market::orders().into()
}

Expand Down
24 changes: 18 additions & 6 deletions src/game/shard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
14 changes: 14 additions & 0 deletions src/js_collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
11 changes: 9 additions & 2 deletions src/objects/impls/creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
Expand Down
3 changes: 1 addition & 2 deletions src/objects/impls/owned_structure.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use js_sys::JsString;
use wasm_bindgen::prelude::*;

use crate::{
Expand Down Expand Up @@ -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;
}
27 changes: 22 additions & 5 deletions src/objects/impls/power_creep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
///
Expand Down
8 changes: 4 additions & 4 deletions src/objects/impls/structure_controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use js_sys::{Date, JsString};
use js_sys::Date;
use wasm_bindgen::prelude::*;

use crate::{
Expand Down Expand Up @@ -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)]
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/objects/impls/structure_portal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 11 additions & 2 deletions src/raw_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,21 @@ extern "C" {
fn set_public_segments(segment_ids: &Array);
}

/// Get a [`JsHashMap<u8, JsString>`] with all of the segments requested on
/// Get a [`JsHashMap<u8, String>`] 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<u8, JsString> {
pub fn segments() -> JsHashMap<u8, String> {
RawMemory::segments().into()
}

/// Get a [`JsHashMap<u8, String>`] 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<u8, JsString> {
RawMemory::segments().into()
}

Expand Down
7 changes: 6 additions & 1 deletion src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 066e5fb

Please sign in to comment.