from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random def common_vars(self): player = self.player form_model = 'player' # Ensure that form_fields matches the number of choices form_fields = ['choice_' + str(k) for k in range(1, Constants.num_choices + 1)] # Each item in option_a_payoffs and option_b_payoffs should be a tuple (high, low) choices = [] for i in range(Constants.num_choices): form_field = form_fields[i] px = int(Constants.probabilities[i] * 100) # Convert to percentage py = int(100 - px) # The complementary probability payoff_a_high, payoff_a_low = Constants.option_a_payoffs[i] payoff_b_high, payoff_b_low = Constants.option_b_payoffs[i] # Append all the values as a tuple choices.append(( form_field, # The field name for form input px, py, # Probabilities for the choices payoff_a_high, payoff_a_low, # Payoffs for Option A payoff_b_high, payoff_b_low, # Payoffs for Option B payoff_a_high == payoff_a_low, payoff_b_high == payoff_b_low, getattr(player, f'choice_{i+1}') )) return { 'choices': choices, } class Instructions(Page): vars_for_template = common_vars def is_displayed(self): return self.participant.vars.get('qualified', True) class Decision(Page): vars_for_template = common_vars form_model = 'player' form_fields = ['choice_' + str(k) for k in range(1, Constants.num_choices+1)] def before_next_page(self): player = self.player # Randomly select a row (1-10) to implement selected_row = random.randint(1, 10) player.selected_row = selected_row # Determine the payout based on the selected row selected_choice = getattr(player, f'choice_{selected_row}') probabilities = Constants.probabilities[selected_row - 1] # Calculate payoffs based on the chosen option if selected_choice == 'A': payoff_high, payoff_low = Constants.option_a_payoffs[selected_row - 1] else: payoff_high, payoff_low = Constants.option_b_payoffs[selected_row - 1] # Determine actual payoff based on the probabilities if random.random() < probabilities: player.payoff = payoff_high else: player.payoff = payoff_low # Store part 3 payoff for payment breakdown display. self.participant.vars['mpl_risk_payoff'] = player.payoff def is_displayed(self): return self.participant.vars.get('qualified', True) class Results(Page): vars_for_template = common_vars def is_displayed(self): return self.session.config.get('feedback',False) and self.participant.vars.get('qualified', True) page_sequence = [Instructions, Decision, Results]