from otree.api import * doc = """ This is a one-shot "Prisoner's Dilemma". Two players are asked separately whether they want to cooperate or defect. Their choices directly determine the payoffs. """ class Constants(BaseConstants): name_in_url = 'prisoner' players_per_group = 2 num_rounds = 1 instructions_template = 'prisoner/instructions.html' payoff_A = cu(30) payoff_B = cu(20) payoff_C = cu(10) payoff_D = cu(0) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): name = models.StringField() cooperate = models.BooleanField( choices=[[True, 'Cooperate'], [False, 'Defect']], doc="""This player's decision""", widget=widgets.RadioSelect, ) # FUNCTIONS def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff_matrix = { (False, True): Constants.payoff_A, (True, True): Constants.payoff_B, (False, False): Constants.payoff_C, (True, False): Constants.payoff_D, } other = other_player(player) player.payoff = payoff_matrix[(player.cooperate, other.cooperate)] + player.payoff # PAGES class Demographics(Page): form_model = 'player' form_fields = ['name'] pass class Introduction(Page): pass class Decision_1(Page): form_model = 'player' form_fields = ['cooperate'] pass class Decision_2(Page): form_model = 'player' form_fields = ['cooperate'] pass class Decision_3(Page): form_model = 'player' form_fields = ['cooperate'] pass class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs pass class Results(Page): @staticmethod def vars_for_template(player: Player): opponent = other_player(player) return dict( opponent=opponent, same_choice=player.cooperate == opponent.cooperate, my_decision=player.field_display('cooperate'), opponent_decision=opponent.field_display('cooperate'), ) page_sequence = [Demographics, Introduction, Decision_1, ResultsWaitPage, Results, Decision_2, ResultsWaitPage, Results, Decision_3, ResultsWaitPage, Results]