"""INTERFER v17: spectator instructions and practice.""" from otree.api import * from interfer_shared import Config import random import itertools import secrets import string import time import json class C(BaseConstants): NAME_IN_URL = 'spectator_intro' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 W = Config.W TRANSFER_STEP = Config.TRANSFER_STEP BELIEF_BONUS = Config.BELIEF_BONUS class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Comprehension questions and practice exist only once per participant. quiz_first_wrong = models.IntegerField(initial=0) quiz_total_wrong = models.IntegerField(initial=0) quiz_first_wrong_ids = models.LongStringField(initial='[]') chance1 = models.StringField(blank=True) role1 = models.StringField(blank=True) role2 = models.StringField(blank=True) type1 = models.StringField(blank=True) type2 = models.StringField(blank=True) type3 = models.StringField(blank=True) pool1 = models.StringField(blank=True) pool2 = models.StringField(blank=True) final1 = models.StringField(blank=True) final2 = models.StringField(blank=True) final3 = models.StringField(blank=True) practice_hidden_state = models.StringField(initial='') practice_messenger_type = models.StringField(initial='') practice_message = models.StringField(initial='') practice_message_revealed = models.BooleanField(initial=False) practice_message_revealed_at = models.FloatField(initial=0) practice_transfer = models.FloatField(blank=True) practice_submitted_at = models.FloatField(initial=0) def assign_prior_and_practice(player): # Allocate on first arrival at Part 2, not when the session is created. if 'assigned_prior' in player.participant.vars: return n = player.session.vars.get('prior_counter', 0) player.participant.vars['assigned_prior'] = next(itertools.islice(itertools.cycle(Config.PRIORS), n, None)) player.session.vars['prior_counter'] = n + 1 player.practice_hidden_state = 'Luck' if random.random() < player.participant.vars['assigned_prior']/100 else 'Performance' practice_types = ['truth'] * 6 + ['random'] * 2 + ['biased'] * 2 player.practice_messenger_type = random.choice(practice_types) if player.practice_messenger_type == 'truth': player.practice_message = player.practice_hidden_state elif player.practice_messenger_type == 'biased': player.practice_message = 'Performance' else: player.practice_message = random.choice(['Luck', 'Performance']) def quiz_errors(player, vals, expected): wrong = [key for key, solution in expected.items() if vals.get(key) != solution] if wrong: old = set(json.loads(player.quiz_first_wrong_ids or '[]')) new = set(wrong)-old player.quiz_first_wrong += len(new) player.quiz_total_wrong += len(wrong) player.quiz_first_wrong_ids = json.dumps(sorted(old | set(wrong))) hints = { 'chance1': 'The percentages describe how likely each method was before the computer selected one.', 'role1': 'Your decision about how earnings are divided may be selected to determine the actual earnings of two other participants.', 'role2': 'Another participant outside your pair decides how the earnings are divided between you and your paired participant.', 'pay1': 'There is a 50% chance that one of your decisions about dividing earnings will be implemented and a 50% chance that one of your belief reports will be selected for payment.', 'pay2': 'If decisions about dividing earnings are selected, each of your decisions is equally likely to be implemented. If belief reports are selected, each of your belief reports is equally likely to be selected for payment.', 'type1': 'A Truthful messenger reports what actually determined earnings.', 'type2': 'Always Performance never reports Luck.', 'type3': 'Random reports either mechanism with equal likelihood.', 'pool1': 'Every one of the ten messengers is equally likely to be selected.', 'pool2': 'You see the message, but not the selected messenger identity.', 'final1': 'Under Performance, the worker who correctly completed more words receives the full amount (with a random tie-break). Under Luck, both workers have an equal chance of receiving the full amount, regardless of their scores.', 'final2': 'You see the initial chances, messenger pool, and message; the method and messenger identity remain unknown.', 'final3': 'You decide the final earnings of both participants. Their earnings must add up to the total amount of $8.', } return {key:hints[key] for key in wrong} class Part2Welcome(Page): @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): assign_prior_and_practice(player) class SpectatorRole(Page): form_model = 'player' form_fields = ['role1', 'role2'] @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): return dict(amount_after_three=f'{C.W - 3:.0f}') @staticmethod def error_message(player, values): return quiz_errors(player, values, dict(role1='outside', role2='other')) class InitialChances(Page): form_model = 'player' form_fields = ['chance1'] @staticmethod def vars_for_template(player): p = player.participant.vars['assigned_prior'] return dict(prior=p, performance=100-p) @staticmethod def error_message(player, values): p = player.participant.vars['assigned_prior'] correct = 'equal' if p == 50 else 'luck_more' if p > 50 else 'performance_more' return quiz_errors(player, values, dict(chance1=correct)) @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) class MessengerTypes(Page): form_model='player' form_fields=['type1','type2','type3'] @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def error_message(player,values): return quiz_errors(player,values,dict(type1='actual',type2='performance',type3='equal')) class MessengerPools(Page): form_model='player' form_fields=['pool1','pool2'] @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def error_message(player,values): return quiz_errors(player,values,dict(pool1='equal',pool2='hidden')) class BeliefIntro(Page): @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) class ScenarioOverview(Page): @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): p = player.participant.vars['assigned_prior'] return dict(prior=p, performance=100-p) class Practice(Page): """One anonymous practice signal, genuine interface, no payment effects. The state and message are pre-realized upon reaching Part 2. They cannot be changed by clicking the button twice, refreshing, or reloading the page. """ form_model = 'player' form_fields = ['practice_transfer'] @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): return dict(W=C.W, w_display=C.W, step=C.TRANSFER_STEP, prior=player.participant.vars['assigned_prior'], performance=100-player.participant.vars['assigned_prior'], is_uncertain=True, icons=['truth']*6+['random']*2+['biased']*2) @staticmethod def live_method(player, data): if data.get('action') == 'reveal': if not player.practice_message_revealed: player.practice_message_revealed = True player.practice_message_revealed_at = time.time() return {player.id_in_group: dict(revealed=True, message=player.practice_message)} if data.get('action') == 'status': return {player.id_in_group: dict( revealed=player.practice_message_revealed, message=player.practice_message if player.practice_message_revealed else None)} @staticmethod def error_message(player, values): errors = {} amount = values.get('practice_transfer') if (amount is None or not 0 <= amount <= C.W or abs(round(amount/C.TRANSFER_STEP)*C.TRANSFER_STEP-amount) > 0.00001): errors['practice_transfer'] = 'Please choose a practice transfer using the slider.' if not player.practice_message_revealed: errors['practice_transfer'] = 'Please receive your practice message first.' return errors or None @staticmethod def before_next_page(player, timeout_happened): player.practice_submitted_at = time.time() class PracticeFeedback(Page): @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): t = player.practice_transfer or 0 return dict(transfer=f'{t:.2f}', worker_a=f'{C.W-t:.2f}', worker_b=f'{t:.2f}') class FinalQuiz(Page): form_model='player' form_fields=['final1','final2','final3'] @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.vars.get('consent_given', False) @staticmethod def vars_for_template(player): return dict(amount_after_three=f'{C.W - 3:.0f}', prize_display=f'{C.W:.0f}') @staticmethod def error_message(player,values): return quiz_errors(player,values,dict(final1='chances',final2='information',final3='five_three')) page_sequence = [Part2Welcome, SpectatorRole, InitialChances, MessengerTypes, MessengerPools, Practice, PracticeFeedback, BeliefIntro, ScenarioOverview, FinalQuiz]