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 NUM_ROUNDS = 18 # before oct 17th, 33. also TASKS are only up to R27 # TASKS = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12', 'R13', 'R14', 'R15', 'R16', # 'R17', 'R18', 'R19', 'R20', 'R21', 'R22', 'R23', 'R24', 'R25', 'R26', 'R27'] # TASKS = ['R1', 'R2', 'R3', 'R4', 'R5', 'R6', 'R7', 'R8', 'R9', 'R10', 'R11', 'R12', 'R13', 'R14', 'R15', 'R16', # 'R17', 'R18'] TASKS = ['R1', 'R2'] # one job, baseline 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 # one job, dom VA_4 = 120 VB_4 = 80 TA_4 = 75 TB_4 = 82 VA_5 = 110 VB_5 = 90 TA5 = 75 TB_5 = 92 VA_6 = 100 VB_6 = 90 TA_6 = 70 TB_6 = 91 # one job, obv VA_7 = 120 VB_7 = 80 TA_7 = 75 TB_7 = 120 VA_8 = 110 VB_8 = 80 TA_8 = 80 TB_8 = 110 VA_9 = 100 VB_9 = 90 TA_9 = 60 TB_9 = 100 # two jobs, baseline VAA_1 = 100 VAB_1 = 90 VBA_1 = 70 VBB_1 = 80 TAA_1 = 55 TAB_1 = 28 TBA_1 = 65 TBB_1 = 55 VAA_2 = 100 VAB_2 = 90 VBA_2 = 75 VBB_2 = 80 TAA_2 = 60 TAB_2 = 50 TBA_2 = 70 TBB_2 = 65 VAA_3 = 120 VAB_3 = 100 VBA_3 = 80 VBB_3 = 90 TAA_3 = 75 TAB_3 = 70 TBA_3 = 80 TBB_3 = 85 # two jobs dom VAA_4 = 120 VAB_4 = 100 VBA_4 = 50 VBB_4 = 60 TAA_4 = 60 TAB_4 = 45 TBA_4 = 62 TBB_4 = 60 VAA_5 = 130 VAB_5 = 120 VBA_5 = 105 VBB_5 = 110 TAA_5 = 75 TAB_5 = 70 TBA_5 = 112 TBB_5 = 110 VAA_6 = 120 VAB_6 = 90 VBA_6 = 75 VBB_6 = 80 TAA_6 = 85 TAB_6 = 80 TBA_6 = 87 TBB_6 = 80 # two jobs obv VAA_7 = 100 VAB_7 = 80 VBA_7 = 50 VBB_7 = 55 TAA_7 = 60 TAB_7 = 50 TBA_7 = 70 TBB_7 = 65 VAA_8 = 100 VAB_8 = 90 VBA_8 = 50 VBB_8 = 60 TAA_8 = 65 TAB_8 = 60 TBA_8 = 70 TBB_8 = 65 VAA_9 = 120 VAB_9 = 90 VBA_9 = 75 VBB_9 = 77 TAA_9 = 80 TAB_9 = 55 TBA_9 = 85 TBB_9 = 82 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Vars for Profits profit_this_round = models.FloatField() profit_r1 = models.FloatField() profit_r2 = models.FloatField() profit_r3 = models.FloatField() profit_r4 = models.FloatField() profit_r5 = models.FloatField() profit_r6 = models.FloatField() profit_r7 = models.FloatField() profit_r8 = models.FloatField() profit_r9 = models.FloatField() profit2_r1 = models.FloatField() profit2_r2 = models.FloatField() profit2_r3 = models.FloatField() profit2_r4 = models.FloatField() profit2_r5 = models.FloatField() profit2_r6 = models.FloatField() profit2_r7 = models.FloatField() profit2_r8 = models.FloatField() profit2_r9 = models.FloatField() w_1 = models.FloatField( label="Your wage choice is:", max=60 ) w_2 = models.FloatField( blank=True, label="Your wage choice is:", max=55 ) w_3 = models.FloatField( label="Your wage choice is:", max=50 ) w_4 = models.FloatField( label="Your wage choice is:", max=120 ) w_5 = models.FloatField( label="Your wage choice is:", max=110 ) w_6 = models.FloatField( label="Your wage choice is:", max=100 ) w_7 = models.FloatField( label="Your wage choice is:", max=120 ) w_8 = models.FloatField( label="Your wage choice is:", max=110 ) w_9 = models.FloatField( label="Your wage choice is:", max=100 ) 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 ) wa_2 = models.FloatField( label="Your wage choice for Job 1 is:", max=100 ) wb_2 = models.FloatField( label="Your wage choice for Job 2 is:", max=90 ) wa_3 = models.FloatField( label="Your wage choice for Job 1 is:", max=120 ) wb_3 = models.FloatField( label="Your wage choice for Job 2 is:", max=100 ) wa_4 = models.FloatField( label="Your wage choice for Job 1 is:", max=120 ) wb_4 = models.FloatField( label="Your wage choice for Job 2 is:", max=100 ) wa_5 = models.FloatField( label="Your wage choice for Job 1 is:", max=130 ) wb_5 = models.FloatField( label="Your wage choice for Job 2 is:", max=120 ) wa_6 = models.FloatField( label="Your wage choice for Job 1 is:", max=120 ) wb_6 = models.FloatField( label="Your wage choice for Job 2 is:", max=90 ) wa_7 = models.FloatField( label="Your wage choice for Job 1 is:", max=100 ) wb_7 = models.FloatField( label="Your wage choice for Job 2 is:", max=80 ) wa_8 = models.FloatField( label="Your wage choice for Job 1 is:", max=100 ) wb_8 = models.FloatField( label="Your wage choice for Job 2 is:", max=90 ) wa_9 = models.FloatField( label="Your wage choice for Job 1 is:", max=120 ) wb_9 = models.FloatField( label="Your wage choice for Job 2 is:", max=90 ) payment_round = models.IntegerField() def final_payoff(player): # player.payment_round = random.randint(1, 18) player.payment_round = random.randint(1, 2) player.payoff = player.in_round(player.payment_round).profit_this_round def creating_session(subsession): if subsession.round_number == 1: for player in subsession.get_players(): # a = list(range(1, 19)) a = [1, 2] # random.shuffle(a) round_numbers = a player.participant.task_rounds = dict(zip(C.TASKS, round_numbers)) # PAGES class Round1(Page): timeout_seconds = 2 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): # temp = payoff_1(player.w_1, C.VA_1, C.VB_1, C.TA_1, C.TB_1) # player.profit_r1 = temp[0] # player.worker_r1 = temp[1] 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 # if player.participant.task_rounds['R1'] == 18: if player.participant.task_rounds['R1'] == 2: return player.final_payoff() 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): # w_2 = player.field_maybe_none('w_2') player.profit_r2 = payoff_1(player.field_maybe_none('w_2'), C.VA_2, C.VB_2, C.TA_2, C.TB_2) player.profit_this_round = player.profit_r2 # if player.participant.task_rounds['R2'] == 18: if player.participant.task_rounds['R2'] == 2: return player.final_payoff() 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 if player.participant.task_rounds['R3'] == 18: return player.final_payoff() class Round4(Page): form_model = "player" form_fields = ["w_4"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R4'] def before_next_page(player, timeout_happened): player.profit_r4 = payoff_1(player.w_4, C.VA_4, C.VB_4, C.TA_4, C.TB_4) player.profit_this_round = player.profit_r4 if player.participant.task_rounds['R4'] == 18: return player.final_payoff() class Round5(Page): form_model = "player" form_fields = ["w_5"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R5'] def before_next_page(player, timeout_happened): player.profit_r5 = payoff_1(player.w_5, C.VA_5, C.VB_5, C.TA_5, C.TB_5) player.profit_this_round = player.profit_r5 if player.participant.task_rounds['R5'] == 18: return player.final_payoff() class Round6(Page): form_model = "player" form_fields = ["w_6"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R6'] def before_next_page(player, timeout_happened): player.profit_r6 = payoff_1(player.w_6, C.VA_6, C.VB_6, C.TA_6, C.TB_6) player.profit_this_round = player.profit_r6 if player.participant.task_rounds['R6'] == 18: return player.final_payoff() class Round7(Page): form_model = "player" form_fields = ["w_7"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R7'] def before_next_page(player, timeout_happened): player.profit_r7 = payoff_1(player.w_7, C.VA_7, C.VB_7, C.TA_7, C.TB_7) player.profit_this_round = player.profit_r7 if player.participant.task_rounds['R7'] == 18: return player.final_payoff() class Round8(Page): form_model = "player" form_fields = ["w_8"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R8'] def before_next_page(player, timeout_happened): player.profit_r8 = payoff_1(player.w_8, C.VA_8, C.VB_8, C.TA_8, C.TB_8) player.profit_this_round = player.profit_r8 if player.participant.task_rounds['R8'] == 18: return player.final_payoff() class Round9(Page): form_model = "player" form_fields = ["w_9"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R9'] def before_next_page(player, timeout_happened): player.profit_r9 = payoff_1(player.w_9, C.VA_9, C.VB_9, C.TA_9, C.TB_9) player.profit_this_round = player.profit_r9 if player.participant.task_rounds['R9'] == 18: return player.final_payoff() class Round10(Page): form_model = "player" form_fields = ["wa_1", "wb_1"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R10'] def before_next_page(player, timeout_happened): player.profit2_r1 = 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.profit2_r1 if player.participant.task_rounds['R10'] == 18: return player.final_payoff() class Round11(Page): form_model = "player" form_fields = ["wa_2", "wb_2"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R11'] def before_next_page(player, timeout_happened): player.profit2_r2 = payoff_2(player.wa_2, player.wb_2, C.VAA_2, C.VAB_2, C.VBA_2, C.VBB_2, C.TAA_2, C.TAB_2, C.TBA_2, C.TBB_2) player.profit_this_round = player.profit2_r2 if player.participant.task_rounds['R11'] == 18: return player.final_payoff() class Round12(Page): form_model = "player" form_fields = ["wa_3", "wb_3"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R12'] def before_next_page(player, timeout_happened): player.profit2_r3 = payoff_2(player.wa_3, player.wb_3, C.VAA_3, C.VAB_3, C.VBA_3, C.VBB_3, C.TAA_3, C.TAB_3, C.TBA_3, C.TBB_3) player.profit_this_round = player.profit2_r3 if player.participant.task_rounds['R12'] == 18: return player.final_payoff() class Round13(Page): form_model = "player" form_fields = ["wa_4", "wb_4"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R13'] def before_next_page(player, timeout_happened): player.profit2_r4 = payoff_2(player.wa_4, player.wb_4, C.VAA_4, C.VAB_4, C.VBA_4, C.VBB_4, C.TAA_4, C.TAB_4, C.TBA_4, C.TBB_4) player.profit_this_round = player.profit2_r4 if player.participant.task_rounds['R13'] == 18: return player.final_payoff() class Round14(Page): form_model = "player" form_fields = ["wa_5", "wb_5"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R14'] def before_next_page(player, timeout_happened): player.profit2_r5 = payoff_2(player.wa_5, player.wb_5, C.VAA_5, C.VAB_5, C.VBA_5, C.VBB_5, C.TAA_5, C.TAB_5, C.TBA_5, C.TBB_5) player.profit_this_round = player.profit2_r5 if player.participant.task_rounds['R14'] == 18: return player.final_payoff() class Round15(Page): form_model = "player" form_fields = ["wa_6", "wb_6"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R15'] def before_next_page(player, timeout_happened): player.profit2_r6 = payoff_2(player.wa_6, player.wb_6, C.VAA_6, C.VAB_6, C.VBA_4, C.VBB_6, C.TAA_6, C.TAB_6, C.TBA_6, C.TBB_6) player.profit_this_round = player.profit2_r6 if player.participant.task_rounds['R15'] == 18: return player.final_payoff() class Round16(Page): form_model = "player" form_fields = ["wa_7", "wb_7"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R16'] def before_next_page(player, timeout_happened): player.profit2_r7 = payoff_2(player.wa_7, player.wb_7, C.VAA_7, C.VAB_7, C.VBA_7, C.VBB_7, C.TAA_7, C.TAB_7, C.TBA_7, C.TBB_7) player.profit_this_round = player.profit2_r7 if player.participant.task_rounds['R16'] == 18: return player.final_payoff() class Round17(Page): form_model = "player" form_fields = ["wa_8", "wb_8"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R17'] def before_next_page(player, timeout_happened): player.profit2_r8 = payoff_2(player.wa_8, player.wb_8, C.VAA_8, C.VAB_8, C.VBA_8, C.VBB_8, C.TAA_8, C.TAB_8, C.TBA_8, C.TBB_8) player.profit_this_round = player.profit2_r8 if player.participant.task_rounds['R17'] == 18: return player.final_payoff() class Round18(Page): form_model = "player" form_fields = ["wa_9", "wb_9"] @staticmethod def is_displayed(player): return player.round_number == player.participant.task_rounds['R18'] def before_next_page(player, timeout_happened): player.profit2_r9 = payoff_2(player.wa_9, player.wb_9, C.VAA_9, C.VAB_9, C.VBA_9, C.VBB_9, C.TAA_9, C.TAB_9, C.TBA_9, C.TBB_9) player.profit_this_round = player.profit2_r9 if player.participant.task_rounds['R18'] == 18: return player.final_payoff() class Results(Page): form_model = "player" def is_displayed(player: Player): # return player.round_number == 18 return player.round_number == 2 @staticmethod def vars_for_template(player): statec = player.payment_round payrange = player.participant.payoff w = -10 wa = -10 wb = -10 va = -10 vb = -10 ta = -10 tb = -10 vaa = -10 vab = -10 vba = -10 vbb = -10 taa = -10 tab = -10 tba = -10 tbb = -10 if statec == player.participant.task_rounds['R1']: w = player.in_round(player.participant.task_rounds['R1']).w_1 va = C.VA_1 vb = C.VB_1 ta = C.TA_1 tb = C.TB_1 elif statec == player.participant.task_rounds['R2']: # w = player.in_round(player.participant.task_rounds['R2']).w_2 w = player.in_round(player.participant.task_rounds['R2']).field_maybe_none('w_2') if w == None: w = 'You do not choose anything for this round' va = C.VA_2 vb = C.VB_2 ta = C.TA_2 tb = C.TB_2 elif statec == player.participant.task_rounds['R3']: w = player.in_round(player.participant.task_rounds['R3']).w_3 va = C.VA_3 vb = C.VB_3 ta = C.TA_3 tb = C.TB_3 elif statec == player.participant.task_rounds['R4']: w = player.in_round(player.participant.task_rounds['R4']).w_4 va = C.VA_4 vb = C.VB_4 ta = C.TA_4 tb = C.TB_4 elif statec == player.participant.task_rounds['R5']: w = player.in_round(player.participant.task_rounds['R5']).w_5 va = C.VA_5 vb = C.VB_5 ta = C.TA_5 tb = C.TB_5 elif statec == player.participant.task_rounds['R6']: w = player.in_round(player.participant.task_rounds['R6']).w_6 va = C.VA_6 vb = C.VB_6 ta = C.TA_6 tb = C.TB_6 elif statec == player.participant.task_rounds['R7']: w = player.in_round(player.participant.task_rounds['R7']).w_7 va = C.VA_7 vb = C.VB_7 ta = C.TA_7 tb = C.TB_7 elif statec == player.participant.task_rounds['R8']: w = player.in_round(player.participant.task_rounds['R8']).w_8 va = C.VA_8 vb = C.VB_8 ta = C.TA_8 tb = C.TB_8 elif statec == player.participant.task_rounds['R9']: w = player.in_round(player.participant.task_rounds['R9']).w_9 va = C.VA_9 vb = C.VB_9 ta = C.TA_9 tb = C.TB_9 elif statec == player.participant.task_rounds['R10']: wa = player.in_round(player.participant.task_rounds['R10']).wa_1 wb = player.in_round(player.participant.task_rounds['R10']).wb_1 vaa = C.VAA_1 vab = C.VAB_1 vba = C.VBA_1 vbb = C.VBB_1 taa = C.TAA_1 tab = C.TAB_1 tba = C.TBA_1 tbb = C.TBB_1 elif statec == player.participant.task_rounds['R11']: wa = player.in_round(player.participant.task_rounds['R11']).wa_2 wb = player.in_round(player.participant.task_rounds['R11']).wb_2 vaa = C.VAA_2 vab = C.VAB_2 vba = C.VBA_2 vbb = C.VBB_2 taa = C.TAA_2 tab = C.TAB_2 tba = C.TBA_2 tbb = C.TBB_2 elif statec == player.participant.task_rounds['R12']: wa = player.in_round(player.participant.task_rounds['R12']).wa_3 wb = player.in_round(player.participant.task_rounds['R12']).wb_3 vaa = C.VAA_3 vab = C.VAB_3 vba = C.VBA_3 vbb = C.VBB_3 taa = C.TAA_3 tab = C.TAB_3 tba = C.TBA_3 tbb = C.TBB_3 elif statec == player.participant.task_rounds['R13']: wa = player.in_round(player.participant.task_rounds['R13']).wa_4 wb = player.in_round(player.participant.task_rounds['R13']).wb_4 vaa = C.VAA_4 vab = C.VAB_4 vba = C.VBA_4 vbb = C.VBB_4 taa = C.TAA_4 tab = C.TAB_4 tba = C.TBA_4 tbb = C.TBB_4 elif statec == player.participant.task_rounds['R14']: wa = player.in_round(player.participant.task_rounds['R14']).wa_5 wb = player.in_round(player.participant.task_rounds['R14']).wb_5 vaa = C.VAA_5 vab = C.VAB_5 vba = C.VBA_5 vbb = C.VBB_5 taa = C.TAA_5 tab = C.TAB_5 tba = C.TBA_5 tbb = C.TBB_5 elif statec == player.participant.task_rounds['R15']: wa = player.in_round(player.participant.task_rounds['R15']).wa_6 wb = player.in_round(player.participant.task_rounds['R15']).wb_6 vaa = C.VAA_6 vab = C.VAB_6 vba = C.VBA_6 vbb = C.VBB_6 taa = C.TAA_6 tab = C.TAB_6 tba = C.TBA_6 tbb = C.TBB_6 elif statec == player.participant.task_rounds['R16']: wa = player.in_round(player.participant.task_rounds['R16']).wa_7 wb = player.in_round(player.participant.task_rounds['R16']).wb_7 vaa = C.VAA_7 vab = C.VAB_7 vba = C.VBA_7 vbb = C.VBB_7 taa = C.TAA_7 tab = C.TAB_7 tba = C.TBA_7 tbb = C.TBB_7 elif statec == player.participant.task_rounds['R17']: wa = player.in_round(player.participant.task_rounds['R17']).wa_8 wb = player.in_round(player.participant.task_rounds['R17']).wb_8 vaa = C.VAA_8 vab = C.VAB_8 vba = C.VBA_8 vbb = C.VBB_8 taa = C.TAA_8 tab = C.TAB_8 tba = C.TBA_8 tbb = C.TBB_8 elif statec == player.participant.task_rounds['R18']: wa = player.in_round(player.participant.task_rounds['R18']).wa_9 wb = player.in_round(player.participant.task_rounds['R18']).wb_9 vaa = C.VAA_9 vab = C.VAB_9 vba = C.VBA_9 vbb = C.VBB_9 taa = C.TAA_9 tab = C.TAB_9 tba = C.TBA_9 tbb = C.TBB_9 return dict( payrange=payrange >= 0, state=player.payment_round < 28, lottery_num=player.payment_round - 27, w=w, wa=wa, wb=wb, va=va, vb=vb, ta=ta, tb=tb, vaa=vaa, vab=vab, vba=vba, vbb=vbb, taa=taa, tab=tab, tba=tba, tbb=tbb, statec=statec, state1=(statec == player.participant.task_rounds['R1']) | (statec == player.participant.task_rounds['R2']), # (statec == player.participant.task_rounds['R3']) | # (statec == player.participant.task_rounds['R4']) | # (statec == player.participant.task_rounds['R5']) | # (statec == player.participant.task_rounds['R6']) | # (statec == player.participant.task_rounds['R7']) | # (statec == player.participant.task_rounds['R8']) | # (statec == player.participant.task_rounds['R9']), # state2=(statec == player.participant.task_rounds['R10']) | # (statec == player.participant.task_rounds['R11']) | # (statec == player.participant.task_rounds['R12']) | # (statec == player.participant.task_rounds['R13']) | # (statec == player.participant.task_rounds['R14']) | # (statec == player.participant.task_rounds['R15']) | # (statec == player.participant.task_rounds['R16']) | # (statec == player.participant.task_rounds['R17']) | # (statec == player.participant.task_rounds['R18']), ) # page_sequence = [Round1, Round2, Round3, Round4, Round5, Round6, # Round7, Round8, Round9, Round10, Round11, Round12, # Round13, Round14, Round15, Round16, Round17, Round18, Results] page_sequence = [Round1, Round2, Results] def payoff_1(wage, reva, revb, mina, minb): y = 0 if wage == None: y = 0 elif mina <= wage < minb: y = reva - wage elif wage >= minb: y = reva - wage + revb - wage return 0.5 * y def payoff_2(wage1, wage2, reva1, reva2, revb1, revb2, mina1, mina2, minb1, minb2): if mina1 <= wage1 < minb1: y1 = reva1 - wage1 elif wage1 >= minb1: y1 = reva1 - wage1 + revb1 - wage1 else: y1 = 0 if mina2 <= wage2 < minb2: y2 = reva2 - wage2 elif wage1 >= minb1: y2 = reva2 - wage2 + revb2 - wage2 else: y2 = 0 return 0.5 * (y1 + y2)