from otree.api import * import random from os import listdir class Constants(BaseConstants): name_in_url = 'cfd' players_per_group = None num_rounds = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): photo_this_round = models.StringField() action_predicted = models.IntegerField( choices=[1, 2], doc="1: defect; 2: cooperate" ) modified_payoffs = models.BooleanField( doc="If a participant will predict behavior of people under modified payoffs or not" ) # FUNCTIONS def creating_session(subsession): if subsession.round_number == 1: for player in subsession.get_players(): # Create lists of photos to be shown to a subject in all rounds player.participant.photo_ids_global = random.sample([f for f in listdir("_static/CFD") if f.endswith(".jpg")], k=Constants.num_rounds) # Choose the first photo for this round player.photo_this_round = player.participant.photo_ids_global[0] # Randomly decide if the participant predicts behavior under modified payoffs or not player.modified_payoffs = bool(random.getrandbits(1)) else: for player in subsession.get_players(): # In all other rounds, choose from the list created in round 1 player.photo_this_round = player.participant.photo_ids_global[subsession.round_number-1] # Choice of modified payoffs is the same for all rounds player.modified_payoffs = player.in_round(subsession.round_number-1).modified_payoffs # PAGES class CFDstart(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def vars_for_template(player: Player): return dict( mod=player.modified_payoffs ) class GenderCFD(Page): form_model = 'player' form_fields = ['action_predicted'] @staticmethod def vars_for_template(player): return dict( mod=player.modified_payoffs, your_photo='CFD/'+player.photo_this_round ) page_sequence = [CFDstart, GenderCFD]