from otree.api import * import random doc = """ Results of the guessing tasks """ class C(BaseConstants): NAME_IN_URL = 'dem_guess_result' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): pass class Group(BaseGroup): pass class Player(BasePlayer): guessed = models.BooleanField( doc="True if the participant guessed at least once" ) question_selected = models.IntegerField() level_selected = models.IntegerField() random_number = models.IntegerField() # FUNCTIONS # PAGES class BeforeResults(Page): @staticmethod def before_next_page(player: Player, timeout_happened): # Retrieve information about guesses g = player.participant.guess_choices a = player.participant.guess_partner_actions # Bonus for the correct guess bonus = player.session.config['guess_bonus'] # Check if participant guessed at all if len(g) > 0: player.guessed = True else: player.guessed = False if player.guessed: # Choose the task, the threshold and a random number to be compared with the level player.question_selected = random.randint(0, len(g)-1) player.level_selected = random.randint(0, 100) player.random_number = random.randint(0, 100) # What was player's guess and partner's action in this question this_guess = g[player.question_selected] this_action = a[player.question_selected] # If belief that action is 2 is smaller than the selected level, compare level with the random number if this_guess <= player.level_selected: player.payoff = bonus * (player.random_number <= player.level_selected) # If belief is greater than the level, check if the action is 2 else: player.payoff = bonus * (this_action == 2) # If never guessed, just pay a bonus else: player.payoff = bonus class GuessResult(Page): @staticmethod def vars_for_template(player: Player): return dict( guessed=player.guessed, payoff=player.payoff, c=player.participant.guess_choices, l=len(player.participant.guess_choices) ) page_sequence = [BeforeResults, GuessResult]