from otree.api import * import csv from pathlib import Path from secrets import SystemRandom, token_urlsafe from timed_response import response_payload_error, store_timed_response from withdrawal import ExperimentPage secure_random = SystemRandom() TRIALS_CSV = Path(__file__).with_name('trials.csv') def load_trials(): with TRIALS_CSV.open(encoding='utf-8-sig', newline='') as csv_file: trials = [ dict( source_trial=index, option_a_self=int(row['SelfA']), option_a_other=int(row['OtherA']), option_b_self=int(row['SelfB']), option_b_other=int(row['OtherB']), ) for index, row in enumerate(csv.DictReader(csv_file), start=1) ] if len(trials) != 72: raise ValueError(f'Expected 72 EEM trials, found {len(trials)}.') return trials TRIALS = load_trials() class C(BaseConstants): NAME_IN_URL = 'game-02' PLAYERS_PER_GROUP = None NUM_ROUNDS = len(TRIALS) JITTER_MIN_MS = 200 JITTER_MAX_MS = 600 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): rt = models.FloatField(blank=True) chosen_side = models.StringField(choices=['Left', 'Right'], blank=True) missed = models.BooleanField(initial=False) response_payload = models.LongStringField() response_token = models.StringField() jitter_ms = models.IntegerField() source_trial = models.IntegerField() left_option = models.StringField(choices=['A', 'B']) left_self = models.IntegerField() left_other = models.IntegerField() right_self = models.IntegerField() right_other = models.IntegerField() check1 = models.StringField( choices=[ ('A', 'Choose between two point distributions for myself and another participant'), ('B', 'Predict which distribution the computer will choose'), ('C', 'Choose only how many points I receive'), ], label='1. What do you need to do on each trial?', widget=widgets.RadioSelect, ) check2 = models.StringField( choices=[ ('A', 'All trials are added together'), ('B', 'One trial is randomly selected and the chosen distribution determines both participants’ points'), ('C', 'The other participant chooses the payment after the task'), ], label='2. What determines the points earned from this task?', widget=widgets.RadioSelect, ) check3 = models.StringField( choices=[ ('A', 'A participant I already know'), ('B', 'A computer program'), ('C', 'A participant randomly selected from all participants in the current experiment'), ], label='3. Who is the other participant?', widget=widgets.RadioSelect, ) check4 = models.StringField( choices=[ ('A', 'You receive 8 points and the other participant receives 8 points'), ('B', 'You receive 15 points and the other participant receives 5 points'), ('C', 'You receive 5 points and the other participant receives 15 points'), ], label='4. In the distribution shown below, the right option is selected. How many points do you and the other participant receive?', widget=widgets.RadioSelect, ) def creating_session(subsession): for player in subsession.get_players(): player.response_token = token_urlsafe(16) if player.round_number == 1: trials = [ dict(trial, left_option=secure_random.choice(('A', 'B'))) for trial in TRIALS ] secure_random.shuffle(trials) player.participant.vars['social_preference_trials'] = trials player.jitter_ms = secure_random.randint(C.JITTER_MIN_MS, C.JITTER_MAX_MS) class SocialPreferenceInstructions(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class SocialPreferenceExample(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def vars_for_template(player): return dict(left_self=8, left_other=8, right_self=15, right_other=5) class SocialPreferenceCheck(ExperimentPage): form_model = 'player' form_fields = ['check1', 'check2', 'check3', 'check4'] @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def vars_for_template(player): return dict(left_self=8, left_other=8, right_self=15, right_other=5) @staticmethod def error_message(player, values): if values != dict(check1='A', check2='B', check3='C', check4='B'): return 'At least one answer is incorrect. Please try again.' class SocialPreferencePracticeIntro(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class SocialPreferencePractice(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def vars_for_template(player): return dict(left_self=8, left_other=8, right_self=15, right_other=5) class SocialPreferencePracticeEnd(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class SocialPreferenceTask(ExperimentPage): form_model = 'player' form_fields = ['response_payload'] @staticmethod def vars_for_template(player): trial = player.participant.vars['social_preference_trials'][player.round_number - 1] left_option = trial['left_option'] right_option = 'B' if left_option == 'A' else 'A' player.source_trial = trial['source_trial'] player.left_option = left_option player.left_self = trial[f'option_{left_option.lower()}_self'] player.left_other = trial[f'option_{left_option.lower()}_other'] player.right_self = trial[f'option_{right_option.lower()}_self'] player.right_other = trial[f'option_{right_option.lower()}_other'] return dict( left_self=player.left_self, left_other=player.left_other, right_self=player.right_self, right_other=player.right_other, response_token=player.response_token, ) @staticmethod def error_message(player, values): return response_payload_error(values['response_payload'], player.response_token) @staticmethod def before_next_page(player, timeout_happened): store_timed_response(player) class Fixation(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number < C.NUM_ROUNDS class SocialPreferenceEnd(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS page_sequence = [ SocialPreferenceInstructions, SocialPreferenceExample, SocialPreferenceCheck, SocialPreferencePracticeIntro, SocialPreferencePractice, SocialPreferencePracticeEnd, SocialPreferenceTask, Fixation, SocialPreferenceEnd, ]