from otree.api import * # Import risk params from input.riskParams import expParts ## MARK: Only keep this! from random import randint import numpy as np import matplotlib as plt from matplotlib import cm import json # Save dict as string doc = """ Your app description """ # MARK: Change study number here to make pages visible thisStudyId = "probMatching" # Function to make feedback field def make_field(label): return models.LongStringField( blank=True, label=label, ) class Constants(BaseConstants): name_in_url = 'pm' players_per_group = None num_rounds = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Select fixed amount randomStudy = models.StringField() # Save name of randomly selected study study = models.IntegerField() compQuestion_risk_1 = models.StringField() compQuestion_risk_2 = models.StringField() # Safe info about instruction choices riskChoice_Instructions = models.StringField() riskOutcome_Instructions = models.StringField() choice_Instructions = models.IntegerField( min= 0, max = 101, ) # Save shell selected during instructions # Select color to determine type colorSelect=models.IntegerField() # Save key variable choice_Study3 = models.IntegerField( min= 0, max = 101, ) # Internal Feedback feedback_colorSelect = make_field("Feedback commentary:") feedback_generalTask = make_field("Feedback commentary:") feedback_option1 = make_field("Feedback commentary:") feedback_option2 = make_field("Feedback commentary:") feedback_attentionScreen = make_field("Feedback commentary:") feedback_task = make_field("Feedback commentary:") # PAGES class taskExplain(Page): form_model = "player" form_fields = [ "feedback_generalTask", ] @staticmethod def is_displayed(player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def js_vars(player: Player): return dict( testing = player.session.config["testing"], ) @staticmethod def vars_for_template(player: Player): return dict( testing = player.session.config["testing"] ) class option1(Page): form_model = "player" form_fields = [ "feedback_option1", "riskChoice_Instructions", "riskOutcome_Instructions", ] @staticmethod def is_displayed(player: Player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def js_vars(player: Player): return dict( payoffs = ['£1.50', '£0.50'], probabilities = [0.46, 0.54], treatment = player.participant.treatment ) @staticmethod def vars_for_template(player: Player): return dict( testing = player.session.config["testing"], ) class option2(Page): form_model = "player" form_fields = [ "feedback_option2", "choice_Instructions" ] @staticmethod def is_displayed(player: Player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def js_vars(player: Player): return dict( payoffs = ['£1.50', '£0.50'], probabilities = ["P", "100-P"], fixedAmount = '£0.75' ) @staticmethod def vars_for_template(player: Player): return dict( testing = player.session.config["testing"], fixedAmount = '£0.75', highPayoff = '£1.50', lowPayoff = '£0.50', ) class colorSelect(Page): form_model = "player" form_fields = [ "feedback_colorSelect", "colorSelect", ] @staticmethod def is_displayed(player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def js_vars(player: Player): colorScheme = [ [idx, val[0], val[1]] for idx, val in player.participant.typeMatching.items() ] colorScheme = [colorScheme[x:x+10] for x in range(0, len(colorScheme), 10)] return dict( testing = player.session.config["testing"], colorScheme = colorScheme, ) def error_message(player, values): if values["colorSelect"] == -9999: msg = "Please select a color before proceeding." return msg @staticmethod def vars_for_template(player: Player): colorScheme = [ [idx, val[0], val[1]] for idx, val in player.participant.typeMatching.items() ] numColors = len(colorScheme) colorScheme = [colorScheme[x:x+10] for x in range(0, len(colorScheme), 10)] return dict( testing = player.session.config["testing"], numColors = numColors, colorScheme = colorScheme, treatment = player.participant.treatment ) @staticmethod def before_next_page(player, timeout_happened): player.participant.type = player.colorSelect tmpData = player.participant.typeMatching[player.colorSelect] player.participant.color= tmpData[0] player.participant.shape= tmpData[1] class attentionScreen(Page): form_model = "player" form_fields = [ "feedback_attentionScreen" ] @staticmethod def is_displayed(player: Player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def vars_for_template(player: Player): return dict( testing = player.session.config["testing"], ) class task(Page): form_model = "player" form_fields = [ "feedback_task", "choice_Study3" ] def error_message(player, values): if values["choice_Study3"] == -9999: msg = "Please type a threshold value before proceeding." return msg @staticmethod def is_displayed(player: Player): # Only in the first round and correct study return (player.round_number == 1) and (player.participant.studyName == thisStudyId) @staticmethod def js_vars(player: Player): studyKey = list(expParts.keys())[player.participant.study] studyProps = expParts[studyKey] payoffs = [] probabilities = [] for elem in studyProps[2]: payoffs.append("£" + '%.2f' %elem[0]) probabilities.append(elem[1]) fixedAmount = "£" + '%.2f' %studyProps[-1] return dict( payoffs = payoffs, probabilities = probabilities, fixedAmount = fixedAmount, ) @staticmethod def vars_for_template(player: Player): return dict( testing = player.session.config["testing"], ) @staticmethod def app_after_this_page(player, upcoming_apps): # Always go to risk resolution return 'riskResolution' @staticmethod def before_next_page(player, timeout_happened): player.participant.singleChoice = player.choice_Study3 page_sequence = [ taskExplain, option1, option2, colorSelect, attentionScreen, task ]