ECMAScript API

Volity clients that use ECMAScript (such as Gamut and Friv) need to provide several ES functions, objects and methods that UI files can call upon.

The API is described by a version number. (The UI can read this version number from info.version -- see below. This may allow you to write UI code that runs on old interpreters.)

The UI should specify the API version (or versions) it requires in metadata. This allows the interpreter to sort out which UIs will run upon it.

The version number follows the version matching definition. A backwards-compatible change (such as adding a new info field or API function) will be noted by incrementing the minor version number. A compatibility-breaking change (like removing or changing the behavior of an API function) will be noted by incrementing the major version number.

This page describes API version 3.2.

Functions

rpc ( methodName [ , arg , ... ] )
Sends the RPC methodName (a string) to the table's referee, with the given argument list.
localize ( token [ , token, ... ] )
Given one or more token strings, return a string localized to the client's current language. (The translations are provided by the client and by the UI file's own localization files.)
message ( token [ , token, ... ] )
Given one or more token strings, print the localized string in the client's message stream.
literalmessage ( message )
Print the message in the client's message stream, without trying to translate it. This is a bad idea as a rule, but the rule has too many exceptions (like debugging) to not support this.
audio ( url [ , alt ] )
Create an audio object with the given URL and (optional) alternate text label. See below for the behavior of audio objects.
seatmark ( ... )
Adjust the marks which annotate the client's seating display. (The client may not be able to display such marks, in which case this function will do nothing.) The function takes various kinds of arguments: see below.

Calling seatmark()

A seat can be annotated with one (no more than one at a time) of the following marks. Each mark has a (string) name, which should be taken as a general guide to the mark's use.

turn
it is this seat's turn.
win
this seat has won, or is winning.
first
this seat currently plays first, or leads the play in some way.
other
this seat is significant for some other reason.

A call to the seatmark() function defines a new set of marks. (The state of the marks depend solely on the last seatmark() call; calls are not cumulative.) Marks are not automatically reset at the beginning or end of a game. They are reset (all unmarked) when the UI is loaded in or reloaded.

Objects

volity

This is a blank object. The UI must add properties to it; these are used as function handlers for the volity RPC requests which arrive from the referee.

For example, if you added a volity.start_game property, it would be called when the game started.

You do not have to assign a property for every volity RPC in the list. If a property is not found for a given RPC, then nothing happens.

Note: Earlier versions of Gamut called game.START and game.END instead of volity.start_game and volity.end_game. This old configuration still works, but it is deprecated. One day it will be withdrawn.

game

This is a blank object. The UI must add properties to it; these are used as function handlers for the game RPC requests which arrive from the referee.

For example, if a game.win(true) RPC arrived, the client would look up the game.win property, and call it with one boolean argument (true). If the property was not found, or if it was not a function, this would be reported as an error back to the referee.

Warning about array and struct arguments: These are translated into ECMAScript objects which act like arrays and structs; but they are not full-fledged ECMAScript arrays. You should not store these objects long-term in your UI code; read them in place and then discard them. If you need to store their information, copy it into new ECMAScript arrays.

info

This object contains fields holding meta-information about the table that can be useful to UI files.

Object properties:

version
The version number of the ECMAScript API which is available.
nickname
The player's current Jabber MUC nickname.
state
The current game state, as a string: setup, active, or suspended. (The disrupted and abandoned states are not visible to the client -- they will appear as active.)
recovery
A boolean indicating whether the client is in the middle of a state recovery burst. (Between receive_state() and state_sent().)
seat
The seat that the user occupies at this table. This is a Seat object (see below). If you are not seated, this field is null.
allseats
An array of seats, listing all the seats at the table. This includes unoccupied and optional seats.
gameseats
An array of seats, listing all the seats involved in the current game. If no game is in progress, this field is null.
ruleset
The ruleset URI of the game being played.
versionmatch(version number, version spec)
Return a boolean indicating whether the given number matches the given spec.

Add the global config properties (show_table, record_games, language, kill_game)?

metadata

Your script code can call one of the following functions to extract metadata, either from itself or from one of the resource files it loads:

metadata.get(URI, key)
Get the entry labelled key. If there are several such entries, this returns the first one (preferring one whose xml:lang attribute matches the client's language setting). If there are none, returns null.
metadata.get(URI, key, default)
Same as above, except that if there is no entry, default is returned.
metadata.getall(URI, key)
Get a list of all the entries labelled key.

The URI is a resource URI (see above). If null, undefined, or the empty string, this will return the metadata from the UI file itself.

The key is a label, optionally preceded by a namespace and a period. (Rather like translation tokens.) If you don't give a namespace, game. is assumed.

The namespaces for these keys are:

dc
http://purl.org/dc/elements/1.1/")
volity
http://volity.org/protocol/metadata")
game
http://volity.org/protocol/gamedata")

Note that a namespace is not the same as an XML qualifier prefix. The call metadata.get(URI, "ui_file.html" class=wikipagelink>UI file as a <dc:title> element, but this is not required -- the XML could assign any prefix to the "http://purl.org/dc/elements/1.1/" namespace.

Classes

Seat

Returned by info methods. (See above.)

Seat objects may be compared by equality (seat1 == seat2). They evaluate as strings in expressions, so you can also test (seat1 == "seatid").

Object properties:

players
Returns an array of player JIDs (as strings), representing the players sitting in this seat.
nicknames
Returns an array of player nicknames (as strings), representing the players sitting in this seat.

Audio

Allows construction of audio objects. Returned by the audio() function.

What do we say about supported audio formats? AIFF and MP3 work. (MP3 by virtue of a library that Gamut includes.) I don't know what else Java can handle, or be made to handle.

Object methods:

url
The URL where this sound can be found. (read-only)
alt
The "alt tag" for this sound, a text description of the sound effect.
loop
If true, the sound will repeat forever once started. If false (or null, or undefined, or 1), it plays once. If a positive integer, it plays the given number of times.
play ( )
Begins the sound playing. Returns an audio-instance object, which you can use to stop the sound.

Possible future expansion: a stopall() method? Properties to adjust volume, pitch, pan? A way to create an instance which is not yet started, so that you can adjust its properties and then start it? Pause and unpause?

Audio Instance

Returned by an Audio object's play() method.

Unlike the Audio object, all the instance object's fields are read-only.

stop ( )
Stops playing the sound.
url
The URL where this sound can be found.
alt
The "alt tag" for this sound, a text description of the sound effect.
loop
If true, the sound will repeat forever once started. If false (or null, or undefined, or 1), it plays once. If a positive integer, it plays the given number of times.