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.

Callbacks

create the game-room.

Handle player leaving.

Link to this section Functions

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"})

Shutdowns the game-room gracefully.

Link to this section Callbacks

Specs

create(opts :: term()) :: game_state :: term()

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.

Link to this callback

leave(player_id, game_state)

Specs

leave(player_id :: String.t(), game_state :: term()) ::
  {:ok, game_state :: term()}

Handle player leaving.

We can handle the gamestate, when a player leaves.