LogoLogo
  • Introduction
  • Getting Started
    • Setup
      • Windows WSL Instructions (new)
      • Windows WSL Instructions
      • Linux Instructions
    • Creating your experiment
    • Running your experiment
    • Updating your experiment
  • Conceptual Overview
    • Game Life Cycle
      • Customising when players submit stages
    • Concepts
    • Randomization & Batches
    • API
  • Guides
    • V2 Migration
    • Managing the Data
    • Special Empirica Components
    • The Admin Panel
    • Deploying Your Experiment
      • Ubuntu tutorial
  • Tutorials
    • Beginner Experiment: Prisoner's Dilemma
      • Part 1: Before you start
      • Part 2: Creating the Experiment in Empirica
      • Part 3: Getting Accustomed to the Code
      • Part 4: Coding the Prisoner's Dilemma Game
        • Part 4.1: Removing example code
        • Part 4.2: Intro Text
        • Part 4.3: Set up the game stages
        • Part 4.4: Build the "Choice" React Component
        • Part 4.5: Build the "Result" React Component
        • Part 4.6: Compute the Score
      • Part 5: Customizing the experiment
        • Part 5.1: Changing the number of rounds
        • Part 5.2: Turning the chat on and off
      • Part 6: Deployment
  • FAQ
    • I need help!
    • The Processes and Elements of an Empirica Experiment
    • Managing Players and Games
  • Resources
    • Helpful Linux Commands
    • Code Editors
    • Javascript and React
  • Links
    • Empirica website
    • Twitter
    • GitHub
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
Export as PDF
  1. Tutorials
  2. Beginner Experiment: Prisoner's Dilemma
  3. Part 4: Coding the Prisoner's Dilemma Game

Part 4.1: Removing example code

When you create an experiment with the empirica create command, Empirica populates a template experiment with the jelly bean and minesweeper code that you saw in the previous step. We won't use this example code in our experiment, so let's clean it out. There are a few steps:

  • delete the entire client/src/examples folder

  • In client/src/Stage.jsx:

    • remove the imports for JellyBeans and MineSweeper (2 lines)

    • replace the switch statement and its three cases (8 lines, including brackets) with the code `

      return (<p>Not yet implemented...</p>)

The file should look like this when you are done:

Stage.jsx
import {
  usePlayer,
  usePlayers,
  useRound,
} from "@empirica/core/player/classic/react";
import { Loading } from "@empirica/core/player/react";
import React from "react";

export function Stage() {
  const player = usePlayer();
  const players = usePlayers();
  const round = useRound();

  if (player.stage.get("submit")) {
    if (players.length === 1) {
      return <Loading />;
    }

    return (
      <div className="text-center text-gray-400 pointer-events-none">
        Please wait for other player(s).
      </div>
    );
  }

  return (<p>Not yet implemented...</p>)
}
  • In the server/src/callbacks.js file:

    • empty the contents of the onGameStart and onStageEnded functions, keeping the function definitions

    • delete everything after the empty function definition for Empirica.onGameEnded({}).

When you are done, the file should look like this:

import { ClassicListenersCollector } from "@empirica/core/admin/classic";
export const Empirica = new ClassicListenersCollector();

Empirica.onGameStart(({ game }) => {});

Empirica.onRoundStart(({ round }) => {});

Empirica.onStageStart(({ stage }) => {});

Empirica.onStageEnded(({ stage }) => {});

Empirica.onRoundEnded(({ round }) => {});

Empirica.onGameEnded(({ game }) => {});

Note: In JavaScript, a callback function is a function that is passed as an argument to another function and is invoked or called at a specific point within that function's execution. The callback function is typically used to ensure that certain code is executed only after the completion of a specific task or operation. In our case, these callbacks are invoked when parts of the experiment start and end.

Restart empirica

Now we can restart Empirica to have it reflect our changes. The command below will remove our existing datafile and start the server.

rm .empirica/local/tajriba.json; empirica

PreviousPart 4: Coding the Prisoner's Dilemma GameNextPart 4.2: Intro Text

Last updated 1 year ago

Was this helpful?

At this point of the tutorial, you should be able to start a new game on the admin site ().

At this stage, if we login as a participant (by visiting ), the game will be stuck in loading, as we haven't yet added any content - that's what we'll do next!

http://localhost:3000/admin/
http://localhost:3000