from otree.api import * #import pandas as pd import time #c = cu doc = 'During this short experiment you will see various games as in the image below. We will first explain the meaning of this table.' def make_group(comma_delim_string): return [int(x) for x in comma_delim_string.split(',')] class C(BaseConstants): NAME_IN_URL = 'T5_Without_Game_Miner' #Set variable PLAYERS_PER_GROUP = None when running a session PLAYERS_PER_GROUP = None NUM_ROUNDS = 40 GAMES_S1 = 40 GAMES_S2 = 0 #Payoff screen: Instructions PAYOFF_Instructions = (1, 2, 2, 6, 7, 4, 4, 3) #Payoff screen: Stage 1 PAYOFF_G1AG2_1 = (6, 10, 11, 9, 5, 6, 10, 10) #Payoff screen: Game 1 OR Game 2 PAYOFF_G1OG2_1 = (1, 10, 6, 9, 2, 6, 7, 10) PAYOFF_G1OG2_2 = (6, 10, 11, 9, 5, 6, 10, 10) Profit = 0 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session if subsession.round_number == 1: num_participants = session.num_participants fn = 'T5_Without_Game_Miner/groups{}.csv'.format(num_participants) with open(fn) as f: matrices = [] for line in f: line = line.strip() group_specs = line.split(',,') matrix = [make_group(spec) for spec in group_specs] matrices.append(matrix) session.matrices = matrices this_round_matrix = session.matrices[subsession.round_number - 1] subsession.set_group_matrix(this_round_matrix) class Group(BaseGroup): pass def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) class Player(BasePlayer): my_choiceTrialP1_A = models.IntegerField(label='Exercice: If Player 1 chooses row B and Player 2 chooses column C, what is the payoff for Player 1?', min=7, max=7) my_choiceTrialP1_B = models.IntegerField(label='If Player 2 chooses column C and Player 1 chooses row B, what is the payoff for Player 2?', min=4, max=4) my_choiceTrialP2_A = models.IntegerField(label='Exercice: If Player 2 chooses row B and Player 1 chooses column C, what is the payoff for Player 2?', min=6, max=6) my_choiceTrialP2_B = models.IntegerField(label='If Player 1 chooses column C and Player 2 chooses row B, what is the payoff for Player 1?', min=2, max=2) choiceG1ANDG2_A = models.IntegerField(initial='-9999') choiceG1ANDG2_B = models.IntegerField(initial='-9999') myDecision = models.StringField() otherDecision = models.StringField() choiceG1orG2 = models.IntegerField(initial='-9999') myGame = models.StringField() otherGame = models.StringField() Game1P1Decision = models.IntegerField(initial='-9999') Game2P1Decision = models.IntegerField(initial='-9999') Game1P2Decision = models.IntegerField(initial='-9999') Game2P2Decision = models.IntegerField(initial='-9999') #my_Name = models.StringField(label='We will send you the results and your payment by Bizum. Please write your phone number. ', blank=True) #my_StrategyST1 = models.LongStringField(label='Please describe how you made your decisions in Stage 1.') #my_StrategyST2 = models.LongStringField(label='Please describe how you made your decisions in Stage 2.') #Profit = models.IntegerField() my_Strategy = models.LongStringField(label='Please describe how you made your decisions and when you changed, why?') CurrentRoundG1 = models.IntegerField() CurrentRoundG2 = models.IntegerField() Time_spent = models.FloatField() decision_time = models.FloatField() def record_decision_time(self): self.decision_time = time.time() - self.participant.vars['start_time'] self.participant.vars['start_time'] = time.time() def role(self): if self.id_in_group == 1: return 'Player 1' if self.id_in_group == 2: return 'Player 2' def other_player(player: Player): group = player.group return player.get_others_in_group()[0] class Welcome(Page): form_model = 'player' def is_displayed(player: Player): return player.round_number == 1 class InstructionsP1(Page): form_model = 'player' form_fields = ['my_choiceTrialP1_A', 'my_choiceTrialP1_B'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number == 1 def vars_for_template(player: Player): return dict( intro1 = C.PAYOFF_Instructions[0], intro2 = C.PAYOFF_Instructions[1], intro3 = C.PAYOFF_Instructions[2], intro4 = C.PAYOFF_Instructions[3], intro5 = C.PAYOFF_Instructions[4], intro6 = C.PAYOFF_Instructions[5], intro7 = C.PAYOFF_Instructions[6], intro8 = C.PAYOFF_Instructions[7], ) def before_next_page(player: Player, timeout_happened): player.participant.vars['start_time'] = time.time() class InstructionsP2(Page): form_model = 'player' form_fields = ['my_choiceTrialP2_A', 'my_choiceTrialP2_B'] def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number == 1 def vars_for_template(player: Player): return dict( intro1 = C.PAYOFF_Instructions[1], intro2 = C.PAYOFF_Instructions[0], intro3 = C.PAYOFF_Instructions[5], intro4 = C.PAYOFF_Instructions[4], intro5 = C.PAYOFF_Instructions[3], intro6 = C.PAYOFF_Instructions[2], intro7 = C.PAYOFF_Instructions[7], intro8 = C.PAYOFF_Instructions[6], ) def before_next_page(player: Player, timeout_happened): player.participant.vars['start_time'] = time.time() #Stage 1 class Game1Stage1P1(Page): form_model = 'player' form_fields = ['choiceG1ANDG2_A'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number <= C.GAMES_S1 def vars_for_template(player: Player): return dict( TotalRoundsG1 = C.GAMES_S1, payoffG1G2_11 = C.PAYOFF_G1AG2_1[0], payoffG1G2_12 = C.PAYOFF_G1AG2_1[1], payoffG1G2_13 = C.PAYOFF_G1AG2_1[2], payoffG1G2_14 = C.PAYOFF_G1AG2_1[3], payoffG1G2_15 = C.PAYOFF_G1AG2_1[4], payoffG1G2_16 = C.PAYOFF_G1AG2_1[5], payoffG1G2_17 = C.PAYOFF_G1AG2_1[6], payoffG1G2_18 = C.PAYOFF_G1AG2_1[7], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game1Stage1P2(Page): form_model = 'player' form_fields = ['choiceG1ANDG2_A'] def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number <= C.GAMES_S1 def vars_for_template(player: Player): return dict( TotalRoundsG1 = C.GAMES_S1, payoffG1G2_11 = C.PAYOFF_G1AG2_1[1], payoffG1G2_12 = C.PAYOFF_G1AG2_1[0], payoffG1G2_13 = C.PAYOFF_G1AG2_1[5], payoffG1G2_14 = C.PAYOFF_G1AG2_1[4], payoffG1G2_15 = C.PAYOFF_G1AG2_1[3], payoffG1G2_16 = C.PAYOFF_G1AG2_1[2], payoffG1G2_17 = C.PAYOFF_G1AG2_1[7], payoffG1G2_18 = C.PAYOFF_G1AG2_1[6], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game1ANDGame2WaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): for p in group.get_players(): other = other_player(p) if(p.role() == 'Player 1' and p.choiceG1ANDG2_A == 1 and other.choiceG1ANDG2_A == 1): p.payoff = C.PAYOFF_G1AG2_1[0] elif(p.role() == 'Player 1' and p.choiceG1ANDG2_A == 1 and other.choiceG1ANDG2_A == 2): p.payoff = C.PAYOFF_G1AG2_1[2] elif(p.role() == 'Player 1' and p.choiceG1ANDG2_A == 2 and other.choiceG1ANDG2_A == 1): p.payoff = C.PAYOFF_G1AG2_1[4] elif(p.role() == 'Player 1' and p.choiceG1ANDG2_A == 2 and other.choiceG1ANDG2_A == 2): p.payoff = C.PAYOFF_G1AG2_1[6] elif(p.role() == 'Player 2' and other.choiceG1ANDG2_A == 1 and p.choiceG1ANDG2_A == 1): p.payoff = C.PAYOFF_G1AG2_1[1] elif(p.role() == 'Player 2' and other.choiceG1ANDG2_A == 1 and p.choiceG1ANDG2_A == 2): p.payoff = C.PAYOFF_G1AG2_1[3] elif(p.role() == 'Player 2' and other.choiceG1ANDG2_A == 2 and p.choiceG1ANDG2_A == 1): p.payoff = C.PAYOFF_G1AG2_1[5] elif(p.role() == 'Player 2' and other.choiceG1ANDG2_A == 2 and p.choiceG1ANDG2_A == 2): p.payoff = C.PAYOFF_G1AG2_1[7] class PayoffsStage1P1(Page): def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number <= C.GAMES_S1 def vars_for_template(player: Player): other = other_player(player) if(player.choiceG1ANDG2_A == 1): player.myDecision = "Row A" else: player.myDecision = "Row B" if(other.choiceG1ANDG2_A == 1): player.otherDecision = "Column C" else: player.otherDecision = "Column D" return dict( TotalRoundsG1 = C.GAMES_S1, otherPayoff = other.payoff ) class PayoffsStage1P2(Page): def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number <= C.GAMES_S1 def vars_for_template(player: Player): other = other_player(player) if(player.choiceG1ANDG2_A == 1): player.myDecision = "Row A" else: player.myDecision = "Row B" if(other.choiceG1ANDG2_A == 1): player.otherDecision = "Column C" else: player.otherDecision = "Column D" return dict( TotalRoundsG1 = C.GAMES_S1, otherPayoff = other.payoff ) #Stage 2 class InstructionsStage2P1(Page): form_model = 'player' def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number == C.GAMES_S1 def before_next_page(player: Player, timeout_happened): player.participant.vars['start_time'] = time.time() class InstructionsStage2P2(Page): form_model = 'player' def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number == C.GAMES_S1 def before_next_page(player: Player, timeout_happened): player.participant.vars['start_time'] = time.time() class Game1ORGame2P1Decision(Page): form_model = 'player' form_fields = ['choiceG1orG2'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_11 = C.PAYOFF_G1OG2_1[0], payoffG1OG2_12 = C.PAYOFF_G1OG2_1[1], payoffG1OG2_13 = C.PAYOFF_G1OG2_1[2], payoffG1OG2_14 = C.PAYOFF_G1OG2_1[3], payoffG1OG2_15 = C.PAYOFF_G1OG2_1[4], payoffG1OG2_16 = C.PAYOFF_G1OG2_1[5], payoffG1OG2_17 = C.PAYOFF_G1OG2_1[6], payoffG1OG2_18 = C.PAYOFF_G1OG2_1[7], payoffG1OG2_21 = C.PAYOFF_G1OG2_2[0], payoffG1OG2_22 = C.PAYOFF_G1OG2_2[1], payoffG1OG2_23 = C.PAYOFF_G1OG2_2[2], payoffG1OG2_24 = C.PAYOFF_G1OG2_2[3], payoffG1OG2_25 = C.PAYOFF_G1OG2_2[4], payoffG1OG2_26 = C.PAYOFF_G1OG2_2[5], payoffG1OG2_27 = C.PAYOFF_G1OG2_2[6], payoffG1OG2_28 = C.PAYOFF_G1OG2_2[7], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game1ORGame2P2(Page): def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_11 = C.PAYOFF_G1OG2_1[1], payoffG1OG2_12 = C.PAYOFF_G1OG2_1[0], payoffG1OG2_13 = C.PAYOFF_G1OG2_1[5], payoffG1OG2_14 = C.PAYOFF_G1OG2_1[4], payoffG1OG2_15 = C.PAYOFF_G1OG2_1[3], payoffG1OG2_16 = C.PAYOFF_G1OG2_1[2], payoffG1OG2_17 = C.PAYOFF_G1OG2_1[7], payoffG1OG2_18 = C.PAYOFF_G1OG2_1[6], payoffG1OG2_21 = C.PAYOFF_G1OG2_2[1], payoffG1OG2_22 = C.PAYOFF_G1OG2_2[0], payoffG1OG2_23 = C.PAYOFF_G1OG2_2[5], payoffG1OG2_24 = C.PAYOFF_G1OG2_2[4], payoffG1OG2_25 = C.PAYOFF_G1OG2_2[3], payoffG1OG2_26 = C.PAYOFF_G1OG2_2[2], payoffG1OG2_27 = C.PAYOFF_G1OG2_2[7], payoffG1OG2_28 = C.PAYOFF_G1OG2_2[6], ) class Game1P1Decision(Page): form_model = 'player' form_fields = ['Game1P1Decision'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.choiceG1orG2 == 1 and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_11 = C.PAYOFF_G1OG2_1[0], payoffG1OG2_12 = C.PAYOFF_G1OG2_1[1], payoffG1OG2_13 = C.PAYOFF_G1OG2_1[2], payoffG1OG2_14 = C.PAYOFF_G1OG2_1[3], payoffG1OG2_15 = C.PAYOFF_G1OG2_1[4], payoffG1OG2_16 = C.PAYOFF_G1OG2_1[5], payoffG1OG2_17 = C.PAYOFF_G1OG2_1[6], payoffG1OG2_18 = C.PAYOFF_G1OG2_1[7], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game2P1Decision(Page): form_model = 'player' form_fields = ['Game2P1Decision'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.choiceG1orG2 == 2 and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_21 = C.PAYOFF_G1OG2_2[0], payoffG1OG2_22 = C.PAYOFF_G1OG2_2[1], payoffG1OG2_23 = C.PAYOFF_G1OG2_2[2], payoffG1OG2_24 = C.PAYOFF_G1OG2_2[3], payoffG1OG2_25 = C.PAYOFF_G1OG2_2[4], payoffG1OG2_26 = C.PAYOFF_G1OG2_2[5], payoffG1OG2_27 = C.PAYOFF_G1OG2_2[6], payoffG1OG2_28 = C.PAYOFF_G1OG2_2[7], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game1ORGame2WaitPage(WaitPage): pass class Game1P2Decision(Page): form_model = 'player' form_fields = ['Game1P2Decision'] def is_displayed(player: Player): other = other_player(player) return player.role() == 'Player 2' and other.choiceG1orG2 == 1 and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_11 = C.PAYOFF_G1OG2_1[1], payoffG1OG2_12 = C.PAYOFF_G1OG2_1[0], payoffG1OG2_13 = C.PAYOFF_G1OG2_1[5], payoffG1OG2_14 = C.PAYOFF_G1OG2_1[4], payoffG1OG2_15 = C.PAYOFF_G1OG2_1[3], payoffG1OG2_16 = C.PAYOFF_G1OG2_1[2], payoffG1OG2_17 = C.PAYOFF_G1OG2_1[7], payoffG1OG2_18 = C.PAYOFF_G1OG2_1[6], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class Game2P2Decision(Page): form_model = 'player' form_fields = ['Game2P2Decision'] def is_displayed(player: Player): other = other_player(player) return player.role() == 'Player 2' and other.choiceG1orG2 == 2 and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, payoffG1OG2_21 = C.PAYOFF_G1OG2_2[1], payoffG1OG2_22 = C.PAYOFF_G1OG2_2[0], payoffG1OG2_23 = C.PAYOFF_G1OG2_2[5], payoffG1OG2_24 = C.PAYOFF_G1OG2_2[4], payoffG1OG2_25 = C.PAYOFF_G1OG2_2[3], payoffG1OG2_26 = C.PAYOFF_G1OG2_2[2], payoffG1OG2_27 = C.PAYOFF_G1OG2_2[7], payoffG1OG2_28 = C.PAYOFF_G1OG2_2[6], ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant player.record_decision_time() class ResultsWaitPageStage2(WaitPage): @staticmethod def after_all_players_arrive(group: Group): for p in group.get_players(): other = other_player(p) if(p.role() == 'Player 1' and p.choiceG1orG2 == 1 and p.Game1P1Decision == 1 and other.Game1P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_1[0] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 1 and p.Game1P1Decision == 1 and other.Game1P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_1[2] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 1 and p.Game1P1Decision == 2 and other.Game1P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_1[4] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 1 and p.Game1P1Decision == 2 and other.Game1P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_1[6] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 2 and p.Game2P1Decision == 1 and other.Game2P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_2[0] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 2 and p.Game2P1Decision == 1 and other.Game2P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_2[2] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 2 and p.Game2P1Decision == 2 and other.Game2P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_2[4] elif (p.role() == 'Player 1' and p.choiceG1orG2 == 2 and p.Game2P1Decision == 2 and other.Game2P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_2[6] if(p.role() == 'Player 2' and other.choiceG1orG2 == 1 and other.Game1P1Decision == 1 and p.Game1P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_1[1] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 1 and other.Game1P1Decision == 1 and p.Game1P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_1[3] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 1 and other.Game1P1Decision == 2 and p.Game1P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_1[5] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 1 and other.Game1P1Decision == 2 and p.Game1P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_1[7] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 2 and other.Game2P1Decision == 1 and p.Game2P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_2[1] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 2 and other.Game2P1Decision == 1 and p.Game2P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_2[3] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 2 and other.Game2P1Decision == 2 and p.Game2P2Decision == 1): p.payoff = C.PAYOFF_G1OG2_2[5] elif (p.role() == 'Player 2' and other.choiceG1orG2 == 2 and other.Game2P1Decision == 2 and p.Game2P2Decision == 2): p.payoff = C.PAYOFF_G1OG2_2[7] class PayoffsStage2P1(Page): def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): other = other_player(player) if(player.choiceG1orG2 == 1): player.myGame = "Game 1" else: player.myGame = "Game 2" if(player.Game1P1Decision == 1 or player.Game2P1Decision == 1): player.myDecision = "Row A" else: player.myDecision = "Row B" if(other.Game1P2Decision == 1 or other.Game2P2Decision == 1): player.otherDecision = "Column C" else: player.otherDecision = "Column D" return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, otherPayoff = other.payoff ) class PayoffsStage2P2(Page): def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number > C.GAMES_S1 def vars_for_template(player: Player): other = other_player(player) if(other.choiceG1orG2 == 1): player.myGame = "Game 1" else: player.myGame = "Game 2" if(player.Game1P2Decision == 1 or player.Game2P2Decision == 1): player.myDecision = "Row A" else: player.myDecision = "Row B" if(other.Game1P1Decision == 1 or other.Game2P1Decision == 1): player.otherDecision = "Column C" else: player.otherDecision = "Column D" return dict( CurrentRoundG2 = player.round_number - C.GAMES_S1, otherPayoff = other.payoff ) #Total Payoff class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): for p in group.get_players(): other = other_player(p) # prev_player = player.in_round(player.round_number - 1) class PayoffsP1(Page): def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number == C.NUM_ROUNDS def vars_for_template(player: Player): return dict( myProfit = 100 ) class PayoffsP2(Page): def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number == C.NUM_ROUNDS def vars_for_template(player: Player): return dict( myProfit = 200 ) class QuestionsP1(Page): form_model = 'player' form_fields = ['my_Strategy'] def is_displayed(player: Player): return player.role() == 'Player 1' and player.round_number == C.NUM_ROUNDS class QuestionsP2(Page): form_model = 'player' form_fields = ['my_Strategy'] def is_displayed(player: Player): return player.role() == 'Player 2' and player.round_number == C.NUM_ROUNDS page_sequence = [Welcome, InstructionsP1, InstructionsP2, Game1Stage1P1, Game1Stage1P2, Game1ANDGame2WaitPage, PayoffsStage1P1, PayoffsStage1P2, ResultsWaitPage, QuestionsP1, QuestionsP2]