from otree.api import * c = cu doc = '' class C(BaseConstants): # built-in constants NAME_IN_URL = 'Labour_Decisions_JScursor' PLAYERS_PER_GROUP = None NUM_ROUNDS = 3 # user-defined constants WORK_DURATION_SECONDS = 60 BREAK_DURATION_SECONDS = 60 TRIALS_PER_ROUND = 500 WORK_MINUTES = 10 PAY_PER_STRING = 0.1 TAX_RATE = 0.2 SHIFT_CHARS = ('!', '@', '#', '$', '%', '^', '&', '*', '(', ')') BREAK_MINUTES = 10 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): typed_answer = models.StringField(blank=True) is_correct = models.BooleanField(initial=False) correct_ans = models.StringField(blank=True) def live_strings(player: Player, data): group = player.group participant = player.participant import random SPECIAL_CHARS = ['!', '@', '#', '$', '%', '^', '&', '*', '('] if data.get('type') == 'init': if 'correct_ans' not in participant.vars: participant.vars['correct_ans'] = ''.join( random.choice(SPECIAL_CHARS) for _ in range(5) ) return { player.id_in_group: dict( new_target=participant.vars['correct_ans'], message='', is_correct=None, ) } typed = data.get('typed_answer', '').strip() correct_ans = participant.vars.get('correct_ans', '') if typed == correct_ans: new_string = ''.join(random.choice(SPECIAL_CHARS) for _ in range(5)) participant.vars['correct_ans'] = new_string return { player.id_in_group: dict( is_correct=True, new_target=new_string, message='Correct', ) } return { player.id_in_group: dict( is_correct=False, message='Try again', ) } class Task(Page): form_model = 'player' form_fields = ['typed_answer'] page_sequence = [Task]