from otree.api import * import random doc = """ Part 2b Tutorial """ class C(BaseConstants): NAME_IN_URL = 'Part2b_Tutorial' PLAYERS_PER_GROUP = None NUM_ROUNDS = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): repeat = models.BooleanField(label="") round_number = models.IntegerField() number_2tutorial = models.IntegerField() number_2tutorial1 = models.IntegerField() number_2tutorial2 = models.IntegerField() threshold = models.IntegerField() threshold_check = models.BooleanField() skip_remaining_rounds = models.BooleanField(initial=False) warning_active = models.BooleanField() blength = models.IntegerField() noise = models.BooleanField() noise_mood = models.IntegerField(initial=0) warning = models.IntegerField() warning_message = models.BooleanField() sum_claims1 = models.IntegerField(inital=0) sum_claims2 = models.IntegerField(inital=0) noise_mood1 = models.IntegerField(initial=0) noise_mood2 = models.IntegerField(initial=0) def threshold_computation(player): player.threshold = random.randint(0, 100) return { 'threshold_value': player.threshold } def warning_computation(player): if player.number_2tutorial + player.blength <= 100: player.warning = player.number_2tutorial + player.blength else: player.warning = 100 return { 'ews_active': player.warning } def box_range(player): player.blength = player.in_round(1).participant.vars.get('box_length', None) player.noise = player.in_round(1).participant.vars.get('noise', None) return { 'range': player.blength,'noise':player.noise } def threshold_exceeded(player): if player.number_2tutorial > player.threshold: player.threshold_check = True player.skip_remaining_rounds = True else: player.threshold_check = False player.skip_remaining_rounds = False return { 'threshold_checker': player.threshold_check } def warning_issued(player): # checks whether number is higher than warning threshold if player.noise == False: player.noise_mood = 0 if player.number_2tutorial + player.blength > player.threshold: player.warning_active = True else: player.warning_active = False else: player.noise_mood = random.randint(1, 10) if player.noise_mood <= 8: if player.number_2tutorial + player.blength > player.threshold: player.warning_active = True else: player.warning_active = False else: if player.number_2tutorial + player.blength > player.threshold: player.warning_active = False else: player.warning_active = True return { 'active_warning': player.warning_active, 'noise_mood': player.noise_mood } # PAGES class Start(Page): @staticmethod # collects box length for html page def vars_for_template(player): player.participant.vars['range','noise'] = box_range(player) class Decision1(Page): form_model = "player" form_fields = ["number_2tutorial"] @staticmethod # collects chosen number for calculations def before_next_page(player, timeout_happened): player.participant.vars['chosen_number'] = player.number_2tutorial player.participant.vars['threshold_value'] = threshold_computation(player) player.participant.vars['ews_active'] = warning_computation(player) player.number_2tutorial1 = player.number_2tutorial player.round_number = player.subsession.round_number @staticmethod # collects box length for html page def vars_for_template(player): player.warning_message = False class Decision2(Page): form_model = "player" form_fields = ["number_2tutorial"] @staticmethod # collects chosen number for calculations def before_next_page(player, timeout_happened): player.participant.vars['chosen_number'] = player.number_2tutorial player.participant.vars['ews_active'] = warning_computation(player) player.number_2tutorial2 = player.number_2tutorial @staticmethod def is_displayed(player): return player.skip_remaining_rounds == False class Result1(Page): @staticmethod # variables for computations and html def vars_for_template(player): player.participant.vars['threshold_checker'] = threshold_exceeded(player) player.participant.vars['active_warning','noise_mood'] = warning_issued(player) player.sum_claims1 = player.number_2tutorial player.noise_mood1 = player.noise_mood class Result2(Page): @staticmethod # variables for computations and html def vars_for_template(player): player.participant.vars['threshold_checker'] = threshold_exceeded(player) player.participant.vars['active_warning','noise_mood'] = warning_issued(player) player.sum_claims2 = player.sum_claims1 + player.number_2tutorial player.noise_mood2 = player.noise_mood @staticmethod def is_displayed(player): return player.skip_remaining_rounds == False class Repeat(Page): form_model = "player" form_fields = ["repeat"] @staticmethod def is_displayed(player): return player.round_number >= 3 @staticmethod def vars_for_template(player): player.participant.vars['range'] = player.blength @staticmethod def app_after_this_page(player, upcoming_apps): if player.repeat == False: return upcoming_apps[0] page_sequence = [Start, Decision1, Result1, Decision2, Result2, Repeat]