from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants class BeforeResults(Page): form_model = "player" form_fields = [ "feedback_understanding", # "feedback_probabilities", "feedback_fun", # "feedback_payment", "feedback_final_remarks", ] def before_next_page(self): self.session.vars["completions_by_treatment"][ str(self.participant.vars["treatment"]) ] += 1 def is_displayed(self): return self.round_number == 1 def js_vars(self): return dict(min_time_on_page=self.session.config["min_time_on_page_feedback"],) class Risk_Resolution(Page): form_model = "player" form_fields = ["risk_input", "risk_input_helper"] def is_displayed(self): return (self.round_number == 1) or ( not self.participant.vars["stop_showing_flag"] ) def vars_for_template(self): ### Load some setup stuff from before # if self.round_number == 1: envelope = self.participant.vars["envelope"] print(envelope) chosen_type = envelope[1] type_order = [item[2] for item in Constants.gamble] target_round = type_order.index(chosen_type) player_choices = self.participant.vars["player_choices"] player_choice = player_choices[target_round] computer_threshold = envelope[4] if self.round_number == 1: self.participant.vars["player_choice"] = player_choice self.participant.vars["option_1_flag"] = player_choice > computer_threshold self.participant.vars["number_wins"] = [] self.participant.vars["risk_inputs"] = [] self.participant.vars["lost_flag"] = False self.participant.vars["stop_showing_flag"] = False self.participant.vars["last_round_flag"] = False if self.participant.vars["last_round_flag"] == True: self.participant.vars["stop_showing_flag"] = True if self.round_number > envelope[8]: self.participant.vars["stop_showing_flag"] = True if self.participant.vars["option_1_flag"] == False: self.participant.vars["stop_showing_flag"] = True print(f"stop_showing_flag: {self.participant.vars['stop_showing_flag']}") ### Set endowment for payoff ### Option 1 if self.participant.vars["option_1_flag"]: # self.player.endow_payoff = 0 ### if this is the final round if self.participant.vars["stop_showing_flag"] == True: self.player.base_endowment = envelope[2] self.player.endow_payoff = 0 self.player.risky_payoff = ( envelope[5][0] * len(self.participant.vars["number_wins"]) + self.participant.vars["lost_flag"] * envelope[5][1] ) ### if this is not the final round in a repeated risk else: # Only here to be more easy to understand pass ### track losses for display in the template if self.player.round_number > 1 and envelope[8] == 1: win_tracker = -999 elif self.participant.vars["lost_flag"]: win_tracker = self.participant.vars["number_wins"] + [0] win_tracker = [ (win, False) if i < len(win_tracker) - 1 else (win, True) for i, win in enumerate(win_tracker) ] else: win_tracker = self.participant.vars["number_wins"] win_tracker = [ (win, False) if i < len(win_tracker) - 1 else (win, True) for i, win in enumerate(win_tracker) ] ### join risk inputs for display in the template risk_input = "".join( [ f"{input}, " if i < len(self.participant.vars["risk_inputs"]) - 1 else f"{input}" for i, input in enumerate(self.participant.vars["risk_inputs"]) ] ) ### Option 2 else: self.player.base_endowment = envelope[2] self.participant.vars["last_round_flag"] = True self.player.endow_payoff = computer_threshold self.player.risky_payoff = 0 win_tracker = 999 risk_input = 999 ### Prepare Vars for Template if self.participant.vars["stop_showing_flag"] == True: risky_payoff = self.player.risky_payoff endow_payoff = self.player.endow_payoff base_endowment = self.player.base_endowment final_payoff = base_endowment + endow_payoff + risky_payoff self.player.payoff = final_payoff * 1000 else: risky_payoff = 999 endow_payoff = 999 base_endowment = 999 final_payoff = 999 user_chosen_decision = self.participant.vars["user_chosen_decision"] embed_classname = ( "embed-responsive embed-responsive-custombig" if envelope[8] != 1 else "embed-responsive embed-responsive-customsmall" ) print(win_tracker) treatment_order = self.participant.vars["treatment_order"] chosen_round = treatment_order.index(envelope[1]) + 1 print(chosen_round) return dict( condition=3, # Payoff stuff round_number=self.round_number, pay_up=envelope[5][0], pay_down=envelope[5][1], player_choice=self.participant.vars["player_choice"], user_chosen_decision=user_chosen_decision, chosen_round=chosen_round, choice_type=envelope[1], computer_choice=computer_threshold, #pdf_path=f"rnd_vars/{user_chosen_decision}_no_pwd.pdf/#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0", pdf_path=f"rnd_vars/{user_chosen_decision}_no_pwd.pdf", embed_classname=embed_classname, risky_payoff=round(risky_payoff, 2), endow_payoff=round(endow_payoff, 2), base_endowment=round(base_endowment), final_payoff=round(final_payoff, 2), risk_input=risk_input, win_tracker=win_tracker, option_1_flag=self.participant.vars["option_1_flag"], last_round_flag=self.participant.vars["stop_showing_flag"], repetitions=envelope[8], ) def before_next_page(self): ### If not last round ie repeated risk check if player won if not self.participant.vars["stop_showing_flag"] == True: ### if first round and won if ( self.round_number == 1 and int(self.player.risk_input) in self.participant.vars["rnd_numbers"][self.round_number - 1] ): self.participant.vars["number_wins"] = [int(self.player.risk_input)] self.participant.vars["last_round_flag"] = False self.participant.vars["lost_flag"] = False ### if not first round and won elif ( int(self.player.risk_input) in self.participant.vars["rnd_numbers"][self.round_number - 1] ): self.participant.vars["number_wins"].append(int(self.player.risk_input)) self.participant.vars["last_round_flag"] = False self.participant.vars["lost_flag"] = False ### if first round and lost elif self.round_number == 1: self.participant.vars["number_wins"] = [] self.participant.vars["last_round_flag"] = True self.participant.vars["lost_flag"] = True self.player.lose_flag = True ### if not first round and lost else: self.participant.vars["last_round_flag"] = True self.participant.vars["lost_flag"] = True ### Track the risk inputs for final display if self.round_number == 1: self.participant.vars["risk_inputs"] = [int(self.player.risk_input)] else: self.participant.vars["risk_inputs"].append(int(self.player.risk_input)) def js_vars(self): return dict(condition=3) def error_message(self, values): if values["risk_input_helper"] == False: msg = "Please submit a valid number." else: msg = "" return msg page_sequence = [ BeforeResults, Risk_Resolution # ]