from otree.api import * import time import random author = 'Emily Tran' doc = """ Task Performance - Raven's progressive matrices test measuring cognitive ability """ class C(BaseConstants): NAME_IN_URL = 'Part1_ravens' PLAYERS_PER_GROUP = None MINUTES_GIVEN = 10 ECU_PER_CORRECT = 40 # PAYMENT_PER_QUESTION = 1 # PAYMENT_IN_POINTS = 3 NUM_ROUNDS = 3 # number of questions ANSWER_KEYS = [4, 2, 2, 1, 2, 7, 3, 5, 2, 5, 6, 4] INSTRUCTIONS_TEMPLATE = 'Part1_ravens/Instructions.html' class Subsession(BaseSubsession): pass # def creating_session(subsession): # this is run before the start of every round # for player in subsession.get_players(): # if player.round_number == 1: # player.participant.payoff_ravens = 0 class Group(BaseGroup): pass class Player(BasePlayer): answer = models.IntegerField(choices=[1, 2, 3, 4, 5, 6, 7, 8]) is_correct = models.BooleanField(initial=0) payoff_ravens = models.IntegerField(initial=0) num_correct_ravens = models.IntegerField() # FUNCTIONS def get_timeout_seconds(player): import time return player.participant.expiry - time.time() def creating_session(subsession): # this is run before the start of every round for player in subsession.get_players(): if player.round_number == 1: player.payoff_ravens = 0 # PAGES class StartPage(Page): @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.is_dropout is False class Introduction(Page): timeout_seconds = 120 @staticmethod def is_displayed(player): return player.round_number == 1 and player.participant.is_dropout is False @staticmethod def get_timeout_seconds(player): participant = player.participant if participant.is_dropout: return 1 # instant timeout, 1 second else: return player.participant.expiry - time.time() @staticmethod def before_next_page(player, timeout_happened): participant = player.participant if timeout_happened: participant.is_dropout = True # user has 5 minutes to complete as many pages as possible participant.expiry = time.time() + C.MINUTES_GIVEN * 60 class QuestionPage(Page): form_model = 'player' form_fields = ['answer'] timer_text = 'Time left to complete this task:' get_timeout_seconds = get_timeout_seconds @staticmethod def is_displayed(player): return get_timeout_seconds(player) > 2 and player.participant.is_dropout is False @staticmethod def vars_for_template(player): return dict( image_path='Part1_ravens/{}.jpg'.format(player.round_number) ) @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: player.answer = 0 player.is_correct = player.answer == C.ANSWER_KEYS[player.round_number - 1] # player.payoff_ravens = player.is_correct * C.ECU_PER_CORRECT # calculate payoff in each round, not needed class Results(Page): timeout_seconds = 60 @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS and player.participant.is_dropout is False @staticmethod def vars_for_template(player): # Compute players' payoff player.num_correct_ravens = sum([player.is_correct for player in player.in_all_rounds()]) player.payoff_ravens = (sum([player.is_correct for player in player.in_all_rounds()]) * C.ECU_PER_CORRECT) # Add participant variables for outcome player.participant.num_correct_ravens = player.num_correct_ravens player.participant.payoff_ravens = player.payoff_ravens # return { # # 'total_correct': sum([player.is_correct for player in player.in_all_rounds()]), # 'earnings': sum([player.is_correct for player in player.in_all_rounds()]) * C.ECU_PER_CORRECT, # } @staticmethod def get_timeout_seconds(player): if player.participant.is_dropout: return 1 # instant timeout, 1 second else: return player.participant.expiry - time.time() @staticmethod def before_next_page(player, timeout_happened): participant = player.participant if timeout_happened: participant.is_dropout = True # lin: False page_sequence = [ # StartPage, # Introduction, QuestionPage, Results, ]