from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'part_2' PLAYERS_PER_GROUP = None NUM_ROUNDS = 5 # payoffs round 1 PAYOFF_A_1 = cu(5) # if Defect Cooperate PAYOFF_B_1 = cu(4) # if Cooperate Cooperate PAYOFF_C_1 = cu(5) # if Defect Defect PAYOFF_D_1 = cu(3) # if Cooperate Defect # payoffs round 2 PAYOFF_A_2 = cu(5) # if Defect Cooperate PAYOFF_B_2 = cu(4) # if Cooperate Cooperate PAYOFF_C_2 = cu(4) # if Defect Defect PAYOFF_D_2 = cu(3) # if Cooperate Defect # payoffs round 3 PAYOFF_A_3 = cu(5) # if Defect Cooperate PAYOFF_B_3 = cu(4) # if Cooperate Cooperate PAYOFF_C_3 = cu(2) # if Defect Defect PAYOFF_D_3 = cu(2) # if Cooperate Defect # payoffs round 4 PAYOFF_A_4 = cu(5) # if Defect Cooperate PAYOFF_B_4 = cu(3) # if Cooperate Cooperate PAYOFF_C_4 = cu(0) # if Defect Defect PAYOFF_D_4 = cu(1) # if Cooperate Defect # payoffs round 5 PAYOFF_A_5 = cu(2) # if Defect Cooperate PAYOFF_B_5 = cu(2) # if Cooperate Cooperate PAYOFF_C_5 = cu(0) # if Defect Defect PAYOFF_D_5 = cu(1) # if Cooperate Defect class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): cooperate = models.BooleanField( choices=[[True, 'plantes pollinisatrices'], [False, 'plantes exotiques']], doc="""This player's decision""", widget=widgets.RadioSelect, ) other = models.IntegerField() # FUNCTION def opponent(player: Player): participant = player.participant roole = participant.roole if roole == "A": if random.randint(1,2) == 1: player.other = 1 return True else: player.other = 0 return False elif roole == "B": if random.randint(1,2) == 1: player.other = 1 return True else: player.other = 0 return False elif roole == "C": if random.randint(1,2) == 1: player.other = 1 return True else: player.other = 0 return False def set_payoff(player: Player): participant = player.participant roole = participant.roole # Payoff round 1 if player.round_number == 1: payoff_matrix = { (False, True): C.PAYOFF_A_1, (True, True): C.PAYOFF_B_1, (False, False): C.PAYOFF_C_1, (True, False): C.PAYOFF_D_1, } player.payoff = payoff_matrix[(player.cooperate, opponent(player))] # Payoff round 2 if player.round_number == 2: payoff_matrix = { (False, True): C.PAYOFF_A_2, (True, True): C.PAYOFF_B_2, (False, False): C.PAYOFF_C_2, (True, False): C.PAYOFF_D_2, } player.payoff = payoff_matrix[(player.cooperate, opponent(player))] # Payoff round 3 if player.round_number == 3: payoff_matrix = { (False, True): C.PAYOFF_A_3, (True, True): C.PAYOFF_B_3, (False, False): C.PAYOFF_C_3, (True, False): C.PAYOFF_D_3, } player.payoff = payoff_matrix[(player.cooperate, opponent(player))] # Payoff round 4 if player.round_number == 4: payoff_matrix = { (False, True): C.PAYOFF_A_4, (True, True): C.PAYOFF_B_4, (False, False): C.PAYOFF_C_4, (True, False): C.PAYOFF_D_4, } player.payoff = payoff_matrix[(player.cooperate, opponent(player))] # Payoff round 5 if player.round_number == 5: payoff_matrix = { (False, True): C.PAYOFF_A_5, (True, True): C.PAYOFF_B_5, (False, False): C.PAYOFF_C_5, (True, False): C.PAYOFF_D_5, } player.payoff = payoff_matrix[(player.cooperate, opponent(player))] # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Instructions(Page): pass class Decision(Page): form_model = 'player' form_fields = ['cooperate'] @staticmethod def before_next_page(player: Player, timeout_happened): set_payoff(player) class Results(Page): @staticmethod def vars_for_template(player: Player): if player.other == 0 and player.cooperate is False: return dict( same_choice = True, my_decision = "plantes exotiques", opponent_decision = "cooperate" ) elif player.other == 0 and player.cooperate is True: return dict( same_choice = False, my_decision = "plantes pollinisatrice", opponent_decision = "plantes exotiques" ) elif player.other == 1 and player.cooperate is False: return dict( same_choice = False, my_decision = "plantes exotiques", opponent_decision = "plantes pollinisatrices" ) else: return dict( same_choice=True, my_decision="plantes pollinisatrices", opponent_decision="false", ) page_sequence = [Introduction, Instructions, Decision, Results]