from otree.api import * import random doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'part3' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 SELF_GAIN = 430 PUNISHMENT = 340 BELIEF_GAIN = 100 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): players = subsession.get_players() for player in players: player.Detect_2 = random.randint(0, 1) # Randomly assigns 0 or 1 player.participant.vars['Detect_2'] = player.Detect_2 player.selected = random.choice(['Belief_Taking', 'Belief_Vote']) player.participant.vars['selected'] = player.selected class Group(BaseGroup): pass class Player(BasePlayer): Detect_2 = models.IntegerField() punished_2 = models.BooleanField() selected = models.StringField() donation_part3 = models.IntegerField(initial=0) Taking_2 = models.IntegerField( choices=[[1, 'Ja, ich möchte das Spendengeld an mich nehmen.'], [0, 'Nein, ich möchte das Spendengeld nicht an mich nehmen.']], label="Möchten Sie die als Spende vorgesehenen Punkte an sich nehmen?", widget=widgets.RadioSelect ) Belief_Taking = models.IntegerField( choices=[ [1, '0–1 Personen'], [2, '2–4 Personen'], [3, '5–7 Personen'], [4, '8–10 Personen'] ], label="Wie viele von zehn Teilnehmenden in der Session haben im Durchschnitt in Teil 1 das Spendengeld an sich genommen?", widget=widgets.RadioSelect ) Belief_Vote = models.IntegerField( choices=[ [1, 'Die Mehrheit in meiner Gruppe hat für Option 1 gestimmt'], [2, 'Die Mehrheit in meiner Gruppe hat für Option 2 gestimmt'] ], label="Wie ist die Abstimmung in Ihrer Gruppe ausgegangen?", widget=widgets.RadioSelect ) def calculate_part3_payoff(player): print(f"\n=== Calculating Part 3 Payoff for Player {player.id_in_group} ===") actual = None # Taking behavior and detection print(f"Taking_2 = {player.Taking_2}, Detect_2 = {player.Detect_2}") if player.Taking_2: if player.Detect_2: player.payoff += - C.PUNISHMENT player.participant.vars['payoff_part_3'] = - C.PUNISHMENT player.punished_2 = True player.donation_part3 = 430 print(f"Player punished: -{C.PUNISHMENT} points") else: player.payoff += C.SELF_GAIN player.participant.vars['payoff_part_3'] = C.SELF_GAIN player.punished_2 = False player.donation_part3 = 0 print(f"Player gained: +{C.SELF_GAIN} points") else: player.participant.vars['payoff_part_3'] = 0 player.punished_2 = False player.donation_part3 = 570 print("Player did not take. No punishment or gain.") # Belief evaluation print(f"Selected belief type: {player.selected}") if player.selected == 'Belief_Vote': print(f"Belief_Vote = {player.Belief_Vote}, decision_rule = {player.participant.vars.get('decision_rule')}") if player.participant.vars.get('decision_rule') == player.Belief_Vote: player.payoff += C.BELIEF_GAIN player.participant.vars['payoff_part_3'] += C.BELIEF_GAIN print(f"Correct Belief_Vote. +{C.BELIEF_GAIN} points") else: print("Incorrect Belief_Vote. No bonus.") elif player.selected == 'Belief_Taking': print("Evaluating Belief_Taking...") all_participants = player.session.get_participants() taking_flags = [p.vars.get('Taking_1') == 1 for p in all_participants if p.vars.get('Taking_1') is not None] sample_size = len(taking_flags) taking_count = sum(taking_flags) if sample_size == 0: actual = 1 else: share = taking_count / sample_size # Durchschnitt auf 10 Teilnehmer hochgerechnet. # Schwellen für Kategorien: <= 1.5 -> 0-1, <= 4.5 -> 2-4, <= 7.5 -> 5-7, > 7.5 -> 8-10. if share <= 0.15: actual = 1 elif share <= 0.45: actual = 2 elif share <= 0.75: actual = 3 else: actual = 4 print(f"Player's Belief_Taking = {player.Belief_Taking}, Actual category = {actual}") if player.Belief_Taking == actual: player.payoff += C.BELIEF_GAIN player.participant.vars['payoff_part_3'] += C.BELIEF_GAIN print(f"Correct Belief_Taking. +{C.BELIEF_GAIN} points") else: print("Incorrect Belief_Taking. No bonus.") player.participant.vars.update({ 'part3_taking_2': player.Taking_2, 'part3_detect_2': player.Detect_2, 'part3_selected': player.selected, 'part3_belief_vote': getattr(player, 'Belief_Vote', None), 'part3_belief_taking': getattr(player, 'Belief_Taking', None), 'part3_actual_category': actual, }) print(f"Total payoff from Part 3: {player.participant.vars['payoff_part_3']}") print(f"Total cumulative payoff: {player.payoff}") # PAGES class TakingDecision2(Page): form_model = 'player' form_fields = ['Taking_2'] class Beliefs(Page): form_model = 'player' form_fields = ['Belief_Taking', 'Belief_Vote'] def before_next_page(player: Player, timeout_happened): player.calculate_part3_payoff() page_sequence = [TakingDecision2, Beliefs]