from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class Instructions(Page): def is_displayed(self): return self.round_number == 1 class Comprehension(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['comp_instant', 'comp_oneMonthA','comp_oneMonthB'] class Comprehension2(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['comp_prob1', 'comp_prob2'] class Practice1 (Page): def is_displayed(self): return self.round_number == 1 class Practice2 (Page): def is_displayed(self): return self.round_number == 1 class SaveToday(Page): def is_displayed(self): if (self.round_number == 1): self.session.vars['order'] = 0 return True prev_player = self.player.in_round(self.round_number-1) if (prev_player.make_changes == False and prev_player.counter ==Constants.order_max): self.player.make_changes = False self.player.counter = Constants.order_max self.player.round_order = 0 self.player.round_endowment = 0 self.player.round_probA = 0 self.player.round_probB = 0 self.player.round_returnA = 0 self.player.round_returnB = 0 return False return True form_model = 'player' form_fields = ['savings'] def vars_for_template(self): #set the counter if (self.round_number > 1): prev_player = self.player.in_round(self.round_number-1) #if player is altering preferences, counter set to previous round counter if (prev_player.make_changes == True): self.player.counter = prev_player.counter #if new round, counter set to previous round counter +1 else: self.player.counter = prev_player.counter +1 #if first round, counter set to 0 else: self.player.counter = 0 order = Constants.round_order[self.player.counter] if (self.player.counter 0 else "N/A", roundNo_display=self.player.counter+1 ) def before_next_page(self): self.player.round_order = Constants.round_order[self.player.counter] self.player.round_endowment = Constants.endowment[self.player.round_order] self.player.round_probA = Constants.probA[self.player.round_order] self.player.round_returnA = Constants.returnA[self.player.round_order] self.player.round_probB = Constants.probB[self.player.round_order] self.player.round_returnB = Constants.returnB[self.player.round_order] class InvestA(Page): def is_displayed(self): if (self.player.round_returnB == -1): self.player.investA = self.player.round_endowment - self.player.savings return False if (self.round_number == 1): return True prev_player = self.player.in_round(self.round_number - 1) if (prev_player.make_changes == False and prev_player.counter == Constants.order_max): return False return True form_model = 'player' form_fields = ['investA'] def vars_for_template(self): order = Constants.round_order[self.player.counter] endowment = Constants.endowment[order] probA = Constants.probA[order] returnA = Constants.returnA[order] probB = Constants.probB[order] returnB = Constants.returnB[order] max_investA = Constants.endowment[order] - self.player.savings return dict( endowment_display=endowment, probA_display=probA, returnA_display=returnA, probB_display=probB, returnB_display=returnB if returnB>0 else "N/A", max_investA_display=max_investA, roundNo_display = self.player.counter+1 ) class Confirm(Page): def is_displayed(self): if (self.round_number == 1): return True prev_player = self.player.in_round(self.round_number - 1) if (prev_player.make_changes == False and prev_player.counter == Constants.order_max): return False return True form_model = 'player' form_fields = ['make_changes'] def vars_for_template(self): order = Constants.round_order[self.player.counter] endowment = Constants.endowment[order] probA = Constants.probA[order] returnA = Constants.returnA[order] probB = Constants.probB[order] returnB = Constants.returnB[order] max_investA = Constants.endowment[order] - self.player.savings investB = Constants.endowment[order] - self.player.savings - self.player.investA money_today = self.player.savings money_onemonthA = Constants.returnA[order] * self.player.investA money_onemonthB = max(0, Constants.returnB[order] * ( Constants.endowment[order] - self.player.savings - self.player.investA)) return dict( endowment_display=endowment, probA_display=probA, returnA_display=returnA, probB_display=probB, returnB_display=returnB if returnB>0 else "N/A", max_investA_display=max_investA, investB_display=investB, money_today_display=money_today, money_onemonthA_display=money_onemonthA, money_onemonthB_display=money_onemonthB, roundNo_display = self.player.counter+1 ) #for the barchart def js_vars(self): order = Constants.round_order[self.player.counter] money_today = self.player.savings money_onemonthA = Constants.returnA[order] * self.player.investA money_onemonthB = max(0,Constants.returnB[order] * (Constants.endowment[order] - self.player.savings - self.player.investA)) chart_series = [money_today, money_onemonthA, money_onemonthB] return dict( money_today_chart=money_today, money_onemonthA_chart=money_onemonthA, money_onemonthB_chart=money_onemonthB, chart_series=chart_series ) def before_next_page(self): # write investB to data self.player.investB = self.player.round_endowment - self.player.savings - self.player.investA #stage 2 will now play if it is the last stage 1 round if (self.player.make_changes == False and self.player.counter == Constants.order_max): self.session.vars['order'] = 1 # if the round was payoff-determinative, determine the paying asset and assign the values to variables if self.player.counter == self.session.vars['paying_round'] and self.player.make_changes == False: self.session.vars['paying_round_order'] = self.round_number if self.session.vars['paying_asset_number'] <= self.player.round_probA: self.session.vars['paying_asset'] = "A" else: self.session.vars['paying_asset'] = "B" #set the player's payoffs today and one month from today self.player.payoff_today = self.player.savings if self.session.vars['paying_asset'] == "A": self.player.payoff_oneMonth = self.player.investA * self.player.round_returnA if self.session.vars['paying_asset'] == "B": self.player.payoff_oneMonth = max(0, self.player.investB * self.player.round_returnB) class InstructionsStage2(Page): def is_displayed(self): if (self.round_number ==1): return False return self.session.vars['order'] == 1 def before_next_page(self): self.session.vars['order_s2'] = 0 class Stage2(Page): def is_displayed(self): if (self.round_number ==1): return False if (self.player.make_changes == False and self.player.counter == Constants.order_max and self.session.vars['order_s2'] < Constants.order_max_s2): return True form_model = 'player' form_fields = ['choice1', 'choice2', 'choice3', 'choice4', 'choice5', 'choice6', 'choice7', 'choice8', 'choice9', 'choice10', 'choice11', 'choice12', 'choice13', 'choice14', 'choice15', 'choice16', 'choice17', 'choice18', 'choice19'] def vars_for_template(self): prev_player = self.player.in_round(self.session.vars['order']) while (prev_player.make_changes == True or prev_player.round_returnB < 0): temp = self.session.vars['order'] +1 self.session.vars['order'] = temp prev_player = self.player.in_round(self.session.vars['order']) return dict( probA_display="{:.0%}".format(prev_player.round_probA), probB_display="{:.0%}".format(prev_player.round_probB), x_display=prev_player.savings, y_display=prev_player.investA * prev_player.round_returnA, z_display=prev_player.investB * prev_player.round_returnB, alt1_display = "${:,.2f}".format(Constants.stage2_alt[0]), alt2_display = "${:,.2f}".format(Constants.stage2_alt[1]), alt3_display = "${:,.2f}".format(Constants.stage2_alt[2]), alt4_display = "${:,.2f}".format(Constants.stage2_alt[3]), alt5_display="${:,.2f}".format(Constants.stage2_alt[4]), alt6_display="${:,.2f}".format(Constants.stage2_alt[5]), alt7_display="${:,.2f}".format(Constants.stage2_alt[6]), alt8_display="${:,.2f}".format(Constants.stage2_alt[7]), alt9_display="${:,.2f}".format(Constants.stage2_alt[8]), alt10_display="${:,.2f}".format(Constants.stage2_alt[9]), alt11_display="${:,.2f}".format(Constants.stage2_alt[10]), alt12_display="${:,.2f}".format(Constants.stage2_alt[11]), alt13_display="${:,.2f}".format(Constants.stage2_alt[12]), alt14_display="${:,.2f}".format(Constants.stage2_alt[13]), alt15_display="${:,.2f}".format(Constants.stage2_alt[14]), alt16_display="${:,.2f}".format(Constants.stage2_alt[15]), alt17_display="${:,.2f}".format(Constants.stage2_alt[16]), alt18_display="${:,.2f}".format(Constants.stage2_alt[17]), alt19_display="${:,.2f}".format(Constants.stage2_alt[18]), ) def error_message(self, values): answers = [values['choice1'], values['choice2'], values['choice3'], values['choice4'], values['choice5'], values['choice6'], values['choice7'], values['choice8'], values['choice9'], values['choice10'], values['choice11'], values['choice12'], values['choice13'], values['choice14'], values['choice15'], values['choice16'], values['choice17'], values['choice18'], values['choice19']] i = 1 error = False while i<19 and error == False: key = answers[i] for x in range(i): if (key < answers[x]): error = True return "Please make sure that, if you chose option B over option A on one line, you choose option B over option A on all subsequent lines." i = i+1 def before_next_page(self): player_picked = self.player.in_round(self.session.vars['order']) self.player.stage1_round = player_picked.counter+1 if (self.session.vars['order_s2'] == self.session.vars['paying_order_s2']): self.session.vars['paying_round_s2'] = self.round_number self.session.vars['payoff_today_s2'] = player_picked.savings self.session.vars['investA_s2'] = player_picked.investA self.session.vars['investB_s2'] = player_picked.investB if self.session.vars['paying_asset_number_s2'] <= self.player.in_round(self.session.vars['order']).round_probA: self.session.vars['paying_asset_s2'] = "A" self.session.vars['payoff_oneMonth_s2'] = self.player.in_round(self.session.vars['order']).investA * self.player.in_round(self.session.vars['order']).round_returnA else: self.session.vars['paying_asset_s2'] = "B" self.session.vars['payoff_oneMonth_s2'] = max(0, self.player.in_round(self.session.vars['order']).investB * self.player.in_round(self.session.vars['order']).round_returnB) temp1 = self.session.vars['order'] + 1 self.session.vars['order'] = temp1 temp2 = self.session.vars['order_s2'] +1 self.session.vars['order_s2'] = temp2 class FinalPayoff(Page): def is_displayed(self): return self.round_number == Constants.num_rounds def vars_for_template(self): if (self.session.vars['paying_choice_number_s2'] == 0): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice1 if (self.session.vars['paying_choice_number_s2'] == 1): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice2 if (self.session.vars['paying_choice_number_s2'] == 2): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice3 if (self.session.vars['paying_choice_number_s2'] == 3): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice4 if (self.session.vars['paying_choice_number_s2'] == 4): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice5 if (self.session.vars['paying_choice_number_s2'] == 5): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice6 if (self.session.vars['paying_choice_number_s2'] == 6): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice7 if (self.session.vars['paying_choice_number_s2'] == 7): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice8 if (self.session.vars['paying_choice_number_s2'] == 8): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice9 if (self.session.vars['paying_choice_number_s2'] == 9): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice10 if (self.session.vars['paying_choice_number_s2'] == 10): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice11 if (self.session.vars['paying_choice_number_s2'] == 11): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice12 if (self.session.vars['paying_choice_number_s2'] == 12): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice13 if (self.session.vars['paying_choice_number_s2'] == 13): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice14 if (self.session.vars['paying_choice_number_s2'] == 14): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice15 if (self.session.vars['paying_choice_number_s2'] == 15): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice16 if (self.session.vars['paying_choice_number_s2'] == 16): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice17 if (self.session.vars['paying_choice_number_s2'] == 17): paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice18 #if (self.session.vars['paying_choice_number_s2'] == 18): else: paying_round_decision_s2 = self.player.in_round(self.session.vars['paying_round_s2']).choice19 payoff_oneMonth_s2 = self.session.vars['payoff_oneMonth_s2'] if paying_round_decision_s2 == 1 else Constants.stage2_alt[self.session.vars['paying_choice_number_s2']] return dict( paying_round=self.session.vars['paying_round']+1, paying_asset=self.session.vars['paying_asset'], paying_round_investA=self.player.in_round(self.session.vars['paying_round_order']).investA, paying_round_investB=self.player.in_round(self.session.vars['paying_round_order']).investB, payoff_today_s1=self.player.in_round(self.session.vars['paying_round_order']).payoff_today, payoff_oneMonth_s1=self.player.in_round(self.session.vars['paying_round_order']).payoff_oneMonth, paying_round_s2 = self.session.vars['paying_order_s2'] +1, paying_question = self.session.vars['paying_choice_number_s2']+1, payoff_today_s2 = self.session.vars['payoff_today_s2'], investA_s2 = self.session.vars['investA_s2'], investB_s2 = self.session.vars['investB_s2'], paying_asset_s2 = self.session.vars['paying_asset_s2'], payoff_oneMonth_s2_A = "${:,.2f}".format(self.session.vars['payoff_oneMonth_s2']), payoff_oneMonth_s2_B = "${:,.2f}".format(Constants.stage2_alt[self.session.vars['paying_choice_number_s2']]), paying_choice_s2 = 'A' if paying_round_decision_s2 == 1 else 'B', paying_round_decision_s2 = paying_round_decision_s2, payoff_today = self.player.in_round(self.session.vars['paying_round_order']).payoff_today + 5 + self.session.vars['payoff_today_s2'], payoff_oneMonth = self.player.in_round(self.session.vars['paying_round_order']).payoff_oneMonth + payoff_oneMonth_s2 ) #page_sequence = [SaveToday, InvestA, Confirm,InstructionsStage2, Stage2, FinalPayoff] page_sequence = [Instructions, Comprehension, Comprehension2, SaveToday, InvestA, Confirm, InstructionsStage2, Stage2, FinalPayoff]