Skip to content

Commit

Permalink
dev refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
DogLooksGood committed Jan 4, 2024
1 parent 6a66dfe commit adb87dd
Show file tree
Hide file tree
Showing 15 changed files with 197 additions and 239 deletions.
32 changes: 26 additions & 6 deletions api/src/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
error::{Error, HandleError, Result},
event::BridgeEvent,
random::RandomSpec,
types::{DecisionId, RandomId, Settle, Transfer},
types::{DecisionId, GamePlayer, RandomId, Settle, Transfer},
};

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -45,19 +45,37 @@ pub struct ActionTimeout {
pub struct LaunchSubGame {
pub id: usize,
pub bundle_addr: String,
pub players: Vec<GamePlayer>,
pub init_data: Vec<u8>,
pub checkpoint: Vec<u8>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Clone)]
pub struct SubGameJoin {
pub id: usize,
pub players: Vec<GamePlayer>,
}

#[derive(BorshSerialize, BorshDeserialize, Debug, PartialEq, Eq, Clone)]
pub struct SubGameLeave {
pub id: usize,
pub player_ids: Vec<u64>,
}

impl LaunchSubGame {
pub fn try_new<S: BorshSerialize>(
id: usize,
bundle_addr: String,
players: Vec<GamePlayer>,
init_data: S,
checkpoint: S,
) -> Result<Self> {
Ok(Self {
id,
bundle_addr,
players,
init_data: init_data.try_to_vec()?,
checkpoint: checkpoint.try_to_vec()?,
})
}
}
Expand Down Expand Up @@ -306,16 +324,20 @@ impl Effect {
}

/// Launch sub game
pub fn launch_sub_game<S: BorshSerialize>(
pub fn launch_sub_game<D: BorshSerialize, C: BorshSerialize>(
&mut self,
id: usize,
bundle_addr: String,
init_data: S,
players: Vec<GamePlayer>,
init_data: D,
checkpoint: C,
) -> Result<()> {
self.launch_sub_games.push(LaunchSubGame {
id,
bundle_addr,
players,
init_data: init_data.try_to_vec()?,
checkpoint: checkpoint.try_to_vec()?,
});
Ok(())
}
Expand Down Expand Up @@ -414,9 +436,7 @@ mod tests {
curr_random_id: 1,
curr_decision_id: 1,
nodes_count: 4,
asks: vec![Ask {
player_id: 1,
}],
asks: vec![Ask { player_id: 1 }],
assigns: vec![Assign {
player_id: 1,
random_id: 5,
Expand Down
23 changes: 9 additions & 14 deletions api/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,9 @@ pub enum Event {
random_id: RandomId,
},

/// Sync with on-chain account. New players will be added frist to
/// game context and then to game handler (WASM).
/// This event is sent by transactor based on the diff of the account states.
Sync {
new_players: Vec<GamePlayer>,
/// This event is sent when new players joined game.
Join {
players: Vec<GamePlayer>,
},

/// A server left the game.
Expand All @@ -90,10 +88,7 @@ pub enum Event {
},

/// Transactor uses this event as the start for each game.
/// The `access_version` can be used to filter out which players are included.
GameStart {
access_version: u64,
},
GameStart,

/// Timeout when waiting for start
WaitingTimeout,
Expand Down Expand Up @@ -160,18 +155,18 @@ impl std::fmt::Display for Event {
Event::RandomnessReady { random_id } => {
write!(f, "RandomnessReady, random_id: {}", random_id)
}
Event::Sync { new_players } => {
let players = new_players
Event::Join { players } => {
let players = players
.iter()
.map(|p| p.id.to_string())
.collect::<Vec<String>>()
.join(",");

write!(f, "Sync, new_players: [{}]", players)
write!(f, "Join, players: [{}]", players)
}
Event::Leave { player_id } => write!(f, "Leave from {}", player_id),
Event::GameStart { access_version } => {
write!(f, "GameStart, access_version = {}", access_version)
Event::GameStart { } => {
write!(f, "GameStart")
}
Event::WaitingTimeout => write!(f, "WaitTimeout"),
Event::DrawRandomItems {
Expand Down
4 changes: 2 additions & 2 deletions api/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,15 @@ impl ServerJoin {
#[derive(Debug, Default, BorshSerialize, BorshDeserialize, PartialEq, Eq, Copy, Clone)]
pub enum GameStatus {
#[default]
Uninit,
Idle,
Running,
Closed,
}

impl std::fmt::Display for GameStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GameStatus::Uninit => write!(f, "uninit"),
GameStatus::Idle => write!(f, "idle"),
GameStatus::Running => write!(f, "running"),
GameStatus::Closed => write!(f, "closed"),
}
Expand Down
Loading

0 comments on commit adb87dd

Please sign in to comment.