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 5: Customizing the experiment

Part 5.2: Turning the chat on and off

In the standard prisoner's dilemma experiment, participants are not able to communicate with one another. We've left the chat enabled from the template experiment up until this point, but now we should set up a flag to enable or disable it, so we can measure the effect of the chat on the outcomes of the game.

Create a factor and treatments

Let's return to the file .empirica/treatments.yaml. As we did when we changed the number of rounds, we'll create a factor for the chat:

  - desc: Can players chat with each other during the game?
    name: chatEnabled
    values:
      - value: true
      - value: false

We update our existing conditions, and create new ones with the chat in different states. This time, we'll set the default behavior of the chat to be disabled:

treatments:
  - desc: Standard one-shot prisoner's dilemma
    factors:
      playerCount: 2
      numRounds: 1
      chatEnabled: false
    name: baseline
  - desc: A short iterated prisoner's dilemma
    factors:
      playerCount: 2
      numRounds: 3
      chatEnabled: false
    name: short_iterated
  - desc: A long iterated prisoner's dilemma
    factors:
      playerCount: 2
      numRounds: 7
      chatEnabled: false
    name: long_iterated
  - desc: Standard one-shot prisoner's dilemma with chat
    factors:
      playerCount: 2
      numRounds: 1
      chatEnabled: true
    name: baseline_chat
  - desc: A short iterated prisoner's dilemma with chat
    factors:
      playerCount: 2
      numRounds: 3
      chatEnabled: true
    name: short_iterated_chat
  - desc: A long iterated prisoner's dilemma with chat
    factors:
      playerCount: 2
      numRounds: 7
      chatEnabled: true
    name: long_iterated_chat

As we add additional manipulations to the game, the number of different treatments begins to grow rapidly. Managing this type of complexity is exactly what Empirica was built to accomplish. Imagine creating a separate setup for each of these different conditions, rather than being able to simply parameterize the factors!

Enable or disable chat in the player display

In order to make use of the chat feature, we'll make changes to client/src/Game.jsx. Notice in this file that the <Chat...> component is embedded in syntax that looks like this:

{playerCount > 1 && <div>...</div>}

All we need to do at this point is replace the left-hand side condition with one that looks for chatEnabled. We have two changes to make - first to replace playerCount in the treatment object destructuring line with chatEnabled:

const { chatEnabled } = game.get("treatment");

Then we need to use chatEnabled in the left-hand side of the short-circuit operation. As chatEnabled already returns a true or false value, we can just use it directly as the left-hand side expression:

  {chatEnabled && (
    <div className="h-full w-128 border-l flex justify-center items-center">
      <Chat scope={game} attribute="chat" />
    </div>
  )}

Your full code for Game.jsx should now look like this:

client/src/Game.jsx
import { Chat, useGame } from "@empirica/core/player/classic/react";

import React from "react";
import { Profile } from "./Profile";
import { Stage } from "./Stage";

export function Game() {
  const game = useGame();
  const { chatEnabled } = game.get("treatment");

  return (
    <div className="h-full w-full flex">
      <div className="h-full w-full flex flex-col">
        <Profile />
        <div className="h-full flex items-center justify-center">
          <Stage />
        </div>
      </div>

      {chatEnabled && (
        <div className="h-full w-128 border-l flex justify-center items-center">
          <Chat scope={game} attribute="chat" />
        </div>
      )}
    </div>
  );
}

Now when you restart empirica and play through a game, you'll be able to turn the chat on or off by selecting the appropriate treatment condition.

PreviousPart 5.1: Changing the number of roundsNextPart 6: Deployment

Last updated 1 year ago

Was this helpful?

This is a that works as follows. Before the && sign we have an expression that will evaluate to either true or false, depending on the number of players in the game. The double ampersand && indicates a logical "and" statement, which means that it will try and find out if the values on both the left and right side of the && will evaluate to true. If the left-hand side value is false, then it knows that the statement as a whole must evaluate to false, regardless of what is on the right-hand side. So for efficiency, it stops at that point and doesn't even bother to evaluate the right-hand side - that is, it "short circuits" the full expression.

On the other hand, when the left-hand side is true, it will process what is on the right-hand side of the &&. In this case, it will find our chat component, which it evaluates as a value, and displays it to the screen.

short-circuit condition
truthy