from otree.api import * import csv from decimal import Decimal 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') OUTCOME_COLUMNS = [ f'{option}Outcome{index}' for option in ('A', 'B') for index in range(1, 4) ] def number(value): value = Decimal(value) return int(value) if value == value.to_integral_value() else float(value) def load_trials(): with TRIALS_CSV.open(encoding='utf-8-sig', newline='') as csv_file: rows = csv.DictReader(csv_file) trials = [] for row in rows: assert all(float(row[column]) >= 0 for column in OUTCOME_COLUMNS) trial = dict( source_task=int(row['Task']), option_a_outcomes=[number(row[f'AOutcome{i}']) for i in range(1, 4)], option_b_outcomes=[number(row[f'BOutcome{i}']) for i in range(1, 4)], option_a_probs=[number(Decimal(row[f'AProb{i}']) * 100) for i in range(1, 4)], option_b_probs=[number(Decimal(row[f'BProb{i}']) * 100) for i in range(1, 4)], ) assert abs(sum(trial['option_a_probs']) - 100) < 1e-9 assert abs(sum(trial['option_b_probs']) - 100) < 1e-9 trials.append(trial) assert len(trials) == 68 return trials TRIALS = load_trials() doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'game-03' PLAYERS_PER_GROUP = None NUM_ROUNDS = len(TRIALS) JITTER_MIN_MS = 200 JITTER_MAX_MS = 600 def creating_session(subsession): for p in subsession.get_players(): p.response_token = token_urlsafe(16) if p.round_number == 1: trials = [ dict(trial, left_option=secure_random.choice(('A', 'B'))) for trial in TRIALS ] secure_random.shuffle(trials) p.participant.vars['risk_trials'] = trials p.jitter_ms = secure_random.randint(C.JITTER_MIN_MS, C.JITTER_MAX_MS) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): rt = models.FloatField(blank=True) response_payload = models.LongStringField() response_token = models.StringField() source_task = models.IntegerField() left_option = models.StringField(choices=['A', 'B']) left_outcomes = models.StringField() right_outcomes = models.StringField() left_probs = models.StringField() right_probs = models.StringField() jitter_ms = models.IntegerField() missed = models.BooleanField(initial=False) chosen_side = models.StringField( choices=['Left', 'Right'], widget=widgets.RadioSelect, blank=True, ) check1 = models.StringField( choices=[('A', 'Spin a wheel to see if I win points'), ('B', 'Predict which lottery the computer will pick'), ('C', 'Choose between two different lotteries')], label='1. What do you need to do each round?', widget=widgets.RadioSelect ) check2 = models.StringField( choices=[('A', 'My average point winnings across all rounds'), ('B', 'You spin my chosen wheel in the last round'), ('C', 'One round is randomly selected and you spin my chosen wheel in this round')], label='2. What determines your points earning for this game?', widget=widgets.RadioSelect ) class RiskInstructions(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class RiskExample(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def vars_for_template(player): left_payoffs = (0, 5, 10) right_payoffs = (0, 10, 15) left_probs = (12.5, 75, 12.5) right_probs = (25, 50, 25) return dict( left_payoffs=left_payoffs, right_payoffs=right_payoffs, left_probs=left_probs, right_probs=right_probs, ) class RiskCheck(ExperimentPage): form_model = 'player' form_fields = ['check1', 'check2'] @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def error_message(player, values): correct_1 = "C" correct_2 = "C" if ( values['check1'] != correct_1 or values['check2'] != correct_2 ): return 'At least one answer is incorrect. Please try again.' class RiskPractice(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 @staticmethod def vars_for_template(player): return dict( payoffs=(0, 5, 10), left_probs=(50, 37.5, 12.5), right_probs=(12.5, 37.5, 50), ) class RiskPracticeIntro(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class RiskPracticeEnd(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == 1 class RiskTask(ExperimentPage): form_model = 'player' form_fields = ['response_payload'] @staticmethod def vars_for_template(player): trial = player.participant.vars['risk_trials'][player.round_number - 1] left_option = trial['left_option'] right_option = 'B' if left_option == 'A' else 'A' left_outcomes = trial[f'option_{left_option.lower()}_outcomes'] right_outcomes = trial[f'option_{right_option.lower()}_outcomes'] left_probs = trial[f'option_{left_option.lower()}_probs'] right_probs = trial[f'option_{right_option.lower()}_probs'] player.source_task = trial['source_task'] player.left_option = left_option player.left_outcomes = ",".join([str(i) for i in left_outcomes]) player.right_outcomes = ",".join([str(i) for i in right_outcomes]) player.left_probs = ",".join([str(i) for i in left_probs]) player.right_probs = ",".join([str(i) for i in right_probs]) colors = ['#8BCBE6', '#F2D06B', '#A095D8'] left_legend = [(p, c) for p, c in zip(left_probs, colors) if p != 0] right_legend = [(p, c) for p, c in zip(right_probs, colors) if p != 0] return dict( left_outcomes=left_outcomes, right_outcomes=right_outcomes, left_probs = left_probs, right_probs = right_probs, round_num = player.round_number, total_rounds = C.NUM_ROUNDS, left_legend=left_legend, right_legend=right_legend, 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 RiskEnd(ExperimentPage): @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS page_sequence = [ RiskInstructions, RiskExample, RiskCheck, RiskPracticeIntro, RiskPractice, RiskPracticeEnd, RiskTask, Fixation, RiskEnd, ]