from otree.api import Currency as c, currency_range from . import models from ._builtin import Page, WaitPage from .models import Constants # variables for all templates # -------------------------------------------------------------------------------------------------------------------- def vars_for_all_templates(self): return { 'lottery_a_lo': c(Constants.lottery_a_lo), 'lottery_a_hi': c(Constants.lottery_a_hi), 'lottery_b_lo': c(Constants.lottery_b_lo), 'lottery_b_hi': c(Constants.lottery_b_hi) } class P7BisBienvenue(Page): timeout_seconds = 5 form_model = 'player' def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 def before_next_page(self): self.player.lottery_a_hi = round(1000,0) self.player.lottery_a_lo = round(800,0) self.player.lottery_b_hi = 1925 self.player.lottery_b_lo = round(50,0) class P7Instructions(Page): form_model = 'player' def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # specify info for progress bar total = len(self.participant.vars['mpl_choices2']) page = self.subsession.round_number progress = page / total * 100 if Constants.one_choice_per_page: return { 'page': page, 'total': total, 'progress': progress, 'choices': [self.player.participant.vars['mpl_choices2'][page - 1]] } else: return { 'choices': self.player.participant.vars['mpl_choices2'] } class P8qc(Page): form_model = 'player' form_fields = [ 'qc_1', 'qc_2', 'qc_3', 'qc_4' ] def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 def error_message(self, values): print('values is', values) if values['qc_1'] != 4 : return 'Please correct your answer A!!' elif values['qc_2'] != 8 : return 'Please correct your answer B!!' elif values['qc_3'] != 19.25 : return 'Please correct your answer C!!' elif values['qc_4'] != 0.5 : return 'Please correct your answer D!!' # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # specify info for progress bar total = len(self.participant.vars['mpl_choices2']) page = self.subsession.round_number progress = page / total * 100 if Constants.one_choice_per_page: return { 'page': page, 'total': total, 'progress': progress, 'choices': [self.player.participant.vars['mpl_choices2'][page - 1]] } else: return { 'choices': self.player.participant.vars['mpl_choices2'] } class P9Decision(Page): # form model # ---------------------------------------------------------------------------------------------------------------- form_model = 'player' def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 # form fields # ---------------------------------------------------------------------------------------------------------------- def get_form_fields(self): # unzip list of form_fields from list form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] # provide form field associated with pagination or full list if Constants.one_choice_per_page: page = self.subsession.round_number return [form_fields[page - 1]] else: return form_fields # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # specify info for progress bar total = len(self.participant.vars['mpl_choices']) page = self.subsession.round_number progress = page / total * 100 if Constants.one_choice_per_page: return { 'page': page, 'total': total, 'progress': progress, 'choices': [self.player.participant.vars['mpl_choices'][page - 1]] } else: return { 'choices': self.player.participant.vars['mpl_choices'] } # set player's payoff # ---------------------------------------------------------------------------------------------------------------- def before_next_page(self): # unzip indices and form fields from list round_number = self.subsession.round_number form_fields = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][1] indices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])][0] index = indices[round_number - 1] # if choices are displayed sequentially # ------------------------------------------------------------------------------------------------------------ if Constants.one_choice_per_page: # replace current choice in current_choice = getattr(self.player, form_fields[round_number - 1]) self.participant.vars['mpl_choices_made'][index - 1] = current_choice # if current choice equals index to pay ... if index == self.player.participant.vars['mpl_index_to_pay']: # set payoff self.player.set_payoffs() # after final choice if round_number == Constants.num_choices: # determine consistency self.player.set_consistency() # set switching row self.player.set_switching_row() # if choices are displayed in tabular format # ------------------------------------------------------------------------------------------------------------ if not Constants.one_choice_per_page: # replace choices in for j, choice in zip(indices, form_fields): choice_i = getattr(self.player, choice) self.participant.vars['mpl_choices_made'][j - 1] = choice_i # set payoff self.player.set_payoffs() # determine consistency self.player.set_consistency() # set switching row self.player.set_switching_row() class P10Survey(Page): form_model = 'player' form_fields = ['survey_1'] def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 class Results(Page): def is_displayed(self): return self.player.participant.vars['nextq0'] != 1 and self.player.participant.vars['b5'] == 1 # skip results until last page # ---------------------------------------------------------------------------------------------------------------- def is_displayed(self): if Constants.one_choice_per_page: return self.subsession.round_number == Constants.num_rounds else: return True # variables for template # ---------------------------------------------------------------------------------------------------------------- def vars_for_template(self): # unzip into list of lists choices = [list(t) for t in zip(*self.participant.vars['mpl_choices'])] indices = choices[0] # get index, round, and choice to pay index_to_pay = self.player.participant.vars['mpl_index_to_pay'] round_to_pay = indices.index(index_to_pay) + 1 choice_to_pay = self.participant.vars['mpl_choices'][round_to_pay - 1] if Constants.one_choice_per_page: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.in_round(round_to_pay).option_to_pay, 'payoff': self.player.in_round(round_to_pay).payoff, } else: return { 'choice_to_pay': [choice_to_pay], 'option_to_pay': self.player.option_to_pay, 'payoff': self.player.payoff } page_sequence = [ #P7BisBienvenue, #P7Instructions, #P9Decision, ]