The Processes and Elements of an Empirica Experiment

What is a React.js component?

A React.js component is the main building block of the frontend of your Empirica App. There are many tutorials online to help with your understanding of React.js.
In Empirica, we assign certain components for the Intro Steps, the Round, the Exit Steps, and a few other elements of the Game.
A component is composed of:
    states that affect what is rendered for the user, but that can also be changed by the user interacting with the rendered elements of the app.
    props provided from other components that can be used to determine what is rendered for the user.
    other components that it imports and builds into what it renders for the user.
    a render function that determines what is shown to the user with a mix of HTML (with <> tags) and JavaScript (with {}).
Each component is generally made into one .jsx file. Components can be imported into each other to build more complex components. Usually, the type of components used in Empirica are class-based.
A major perk of React.js components is that whenever one of their states change (e.g., because of the action of user), it will refresh every component affected and update what is rendered depending on the new states. This makes for web apps that live update what they look like and do. This is particularly useful for Empirica because you want to update what you show to players depending on states such as which responses they have given, which stage of the Game they are at, what other players are doing, etc.
Hence, there are some elements of a component you might want to render differently depending on certain props and states. You can use syntax such as { condition ? true : false }or { condition && true} where condition is a condition that is tested, true is what is rendered if this condition tests true, and false is what is rendered if this condition tests false.
A component might look like this:
1
// Importing elements and other components
2
import React, { Component } from 'react'
3
import GivingResponse from './GivingResponse'
4
5
export default class Questionnaire extends Component {
6
// The state of the questionnaire
7
state = {
8
showHint: false
9
}
10
11
// Handling if the player clicks to show hint (toggles the hint on and off)
12
handleShowHint = () => {
13
this.setState({ showHint: !this.state.showHint })
14
}
15
16
render() {
17
18
// Getting the props
19
const { player } = this.props;
20
21
return (
22
<div>
23
<p> What is the name of the first person to set foot on the moon?</p>
24
25
{/* A button that affects the conditional that
26
determines whether to show the hint or not */}
27
<button onClick={this.handleShowHint()}>Show hint</button>
28
{this.state.showHint && <p className="hintcolour">He was American.</p>}
29
30
{/* Importing another component for the player to give their answer.
31
We pass down the prop of the player */}
32
<GivingResponse player={player} />
33
</div>
34
)
35
}
36
}
Copied!

How can I get rid of the "Waiting on the other players. Please wait until all players are ready"?

This is not something directly specific to Empirica, it's just how the basic template is set up.
As shown here, it is possible to use the information that a Player has submitted their Stage to render something different than before they submit their Stage. In template, this happens in the TaskResponse.jsx. You can see that two render functions have been created: renderSubmitted() and renderInput(). In the render() part of the component, you can see that there is an if conditional that determines whether the render function used will be renderSubmitted(), which will prevent the renderSubmitted() from being called (and thereby hiding the slider and method of response from the player):
1
render() {
2
const { player } = this.props;
3
4
// If the player already submitted, don't show the slider or submit button
5
if (player.stage.submitted) {
6
return this.renderSubmitted();
7
}
8
9
return (
10
<div className="task-response">
11
<form onSubmit={this.handleSubmit}>
12
{this.renderInput()}
13
{" "}
14
<button type="submit">Submit</button>
15
</form>
16
</div>
17
);
18
}
Copied!
You can get rid of this message by taking out the if conditional, or changing what renderSubmitted() does.

How can I redirect a player if I detect they are using a certain browser or a mobile device?

You might not want players to join your game from a mobile or tablet, nor from certain browsers. To do so you can use react-device-detect and modify the first page of your experiment (e.g., the consent page, the NewPlayer page, or the first page of your Intro Steps) to prevent them from continuing the experiment if you detect the device or browser that you do not want.
To install react-device-detect use:
1
meteor npm install react-device-detect
Copied!
react-device-detect has different Booleans that you can import and use in one of your components to detect whether the player is using a certain browser.
    isMobile for whether they are using a mobile device
    isChrome for whether they are using Chrome
    isFirefox for whether they are using Firefox
    isSafari for whether they are using Safari
    ...
And others than you can find out about here.
Import them into the component with:
1
import { isMobile, isFirefox, isSafari, isChrome } from 'react-device-detect';
Copied!
For example, if you want to render a different consent form if the player is using a mobile device or is not using Chrome:
1
return !isMobile && isChrome ?
2
(
3
<div>This is the consent form...</div>
4
) :
5
(
6
<div>Please use a computer and Chrome.</div>
7
)
Copied!

How can I show a different Exit Step to players depending on whether they have finished the game or if the game was cancelled/had a problem?

In the client/main.js you set which components form the Exit Steps with Empirica.exitSteps(). You can use the player.exitStatus to separate out whether they have finished the game or if they were sent to the exit steps because the game was cancelled/had an issue and send them to different Exit Steps.
For example:
1
Empirica.exitSteps((game, player) => {
2
return player.exitStatus === "finished"
3
? [PostSurvey, Thanks]
4
: [Sorry];
5
});
Copied!

Can players navigate back and forth between the Exit Steps?

For now, players cannot navigate back and forth between the Exit Steps.
Each Exit Step has a name set by static stepName = ""; and players can only move from one Exit Step to the next if the component has an element (e.g., a button) that will call the onSubmit prop. For example:
1
<button type="button" onClick={onSubmit}>
2
Finish this experiment
3
</button>
Copied!
If you wanted multiple pages within the Exit Steps that players can navigate through, you could create a component within one Exit Step that has different components to form "pages" and with a state that knows which page it is at and navigating to and from them.

Can a manually sent a player to an exit step?

You can have a piece of code to manually send a player to an exit stage if they do something (e.g., you want to give them a quit button) by using:
1
player.exit("name of exit step")
Copied!
where the string is the name of the exit stage you want to send them to.

How can I use bots?

In the server/bots.js you can create bots for them to participate in your games.
See this part of the tutorial for more details.
1
Empirica.bot("bob", {
2
// // NOT SUPPORTED Called at the beginning of each stage (after onRoundStart/onStageStart)
3
// onStageStart(bot, game, round, stage, players) {},
4
5
// Called during each stage at tick interval (~1s at the moment)
6
onStageTick(bot, game, round, stage, secondsRemaining) {}
7
8
// // NOT SUPPORTED A player has changed a value
9
// // This might happen a lot!
10
// onStagePlayerChange(bot, game, round, stage, players, player) {}
11
12
// // NOT SUPPORTED Called at the end of the stage (after it finished, before onStageEnd/onRoundEnd is called)
13
// onStageEnd(bot, game, round, stage, players) {}
14
});
Copied!
Last modified 3mo ago