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': self.session.config['cu_step_size'], 'radio_amounts': zip(Constants.list2, Constants.list1), 'interval_increase': unit *2, 'round': self.round_number, 'num_questions': Constants.num_rounds, 'exp_duration': self.session.config['exp_duration'], 'max_duration': self.session.config['max_duration'], } @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_attention = True self.participant.vars['failed_attention'] = True self.player.tests_passed = False self.participant.vars['tests_passed'] = False self.participant.vars['completed'] = 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 self.participant.vars['tests_passed'] 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_attention = True self.participant.vars['failed_attention'] = True self.player.tests_passed = False self.participant.vars['tests_passed'] = False self.participant.vars['completed'] = 0 self.participant.vars['psid'] = self.participant.label self.player.psid = self.participant.vars['psid'] class Sociodemographics(Page): form_model = 'player' form_fields = ['age', 'gender', 'education', 'income', 'race'] def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] 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 self.player.tests_passed = False self.participant.vars['tests_passed'] = False class Welcome(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] class Consent(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] class InstructionsPart1(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] class ExampleList(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] 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 InstructionsPart2(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] class ComprehensionCheck(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] 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 self.player.tests_passed = False self.participant.vars['tests_passed'] = False 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 self.player.tests_passed = False self.participant.vars['tests_passed'] = False 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 self.player.tests_passed = False self.participant.vars['tests_passed'] = False def before_next_page(self): self.player.completed = 1 self.participant.vars['completed'] = 1 class BeginStudy(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] def before_next_page(self): # 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 List(Page): def is_displayed(self): return self.participant.vars['tests_passed'] 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 Valuation(Page): def is_displayed(self): return self.participant.vars['tests_passed'] def vars_for_template(self): return { 'val_up': int(self.player.switching_point), 'val_low': int(self.player.switching_point - Constants.steps), } form_model = 'player' form_fields = ['confidence'] class Wait(Page): def is_displayed(self): return self.round_number != Constants.num_rounds and self.participant.vars['tests_passed'] timeout_seconds = 2 page_sequence = [ AttentionCheck2, Sociodemographics, Consent, Welcome, InstructionsPart1, ExampleList, InstructionsPart2, ComprehensionCheck, BeginStudy, List, Valuation, Wait, ]