pub struct Effect {Show 22 fields
pub action_timeout: Option<ActionTimeout>,
pub wait_timeout: Option<u64>,
pub start_game: bool,
pub stop_game: bool,
pub cancel_dispatch: bool,
pub timestamp: u64,
pub curr_random_id: RandomId,
pub curr_decision_id: DecisionId,
pub players_count: u16,
pub servers_count: u16,
pub asks: Vec<Ask>,
pub assigns: Vec<Assign>,
pub reveals: Vec<Reveal>,
pub releases: Vec<Release>,
pub init_random_states: Vec<RandomSpec>,
pub revealed: HashMap<RandomId, HashMap<usize, String>>,
pub answered: HashMap<DecisionId, String>,
pub settles: Vec<Settle>,
pub handler_state: Option<Vec<u8>>,
pub error: Option<HandleError>,
pub allow_exit: bool,
pub transfers: Vec<Transfer>,
}Expand description
An effect used in game handler provides reading and mutating to the game context. An effect can be created from game context, manipulated by game handler and applied after event processing.
Num of Players and Servers
Effect::count_players and Effect::count_servers return the total number of
players and servers, respectively. The number includes those with pending status.
These functions are useful when detecting if there’s enough players/servers for
a game to start.
Randomness
To create a randomness, use Effect::init_random_state with a RandomSpec.
use race_api::random::RandomSpec;
let mut effect = Effect::default();
let random_spec = RandomSpec::deck_of_cards();
let random_id = effect.init_random_state(random_spec);To assign some items of the randomness to a specific player, use Effect::assign.
It makes those items visible only to this player.
let mut effect = Effect::default();
effect.assign(1 /* random_id */, "Alice", vec![0, 1, 2] /* indexes */);To reveal some items to the public, use Effect::reveal.
It makes those items visible to everyone, including servers.
let mut effect = Effect::default();
effect.reveal(1 /* random_id */, vec![0, 1, 2] /* indexes */);Decisions
To prompt a player for an hidden, immutable decision, use [Effect::prompt].
let mut effect = Effect::default();
let decision_id = effect.ask("Alice");To reveal the answer, use [Effect::reveal_answer].
let mut effect = Effect::default();
effect.release(1 /* decision_id */);Timeouts
Two types of timeout event can be dispatched: action_timeout and
wait_timeout.
-
Action Timeout: Represent a player doesn’t act in time, a player address is required in this case.
-
Wait Timeout: Represent a general waiting. It’s useful when you want to start a game in a certain timeout, regardless of how many players are available.
Settle
Add settlements with Effect::settle.
use race_api::types::Settle;
let mut effect = Effect::default();
// Increase assets
effect.settle(Settle::add("Alice", 100));
// Decrease assets
effect.settle(Settle::sub("Bob", 200));
// Remove player from this game, its assets will be paid out
effect.settle(Settle::eject("Charlie"));Fields§
§action_timeout: Option<ActionTimeout>§wait_timeout: Option<u64>§start_game: bool§stop_game: bool§cancel_dispatch: bool§timestamp: u64§curr_random_id: RandomId§curr_decision_id: DecisionId§players_count: u16§servers_count: u16§asks: Vec<Ask>§assigns: Vec<Assign>§reveals: Vec<Reveal>§releases: Vec<Release>§init_random_states: Vec<RandomSpec>§revealed: HashMap<RandomId, HashMap<usize, String>>§answered: HashMap<DecisionId, String>§settles: Vec<Settle>§handler_state: Option<Vec<u8>>§error: Option<HandleError>§allow_exit: bool§transfers: Vec<Transfer>Implementations§
source§impl Effect
impl Effect
sourcepub fn count_players(&self) -> usize
pub fn count_players(&self) -> usize
Return the number of players, including both the pending and joined.
sourcepub fn count_servers(&self) -> usize
pub fn count_servers(&self) -> usize
Return the number of servers, including both the pending and joined.
sourcepub fn init_random_state(&mut self, spec: RandomSpec) -> RandomId
pub fn init_random_state(&mut self, spec: RandomSpec) -> RandomId
Initialize a random state with random spec, return random id.
sourcepub fn assign<S: Into<String>>(
&mut self,
random_id: RandomId,
player_addr: S,
indexes: Vec<usize>
)
pub fn assign<S: Into<String>>( &mut self, random_id: RandomId, player_addr: S, indexes: Vec<usize> )
Assign some random items to a specific player.
sourcepub fn reveal(&mut self, random_id: RandomId, indexes: Vec<usize>)
pub fn reveal(&mut self, random_id: RandomId, indexes: Vec<usize>)
Reveal some random items to the public.
sourcepub fn get_revealed(
&self,
random_id: RandomId
) -> Result<&HashMap<usize, String>>
pub fn get_revealed( &self, random_id: RandomId ) -> Result<&HashMap<usize, String>>
Return the revealed random items by id.
Return Error::RandomnessNotRevealed when invalid random id is given.
sourcepub fn get_answer(&self, decision_id: DecisionId) -> Result<&str>
pub fn get_answer(&self, decision_id: DecisionId) -> Result<&str>
Return the answer of a decision by id.
Return Error::AnswerNotAvailable when invalid decision id
is given or the answer is not ready.
sourcepub fn ask<S: Into<String>>(&mut self, player_addr: S) -> DecisionId
pub fn ask<S: Into<String>>(&mut self, player_addr: S) -> DecisionId
Ask a player for a decision, return the new decision id.
pub fn release(&mut self, decision_id: DecisionId)
sourcepub fn action_timeout<S: Into<String>>(&mut self, player_addr: S, timeout: u64)
pub fn action_timeout<S: Into<String>>(&mut self, player_addr: S, timeout: u64)
Dispatch action timeout event for a player after certain milliseconds.
sourcepub fn timestamp(&self) -> u64
pub fn timestamp(&self) -> u64
Return current timestamp.
The event handling must be pure, so it’s not allowed to use timestamp from system API.
sourcepub fn wait_timeout(&mut self, timeout: u64)
pub fn wait_timeout(&mut self, timeout: u64)
Dispatch waiting timeout event after certain milliseconds.
sourcepub fn start_game(&mut self)
pub fn start_game(&mut self)
Start the game.
sourcepub fn allow_exit(&mut self, allow_exit: bool)
pub fn allow_exit(&mut self, allow_exit: bool)
Set if exiting game is allowed.
sourcepub fn __handler_state<S>(&self) -> Swhere
S: GameHandler,
pub fn __handler_state<S>(&self) -> Swhere S: GameHandler,
Get handler state.
This is an internal function, DO NOT use in game handler.
sourcepub fn __set_handler_state<S: BorshSerialize>(&mut self, handler_state: S)
pub fn __set_handler_state<S: BorshSerialize>(&mut self, handler_state: S)
Set handler state.
This is an internal function, DO NOT use in game handler.
sourcepub fn __set_error(&mut self, error: HandleError)
pub fn __set_error(&mut self, error: HandleError)
Set error.
This is an internal function, DO NOT use in game handler.
sourcepub fn __take_error(&mut self) -> Option<HandleError>
pub fn __take_error(&mut self) -> Option<HandleError>
Take error
This is an internal function, DO NOT use in game handler.