Gamend
The gamend module connects Balaur scripts to a Gamend backend (login, REST endpoints, lobbies and server hooks) with replies delivered once per tick.
Sur cette page
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
awaittokens, 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:
loginandrestonly that, andcall_hook,join,pushandleavealso the socket's handler. - Socket events are a stream and only dispatch.
- Every event map carries a
kind, agamend::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
reconnectingbefore each try, withattempt,waitandreason. Then comesreopened, whoselostnames any topic the server refused, orerroronce 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.
registermakes 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. Ausernameleft out is generated.gamend::session()is the signed-in session, andgamend::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"
urlis the server a shipped game talks to. A new project starts onhttps://gamend.org.local_urlis a Gamend on your machine, asmix dev.startserves it.gamend/targetpicks between the two for a game played from the editor. It is kept in the editor's own settings, so an exported game always readsurl.configure(url)with a URL still wins, for a game that picks its server at run time.