from otree.api import * import random class C(BaseConstants): NAME_IN_URL = 'anagram' PLAYERS_PER_GROUP = 2 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_MEDIUM = ["hazel", "whale", "width", "frown", "stand", "guard", "train", "query", "viper", "choir", "deemed", "joined", "portal", "tendon", "seemly", "lotion", "meager", "account", "factoid", "isotope"] 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_MEDIUM = ["azhle", "lwahe", "htiwd", "rnowf", "nadts", "augrd", "nrtai", "yqeru", "ivper", "hcoir", "mdedee", "injdeo", "rlopat", "nodnte", "meelsy", "tolion", "gemare", "cnaucot", "todfaci", "tosepoi"] 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 = 50 TIME_LIMIT = 300 TABS = list(range(1, TOTAL_NUMBER + 1)) PAIR_LIST_EASY = list(zip(WORD_LIST_EASY, ANAGRAM_LIST_EASY, TABS)) PAIR_LIST_MEDIUM = list(zip(WORD_LIST_MEDIUM, ANAGRAM_LIST_MEDIUM, TABS)) PAIR_LIST_DIFFICULT = list(zip(WORD_LIST_DIFFICULT, ANAGRAM_LIST_DIFFICULT, TABS)) class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): if subsession.round_number == 1: for s in subsession.get_players(): s.participant.vars['payment'] = random.choice(['anagram', 'allocation']) s.participant.vars['treatment'] = random.choice(['medium', 'medium']) indices = [j for j in range(1, C.TOTAL_NUMBER + 1)] anagram_choice = ['anagram_' + str(j) for j in range(1, C.TOTAL_NUMBER + 1)] word_choice = ['word_' + str(j) for j in range(1, C.TOTAL_NUMBER + 1)] s.participant.vars['list_1'] = zip(indices, anagram_choice, word_choice) s.participant.vars['pair_select_easy'] = C.PAIR_LIST_EASY.copy() s.participant.vars['pair_select_medium'] = C.PAIR_LIST_MEDIUM.copy() s.participant.vars['pair_select_difficult'] = random.sample(C.PAIR_LIST_DIFFICULT, C.TOTAL_NUMBER) s.participant.vars['words_select_easy'] = [x[0] for x in s.participant.vars['pair_select_easy']] s.participant.vars['words_select_medium'] = [x[0] for x in s.participant.vars['pair_select_medium']] s.participant.vars['words_select_difficult'] = [x[0] for x in s.participant.vars['pair_select_difficult']] s.participant.vars['anagram_select_easy'] = [x[1] for x in s.participant.vars['pair_select_easy']] s.participant.vars['anagram_select_medium'] = [x[1] for x in s.participant.vars['pair_select_medium']] s.participant.vars['anagram_select_difficult'] = [x[1] for x in s.participant.vars['pair_select_difficult']] s.participant.vars['choices_made'] = [] ''' This is never used... def group_players_new(self): players = self.get_players() firstT = [p for p in players if p.team == 1] secondT = [p for p in players if p.team == 2] group_matrix = [] group_matrix.append(firstT) group_matrix.append(secondT) self.set_group_matrix(group_matrix) ''' class Group(BaseGroup): pass class Player(BasePlayer): sex = models.IntegerField( choices=[ [1, 'Female'], [2, 'Male'], ], widget=widgets.RadioSelect, label="What is your sex?" ) team = models.IntegerField() # team 1 is Female, team 2 is Male treatment = models.StringField() payment = models.StringField() no_anagrams = models.IntegerField() groupiness = models.IntegerField( choices=[ [1, 'Very Close'], [2, 'Somewhat Close'], [3, 'Neutral'], [4, 'Somewhat Distant'], [5, 'Very Distant'], ], widget=widgets.RadioSelect, label="How close do you feel to your team from the anagram task?", ) completion_code = models.StringField(label="Please insert the completion code") correct = models.IntegerField() # payoff = models.CurrencyField() answer = models.StringField() for j in range(1, C.TOTAL_NUMBER + 1): locals()['anagram_' + str(j)] = models.StringField(blank=True) del j for j in range(1, C.TOTAL_NUMBER + 1): locals()['word_' + str(j)] = models.StringField(blank=True) del j def set_payoffs(self): self.payoff = self.correct * C.BONUS self.participant.vars['correct'] = self.correct self.participant.vars['no_anagrams'] = C.TOTAL_NUMBER self.participant.vars['anagram_payoff'] = self.payoff def check_correct(self): correct = 0 for j, choice, word in self.participant.vars['list_1']: if self.participant.vars['treatment'] == 'easy': choice_i = getattr(self, choice) self.participant.vars['choices_made'].append(choice_i) setattr(self, word, self.participant.vars['words_select_easy'][j - 1]) if self.participant.vars['choices_made'][j - 1] == self.participant.vars['words_select_easy'][j - 1]: correct += 1 if self.participant.vars['treatment'] == 'medium': choice_i = getattr(self, choice) self.participant.vars['choices_made'].append(choice_i) setattr(self, word, self.participant.vars['words_select_medium'][j - 1]) if self.participant.vars['choices_made'][j - 1] == self.participant.vars['words_select_medium'][j - 1]: correct += 1 else: choice_i = getattr(self, choice) self.participant.vars['choices_made'].append(choice_i) setattr(self, word, self.participant.vars['words_select_difficult'][j - 1]) if self.participant.vars['choices_made'][j - 1] == self.participant.vars['words_select_difficult'][ j - 1]: correct += 1 self.correct = correct self.set_payoffs() # FUNCTIONS def sync_team_by_sex(subsession: Subsession): players = subsession.get_players() for p in players: p.team = p.sex p.participant.vars['player_sex'] = p.sex print(p.participant.vars) def group_players(self, player: Player): # .get_player() does not exist... players = player.get_others_in_group(self) team_one = [p for p in players if p.sex == 1] team_two = [p for p in players if p.sex == 2] group_matrix = [team_one, team_two] # Don't have to append. Just insert. player.set_group_matrix(group_matrix) # PAGES class WelcomePage(Page): def is_displayed(player: Player): return player.round_number == 1 def vars_for_template(player: Player): participant = player.participant player.treatment = participant.vars['treatment'] player.payment = participant.vars['payment'] return { 'treatment': player.treatment, 'payment' : player.payment, } form_model = 'player' form_fields = ['sex'] # class General_instructions(Page): # def is_displayed(self): # return self.player.round_number == 1 class Instructions1(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Instructions2(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class ShuffleWaitPage(WaitPage): wait_for_all_groups = True def after_all_players_arrive(subsession: Subsession): # self.subsession.set_player_endowment() sync_team_by_sex(subsession) # group_players() # going to have to re-do this # groups = subsession.get_groups() # commented out since it isn't used... matrix = subsession.get_group_matrix() for row in matrix: print(row) class Task(Page): timeout_seconds = C.TIME_LIMIT form_model = 'player' def get_form_fields(player: Player): return ['anagram_' + str(j) for j in range(1, C.TOTAL_NUMBER + 1)] def vars_for_template(player: Player): participant = player.participant return { 'treatment' : participant.vars['treatment'], 'words_select_easy' : participant.vars['words_select_easy'], 'words_select_medium' : participant.vars['words_select_medium'], 'words_select_difficult' : participant.vars['words_select_difficult'], 'anagram_select_easy' : participant.vars['anagram_select_easy'], 'anagram_select_medium' : participant.vars['anagram_select_medium'], 'anagram_select_difficult': participant.vars['anagram_select_difficult'], 'pair_select_easy' : participant.vars['pair_select_easy'], 'pair_select_medium' : participant.vars['pair_select_medium'], 'pair_select_difficult' : participant.vars['pair_select_difficult'] } def before_next_page(player: Player): player.check_correct() print(player.payoff) print(player.correct) class Results(Page): timeout_seconds = 200 form_model = 'player' form_fields = ['groupiness'] def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS page_sequence = [ WelcomePage, Instructions1, Instructions2, ShuffleWaitPage, Task, Results ]