import random import string from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'experiment' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 payoffShowup = cu(3) payoffPics = cu(0.5) payoffDisc = cu(3) payoffAlloc = 30 payoffInit = '1,50' painter1 = 'Klee' painter1_long = 'Paul Klee' painter2 = 'Kandinsky' painter2_long = 'Wassily Kandinsky' painter3 = 'Marc' painter3_long = 'Franz Marc' painter4 = 'Macke' painter4_long = 'August Macke' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): code = models.StringField() for i in range(1, 6): locals()['choice1_' + str(i)] = models.IntegerField(choices=[[1, 'A'],[2, 'B']], label="In diesem Paar bevorzuge ich Gemälde", widget=widgets.RadioSelect) locals()['choice2_' + str(i)] = models.IntegerField(choices=[[1, 'A'], [2, 'B']], label="In diesem Paar bevorzuge ich Gemälde", widget=widgets.RadioSelect) locals()['alloc_' + str(i)] = models.IntegerField() locals()['dict_' + str(i)] = models.IntegerField() locals()['timeout_alloc_' + str(i)] = models.BooleanField() locals()['timeout_dict_' + str(i)] = models.BooleanField() del i choice1_6 = models.IntegerField(choices=[[1, C.painter1],[2, C.painter2]], label="Das Gemälde ist von", widget=widgets.RadioSelect) choice1_7 = models.IntegerField(choices=[[1, C.painter1],[2, C.painter2]], label="Das Gemälde ist von", widget=widgets.RadioSelect) choice1 = models.IntegerField() paintGroup1 = models.StringField() numGroup1 = models.IntegerField() payoff1 = models.CurrencyField() choice2_6 = models.IntegerField(choices=[[1, C.painter3],[2, C.painter4]], label="Das Gemälde ist von", widget=widgets.RadioSelect) choice2_7 = models.IntegerField(choices=[[1, C.painter3],[2, C.painter4]], label="Das Gemälde ist von", widget=widgets.RadioSelect) choice2 = models.IntegerField() paintGroup2 = models.StringField() numGroup2 = models.IntegerField() payoff2 = models.CurrencyField() numGroup = models.IntegerField() payoff4 = models.CurrencyField() demoGender = models.IntegerField(choices=[[1, 'männlich'],[0, 'weiblich'],[99, 'keine Angabe']],widget=widgets.RadioSelect, label="Was ist Ihr Geschlecht?") demoEducation = models.IntegerField(choices=[[1, 'PhD/Doktorat'],[2, 'Master'],[3, 'Bachelor'],[4, 'Matura/Abitur']], label="Was ist der höchste Abschluss, den Sie haben?") demoParty = models.IntegerField(choices=[[1, 'ÖVP'],[2, 'SPÖ'],[3, 'FPÖ'],[4, 'Grüne'],[5, 'NEOS'],[99, 'Andere']], label="Welche Partei werden Sie bei den nächsten Wahlen am ehesten wählen?") demoAge = models.IntegerField(label="Wie alt sind Sie?") demoEmployment = models.IntegerField(choices=[[1, 'Student'],[2, 'Employed'],[3, 'Self-employed'],[4, 'Unemployed']], label="What is your current employment status?") demoStudy = models.IntegerField(choices=[[1, 'Wirtschaftswissenschaft'],[2, 'Rechtswissenschaft'],[3, 'Naturwissenschaft'],[4, 'Geisteswissenschaft'],[5, 'Medizinwissenschaft'],[99, 'Andere']], label="Was studieren Sie?") # demoStudy = models.StringField(label="Was studieren Sie?") def calculate_choices1(group): players = group.get_players() for player in players: player.choice1 = 0 if player.choice1_1==1: player.choice1 += 1 if player.choice1_2==2: player.choice1 += 1 if player.choice1_3==1: player.choice1 += 1 if player.choice1_4==2: player.choice1 += 1 if player.choice1_5==2: player.choice1 += 1 choices = sorted(players, key=lambda x: x.choice1, reverse=True) for player in choices: if choices.index(player) < len(choices)/2: player.paintGroup1 = C.painter1 player.numGroup1 = 1 else: player.paintGroup1 = C.painter2 player.numGroup1 = 2 def calculate_choices2(group): players = group.get_players() for player in players: player.choice2 = 0 if player.choice2_1==1: player.choice2 += 1 if player.choice2_2==2: player.choice2 += 1 if player.choice2_3==1: player.choice2 += 1 if player.choice2_4==2: player.choice2 += 1 if player.choice2_5==2: player.choice2 += 1 players1 = sorted([player for player in players if player.numGroup1 == 1], key=lambda x: x.choice2, reverse=True) players2 = sorted([player for player in players if player.numGroup1 == 2], key=lambda x: x.choice2, reverse=True) # choices = sorted(players, key=lambda x: (x.choice2, x.choice1), reverse=True) for player in players1: if players1.index(player) < len(players1) / 2: player.paintGroup2 = C.painter3 player.numGroup2 = 3 player.numGroup = 13 else: player.paintGroup2 = C.painter4 player.numGroup2 = 4 player.numGroup = 14 for player in players2: if players2.index(player) <= len(players2) / 2 - 1: player.paintGroup2 = C.painter3 player.numGroup2 = 3 player.numGroup = 23 else: player.paintGroup2 = C.painter4 player.numGroup2 = 4 player.numGroup = 24 if len(players1) % 2 == 1 and len(players2) % 2 == 1 and players1[int(len(players1)/2)].choice2 < players2[int(len(players2)/2)].choice2: players1[int(len(players1)/2)].paintGroup2 = C.painter4 players1[int(len(players1)/2)].numGroup2 = 4 players1[int(len(players1)/2)].numGroup = 14 players2[int(len(players2)/2)].paintGroup2 = C.painter3 players2[int(len(players2)/2)].numGroup2 = 3 players2[int(len(players2)/2)].numGroup = 23 def calculate_payoff12(player: Player, timeout_happened, part): payoff12 = 0 if getattr(player, 'choice' + str(part) + '_6')==1: payoff12 += C.payoffPics if getattr(player, 'choice' + str(part) + '_7')==2: payoff12 += C.payoffPics setattr(player, 'payoff' + str(part), cu(payoff12)) def calculate_payoff4(player): player.payoff4 = cu((C.payoffAlloc - getattr(player, 'dict_' + str(random.randint(1,4))))/10) def calculate_payoff(player): player.payoff = player.payoff1 + player.payoff2 #+ player.payoff4 def tester(group): players = group.get_players() for player in players: player.paintGroup1 = C.painter1 player.paintGroup2 = C.painter3 player.payoff1 = cu(0.5) player.payoff2 = cu(0.5) # PAGES class instructions1(Page): @staticmethod def vars_for_template(player: Player): player.code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6)) return dict(code=player.code) class instructions2(Page): pass class instructions3(Page): timeout_seconds = 90 class instructions4(Page): timeout_seconds = 30 class instructions5(Page): timeout_seconds = 30 class intro1a(Page): timeout_seconds = 60 class intro1b(Page): timeout_seconds = 30 class paint1_1(Page): form_model = 'player' form_fields = ['choice1_1'] timeout_seconds = 30 class paint1_2(Page): form_model = 'player' form_fields = ['choice1_2'] timeout_seconds = 30 class paint1_3(Page): form_model = 'player' form_fields = ['choice1_3'] timeout_seconds = 30 class paint1_4(Page): form_model = 'player' form_fields = ['choice1_4'] timeout_seconds = 30 class paint1_5(Page): form_model = 'player' form_fields = ['choice1_5'] timeout_seconds = 30 class ChoiceWaitPage1(WaitPage): after_all_players_arrive = 'calculate_choices1' class result1(Page): timeout_seconds = 30 class intro1d(Page): timeout_seconds = 90 class paintings1(Page): timeout_seconds = 30 class Waiter(WaitPage): # after_all_players_arrive = 'tester' pass class paint1_67(Page): form_model = 'player' form_fields = ['choice1_6', 'choice1_7'] timeout_seconds = 240 @staticmethod def before_next_page(player: Player, timeout_happened): calculate_payoff12(player, timeout_happened, 1) class payoff1(Page): timeout_seconds = 30 class intro2a(Page): timeout_seconds = 60 class intro2b(Page): timeout_seconds = 30 class paint2_1(Page): form_model = 'player' form_fields = ['choice2_1'] timeout_seconds = 30 class paint2_2(Page): form_model = 'player' form_fields = ['choice2_2'] timeout_seconds = 30 class paint2_3(Page): form_model = 'player' form_fields = ['choice2_3'] timeout_seconds = 30 class paint2_4(Page): form_model = 'player' form_fields = ['choice2_4'] timeout_seconds = 30 class paint2_5(Page): form_model = 'player' form_fields = ['choice2_5'] timeout_seconds = 30 class ChoiceWaitPage2(WaitPage): after_all_players_arrive = 'calculate_choices2' class result2(Page): timeout_seconds = 30 class intro2d(Page): timeout_seconds = 90 class paintings2(Page): timeout_seconds = 30 class paint2_67(Page): form_model = 'player' form_fields = ['choice2_6', 'choice2_7'] timeout_seconds = 240 @staticmethod def before_next_page(player: Player, timeout_happened): calculate_payoff12(player, timeout_happened, 2) class payoff2(Page): timeout_seconds = 30 class overview1(Page): timeout_seconds = 60 class overview2(Page): timeout_seconds = 30 class intro3a(Page): timeout_seconds = 30 class intro3b(Page): timeout_seconds = 60 class alloc1(Page): form_model = 'player' form_fields = ['alloc_1'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_alloc_1 = True class alloc2(Page): form_model = 'player' form_fields = ['alloc_2'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_alloc_2 = True class alloc3(Page): form_model = 'player' form_fields = ['alloc_3'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_alloc_3 = True class alloc4(Page): form_model = 'player' form_fields = ['alloc_4'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_alloc_4 = True class alloc5(Page): form_model = 'player' form_fields = ['alloc_5'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_alloc_5 = True class intro4a(Page): timeout_seconds = 30 class intro4b(Page): timeout_seconds = 60 class dict1(Page): form_model = 'player' form_fields = ['dict_1'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_dict_1 = True class dict2(Page): form_model = 'player' form_fields = ['dict_2'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_dict_2 = True class dict3(Page): form_model = 'player' form_fields = ['dict_3'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): if timeout_happened: player.timeout_dict_3 = True class dict4(Page): form_model = 'player' form_fields = ['dict_4'] timeout_seconds = 60 @staticmethod def before_next_page(player: Player, timeout_happened): #calculate_payoff4(player) if timeout_happened: player.timeout_dict_4 = True class demo(Page): form_model = 'player' form_fields = ['demoGender', 'demoEducation', 'demoParty', 'demoAge', 'demoStudy'] @staticmethod def before_next_page(player: Player, timeout_happened): calculate_payoff(player) timeout_seconds = 120 class payoff(Page): pass timeout_seconds = 120 class outro(Page): pass class part1(Page): pass timeout_seconds = 5 class part2(Page): pass timeout_seconds = 5 class part3(Page): pass timeout_seconds = 5 class part4(Page): pass timeout_seconds = 5 page_sequence = [instructions1, instructions2, instructions3, instructions4, instructions5, part1, intro1a, intro1b, paint1_1,paint1_2,paint1_3,paint1_4,paint1_5,ChoiceWaitPage1, result1, intro1d, paintings1, Waiter, paint1_67, Waiter, payoff1, part2, intro2a, intro2b, paint2_1,paint2_2,paint2_3,paint2_4,paint2_5,ChoiceWaitPage2, result2, intro2d, paintings2, Waiter, paint2_67, Waiter, payoff2, overview1, overview2, part3, intro3a, intro3b, alloc1, alloc2, alloc3, alloc4, alloc5, part4, intro4a, intro4b, dict1, dict2, dict3, dict4, demo, payoff, outro]