-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
735e864
commit 67dbf4b
Showing
12 changed files
with
211 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use crate::types::PlayerJoin; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
use race_api::event::{Event, Message}; | ||
use race_api::types::ServerJoin; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Display; | ||
|
||
use super::TxState; | ||
|
||
#[derive(Default, Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
pub struct BroadcastSync { | ||
pub new_players: Vec<PlayerJoin>, | ||
pub new_servers: Vec<ServerJoin>, | ||
pub transactor_addr: String, | ||
pub access_version: u64, | ||
} | ||
|
||
impl BroadcastSync { | ||
pub fn merge(&mut self, other: &Self) { | ||
self.new_players.append(&mut other.new_players.clone()); | ||
self.new_servers.append(&mut other.new_servers.clone()); | ||
self.access_version = u64::max(self.access_version, other.access_version); | ||
self.transactor_addr = other.transactor_addr.clone(); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
pub enum BroadcastFrame { | ||
// Game event | ||
Event { | ||
game_addr: String, | ||
event: Event, | ||
timestamp: u64, | ||
is_history: bool, | ||
}, | ||
// Arbitrary message | ||
Message { | ||
game_addr: String, | ||
message: Message, | ||
}, | ||
// Transaction state updates | ||
TxState { | ||
tx_state: TxState, | ||
}, | ||
// Node state updates | ||
Sync { | ||
sync: BroadcastSync, | ||
}, | ||
} | ||
|
||
impl Display for BroadcastFrame { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
BroadcastFrame::Event { event, .. } => { | ||
write!(f, "BroadcastFrame::Event: {}", event) | ||
} | ||
BroadcastFrame::Message { message, .. } => { | ||
write!(f, "BroadcastFrame::Message: {}", message.sender) | ||
} | ||
BroadcastFrame::TxState { tx_state } => { | ||
write!(f, "BroadcastFrame::TxState: {:?}", tx_state) | ||
} | ||
BroadcastFrame::Sync { sync } => { | ||
write!(f, "BroadcastFrame::Sync: access_version {}", sync.access_version) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use crate::types::PlayerJoin; | ||
use borsh::{BorshDeserialize, BorshSerialize}; | ||
#[cfg(feature = "serde")] | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
pub struct ConfirmingPlayer { | ||
id: u64, | ||
addr: String, | ||
position: u16, | ||
balance: u64, | ||
} | ||
|
||
impl From<PlayerJoin> for ConfirmingPlayer { | ||
fn from(value: PlayerJoin) -> Self { | ||
Self { | ||
id: value.access_version, | ||
addr: value.addr, | ||
position: value.position, | ||
balance: value.balance, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, Eq, BorshDeserialize, BorshSerialize)] | ||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] | ||
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] | ||
pub enum TxState { | ||
PlayerConfirming { | ||
confirm_players: Vec<ConfirmingPlayer>, | ||
access_version: u64, | ||
}, | ||
|
||
PlayerConfirmingFailed(u64), | ||
|
||
SettleSucceed { | ||
settle_version: u64, | ||
signature: Option<String>, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.