from otree.api import * import random from otree.models import player c = Currency doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'test1' PLAYERS_PER_GROUP = None TASKS = ['R1', 'R2'] NUM_ROUNDS = len(TASKS) # one job, baseline VA = [60, 0] VB = [40, 0] TA = [10, 0] TB = [30, 0] VAA = [0, 100] VAB = [0, 90] VBA = [0, 70] VBB = [0, 80] TAA = [0, 55] TAB = [0, 28] TBA = [0, 65] TBB = [0, 55] VA_1 = 60 VB_1 = 40 TA_1 = 10 TB_1 = 30 # vA_2 = 55 # vB_2 = 45 # tA_2 = 8 # tB_2 = 15 VA_3 = 50 VB_3 = 45 TA_3 = 5 TB_3 = 20 VAA_1 = 100 VAB_1 = 90 VBA_1 = 70 VBB_1 = 80 TAA_1 = 55 TAB_1 = 28 TBA_1 = 65 TBB_1 = 55 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Vars for Profits profit_r1 = models.FloatField() profit_r2 = models.FloatField() # profit_r3 = models.FloatField() profit_this_round = models.FloatField() # profit_lottery1 = models.FloatField() # profit_lottery2 = models.FloatField() profit_lottery3 = models.FloatField() w_record_1 = models.FloatField() w_record_11 = models.FloatField() w_record_a = models.FloatField() w_record_a1 = models.FloatField() w_record_b = models.FloatField() w_record_b1 = models.FloatField() w_1 = models.FloatField( label="Your wage choice is:", max=60 ) # w_2 = models.FloatField( # label="Your wage choice is:", # max=55 # ) # w_3 = models.FloatField( # label="Your wage choice is:", # max=50 # ) wa_1 = models.FloatField( label="Your wage choice for Job 1 is:", max=100 ) wb_1 = models.FloatField( label="Your wage choice for Job 2 is:", max=90 ) # lottery_choice1 = models.BooleanField( # choices=[[True, "Lottery A"], [False, "Lottery B"]], # widget=widgets.RadioSelect, # label="Your lottery choice is:" # ) # # lottery_choice2 = models.BooleanField( # choices=[[True, "Lottery A"], [False, "Lottery B"]], # widget=widgets.RadioSelect, # label="Your lottery choice is:" # ) lottery_choice3 = models.BooleanField( choices=[[True, "Lottery A"], [False, "Lottery B"]], widget=widgets.RadioSelect, label="Your lottery choice is:" ) payment_round = models.IntegerField() payment_round2 = models.IntegerField() # mix_round = models. valA = models.FloatField() valB = models.FloatField() wtaA = models.FloatField() wtaB = models.FloatField() valAa = models.FloatField() valAb = models.FloatField() valBa = models.FloatField() valBb = models.FloatField() wtaAa = models.FloatField() wtaAb = models.FloatField() wtaBa = models.FloatField() wtaBb = models.FloatField() # def final_payoff(self): # profits = [self.profit_r1, self.profit_r2, self.profit_r3, # self.profit_lottery1, self.profit_lottery2, self.profit_lottery3] # self.payment_round = random.randint(1, 6) # self.payoff = profits[self.payment_round - 1] def final_payoff(player): player.participant.payment_round = random.randint(1, 2) print(player.participant.payment_round) player.payment_round2 = player.participant.payment_round player.payoff = max(0, player.in_round(player.participant.payment_round).profit_this_round) def save_wages(player): player.participant.w_record_11 = player.in_round(player.payment_round2).w_record_1 player.participant.w_record_a1 = player.in_round(player.payment_round2).w_record_a player.participant.w_record_b1 = player.in_round(player.payment_round2).w_record_b def creating_session(subsession): if subsession.round_number == 1: for player in subsession.get_players(): round_numbers = list(range(1, C.NUM_ROUNDS + 1)) random.shuffle(round_numbers) player.participant.task_rounds = dict(zip(C.TASKS, round_numbers)) print(round_numbers) print(player.participant.task_rounds) # PAGES class Round1(Page): form_model = "player" form_fields = ["w_1"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R1'] def before_next_page(player, timeout_happened): player.profit_r1 = payoff_1(player.w_1, C.VA_1, C.VB_1, C.TA_1, C.TB_1) player.profit_this_round = player.profit_r1 player.w_record_1 = player.w_1 player.w_record_a = 1000 player.w_record_b = 1000 # player.payoff = player.profit_r1 class Round2(Page): # form_model = "player" # form_fields = ["w_2"] # # @staticmethod # def is_displayed(player): # return player.round_number == player.participant.task_rounds['R2'] # # def before_next_page(player, timeout_happened): # player.profit_r2 = payoff_1(player.w_2, C.VA_2, C.VB_2, C.TA_2, C.TB_2) # player.profit_this_round = player.profit_r2 # player.w_record_1 = player.w_2 # # player.payoff = player.profit_r2 form_model = "player" form_fields = ["wa_1", "wb_1"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R2'] def before_next_page(player, timeout_happened): player.profit_r2 = payoff_2(player.wa_1, player.wb_1, C.VAA_1, C.VAB_1, C.VBA_1, C.VBB_1, C.TAA_1, C.TAB_1, C.TBA_1, C.TBB_1) player.profit_this_round = player.profit_r2 player.w_record_a = player.wa_1 player.w_record_b = player.wb_1 player.w_record_1 = 1000 # class Round3(Page): # form_model = "player" # form_fields = ["w_3"] # # @staticmethod # def is_displayed(player): # return player.round_number == player.participant.task_rounds['R3'] # # def before_next_page(player, timeout_happened): # player.profit_r3 = payoff_1(player.w_3, C.VA_3, C.VB_3, C.TA_3, C.TB_3) # player.profit_this_round = player.profit_r3 # player.w_record_1 = player.w_3 # player.payoff = player.profit_r3 # # class Lottery1(Page): # form_model = "player" # form_fields = ["lottery_choice1"] # # @staticmethod # def is_displayed(player): # return player.round_number == 3 # # def vars_for_template(player): # return dict( # l1a=C.VA_1 - C.TA_1, # l1b=0, # l2a=C.VA_1 - C.TB_1, # l2b=C.VB_1 - C.TB_1 # ) # # def before_next_page(player, timeout_happened): # player.profit_lottery1 = lottery_1(player.lottery_choice1, C.VA_1, C.VB_1, C.TA_1, # C.TB_1) # # # class Lottery2(Page): # form_model = "player" # form_fields = ["lottery_choice2"] # # @staticmethod # def is_displayed(player): # return player.round_number == 3 # # def vars_for_template(player): # return dict( # l1a=C.VA_2 - C.TA_2, # l1b=0, # l2a=C.VA_2 - C.TB_2, # l2b=C.VB_2 - C.TB_2 # ) # # def before_next_page(player, timeout_happened): # player.profit_lottery2 = lottery_1(player.lottery_choice2, C.VA_2, C.VB_2, C.TA_2, # C.TB_2) class Lottery3(Page): form_model = "player" form_fields = ["lottery_choice3"] @staticmethod def is_displayed(player): return player.round_number == 2 def vars_for_template(player): return dict( l1a=C.VA_3 - C.TA_3, l1b=0, l2a=C.VA_3 - C.TB_3, l2b=C.VB_3 - C.TB_3 ) def before_next_page(player, timeout_happened): player.profit_lottery3 = lottery_1(player.lottery_choice3, C.VA_3, C.VB_3, C.TA_3, C.TB_3) player.final_payoff() player.save_wages() class Results1(WaitPage): pass class Results(Page): pass # def before_next_page(player, timeout_happened): # player.final_payoff() page_sequence = [Round1, Round2, Lottery3] def payoff_1(wage, reva, revb, mina, minb): x = random.randint(0, 1) if mina <= wage < minb: if x == 0: y = reva - wage else: y = 0 elif wage >= minb: if x == 0: y = reva - wage else: y = revb - wage else: y = 0 return y def payoff_2(wage1, wage2, reva1, reva2, revb1, revb2, mina1, mina2, minb1, minb2): x = random.randint(0, 1) if mina1 <= wage1 < minb1: if x == 0: y1 = reva1 - wage1 else: y1 = 0 elif minb1 <= wage1: if x == 0: y1 = reva1 - wage1 else: y1 = revb1 - wage1 else: y1 = 0 if mina2 <= wage2 < minb2: if x == 1: y2 = reva2 - wage2 else: y2 = 0 elif minb2 <= wage2: if x == 1: y2 = reva2 - wage2 else: y2 = revb2 - wage2 else: y2 = 0 return y1 + y2 def payoff_3(wage, reva, revb, mina, minb): x = random.randint(0, 2) z = random.randint(0, 1) if mina <= wage < minb: if x == 0: y = reva - wage elif (x == 1) and (z == 0): y = reva - wage elif (x == 1) and (z == 1): y = 0 elif x == 2: y = 0 elif wage >= minb: if x == 0: y = reva - wage elif (x == 1) and (z == 0): y = reva - wage elif (x == 1) and (z == 1): y = revb - wage elif x == 2: y = revb - wage else: y = 0 return y def lottery_1(choice, reva, revb, mina, minb): x = random.randint(1, 100) if choice: if x <= 50: return reva - mina else: return 0 else: if x <= 50: return reva - minb else: return revb - minb def lottery_2(choice, revAa, revAb, revBa, revBb, minAa, minAb, minBa, minBb): x = random.randint(0, 1) if choice == 1: if x == 0: return revAa - minAa else: return revAb - minAb elif choice == 2: if x == 0: return revAa - minAa - revBb - minBb else: return revAb - minBb elif choice == 3: if x == 0: return revAa - minBa else: return revAb - minAb + revBa - minBa elif choice == 4: if x == 0: return revAa - minBa + revBb - minBb else: return revAb - minBb + revBa - minBa