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 C(BaseConstants): NAME_IN_URL = 'prisoner' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 5 INSTRUCTIONS_TEMPLATE = 'prisoner/instructions.html' PAYOFF_A = cu(300) PAYOFF_B = cu(200) PAYOFF_C = cu(100) PAYOFF_D = cu(0) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): answer1 = models.IntegerField(label="How much money will you earn in this case?") answer2 = models.IntegerField(label="How much money each of you will earn") answer3 = models.IntegerField(label="How much money each of you will earn") pass age = models.IntegerField(label="How old are you? Please write your age?:") gender = models.StringField(choices=['Female', 'Male', 'Other'], label='What is your gender?') place = models.StringField( label='Where are you from?') education = models.StringField(choices=['High-School diploma', 'Bachelor Degree', 'Masters Degree', 'Doctor of Philosophy', ], label='What is your highest educational degree?') comment = models.StringField( label='Do you have any other comments?') 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): timeout_seconds = 100 class Understanding_question(Page): form_model = 'player' form_fields = ['answer1', 'answer2', 'answer3'] def error_message(self, values): solutions = dict(answer1=0, answer2=200, answer3=100) if values != solutions: return 'One or more answers were incorrect.' def is_displayed(player): return player.round_number == 1 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 Survey(Page): form_model = 'player' form_fields = ['age', 'gender', 'place', 'education', 'comment'] def is_displayed(player): return player.round_number == 5 page_sequence = [Introduction, Understanding_question, Decision, ResultsWaitPage, Results, Survey]