On Race, the game core is written in Rust and compiled to WebAssembly. All it needs is a game handler, a simple state machine, described as below:
trait GameHandler {
init_state(effect: Effect, init_account: InitAccount) -> Result<Self>;
handle_event(&mut self, effect: Effect, event: Event) -> Result<()>;
}
The game handler is initialized with the state of an on-chain account, which stores the game data such as the players, servers and so on. Once the game handler is created, it's ready to receive and handle events.
The game handler should be pure: no randomization nor clock access. The calculation remains the same with the same input. We use Effect to ship all necessary information from execution runtime. Additionally, game handler uses Effect to acquire randomness, assign secrets and reveal hidden knowledge. This model makes it easy to test and simulate.
Here are the context inforamtion provided by effect:
- Event timestamp
- Number of players and servers
- Revealed hidden knowledge
The game progress is driven by handling events, Race provides a list of predefined types of event, with each automatically handled by the protocol. Developers only need to deal with those of interest. Each game has its own specific events which are defined separately in the game handler and will be transferred to CustomEvent.
Here is the list (or table?) of builtin event types:
Custom
: a specific game eventGameStart
: the game is startedGameStart
: the update of on-chain account, with new players and serversGameStart
: a player has leftServerLeave
: a server has leftActionTimeout
: a waiting timeout for player actionWaitingTimeout
: a general waiting timeout used for any purposesOperationTimeout
: a situation where the server is probably disconnectedMask
: an event for randomizationLock
: an event for randomizationShareSecrets
: an event for assignment or revealing a hidden informationRandomnessReady
: indicates the randomness is generatedSecretsReady
: the secrets shared and revealed information accessibleShutdown
: the game is over
- an example to show what events are usually or must be included in a game
- an flowchart to illustrate the above example