from otree.api import * import random doc = """ Practice for first main task """ class C(BaseConstants): NAME_IN_URL = 'practice1' PLAYERS_PER_GROUP = 4 NUM_ROUNDS = 5 instructions_template = 'Instructions1.html' options = ['Do not protest', 'Protest'] practice_treatments = [(0, 0), (0, 1), (2, 3), (4, 4), (1, 0)] player1_ROLE = 'Player 1' player2_ROLE = 'Player 2' player3_ROLE = 'Player 3' player4_ROLE = 'Player 4' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): protest_action = models.IntegerField( choices=[[0,'Do not protest'], [1, 'Protest']], widget=widgets.RadioSelect, ) knowledge_level = models.IntegerField() q1 = models.StringField( choices=['Do not protest', 'Protest'], label='Which option gives a constant payoff regardless of what the other player does?' ) q2 = models.StringField( choices=['a. ', 'b. ', 'c. ', 'd. '], label="Choose one of the following options:" ) q3 = models.StringField( choices=['Yes.', 'No.', 'I can\'t say with the information given.'], label="Choose one of the following options:" ) q4 = models.StringField( choices=['Yes.', 'No.', 'I can\'t say with the information given.'], label="Choose one of the following options:" ) q5 = models.StringField( choices=['Yes.', 'No.', 'I can\'t say with the information given.'], label="Choose one of the following options:" ) q6 = models.StringField( choices=['Yes.', 'No.', 'I can\'t say with the information given.'], label="Choose one of the following options:" ) # FUNCTIONS def other_players(player: Player): # get other player return dict(opp1=player.get_others_in_group()[0], opp2=player.get_others_in_group()[1], opp3=player.get_others_in_group()[2]) def creating_session(subsession): # group players randomly each round subsession.group_randomly(fixed_id_in_group=True) def role_to_number(player: Player): if player.role in ['Player 1', 'Player 2']: return 0 else: return 1 # PAGES class understandingQs1(Page): @staticmethod def is_displayed(player: Player): if player.round_number == 1: return True else: return False form_model = 'player' form_fields = ['q1', 'q2', 'q3', 'q4', 'q5', 'q6'] @staticmethod def error_message(player: Player, values): ans = dict(q1='Do not protest', q2='b. ', q3='Yes.', q4='I can\'t say with the information given.', q5='Yes.', q6='Yes.') error_message = {f: 'Incorrect. Please re-read the question carefully, and check the instructions if needed.' for f in ans if values[f] != ans[f]} if error_message: return error_message class Intro(Page): @staticmethod def is_displayed(player): if player.round_number == 1: return True else: return False class Decision(Page): form_model = 'player' form_fields = ['protest_action'] @staticmethod def vars_for_template(player: Player): # assign player treatment player.knowledge_level = C.practice_treatments[player.round_number - 1][role_to_number(player)] @staticmethod def before_next_page(player: Player, timeout_happened): if player.round_number == 1: player.participant.practice_1_choices = [] class ResultsWaitPage(WaitPage): pass class Results(Page): @staticmethod def vars_for_template(player: Player): opponents = other_players(player) opp1 = opponents['opp1'] opp2 = opponents['opp2'] opp3 = opponents['opp3'] protesters = player.protest_action + opp1.protest_action + opp2.protest_action + opp3.protest_action if protesters == 4: protest = True else: protest = False return dict( protesters=protesters, others=protesters-player.protest_action, outcome=protest, same_choice=player.protest_action == opp1.protest_action == opp2.protest_action == opp3.protest_action, my_decision=player.field_display('protest_action')) @staticmethod def before_next_page(player: Player, timeout_happened): # store player's choice player.participant.practice_1_choices.append(player.protest_action) page_sequence = [understandingQs1, Intro, Decision, ResultsWaitPage, Results]