from otree.api import * doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'trust' players_per_group = None tasks = ['A', 'B', 'C','D'] num_rounds = len(tasks) class Subsession(BaseSubsession): pass def creating_session(subsession): import random if subsession.round_number == 1: for p in subsession.get_players(): round_numbers = list(range(1, Constants.num_rounds+1)) random.shuffle(round_numbers) print(round_numbers) p.participant.vars['task_rounds'] = dict(zip(Constants.tasks, round_numbers)) print(p.participant.vars['task_rounds']) for p in subsession.get_players(): p.participant.treatment_1 = random.choice([1,2]) p.participant.treatment_2 = random.choice([1,2]) p.participant.treatment_3 = random.choice([1,2]) p.participant.treatment_4 = random.choice([1,2,3]) p.participant.send = 0 p.participant.remain = 0 p.participant.price = 0 class Group(BaseGroup): pass class Player(BasePlayer): taskA = models.IntegerField() taskB = models.IntegerField() taskC = models.IntegerField() taskD = models.IntegerField() treatment_1 = models.IntegerField() treatment_2 = models.IntegerField() treatment_3 = models.IntegerField() treatment_4 = models.IntegerField() send = models.IntegerField() price = models.IntegerField() remain = models.IntegerField() class A(Page): form_model = 'player' form_fields = ['taskA'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['A'] @staticmethod def vars_for_template(player: Player): aux = player.treatment_1 print(str(player.treatment_1)) return dict(treatment=aux) @staticmethod def before_next_page(player, timeout_happened): envio = player.taskA*3 saldo = 10 - player.taskA price = saldo + round(envio/2) player.participant.send = player.participant.send + (envio/3) player.participant.price = player.participant.price + price player.participant.remain = player.participant.remain + saldo class B(Page): form_model = 'player' form_fields = ['taskB'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['B'] @staticmethod def vars_for_template(player: Player): aux = player.treatment_2 return dict(treatment=aux) @staticmethod def before_next_page(player, timeout_happened): envio = player.taskB*3 saldo = 10 - player.taskB price = saldo + round(envio/2) player.participant.send = player.participant.send + (envio/3) player.participant.price = player.participant.price + price player.participant.remain = player.participant.remain + saldo class C(Page): form_model = 'player' form_fields = ['taskC'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['C'] @staticmethod def vars_for_template(player: Player): aux = player.treatment_3 return dict(treatment=aux) @staticmethod def before_next_page(player, timeout_happened): envio = player.taskC*3 saldo = 10 - player.taskC price = saldo + round(envio/2) player.participant.send = player.participant.send + (envio/3) player.participant.price = player.participant.price + price player.participant.remain = player.participant.remain + saldo class D(Page): form_model = 'player' form_fields = ['taskD'] @staticmethod def is_displayed(player: Player): participant = player.participant return player.round_number == participant.task_rounds['D'] @staticmethod def vars_for_template(player: Player): aux = player.treatment_4 return dict(treatment=aux) @staticmethod def before_next_page(player, timeout_happened): envio = player.taskD*3 saldo = 10 - player.taskD price = saldo + round(envio/2) player.participant.send = player.participant.send + (envio/3) player.participant.price = player.participant.price + price player.participant.remain = player.participant.remain + saldo class Instructions(Page): @staticmethod def before_next_page(player, timeout_happened): player.treatment_1 = player.participant.treatment_1 player.treatment_2 = player.participant.treatment_2 player.treatment_3 = player.participant.treatment_3 player.treatment_4 = player.participant.treatment_4 print('treat1 ='+str(player.treatment_1)) print('treat2 ='+str(player.treatment_2)) print('treat3 ='+str(player.treatment_3)) print('treat4 ='+str(player.treatment_4)) page_sequence = [Instructions,A,B,C,D]