import random from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'Task2' PLAYERS_PER_GROUP = None TASKS = ['10u', '10d', '11u', '11d','12u', '12d','13u', '13d','14u', '14d','15u', '15d','16u', '16d','17u', '17d','18u', '18d','19u', '19d'] NUM_ROUNDS = len(TASKS) TIMER_TEXT = " " class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): treat = models.IntegerField() task2_q1 = models.BooleanField( label="1. I need to predict the data in this task.", widget=widgets.RadioSelect ) task2_q2 = models.IntegerField( label="2. I can get $___ for 10 correct predictions. ", ) task2_q3 = models.BooleanField( label="3. I will know the correct number of predictions at the end of the experiment.", widget=widgets.RadioSelect ) task2_pre_predict=models.IntegerField(min=0,max=20) predict_10u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_10d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_11u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_11d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_12u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_12d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_13u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_13d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_14u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_14d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_15u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_15d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_16u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_16d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_17u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_17d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_18u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_18d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_19u=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) predict_19d=models.IntegerField(choices=[ [-1, 'Downward'], [+1, 'Upward'], ],initial=0,widget=widgets.RadioSelect) task2_post_predict=models.IntegerField(min=0,max=20) #plot_correct_task2=models.IntegerField(initial=0) treatment_task2=models.IntegerField() # FUNCTIONS def creating_session(subsession:Subsession): import itertools treatments = itertools.cycle( itertools.product([17 , 14 , 1 , 6 , 26 , 12 , 27 , 30 , 3 , 2 , 9 , 10 , 22 , 19 , 4 , 16 , 20 , 28 , 13 , 18 , 8 , 21 , 7 , 32 , 29 , 23 , 11 , 25 , 31 , 5 , 15 , 24 ,]) ) for player in subsession.get_players(): treatment = next(treatments) player.treat = treatment[0] # print('player treat2 is', player.treat) if 1<=player.treat<=3 or 17<=player.treat<=19: player.treatment_task2 = 2 elif 4<=player.treat<=6 or 20<=player.treat<=22: player.treatment_task2 = 3 elif 7<=player.treat<=9 or 23<=player.treat<=25: player.treatment_task2 = 4 elif 10<=player.treat<=12 or 26<=player.treat<=28: player.treatment_task2 = 5 else: player.treatment_task2 = 6 participant=player.participant participant.treatment_task2 = player.treatment_task2 if subsession.round_number == 1: for p in subsession.get_players(): round_numbers = list(range(1, C.NUM_ROUNDS+1)) random.shuffle(round_numbers) task2_rounds = dict(zip(C.TASKS, round_numbers)) print('player', p.id_in_subsession) print('task2_rounds is', task2_rounds) p.participant.task2_rounds = task2_rounds def get_timeout_seconds2(player: Player): participant = player.participant import time return participant.expiry2 - time.time() #def is_displayed1(player: Player): # """only returns True if there is time left.""" # return get_timeout_seconds1(player) > 0 class Instructions(Page): form_model = 'player' form_fields = ['task2_q1', 'task2_q2', 'task2_q3'] @staticmethod def error_message(player: Player, values): if 1<=player.treat<=3 or 17<=player.treat<=19: solutions = dict(task2_q1=True, task2_q2=1, task2_q3=True) elif 4<=player.treat<=6 or 20<=player.treat<=22: solutions = dict(task2_q1=True, task2_q2=1, task2_q3=False) elif 7<=player.treat<=9 or 23<=player.treat<=25: solutions = dict(task2_q1=True, task2_q2=1, task2_q3=True) elif 10<=player.treat<=12 or 26<=player.treat<=28: solutions = dict(task2_q1=True, task2_q2=1, task2_q3=False) else: solutions = dict(task2_q1=True, task2_q2=4, task2_q3=False) errors = {name: 'Wrong' for name in solutions if values[name] != solutions[name]} print('errors is', errors) if errors: return 'Wrong answer. Please read the Instructions and answer the understanding questions again.' @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant import time participant.expiry2 = time.time() + 60*5 participant.plot_correct_task2 = 0 class Task2_pre_predict(Page): form_model = 'player' form_fields = ['task2_pre_predict'] @staticmethod def is_displayed(player: Player): return player.round_number == 1 and (7<=player.treat<=16 or 23<=player.treat<=32) class Task10u(Page): form_model = 'player' form_fields = ['predict_10u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['10u'] and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_10u==-1: participant.plot_correct_task2 += 1 class Task10d(Page): form_model = 'player' form_fields = ['predict_10d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['10d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_10d==1: participant.plot_correct_task2 += 1 class Task11u(Page): form_model = 'player' form_fields = ['predict_11u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['11u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_11u==-1: participant.plot_correct_task2 += 1 class Task11d(Page): form_model = 'player' form_fields = ['predict_11d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['11d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_11d==1: participant.plot_correct_task2 += 1 class Task12u(Page): form_model = 'player' form_fields = ['predict_12u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['12u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_12u==1: participant.plot_correct_task2 += 1 class Task12d(Page): form_model = 'player' form_fields = ['predict_12d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['12d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_12d==-1: participant.plot_correct_task2 += 1 class Task13u(Page): form_model = 'player' form_fields = ['predict_13u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['13u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_13u==-1: participant.plot_correct_task2 += 1 class Task13d(Page): form_model = 'player' form_fields = ['predict_13d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['13d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_13d==1: participant.plot_correct_task2 += 1 class Task14u(Page): form_model = 'player' form_fields = ['predict_14u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['14u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_14u==1: participant.plot_correct_task2 += 1 class Task14d(Page): form_model = 'player' form_fields = ['predict_14d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['14d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_14d==-1: participant.plot_correct_task2 += 1 class Task15u(Page): form_model = 'player' form_fields = ['predict_15u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['15u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_15u==-1: participant.plot_correct_task2 += 1 class Task15d(Page): form_model = 'player' form_fields = ['predict_15d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['15d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_15d==1: participant.plot_correct_task2 += 1 class Task16u(Page): form_model = 'player' form_fields = ['predict_16u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['16u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_16u==1: participant.plot_correct_task2 += 1 class Task16d(Page): form_model = 'player' form_fields = ['predict_16d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['16d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_16d==-1: participant.plot_correct_task2 += 1 class Task17u(Page): form_model = 'player' form_fields = ['predict_17u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['17u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_17u==-1: participant.plot_correct_task2 += 1 class Task17d(Page): form_model = 'player' form_fields = ['predict_17d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['17d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_17d==-1: participant.plot_correct_task2 += 1 class Task18u(Page): form_model = 'player' form_fields = ['predict_18u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['18u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_18u==1: participant.plot_correct_task2 += 1 class Task18d(Page): form_model = 'player' form_fields = ['predict_18d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['18d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_18d==1: participant.plot_correct_task2 += 1 class Task19u(Page): form_model = 'player' form_fields = ['predict_19u'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['19u']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_19u==-1: participant.plot_correct_task2 += 1 class Task19d(Page): form_model = 'player' form_fields = ['predict_19d'] get_timeout_seconds = get_timeout_seconds2 timer_text = C.TIMER_TEXT @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task2_rounds['19d']and get_timeout_seconds2(player) > 0 @staticmethod def vars_for_template(player): a = player.round_number return dict( a=a, ) @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant if player.predict_19d==1: participant.plot_correct_task2 += 1 class Task2_post_predict(Page): form_model = 'player' form_fields = ['task2_post_predict'] @staticmethod def is_displayed(player: Player): return player.round_number == 20 and (1<=player.treat<=6 or 17<=player.treat<=22) class Task2_over(Page): @staticmethod def app_after_this_page(player, upcoming_apps): print('upcoming_apps is', upcoming_apps) return "Task3" @staticmethod def before_next_page(player: Player, timeout_happened): participant = player.participant print('participant.plot_correct_task2 is',participant.plot_correct_task2) @staticmethod def is_displayed(player: Player): return player.round_number == 20 # @staticmethod # def app_after_this_page(player, upcoming_apps): # print('upcoming_apps is', upcoming_apps) # if player.treat <=16: # return "Task1" # else: # return "Task2" page_sequence = [Instructions,Task2_pre_predict,Task10u,Task10d,Task11u,Task11d,Task12u,Task12d,Task13u,Task13d,Task14u,Task14d,Task15u,Task15d,Task16u,Task16d,Task17u,Task17d,Task18u,Task18d,Task19u,Task19d,Task2_post_predict,Task2_over] #page_sequence = [verbalInstructions, verbal_prac_instructions, verbal_prac, verbal_pr_instructions, verbal_pr_predict, verbal_pr, verbal_t_instructions,verbal_t_predict, verbal_t,verbal_choice, # mathInstructions, math_prac_instructions, math_prac, math_pr_instructions,math_pr_predict, # math_pr, math_t_instructions, math_t_predict, math_t, final_predict, math_choice, Investment_Choice, survey1, survey2, survey3, Results] # page_sequence = [survey4, survey4_result]