Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/misc.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 36 additions & 14 deletions src/net.mbt
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() }
}

///|
Expand Down