from otree.api import * import random import string import time doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'count_transcribe' PLAYERS_PER_GROUP = None NUM_ROUNDS = 6 POINTS_PER_CORRECT_ANSWER = cu(1) STRING_LENGTH = 15 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): # Define a string where we cut out 1's and l's to prevent confusion distinct_characters = string.ascii_letters + string.digits.replace('l', '').replace('1', '') # Generate the random string using only the distinct characters random_symbols = ''.join(random.choice(distinct_characters) for _ in range(C.STRING_LENGTH)) subsession.session.vars[f't1_random_string_round_{subsession.round_number}'] = random_symbols class Group(BaseGroup): pass class Player(BasePlayer): transcription = models.StringField(label='Please enter the transcription:') result = models.StringField() random_string = models.StringField(initial="") # Ensures that the field always starts as an empty string start_time = models.FloatField(blank=True) # Timestamp when the page is loaded end_time = models.FloatField(blank=True) # Timestamp when the page is submitted # PAGES class Start(Page): form_model = "player" form_fields = ["transcription"] @staticmethod def vars_for_template(player: Player): player.start_time = time.time() # Retrieve the random string for this round random_string = player.session.vars[f't1_random_string_round_{player.round_number}'] player.random_string = random_string # Initialize progress_percent and treatment progress_percent = player.participant.vars['total_progress'] treatment = player.participant.vars.get('treatment', None) # Using get() with a default of None return { "random_string": random_string, 'progress_percent': progress_percent, 'treatment': treatment, } @staticmethod def before_next_page(player: Player, timeout_happened): total_rounds = player.session.config['total_rounds'] progress_increment = 100 / total_rounds player.participant.vars['total_progress'] += progress_increment if player.transcription == "999": player.result = "Skipped" elif player.transcription == player.random_string: player.result = "Correct" player.payoff = C.POINTS_PER_CORRECT_ANSWER else: player.result = "Wrong" player.payoff = cu(0) # Record end time player.end_time = time.time() # @staticmethod # def error_message(player: Player, values): #make sure they have to repeat if wrong # if values["transcription"] != player.random_string: # return 'The transcription is incorrect. Please try again.' class Results(Page): pass class DelayPage(Page): timeout_seconds = 45 @staticmethod def is_displayed(player:Player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player: Player): return { 'delay_time_seconds': 30 } page_sequence = [Start, DelayPage] #delaypage here