from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator import numpy as np def vars_for_all_templates(self): num_rounds = Constants.num_rounds unit = self.session.config['cu_step_size'] return { 'cu_step_size': unit, 'radio_amounts': zip(Constants.list2, Constants.list1), 'interval_increase': unit *2, 'participation_fee': c(self.session.config['participation_fee']), 'round': self.round_number, 'exp_duration': self.session.config['exp_duration'], 'num_questions': num_rounds, 'max_duration': self.session.config['max_duration'], 'total_fixed_payment': c(self.session.config['participation_fee']+self.session.config['completion_award']), 'load_incentive': c(self.session.config['load_incentive']), } @method_decorator(csrf_exempt, name='dispatch') class AttentionCheck(Page): def is_displayed(self): return self.round_number == 1 def js_vars(self): return dict( psid = self.player.psid, ) form_model = 'player' form_fields = ['attention_check','psid',] def before_next_page(self): self.participant.vars['attention_check'] = self.player.attention_check if self.player.attention_check != Constants.attention_list: self.player.failed_comprehension = True self.participant.vars['failed_comprehension'] = True self.player.failed_attention = True self.participant.vars['failed_attention'] = True self.player.failed_demographics = True self.participant.vars['failed_demographics'] = True self.participant.vars['pay_or_not'] = 0 self.participant.vars['psid'] = self.participant.label self.player.psid = self.participant.vars['psid'] class AttentionCheck2(Page): def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_attention'] form_model = 'player' form_fields = ['attention_check2'] def before_next_page(self): self.participant.vars['attention_check2'] = self.player.attention_check2 self.player.attention_check2_num = len(self.player.attention_check2.split()) if self.player.attention_check2_num < 15: self.player.failed_comprehension = True self.participant.vars['failed_comprehension'] = True self.player.failed_attention = True self.participant.vars['failed_attention'] = True self.player.failed_demographics = True self.participant.vars['failed_demographics'] = True class Welcome(Page): def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_demographics'] class Consent(Page): def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_demographics'] class InstructionsPart1(Page): def is_displayed(self): return self.player.round_number == 1 and not self.participant.vars['failed_demographics'] class InstructionsPart2(Page): def is_displayed(self): return self.player.round_number == 1 and not self.participant.vars['failed_demographics'] class InstructionsPart3(Page): def is_displayed(self): return self.player.round_number == 1 and not self.participant.vars['failed_demographics'] class BeginStudy(Page): def is_displayed(self): return self.player.round_number == 1 and not self.participant.vars['failed_comprehension'] and not self.participant.vars['failed_demographics'] def before_next_page(self): self.player.pay_or_not = 1 self.participant.vars['pay_or_not'] = 1 # age counter if self.participant.vars['counter_age'] == 1: self.session.vars['age_1'] += 1 elif self.participant.vars['counter_age'] == 2: self.session.vars['age_2'] += 1 elif self.participant.vars['counter_age'] == 3: self.session.vars['age_3'] += 1 elif self.participant.vars['counter_age'] == 4: self.session.vars['age_4'] += 1 elif self.participant.vars['counter_age'] == 5: self.session.vars['age_5'] += 1 elif self.participant.vars['counter_age'] == 6: self.session.vars['age_6'] += 1 # gender counter if self.participant.vars['counter_gender'] == 1: self.session.vars['gender_1'] += 1 elif self.participant.vars['counter_gender'] == 2: self.session.vars['gender_2'] += 1 # education counter if self.participant.vars['counter_education'] == 1: self.session.vars['education_1'] += 1 elif self.participant.vars['counter_education'] == 2: self.session.vars['education_2'] += 1 elif self.participant.vars['counter_education'] == 3: self.session.vars['education_3'] += 1 elif self.participant.vars['counter_education'] == 4: self.session.vars['education_4'] += 1 # income counter if self.participant.vars['counter_income'] == 1: self.session.vars['income_1'] += 1 elif self.participant.vars['counter_income'] == 2: self.session.vars['income_2'] += 1 elif self.participant.vars['counter_income'] == 3: self.session.vars['income_3'] += 1 elif self.participant.vars['counter_income'] == 4: self.session.vars['income_4'] += 1 elif self.participant.vars['counter_income'] == 5: self.session.vars['income_5'] += 1 elif self.participant.vars['counter_income'] == 6: self.session.vars['income_6'] += 1 elif self.participant.vars['counter_income'] == 7: self.session.vars['income_7'] += 1 elif self.participant.vars['counter_income'] == 8: self.session.vars['income_8'] += 1 elif self.participant.vars['counter_income'] == 9: self.session.vars['income_9'] += 1 # race counter if self.participant.vars['counter_race'] == 1: self.session.vars['race_1'] += 1 elif self.participant.vars['counter_race'] == 2: self.session.vars['race_2'] += 1 elif self.participant.vars['counter_race'] == 3: self.session.vars['race_3'] += 1 elif self.participant.vars['counter_race'] == 4: self.session.vars['race_4'] += 1 elif self.participant.vars['counter_race'] == 5: self.session.vars['race_5'] += 1 class ComprehensionCheck(Page): def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_demographics'] form_model = 'player' form_fields = [ 'qn_hypo', 'qn_confidence', 'qn_uncertain', ] def qn_hypo_error_message(self, value): if value != 1: self.player.qn_hypo_got_wrong = True self.player.failed_comprehension = True self.participant.vars['failed_comprehension'] = True def qn_confidence_error_message(self, value): if value != 16: self.player.qn_confidence_got_wrong = True self.player.failed_comprehension = True self.participant.vars['failed_comprehension'] = True def qn_uncertain_error_message(self, value): if value != 1: self.player.qn_uncertain_got_wrong = True self.player.failed_comprehension = True self.participant.vars['failed_comprehension'] = True class List(Page): def is_displayed(self): return not self.participant.vars['failed_comprehension'] and self.player.load_identifier == -1 and not self.participant.vars['failed_demographics'] def vars_for_template(self): return { 'table_length1': self.player.table_length1(), } form_model = 'player' form_fields = ['switching_point'] def before_next_page(self): self.player.set_switching_point_and_indicator() class ListLoadD(Page): def is_displayed(self): return not self.participant.vars['failed_comprehension'] and self.player.load_identifier == 1 and not self.participant.vars['failed_demographics'] def vars_for_template(self): return { 'table_length1': self.player.table_length1(), } form_model = 'player' form_fields = ['switching_point', 'load_sum', 'timeout_load'] def before_next_page(self): self.player.set_switching_point_and_indicator() class Valuation(Page): form_model = 'player' form_fields = ['confidence'] def vars_for_template(self): return { 'val_up': int(self.player.switching_point), 'val_low': int(self.player.switching_point - Constants.steps), } def is_displayed(self): return not self.participant.vars['failed_comprehension'] and not self.participant.vars['failed_demographics'] def before_next_page(self): if self.player.load_identifier == -1: if self.player.round_number == 1: self.participant.vars['load_num_correct'] = 0 else: self.player.load_num_correct = self.participant.vars['load_num_correct'] self.player.payoff = c(self.session.config['fixed_payment_per_question']) class LoadSumEntry(Page): def is_displayed(self): return not self.participant.vars['failed_comprehension'] and self.player.load_identifier != -1 and not self.participant.vars['failed_demographics'] form_model = 'player' form_fields = ['load_guess'] def before_next_page(self): if self.player.load_guess == self.player.load_sum: self.player.load_correct = 1 if self.player.round_number == 1: self.player.load_num_correct = 1 else: self.player.load_num_correct = self.participant.vars['load_num_correct'] + self.player.load_correct else: self.player.load_correct = 0 if self.player.round_number == 1: self.player.load_num_correct = 0 else: self.player.load_num_correct = self.participant.vars['load_num_correct'] + self.player.load_correct self.participant.vars['load_num_correct'] = self.player.load_num_correct self.player.payoff = c(self.session.config['fixed_payment_per_question'] + self.player.load_correct * self.session.config['load_incentive']) if self.player.round_number == 12: self.player.set_load_payoff() class Sociodemographics(Page): form_model = 'player' form_fields = ['age', 'gender', 'education', 'income', 'race'] def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_attention'] def before_next_page(self): # age counter if self.player.age <= 29: if self.session.vars['age_1'] <= Constants.num_subjects * Constants.cap * 0.210: self.participant.vars['counter_age'] = 1 elif self.player.age <= 39: if self.session.vars['age_2'] <= Constants.num_subjects * Constants.cap * 0.160: self.participant.vars['counter_age'] = 2 elif self.player.age <= 49: if self.session.vars['age_3'] <= Constants.num_subjects * Constants.cap * 0.160: self.participant.vars['counter_age'] = 3 elif self.player.age <= 59: if self.session.vars['age_4'] <= Constants.num_subjects * Constants.cap * 0.170: self.participant.vars['counter_age'] = 4 elif self.player.age <= 69: if self.session.vars['age_5'] <= Constants.num_subjects * Constants.cap * 0.140: self.participant.vars['counter_age'] = 5 else: if self.session.vars['age_6'] <= Constants.num_subjects * Constants.cap * 0.160: self.participant.vars['counter_age'] = 6 # gender counter if self.player.gender == 1: if self.session.vars['gender_1'] <= Constants.num_subjects * Constants.cap * 0.490: self.participant.vars['counter_gender'] = 1 elif self.player.gender == 2: if self.session.vars['gender_2'] <= Constants.num_subjects * Constants.cap * 0.510: self.participant.vars['counter_gender'] = 2 # education counter if self.player.education == 1: if self.session.vars['education_1'] <= Constants.num_subjects * Constants.cap * 0.110: self.participant.vars['counter_education'] = 1 elif self.player.education == 2: if self.session.vars['education_2'] <= Constants.num_subjects * Constants.cap * 0.290: self.participant.vars['counter_education'] = 2 elif self.player.education == 3: if self.session.vars['education_3'] <= Constants.num_subjects * Constants.cap * 0.290: self.participant.vars['counter_education'] = 3 elif self.player.education >= 4: if self.session.vars['education_4'] <= Constants.num_subjects * Constants.cap * 0.310: self.participant.vars['counter_education'] = 4 # income counter if self.player.income == 1: if self.session.vars['income_1'] <= Constants.num_subjects * Constants.cap * 0.110: self.participant.vars['counter_income'] = 1 elif self.player.income == 2: if self.session.vars['income_2'] <= Constants.num_subjects * Constants.cap * 0.090: self.participant.vars['counter_income'] = 2 elif self.player.income == 3: if self.session.vars['income_3'] <= Constants.num_subjects * Constants.cap * 0.090: self.participant.vars['counter_income'] = 3 elif self.player.income == 4: if self.session.vars['income_4'] <= Constants.num_subjects * Constants.cap * 0.120: self.participant.vars['counter_income'] = 4 elif self.player.income == 5: if self.session.vars['income_5'] <= Constants.num_subjects * Constants.cap * 0.170: self.participant.vars['counter_income'] = 5 elif self.player.income == 6: if self.session.vars['income_6'] <= Constants.num_subjects * Constants.cap * 0.130: self.participant.vars['counter_income'] = 6 elif self.player.income == 7: if self.session.vars['income_7'] <= Constants.num_subjects * Constants.cap * 0.150: self.participant.vars['counter_income'] = 7 elif self.player.income == 8: if self.session.vars['income_8'] <= Constants.num_subjects * Constants.cap * 0.070: self.participant.vars['counter_income'] = 8 elif self.player.income == 9: if self.session.vars['income_9'] <= Constants.num_subjects * Constants.cap * 0.070: self.participant.vars['counter_income'] = 9 # race counter if self.player.race == 1: if self.session.vars['race_1'] <= Constants.num_subjects * Constants.cap * 0.630: self.participant.vars['counter_race'] = 1 elif self.player.race == 2: if self.session.vars['race_2'] <= Constants.num_subjects * Constants.cap * 0.170: self.participant.vars['counter_race'] = 2 elif self.player.race == 3: if self.session.vars['race_3'] <= Constants.num_subjects * Constants.cap * 0.120: self.participant.vars['counter_race'] = 3 elif self.player.race == 4: if self.session.vars['race_4'] <= Constants.num_subjects * Constants.cap * 0.050: self.participant.vars['counter_race'] = 4 elif self.player.race == 5: if self.session.vars['race_5'] <= Constants.num_subjects * Constants.cap * 0.030: self.participant.vars['counter_race'] = 5 self.player.counter_age = self.participant.vars['counter_age'] self.player.counter_gender = self.participant.vars['counter_gender'] self.player.counter_education = self.participant.vars['counter_education'] self.player.counter_income = self.participant.vars['counter_income'] self.player.counter_race = self.participant.vars['counter_race'] if self.participant.vars['counter_age'] * self.participant.vars['counter_gender'] * self.participant.vars['counter_education'] * self.participant.vars['counter_income'] * self.participant.vars['counter_race'] == 0: self.player.failed_demographics = True self.participant.vars['failed_demographics'] = self.player.failed_demographics #class LoadGuessSummary(Page): # def is_displayed(self): # return not self.participant.vars['failed_comprehension'] and self.player.round_number == 12 # # def before_next_page(self): # self.player.set_load_payoff() # # def vars_for_template(self): # return { # 'load_num_correct': self.player.load_num_correct, # 'load_payoff': c(self.player.load_payoff), # } class HeadsUpCounting(Page): def is_displayed(self): return not self.participant.vars['failed_comprehension'] and not self.participant.vars['failed_demographics'] class ExampleList(Page): def is_displayed(self): return self.round_number == 1 and not self.participant.vars['failed_demographics'] def vars_for_template(self): return { 'table_length2': Constants.example_set[0][2]/Constants.example_steps + 1 } form_model = 'player' form_fields = ['switching_point'] def before_next_page(self): self.player.set_switching_point_and_indicator() class Timeout(Page): def is_displayed(self): return self.player.timeout_load == 1 and not self.participant.vars['failed_demographics'] page_sequence = [ AttentionCheck, AttentionCheck2, Sociodemographics, Consent, Welcome, InstructionsPart1, ExampleList, InstructionsPart2, InstructionsPart3, ComprehensionCheck, BeginStudy, HeadsUpCounting, List, ListLoadD, Valuation, LoadSumEntry, Timeout, ]