diff --git a/src/misc.mbt b/src/misc.mbt index 08014bc..a000cdf 100644 --- a/src/misc.mbt +++ b/src/misc.mbt @@ -86,8 +86,12 @@ pub fn get_name(peer : Peer) -> Utf8 { ///| /// Get system settings. -pub fn get_settings(peer : Peer) -> Settings { - let raw = @ffi.get_settings(peer.raw) +/// +/// **IMPORTANT:** This is the only function that accepts as input not only [`Peer`] +/// but also [`Me`], which might lead to a state drift if used incorrectly. +/// See [the docs](https://docs.fireflyzero.com/dev/net/) for more info. +pub fn[P : AnyPeer] get_settings(peer : P) -> Settings { + let raw = @ffi.get_settings(peer.to_int()) let code = raw.to_uint16() let language = Language::from_raw(code).unwrap_or_default() let flags = raw >> 16 diff --git a/src/net.mbt b/src/net.mbt index b561018..f20f429 100644 --- a/src/net.mbt +++ b/src/net.mbt @@ -1,15 +1,44 @@ ///| -/// Multi-player peer/player ID. +/// A peer obtained either from `Peers` (`get_peers`) or from `get_me`. +pub trait AnyPeer { + to_int(self : Self) -> Int +} + +///| +/// The peer representing the current device. +/// +/// Can be compared to `Peer` or used to `get_settings`. /// -/// Firefly does not document a maximum number of players, however this SDK -/// (as well as many other Firefly SDKs for other languages) store the list -/// of peers in a 32-bit bitmap. -/// Meaning the `Peer` ID must be lower than 32 (`0x20`) +/// **IMPORTANT:** Using this type may cause state drift between device in multiplayer. +/// See [the docs](https://docs.fireflyzero.com/dev/net/) for more info. +#valtype +struct Me { + raw : Int +} derive(Hash) + +///| +pub impl AnyPeer for Me with to_int(self : Me) -> Int { + self.raw +} + +///| +/// Check if the given `Peer` represents the current device. +pub fn Me::equal(self : Self, peer : Peer) -> Bool { + self.raw == peer.raw +} + +///| +/// Multi-player peer/player ID. #valtype struct Peer { raw : Int } derive(Eq, Compare, Hash) +///| +pub impl AnyPeer for Peer with to_int(self : Peer) -> Int { + self.raw +} + ///| pub impl Show for Peer with to_string(self) -> String { match self.raw { @@ -38,13 +67,6 @@ pub fn Peer::combined() -> Peer { Peer::{ raw: 0xFF } } -///| -/// Get the `Peer` representing the local device. -#inline -pub fn Peer::me() -> Peer { - get_me() -} - ///| /// `Peer` type default value, which is `Peer::combined`. #inline @@ -106,8 +128,8 @@ test "peers to array" { ///| /// Get the `Peer` representing the local device. -pub fn get_me() -> Peer { - Peer::{ raw: @ffi.get_me() } +pub fn get_me() -> Me { + Me::{ raw: @ffi.get_me() } } ///|