Skip to content

Search

說明文件

Gamend

The gamend module connects Balaur scripts to a Gamend backend (login, REST endpoints, lobbies and server hooks) with replies delivered once per tick.

本頁內容

The balaur_gamend plugin connects scripts to a Gamend backend through the gamend module: login, REST endpoints, lobbies and server hooks. It is on by default; building without the gamend feature drops it.

  • Delivery is the networking contract: all I/O on worker threads, completions entering the simulation once per tick.
  • Dispatched to handler methods or waking await tokens, in arrival order.
  • Handlers never run from an I/O thread. A recorded snapshot replays a whole online session.

The sequential shape

pub class Online {
    node: Node,
    socket: int,
}

impl Online {
    pub async fn init() {
        gamend::configure();   // [gamend] url: gamend.org unless the project names its own
        let device = gamend::Credentials { device_id: engine::device_id() };
        let login = await task::wait(gamend::login(device));
        self.socket = gamend::connect(self.node);
        let hook = await task::wait(gamend::call_hook(self.socket, "arena", "start"));
    }

    pub fn on_gamend_event(e: any) {
        if e["kind"].as_str() == gamend::EVENT_REOPENED {
            log::info("back online");
        }
    }
}
  • One-shot operations wake their returned id: login and rest only that, and call_hook, join, push and leave also the socket's handler.
  • Socket events are a stream and only dispatch.
  • Every event map carries a kind, a gamend::EVENT_* constant: login, rest, reply, error, open, message, reconnecting, reopened, closed. One handler takes them all.

Staying connected

A socket dropped without gamend::close comes back by itself.

  • It waits 1, 2, 4 seconds and on up to 30 between tries, eight tries in all.
  • Each try renews an expired token and joins every topic the game had joined again.
  • The handler hears reconnecting before each try, with attempt, wait and reason. Then comes reopened, whose lost names any topic the server refused, or error once the socket gives up.
  • A call made while it waits fails at once.
  • A channel the server crashes is joined again the same way. One it closes on purpose, a kicked player's lobby, is let go, and a reconnect does not ask for it again.
  • gamend::interrupt(socket) cuts the connection as a network failure would, to try a game's handling of it.
  • A REST call that meets an expired token refreshes it and runs again.

The full function list is in the script modules reference.

Accounts

login and register cover the ways a player gets an account.

let device = gamend::Credentials { device_id: engine::device_id() };
await task::wait(gamend::login(device));

let account = gamend::Credentials { email: "ana@example.com", password: "a long password" };
await task::wait(gamend::register(account));
await task::wait(gamend::login(account));
  • A device id the server has not seen makes an anonymous account.
  • register makes an account with an email and a password and signs it in. The server mails the address a confirmation link, as its web sign-up does. A username left out is generated.
  • gamend::session() is the signed-in session, and gamend::restore(session) signs in from one a previous run kept.

The server

gamend::configure() with no URL reads the project's [gamend] settings.

[gamend]
url = "https://gamend.org"
local_url = "http://localhost:4000"
plugin = "skyrace"
  • url is the server a shipped game talks to. A new project starts on https://gamend.org.
  • local_url is a Gamend on your machine, as mix dev.start serves it.
  • gamend/target picks between the two for a game played from the editor. It is kept in the editor's own settings, so an exported game always reads url.
  • configure(url) with a URL still wins, for a game that picks its server at run time.

確定嗎?