from otree.api import Currency as c, currency_range from otree.api import safe_json from ._builtin import Page, WaitPage from .models import Constants import random import json def bisection(prev_upper_bound,prev_lower_bound,dec): midpoint = (prev_upper_bound+prev_lower_bound)/2 if dec == True: upper_bound = prev_upper_bound lower_bound = midpoint else: upper_bound = midpoint lower_bound = prev_lower_bound return upper_bound,lower_bound,(upper_bound+lower_bound)/2 class Instructions_1(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['Instructions_1_warnings', 'Instructions_1_time_spent' ] def js_vars(self): return dict( page_name='Instructions_1') class WTP_Instructions(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = [ 'control_q1', 'control_q2', #'control_q3', 'WTP_Instructions_warnings', 'WTP_Instructions_time_spent' ] def js_vars(self): return dict( page_name='WTP_Instructions') def error_message(self,values): """Customized error message for control question 2. The error message is not actually displayed. This function simply validates the form for us. For the actual error message code look at the html code of the page. """ cond_1 = (values['control_q1'] != 0) cond_2 = (values['control_q2'] != 1) #cond_3 = (values['control_q3'] != Constants.no_decisions) if cond_1: return 'Your input is incorrect! Please read the instructions again.' elif cond_2: return 'Your input is incorrect! Please read the instructions again.' #elif cond_3: # return 'Your input is incorrect! Please read the instructions again.' class WTP_Dec1(Page): form_model = 'player' form_fields = ['WTP_Dec1', 'WTP_Dec1_warnings', 'WTP_Dec1_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec1') def before_next_page(self): p_vars = self.player.participant.vars p_vars[f'WTP_Sequence_{self.round_number}'] = [] p_vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec1) p_vars['WTP_upper_bound'] = Constants.max_amount p_vars['WTP_lower_bound'] = -Constants.max_amount def vars_for_template(self): midpoint = (Constants.max_amount+(-Constants.max_amount))/2 if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(midpoint=c(midpoint), incentives=incentives) class WTP_Dec2(Page): form_model = 'player' form_fields = ['WTP_Dec2', 'WTP_Dec2_warnings', 'WTP_Dec2_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec2') def vars_for_template(self): p_vars = self.player.participant.vars prev_upper_bound, prev_lower_bound = p_vars['WTP_upper_bound'], p_vars['WTP_lower_bound'] upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec1) p_vars['new_WTP_upper_bound'] = upper_bound p_vars['new_WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): self.player.participant.vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec2) self.player.participant.vars['WTP_upper_bound'] = self.player.participant.vars['new_WTP_upper_bound'] self.player.participant.vars['WTP_lower_bound'] = self.player.participant.vars['new_WTP_lower_bound'] class WTP_Dec3(Page): form_model = 'player' form_fields = ['WTP_Dec3', 'WTP_Dec3_warnings', 'WTP_Dec3_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec3') def vars_for_template(self): p_vars = self.player.participant.vars prev_upper_bound, prev_lower_bound = p_vars['WTP_upper_bound'], p_vars['WTP_lower_bound'] upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec2) p_vars['WTP_upper_bound'] = upper_bound p_vars['WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): self.player.participant.vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec3) class WTP_Dec4(Page): form_model = 'player' form_fields = ['WTP_Dec4', 'WTP_Dec4_warnings', 'WTP_Dec4_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec4') def vars_for_template(self): p_vars = self.player.participant.vars upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec3) p_vars['WTP_upper_bound'] = upper_bound p_vars['WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): self.player.participant.vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec4) class WTP_Dec5(Page): form_model = 'player' form_fields = ['WTP_Dec5', 'WTP_Dec5_warnings', 'WTP_Dec5_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec5') def vars_for_template(self): p_vars = self.player.participant.vars upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec4) p_vars['WTP_upper_bound'] = upper_bound p_vars['WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): self.player.participant.vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec5) class WTP_Dec6(Page): form_model = 'player' form_fields = ['WTP_Dec6', 'WTP_Dec6_warnings', 'WTP_Dec6_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec6') def vars_for_template(self): p_vars = self.player.participant.vars upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec5) p_vars['WTP_upper_bound'] = upper_bound p_vars['WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): self.player.participant.vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec6) class WTP_Dec7(Page): form_model = 'player' form_fields = ['WTP_Dec7', 'WTP_Dec7_warnings', 'WTP_Dec7_time_spent' ] def js_vars(self): return dict( page_name='WTP_Dec7') def vars_for_template(self): p_vars = self.player.participant.vars upper_bound,lower_bound,midpoint = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec6) p_vars['WTP_upper_bound'] = upper_bound p_vars['WTP_lower_bound'] = lower_bound if self.player.WTP_Dec1 == True: rec_points = c(midpoint) no_rec = c(0) else: rec_points = c(0) no_rec = c(-midpoint) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high return dict(rec_points=rec_points, no_rec=no_rec, incentives=incentives) def before_next_page(self): p_vars = self.player.participant.vars p_vars[f'WTP_Sequence_{self.round_number}'].append(self.player.WTP_Dec7) wtp_upper, wtp_lower, wtp = bisection(p_vars['WTP_upper_bound'],p_vars['WTP_lower_bound'],self.player.WTP_Dec7) if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: self.player.WTP_high_highest,self.player.WTP_high_lowest,self.player.WTP_high_midpoint = wtp_upper, wtp_lower, wtp p_vars['WTP_high_highest'],p_vars['WTP_high_lowest'], p_vars['WTP_high_midpoint'] = wtp_upper, wtp_lower, wtp elif self.round_number == 2: self.player.WTP_low_highest,self.player.WTP_low_lowest,self.player.WTP_low_midpoint = wtp_upper, wtp_lower, wtp p_vars['WTP_low_highest'],p_vars['WTP_low_lowest'], p_vars['WTP_low_midpoint'] = wtp_upper, wtp_lower, wtp elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: self.player.WTP_low_highest,self.player.WTP_low_lowest,self.player.WTP_low_midpoint = wtp_upper, wtp_lower, wtp p_vars['WTP_low_highest'],p_vars['WTP_low_lowest'], p_vars['WTP_low_midpoint'] = wtp_upper, wtp_lower, wtp elif self.round_number == 2: self.player.WTP_high_highest,self.player.WTP_high_lowest,self.player.WTP_high_midpoint = wtp_upper, wtp_lower, wtp p_vars['WTP_high_highest'],p_vars['WTP_high_lowest'], p_vars['WTP_high_midpoint'] = wtp_upper, wtp_lower, wtp class WTP_Start(Page): form_model = 'player' form_fields = ['WTP_Start_warnings', 'WTP_Start_time_spent' ] def js_vars(self): return dict( page_name='WTP_Start') def is_displayed(self): return self.round_number == 1 def vars_for_template(self): if self.player.WTP_sequence == 'HighLow': if self.round_number == 1: incentives = Constants.pwd_rate_high elif self.round_number == 2: incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': if self.round_number == 1: incentives = Constants.pwd_rate_low elif self.round_number == 2: incentives = Constants.pwd_rate_high max_payoff = (incentives*Constants.num_pwd_tasks)+(incentives*Constants.num_math_tasks) ex_pwd = 5 ex_math = 5 example_payoff = (incentives*5)+(incentives*5) return dict(incentives=incentives, max_payoff=max_payoff, ex_pwd=ex_pwd, ex_math=ex_math, example_payoff=example_payoff) class WTP_Middle(Page): form_model = 'player' form_fields = ['WTP_Middle_warnings', 'WTP_Middle_time_spent' ] def js_vars(self): return dict( page_name='WTP_Middle') def is_displayed(self): return self.round_number == 1 def vars_for_template(self): if self.player.WTP_sequence == 'HighLow': next_incentives = Constants.pwd_rate_low elif self.player.WTP_sequence == 'LowHigh': next_incentives = Constants.pwd_rate_high max_payoff = (next_incentives*Constants.num_pwd_tasks)+(next_incentives*Constants.num_math_tasks) ex_pwd = 5 ex_math = 5 example_payoff = (next_incentives*5)+(next_incentives*5) return dict(next_incentives=next_incentives, max_payoff=max_payoff, ex_pwd=ex_pwd, ex_math=ex_math, example_payoff=example_payoff) page_sequence = [ Instructions_1, WTP_Instructions, WTP_Start, WTP_Dec1, WTP_Dec2, WTP_Dec3, WTP_Dec4, WTP_Dec5, WTP_Dec6, WTP_Dec7, WTP_Middle ]