import random from random import shuffle, randrange from otree.api import * author = 'Austeja Kazemekaityte' doc = """ First part (anagram task) of anagram+ctb experiment """ class Constants(BaseConstants): name_in_url = 'anagram_ctb_1' players_per_group = None num_rounds = 1 word_list_easy = [ "hazel", "whale", "width", "frown", "stand", "guard", "train", "query", "viper", "choir", "match", "chair", "child", "logic", "clown", "brown", "cloud", "fancy", "joker", "rough", ] word_list_difficult = [ "gular", "jorum", "tumid", "tagma", "numen", "oribi", "epode", "roshi", "sapid", "roily", "apeak", "gigue", "besom", "cover", "papaw", "vicar", "aioli", "force", "lurid", "pasha", ] anagram_list_easy = [ "azhle", "lwahe", "htiwd", "rnowf", "nadts", "augrd", "nrtai", "yqeru", "ivper", "hcoir", "hactm", "ciahr", "dhilc", "ioglc", "ownlc", "wnobr", "ldouc", "acnfy", "erjko", "ougrh", ] anagram_list_difficult = [ "laurg", "rmjuo", "miudt", "agtam", "eunnm", "iibro", "dpeeo", "isorh", "iapsd", "iylor", "epkaa", "uiegg", "smbeo", "verco", "ppaaw", "criav", "iiola", "ofrec", "iurdl", "sphaa", ] # num_to_select = 20 total_number = len(word_list_easy) bonus = cu(0.02) time_limit = 300 tabs = list(range(1, total_number + 1)) pair_list_easy = list(zip(word_list_easy, anagram_list_easy, tabs)) pair_list_difficult = list(zip(word_list_difficult, anagram_list_difficult, tabs)) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): treatment = models.StringField() payment = models.StringField() no_anagrams = models.IntegerField() prolific_ID = models.StringField(label="Please insert your prolific ID") completion_code = models.StringField(label="Please insert the completion code") correct = models.IntegerField() # payoff = models.CurrencyField() answer = models.StringField() for j in range(1, Constants.total_number + 1): locals()['anagram_' + str(j)] = models.StringField(blank=True) del j for j in range(1, Constants.total_number + 1): locals()['word_' + str(j)] = models.StringField(blank=True) del j # FUNCTIONS def creating_session(subsession: Subsession): if subsession.round_number == 1: for p in subsession.get_players(): p.participant.vars['payment'] = random.choice(['anagram', 'allocation']) p.participant.vars['treatment'] = random.choice(['easy', 'difficult']) indices = [j for j in range(1, Constants.total_number + 1)] anagram_choice = ['anagram_' + str(j) for j in range(1, Constants.total_number + 1)] word_choice = ['word_' + str(j) for j in range(1, Constants.total_number + 1)] p.participant.vars['list_1'] = zip(indices, anagram_choice, word_choice) p.participant.vars['pair_select_easy'] = random.sample( Constants.pair_list_easy, Constants.total_number ) p.participant.vars['pair_select_difficult'] = random.sample( Constants.pair_list_difficult, Constants.total_number ) p.participant.vars['words_select_easy'] = [ x[0] for x in p.participant.vars['pair_select_easy'] ] p.participant.vars['words_select_difficult'] = [ x[0] for x in p.participant.vars['pair_select_difficult'] ] p.participant.vars['anagram_select_easy'] = [ x[1] for x in p.participant.vars['pair_select_easy'] ] p.participant.vars['anagram_select_difficult'] = [ x[1] for x in p.participant.vars['pair_select_difficult'] ] p.participant.vars['choices_made'] = [] def set_payoff(player: Player): player.payoff = player.correct * Constants.bonus player.participant.vars['correct'] = player.correct player.participant.vars['no_anagrams'] = Constants.total_number def check_correct(player: Player): correct = 0 for j, choice, word in player.participant.vars['list_1']: if player.participant.vars['treatment'] == 'easy': choice_i = getattr(player, choice) player.participant.vars['choices_made'].append(choice_i) setattr(player, word, player.participant.vars['words_select_easy'][j - 1]) if ( player.participant.vars['choices_made'][j - 1] == player.participant.vars['words_select_easy'][j - 1] ): correct += 1 else: choice_i = getattr(player, choice) player.participant.vars['choices_made'].append(choice_i) setattr(player, word, player.participant.vars['words_select_difficult'][j - 1]) if ( player.participant.vars['choices_made'][j - 1] == player.participant.vars['words_select_difficult'][j - 1] ): correct += 1 player.correct = correct set_payoff(player) # PAGES class Information(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 @staticmethod def vars_for_template(player: Player): player.treatment = player.participant.vars['treatment'] player.payment = player.participant.vars['payment'] return { 'treatment': player.treatment, 'payment': player.payment, } class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 form_model = 'player' form_fields = ['prolific_ID'] class General_instructions(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Instructions(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Task(Page): timeout_seconds = Constants.time_limit form_model = 'player' @staticmethod def get_form_fields(player: Player): return ['anagram_' + str(j) for j in range(1, Constants.total_number + 1)] @staticmethod def vars_for_template(player: Player): return { 'treatment': player.participant.vars['treatment'], 'words_select_easy': player.participant.vars['words_select_easy'], 'words_select_difficult': player.participant.vars['words_select_difficult'], 'anagram_select_easy': player.participant.vars['anagram_select_easy'], 'anagram_select_difficult': player.participant.vars['anagram_select_difficult'], 'pair_select_easy': player.participant.vars['pair_select_easy'], 'pair_select_difficult': player.participant.vars['pair_select_difficult'], } @staticmethod def before_next_page(player: Player, timeout_happened): check_correct(player) class Results(Page): timeout_seconds = 200 @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds page_sequence = [Information, Introduction, General_instructions, Instructions, Task, Results]