from otree.api import (
Page,
WaitPage,
models,
widgets,
BaseConstants,
BaseSubsession,
BaseGroup,
BasePlayer,
Currency as c,
currency_range,
)
doc = """
In the following games, students will play against each other to win bonus points.
Prior to each game, .
This actual value is revealed after the bidding.
Bids are private. The player with the highest bid wins the auction, but
payoff depends on the bid amount and the actual value.
"""
class Constants(BaseConstants):
name_in_url = 'game_theory_bonus_game'
players_per_group = 2
num_rounds = 6
instructions_template = 'game_theory_bonus_game/instructions.html'
class Subsession(BaseSubsession):
pass
class Group(BaseGroup):
game_outcome = models.IntegerField()
class Player(BasePlayer):
other_payoff = models.CurrencyField()
decision = models.StringField(
choices=['Cooperate', 'Dont Cooperate'],
label='What action do you want to take?',
widget=widgets.RadioSelectHorizontal,
)
decision2 = models.StringField(
choices=['Left', 'Right'],
label='What action do you want to take?',
widget=widgets.RadioSelectHorizontal,
)
decision_sh = models.StringField(
choices=['High', 'Low'],
label='What action do you want to take?',
widget=widgets.RadioSelectHorizontal,
)
reward = models.IntegerField()
name = models.StringField()
bonus_points = models.StringField(
choices=[['Prisoners Dilemma', 'Prisoners Dilemma'], ['Cooperation Game', 'Cooperation Game'],
['Stag-Hunt Game', 'Stag-Hunt Game']],
label='What game would you like to have your bonus points from?',
widget=widgets.RadioSelect,
)
last_q = models.StringField(
choices=[['TRUE', 'True'], ['FALSE', 'False']],
label="This choice is another example of a one-shot simultaneous game.",
widget=widgets.RadioSelect,
)
# FUNCTIONS
def creating_session(subsession: Subsession):
subsession.group_randomly()
for g in subsession.get_groups():
players = g.get_players()
def set_payoffs(group: Group):
if group.round_number == 1 or group.round_number == 2:
p1 = group.get_player_by_id(1)
p2 = group.get_player_by_id(2)
if p1.decision == 'Cooperate' and p2.decision == 'Cooperate':
p1.payoff = 10
p2.payoff = 10
group.game_outcome = 1
elif p1.decision == 'Cooperate' and p2.decision == 'Dont Cooperate':
p1.payoff = 2
p2.payoff = 12
group.game_outcome = 2
elif p1.decision == 'Dont Cooperate' and p2.decision == 'Cooperate':
p1.payoff = 12
p2.payoff = 2
group.game_outcome = 2
else:
p1.payoff = 5
p2.payoff = 5
group.game_outcome = 3
if group.round_number == 5 or group.round_number == 6:
p1 = group.get_player_by_id(1)
p2 = group.get_player_by_id(2)
if p1.decision_sh == 'High' and p2.decision_sh == 'High':
p1.payoff = 10
p2.payoff = 10
group.game_outcome = 1
elif p1.decision_sh == 'High' and p2.decision_sh == 'Low':
p1.payoff = 0
p2.payoff = 6
group.game_outcome = 2
elif p1.decision_sh == 'Low' and p2.decision_sh == 'High':
p1.payoff = 6
p2.payoff = 0
group.game_outcome = 2
else:
p1.payoff = 5
p2.payoff = 5
group.game_outcome = 3
if group.round_number == 3 or group.round_number == 4:
p1 = group.get_player_by_id(1)
p2 = group.get_player_by_id(2)
if p1.decision2 == 'Left' and p2.decision2 == 'Left':
p1.payoff = 7
p2.payoff = 7
group.game_outcome = 1
elif p1.decision2 == 'Left' and p2.decision2 == 'Right':
p1.payoff = 0
p2.payoff = 0
group.game_outcome = 2
elif p1.decision2 == 'Right' and p2.decision2 == 'Left':
p1.payoff = 0
p2.payoff = 0
group.game_outcome = 2
else:
p1.payoff = 7
p2.payoff = 7
group.game_outcome = 1
for p in group.get_players():
if p1:
p.other_payoff = p2.payoff
else:
p.other_payoff = p1.payoff
# PAGES
class Introduction(Page):
def is_displayed(player):
return player.round_number ==1
# Prisoner's dilemma page
class Prisoner(Page):
def is_displayed(player):
return player.round_number <= 2
form_model = 'player'
form_fields = ['decision']
@staticmethod
def get_timeout_seconds(player: Player):
return 90
def before_next_page(player, timeout_happened):
if timeout_happened:
player.decision = 'Cooperation'
class Cooperation(Page):
def is_displayed(player):
return player.round_number == 3 or player.round_number == 4
form_model = 'player'
form_fields = ['decision2']
@staticmethod
def get_timeout_seconds(player: Player):
return 90
def before_next_page(player, timeout_happened):
if timeout_happened:
player.decision2 = 'Left'
class StagHunt(Page):
def is_displayed(player):
return player.round_number >= 5
form_model = 'player'
form_fields = ['decision_sh']
@staticmethod
def get_timeout_seconds(player: Player):
return 90
def before_next_page(player, timeout_happened):
if timeout_happened:
player.decision = 'High'
class ResultsWaitPage(WaitPage):
after_all_players_arrive = 'set_payoffs'
class Results(Page):
@staticmethod
def vars_for_template(player: Player):
group = player.group
p1 = group.get_player_by_id(1)
p2 = group.get_player_by_id(2)
# return dict(total_earnings.p1 = p1.payoff)
class RoundWait(WaitPage):
pass
class Question(Page):
def is_displayed(player):
return player.round_number == Constants.num_rounds
form_model = 'player'
form_fields = ['name', 'bonus_points', 'last_q']
page_sequence = [Introduction, Prisoner, Cooperation, StagHunt, ResultsWaitPage, Results, RoundWait, Question]