from otree.api import * doc = """ A classical one-shot "Prisoner's Dilemma", but played over 5 rounds. Two players are asked separately whether they want to cooperate or defect. Their choices directly determine the payoffs. """ class C(BaseConstants): NAME_IN_URL = 'PD_Chat' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 5 PAYOFF_A = 5 PAYOFF_B = 3 PAYOFF_C = 1 PAYOFF_D = 0 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): 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): C.PAYOFF_A, (True, True): C.PAYOFF_B, (False, False): C.PAYOFF_C, (True, False): C.PAYOFF_D, } other = other_player(player) player.payoff = payoff_matrix[(player.cooperate, other.cooperate)] # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 timeout_seconds = 100 class Decision(Page): form_model = 'player' form_fields = ['cooperate'] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs 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'), ) class TotalResult(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player: Player): total_points = player.in_all_rounds() cumulative_points = 0 for points in total_points: cumulative_points += points.payoff return{ "cumulative_payoff": cumulative_points } page_sequence = [Introduction, Decision, ResultsWaitPage, Results, TotalResult]