Garuda v0.2.5 Garuda.GameRoom behaviour
Behaviours and functions for implementing core game-logic rooms.
Game-rooms are under-the-hood genservers, with certain extra gamey properties. We can write our gameplay code in game-room, and game-channel act as event handler. Events from game-channel can be then route to corresponding game-room functions.
Using GameRoom
defmodule TictactoePhx.TictactoeRoom do
use Garuda.GameRoom, expiry: 120_000
def create(_opts) do
# Return the initial game state.
gamestate
end
def leave(player_id, game_state) do
# handle player leaving.
{:ok, gamestate}
end
end
Options
- expiry - game-room will shutdown itself after given time(ms). Default 3hr
- reconnection_timeout - Time game-room will wait for a player who left non-explicitly. Default 20s
Link to this section Summary
Functions
Returns the corresponding game-channel of the game-room.
Shutdowns the game-room gracefully.
Link to this section Functions
get_channel()
Returns the corresponding game-channel of the game-room.
We can broadcast to game-channel from game-room itself like,
DingoWeb.Endpoint.broadcast!(get_channel(), "line_counts", %{"msg" => "heelp"})
shutdown()
Shutdowns the game-room gracefully.
Link to this section Callbacks
create(opts)
Specs
create the game-room.
We can setup the inital gamestate by returning game_state, where game_state is any erlang term.
opts
is a keyword list containing details about room and player.
ex: [room_id: "bingo:e6cf6669", player_id: "bingo_anon_759", max_players: 2]
Note: create
is called only once.
leave(player_id, game_state)
Specs
Handle player leaving.
We can handle the gamestate, when a player leaves.