Search…
⌃K

API

This document describes Empirica's server, client and shared APIs.

Server

Empirica.onGameStart(callback)

The onGameStart callback is called just before a game starts, when all players are ready, and it must create rounds and stages for the game.
One (and one only) onGameStart callback is required for Empirica to work.
The callback receives one argument, the game object, which gives access to the players and the treatment for this game.
It also offers the addRound() method, which allows to add a round to the game. The returned Round object will implement the addStage(stageArgs) method, which allows to add a Stage to the Round. The stageArgs object to be passed to the stage creation method must contain:
  • duration: the stage duration, in seconds
Note that the Game has not yet been created when the callback is called, and you do not have access to the other properties of the Game which will be created subsequently.

Example

Empirica.gameInit(game => {
game.players.forEach((player, i) => {
player.set("score", 0);
});
const round1 = game.addRound();
round1.addStage({
duration: 120
name: "Response",
});
if (game.treatment.playerCount > 1) {
round1.addStage({
duration: 120
name: "Result",
});
}
const round2 = game.addRound();
round1.addStage({
duration: 300
name: "Response",
someotherfield: "mydata"
});
if (game.treatment.playerCount > 1) {
round1.addStage({
duration: 120
name: "Result",
});
}
});

Game Callbacks

Game hooks are optional methods attached to various events throughout the game life cycle to update data on the server-side.
Contrary to client side data updates, sever-side updates are synchronous, there is no risk of conflicting updates, and important calculations can be taken at precise points along the game.
``

Empirica.onRoundStart(callback)

onRoundStart is triggered before each round starts, and before onStageStart. It receives the same options as onGameStart, and the round that is starting.

Example

Empirica.onRoundStart(({ round }) => {
round.set("scoreToReach", round.game.get("maxScore"));
});

Empirica.onStageStart(callback)

onRoundStart is triggered before each stage starts. It receives the same options as onRoundStart, and the stage that is starting.

Example

Empirica.onStageStart(({ stage }) => {
stage.set("randomColor", myRandomColorGenerator());
});

Empirica.onStageEnd(callback)

onStageEnd is triggered after each stage. It receives the current game, the current round, and stage that just ended.

Example

Empirica.onStageEnd(({ stage }) => {
const expectedScore = stage.round.get("expectedScore");
const group = stage.get("score") > expectedScore ? "great" : "not_great";
stage.set("scoreGroup", group);
});

Empirica.onRoundEnd(callback)

onRoundEnd is triggered after each round. It receives the current game, and the round that just ended.

Example

Empirica.onRoundEnd(({ round }) => {
let maxScore = 0;
round.game.players.forEach((player) => {
const playerScore = player.round.get("score") || 0;
if (playerScore > maxScore) {
maxScore = playerScore;
}
});
round.set("maxScore", maxScore);
});

Empirica.onGameEnd(callback)

onGameEnd is triggered when the game ends. It receives the game that just ended.

Example

Empirica.onGameEnd(({ game }) => {
let maxScore = 0;
game.rounds.forEach((round) => {
const roundMaxScore = round.get("maxScore") || 0;
if (roundMaxScore > maxScore) {
maxScore = roundMaxScore;
}
});
game.set("maxScore", maxScore);
});

Client

See the Special Empirica Component page for more info.

Server Objects

Game object

Property
Type
Description
players
Array of Player objects
Players participating in this Game.
rounds
Array of Round objects
This will return every round that makes up the game.
stages
Array of Stage objects
This will return every stage that makes up the game.
currentRound
The current Round.
currentStage
The current Stage.

Round object

Property
Type
Description
stages
Array of Stage objects
Stages composing this Round.
currentGame
Game this round is a part of.

Stage object

Property
Type
Description
Select
round
Round this stage is a part of.
currentGame
Game this stage is a part of.

Player object

Property
Type
Description
id
String
The ID the player used to register (e.g. MTurk ID).
currentRound
Round the player is currently in.
currentStage
Stage the player is currently in.