Struct race_api::effect::Effect

source ·
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

source

pub fn count_players(&self) -> usize

Return the number of players, including both the pending and joined.

source

pub fn count_servers(&self) -> usize

Return the number of servers, including both the pending and joined.

source

pub fn init_random_state(&mut self, spec: RandomSpec) -> RandomId

Initialize a random state with random spec, return random id.

source

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.

source

pub fn reveal(&mut self, random_id: RandomId, indexes: Vec<usize>)

Reveal some random items to the public.

source

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.

source

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.

source

pub fn ask<S: Into<String>>(&mut self, player_addr: S) -> DecisionId

Ask a player for a decision, return the new decision id.

source

pub fn release(&mut self, decision_id: DecisionId)

source

pub fn action_timeout<S: Into<String>>(&mut self, player_addr: S, timeout: u64)

Dispatch action timeout event for a player after certain milliseconds.

source

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.

source

pub fn wait_timeout(&mut self, timeout: u64)

Dispatch waiting timeout event after certain milliseconds.

source

pub fn start_game(&mut self)

Start the game.

source

pub fn stop_game(&mut self)

Stop the game.

source

pub fn allow_exit(&mut self, allow_exit: bool)

Set if exiting game is allowed.

source

pub fn settle(&mut self, settle: Settle)

Submit settlements.

source

pub fn transfer(&mut self, slot_id: u8, amount: u64)

Transfer the assets to a recipient slot

source

pub fn __handler_state<S>(&self) -> Swhere S: GameHandler,

Get handler state.

This is an internal function, DO NOT use in game handler.

source

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.

source

pub fn __set_error(&mut self, error: HandleError)

Set error.

This is an internal function, DO NOT use in game handler.

source

pub fn __take_error(&mut self) -> Option<HandleError>

Take error

This is an internal function, DO NOT use in game handler.

Trait Implementations§

source§

impl BorshDeserialize for Effectwhere Option<ActionTimeout>: BorshDeserialize, Option<u64>: BorshDeserialize, bool: BorshDeserialize, u64: BorshDeserialize, RandomId: BorshDeserialize, DecisionId: BorshDeserialize, u16: BorshDeserialize, Vec<Ask>: BorshDeserialize, Vec<Assign>: BorshDeserialize, Vec<Reveal>: BorshDeserialize, Vec<Release>: BorshDeserialize, Vec<RandomSpec>: BorshDeserialize, HashMap<RandomId, HashMap<usize, String>>: BorshDeserialize, HashMap<DecisionId, String>: BorshDeserialize, Vec<Settle>: BorshDeserialize, Option<Vec<u8>>: BorshDeserialize, Option<HandleError>: BorshDeserialize, Vec<Transfer>: BorshDeserialize,

source§

fn deserialize(buf: &mut &[u8]) -> Result<Self, Error>

Deserializes this instance from a given slice of bytes. Updates the buffer to point at the remaining bytes.
source§

fn try_from_slice(v: &[u8]) -> Result<Self, Error>

Deserialize this instance from a slice of bytes.
source§

impl BorshSerialize for Effectwhere Option<ActionTimeout>: BorshSerialize, Option<u64>: BorshSerialize, bool: BorshSerialize, u64: BorshSerialize, RandomId: BorshSerialize, DecisionId: BorshSerialize, u16: BorshSerialize, Vec<Ask>: BorshSerialize, Vec<Assign>: BorshSerialize, Vec<Reveal>: BorshSerialize, Vec<Release>: BorshSerialize, Vec<RandomSpec>: BorshSerialize, HashMap<RandomId, HashMap<usize, String>>: BorshSerialize, HashMap<DecisionId, String>: BorshSerialize, Vec<Settle>: BorshSerialize, Option<Vec<u8>>: BorshSerialize, Option<HandleError>: BorshSerialize, Vec<Transfer>: BorshSerialize,

source§

fn serialize<W: Write>(&self, writer: &mut W) -> Result<(), Error>

source§

fn try_to_vec(&self) -> Result<Vec<u8, Global>, Error>

Serialize this instance into a vector of bytes.
source§

impl Debug for Effect

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Effect

source§

fn default() -> Effect

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.