asobi_lua_match (asobi v0.75.1)

View Source

An asobi_match implementation that delegates all callbacks to Lua scripts via Luerl.

Game developers write their match logic in Lua. This module bridges the asobi_match behaviour to Luerl function calls.

Configuration

In game_modes config, use {lua, ScriptPath} instead of a module name:

{asobi, [
    {game_modes, #{
        ~"arena" => #{module => {lua, "priv/lua/match.lua"}, match_size => 4}
    }}
]}

The Lua script must define these functions:

function init(config)        -- return initial game state table
function join(player_id, state, ctx)  -- ctx is the client join context; return updated state
function leave(player_id, state)      -- return updated state
function handle_input(player_id, input, state) -- return updated state
function tick(state)         -- return state, or state + finished flag
function get_state(player_id, state)  -- return state visible to player
-- Optional:
function vote_requested(state)        -- return vote config or nil
function vote_resolved(template, result, state) -- return updated state
function phases(config)                      -- return list of phase definitions
function on_phase_started(phase_name, state) -- return updated state
function on_phase_ended(phase_name, state)   -- return updated state

Summary

Functions

get_state/2

-spec get_state(binary(), map()) -> map().

handle_input/3

-spec handle_input(binary(), map(), map()) -> {ok, map()}.

init(Config)

-spec init(map()) -> {ok, map()}.

join(PlayerId, State)

-spec join(binary(), map()) -> {ok, map()} | {error, term()}.

join/3

-spec join(binary(), map(), map()) -> {ok, map()} | {error, term()}.

Join carrying the client-supplied join context (asobi's optional join/3).

The context is passed to the Lua join as a third argument, so a script declaring function join(player_id, state) keeps working unchanged - Lua discards extra arguments - and one declaring function join(player_id, state, ctx) receives it.

Without this, ctx would reach asobi, find no join/3 on the Lua bridge, fall back to join/2 and be silently discarded - so every Lua game would be unable to implement join codes, invites or passwords despite the docs saying otherwise.

leave/2

-spec leave(binary(), map()) -> {ok, map()}.

on_phase_ended/2

-spec on_phase_ended(binary(), map()) -> {ok, map()}.

on_phase_started/2

-spec on_phase_started(binary(), map()) -> {ok, map()}.

phases/1

-spec phases(map()) -> [map()].

tick(State0)

-spec tick(map()) -> {ok, map()} | {finished, map(), map()}.

vote_requested/1

-spec vote_requested(map()) -> {ok, map()} | none.

vote_resolved/3

-spec vote_resolved(binary(), map(), map()) -> {ok, map()}.