from otree.api import * from otree.models import participant author = 'Your name here.' doc = """ Decision Study """ # Models class C(BaseConstants): NAME_IN_URL = 'Part3' PLAYERS_PER_GROUP = 3 NUM_ROUNDS = 1 ENDOWMENT = cu(120.00) MULTIPLIER = 2 PARTICIPATION_FEE = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): p1_decision = models.FloatField(initial=0) p2_decision = models.IntegerField(initial=0) p3_decision = models.IntegerField(initial=0) def is_displayed(self): return self.round_number == C.NUM_ROUNDS # If you need group-level payoff computation, add here def set_group_payoffs(self): # Compute and set group-level payoffs if necessary pass class Player(BasePlayer): participation_fee = models.IntegerField(initial=10) converted_chf = models.FloatField() final_payoff = models.FloatField() final_payoff_display = models.StringField() def set_payoffs(self): p2_decision = self.group.p2_decision p3_decision = self.group.p3_decision p1_decision11 = self.participant.vars['decision11_from_part2'] p1_decision12 = self.participant.vars['decision12_from_part2'] p1_decision13 = self.participant.vars['decision13_from_part2'] # For the CEO's decision of Decision3 == 1 if p3_decision == 1: self.payoff = (C.ENDOWMENT - p1_decision13) # For the CEO's decision of Decision3 == 2 elif p3_decision == 2: # Based on the Supervisor's Decision2 if p2_decision == 2: self.payoff = (C.ENDOWMENT - p1_decision12) elif p2_decision == 1: self.payoff = (C.ENDOWMENT - p1_decision11) # Setting Supervisor and CEO payoffs based on the role assigned if self.id_in_group == 2: # Supervisor self.payoff = float(p1_decision12 * C.MULTIPLIER) elif self.id_in_group == 3: # CEO self.payoff = float(p1_decision13) # Pages # Create a waiting page to form groups dynamically class WaitForGroupPage(WaitPage): template_name = 'custom/custom_wait_page_3.html' group_by_arrival_time = True # in Part3's pages.py, at the start of Part3 class StartOfPage3(Page): timeout_seconds = 0 @staticmethod def before_next_page(player, timeout_happened): for idx, decision in enumerate(['p1_decision', 'p2_decision', 'p3_decision']): # Retrieve the value and ensure it's an integer decision_value = player.participant.vars.get(f"{decision}_from_part2", 0) # Convert the value to integer if it's not already one integer_value = int(decision_value) if isinstance(decision_value, (float, Currency)) else decision_value # Set the attribute on the group object setattr(player.group, decision, integer_value) @staticmethod def vars_for_template(player): decisions = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2', 'decision2_from_part2', 'decision3_from_part2'] return {decision: player.participant.vars.get(decision) for decision in decisions} class PayoffEmployee(Page): def is_displayed(self: Player): return self.id_in_group == 1 def vars_for_template(player: Player): # Getting decisions of other players supervisor_decision = player.group.get_player_by_id(2).participant.vars['decision2_from_part2'] ceo_decision = player.group.get_player_by_id(3).participant.vars['decision3_from_part2'] decisions_from_part2_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] points_kept = {} converted_chf = {} # Calculating points kept and converted amounts for each decision for key in decisions_from_part2_keys: decision_value = player.participant.vars.get(key, 0) points_kept[key] = int(C.ENDOWMENT - decision_value) converted_chf[key] = "{:.2f}".format(round(points_kept[key] * 0.1, 2)) if ceo_decision == 2 and supervisor_decision == 1: player.converted_chf = float(converted_chf['decision11_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'supervisor_decision': supervisor_decision, 'points_kept_11': points_kept['decision11_from_part2'], 'points_kept_12': points_kept['decision12_from_part2'], 'points_kept_13': points_kept['decision13_from_part2'], 'decision11_from_part2': player.participant.vars.get('decision11_from_part2', 0), 'decision12_from_part2': player.participant.vars.get('decision12_from_part2', 0), 'decision13_from_part2': player.participant.vars.get('decision13_from_part2', 0), 'converted_chf': converted_chf['decision11_from_part2'] } if ceo_decision == 2 and supervisor_decision == 2: player.converted_chf = float(converted_chf['decision12_from_part2']) player.final_payoff = round((player.converted_chf + C.PARTICIPATION_FEE), 2) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'supervisor_decision': supervisor_decision, 'points_kept_11': points_kept['decision11_from_part2'], 'points_kept_12': points_kept['decision12_from_part2'], 'points_kept_13': points_kept['decision13_from_part2'], 'decision11_from_part2': player.participant.vars.get('decision11_from_part2', 0), 'decision12_from_part2': player.participant.vars.get('decision12_from_part2', 0), 'decision13_from_part2': player.participant.vars.get('decision13_from_part2', 0), 'converted_chf': converted_chf['decision12_from_part2'] } if ceo_decision == 1: player.converted_chf = float(converted_chf['decision13_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'supervisor_decision': supervisor_decision, 'points_kept_11': points_kept['decision11_from_part2'], 'points_kept_12': points_kept['decision12_from_part2'], 'points_kept_13': points_kept['decision13_from_part2'], 'decision11_from_part2': player.participant.vars.get('decision11_from_part2', 0), 'decision12_from_part2': player.participant.vars.get('decision12_from_part2', 0), 'decision13_from_part2': player.participant.vars.get('decision13_from_part2', 0), 'converted_chf': converted_chf['decision13_from_part2'] } def before_next_page(player: Player, timeout_happened): decision_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] for key in decision_keys: decision_from_part2 = player.participant.vars.get(key, 0) points_kept = C.ENDOWMENT - decision_from_part2 class PayoffSupervisor(Page): def is_displayed(self: Player): return self.id_in_group == 2 def vars_for_template(player: Player): supervisor_decision = player.group.get_player_by_id(2).participant.vars['decision2_from_part2'] ceo_decision = player.group.get_player_by_id(3).participant.vars['decision3_from_part2'] decision_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] employee_decisions = {} multiplied_values = {} converted_chf = {} # Fetch decisions, calculate multiplied and converted values for key in decision_keys: decision_value = player.group.get_player_by_id(1).participant.vars[key] employee_decisions[key] = decision_value multiplied_values[key] = decision_value * C.MULTIPLIER converted_chf[key] = "{:.2f}".format(round(multiplied_values[key] * 0.1, 2)) if ceo_decision == 2 and supervisor_decision == 1: player.converted_chf = float(converted_chf['decision11_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision11_from_part2'] } if ceo_decision == 2 and supervisor_decision == 2: player.converted_chf = float(converted_chf['decision12_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision12_from_part2'] } if ceo_decision == 1: player.converted_chf = float(converted_chf['decision13_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'ceo_decision': ceo_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision13_from_part2'] } def before_next_page(player: Player, timeout_happened): decision_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] for key in decision_keys: decision_value = player.group.get_player_by_id(1).participant.vars[key] multiplied_value = decision_value * C.MULTIPLIER class PayoffCEO(Page): def is_displayed(self: Player): return self.id_in_group == 3 def vars_for_template(player: Player): supervisor_decision = player.group.get_player_by_id(2).participant.vars['decision2_from_part2'] ceo_decision = player.group.get_player_by_id(3).participant.vars['decision3_from_part2'] decision_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] employee_decisions = {} multiplied_values = {} converted_chf = {} # Fetch decisions and calculate converted values for key in decision_keys: decision_value = player.group.get_player_by_id(1).participant.vars[key] employee_decisions[key] = decision_value multiplied_values[key] = decision_value * C.MULTIPLIER converted_chf[key] = "{:.2f}".format(round(multiplied_values[key] * 0.1, 2)) if ceo_decision == 2 and supervisor_decision == 1: player.converted_chf = float(converted_chf['decision11_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'supervisor_decision': supervisor_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision11_from_part2'] } if ceo_decision == 2 and supervisor_decision == 2: player.converted_chf = float(converted_chf['decision12_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'supervisor_decision': supervisor_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision12_from_part2'] } if ceo_decision == 1: player.converted_chf = float(converted_chf['decision13_from_part2']) player.final_payoff = float(player.converted_chf + C.PARTICIPATION_FEE) player.final_payoff_display = "{:.2f}".format(player.final_payoff) return { 'supervisor_decision': supervisor_decision, 'employee_decision11': employee_decisions['decision11_from_part2'], 'employee_decision12': employee_decisions['decision12_from_part2'], 'employee_decision13': employee_decisions['decision13_from_part2'], 'multiplied_11': multiplied_values['decision11_from_part2'], 'multiplied_12': multiplied_values['decision12_from_part2'], 'multiplied_13': multiplied_values['decision13_from_part2'], 'converted_chf': converted_chf['decision13_from_part2'] } def before_next_page(player: Player, timeout_happened): decision_keys = ['decision11_from_part2', 'decision12_from_part2', 'decision13_from_part2'] for key in decision_keys: decision_value = player.group.get_player_by_id(1).participant.vars[key] multiplied_value = decision_value * C.MULTIPLIER class FinalPage(Page): pass page_sequence = [WaitForGroupPage, StartOfPage3, PayoffCEO, PayoffSupervisor, PayoffEmployee, FinalPage]