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 shared_vars(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'], 'total_fixed_payment': c(self.session.config['participation_fee'] + self.session.config['completion_award']), 'participation_fee': c(self.session.config['participation_fee']), 'load_incentive': c(self.session.config['load_incentive']), 'time_limit': 16, } @method_decorator(csrf_exempt, name='dispatch') class AttentionCheck(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) form_model = 'player' form_fields = ['attention_check'] def before_next_page(self): # count number of words for attention answer self.player.attention_check_num = len(self.player.attention_check.split()) # check if participants failed attention check if self.player.attention_check_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 class Sociodemographics(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) form_model = 'player' form_fields = ['age', 'gender', 'education', 'income', 'race'] def before_next_page(self): # determine age type and eligibility 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 # determine gender type and eligibility 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 # determine education type and eligibility 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 # determine counter type and eligibility 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 # determine race type and eligibility 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 # set participant-level sociodemographic indicators 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'] # check if specific sociodemographic type is still eligible to participate 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'] def vars_for_template(self): return shared_vars(self) form_model = 'player' form_fields = ['prolific_id'] class Consent(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) class InstructionsPart1(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) 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, **shared_vars(self) } class InstructionsPart2(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) class InstructionsPart3(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) class ComprehensionCheck(Page): def is_displayed(self): return self.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) form_model = 'player' form_fields = [ 'qn_hypo', 'qn_confidence', 'qn_uncertain', ] def before_next_page(self): # pay base fee if finished comprehension check self.player.completed = True self.participant.vars['completed'] = True if self.player.qn_hypo != 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 if self.player.qn_confidence != 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 if self.player.qn_uncertain != 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 class BeginStudy(Page): def is_displayed(self): return self.player.round_number == 1 and self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) 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'] and self.player.load_identifier == -1 def vars_for_template(self): return { 'table_length1': self.player.table_length1(), **shared_vars(self) } 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), **shared_vars(self) } form_model = 'player' form_fields = ['confidence'] 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'] class HeadsUpCounting(Page): def is_displayed(self): return self.participant.vars['tests_passed'] def vars_for_template(self): return shared_vars(self) class ListLoadD(Page): def is_displayed(self): return self.participant.vars['tests_passed'] and self.player.load_identifier == 1 def vars_for_template(self): return { 'table_length1': self.player.table_length1(), **shared_vars(self) } form_model = 'player' form_fields = ['switching_point', 'load_sum', 'timeout_load'] def before_next_page(self): self.player.set_switching_point_and_indicator() class LoadSumEntry(Page): def is_displayed(self): return self.participant.vars['tests_passed'] and self.player.load_identifier != -1 def vars_for_template(self): return shared_vars(self) 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 if self.player.round_number == 12: self.player.set_load_payoff() class Timeout(Page): def is_displayed(self): return self.participant.vars['tests_passed'] and self.player.timeout_load == 1 def vars_for_template(self): return shared_vars(self) page_sequence = [ AttentionCheck, Sociodemographics, Consent, Welcome, InstructionsPart1, ExampleList, InstructionsPart2, InstructionsPart3, ComprehensionCheck, BeginStudy, HeadsUpCounting, List, ListLoadD, Valuation, LoadSumEntry, Timeout, ]