from otree.api import * from random import sample, shuffle doc = """ Introduction to Dictator game, including slider task, entitlement dictator and random dictator. """ class Constants(BaseConstants): name_in_url = 'dictator_intro' players_per_group = None num_rounds = 1 roles = ['dictator', 'recipient'] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): pass # FUNCTIONS def creating_session(subsession: Subsession): # Earning treatment players = subsession.get_players() shuffle(players) # TODO: shorten these code, shuffling is enough, no need for sampling num_earnings = len(players) // 4 * 2 # make it even earning_players = sample([i for i in range(len(players))], num_earnings) for i in range(len(players)): players[i].participant.earning = (i in earning_players) print(f'Participant {players[i].id_in_group}: {players[i].participant.earning}') # Assign roles earning_players = [] no_earning_players = [] for player in players: if player.participant.earning: earning_players.append(player) else: no_earning_players.append(player) for i in range(len(earning_players)): earning_players[i].participant.role = Constants.roles[i % 2] for i in range(len(no_earning_players)): no_earning_players[i].participant.role = Constants.roles[i % 2] # PAGES class Introduction(Page): @staticmethod def app_after_this_page(player: Player, upcoming_apps): if not player.participant.earning: return upcoming_apps[1] # skip QuizIntro and Quiz class QuizIntro(Page): @staticmethod def is_displayed(player: Player): return player.participant.earning page_sequence = [Introduction, QuizIntro]