from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random from random import shuffle from random import randrange author = 'Austeja Kazemekaityte' doc = """ Team based Anagram Task for Idnetity pgg """ class Constants(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): def creating_session(self): if self.round_number == 1: for p in self.get_players(): p.participant.vars['payment'] = random.choice(['anagram', 'allocation']) p.participant.vars['treatment'] = random.choice(['medium', 'medium']) 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'] = list(zip(indices, anagram_choice, word_choice)) p.participant.vars['pair_select_easy'] = Constants.pair_list_easy.copy() p.participant.vars['pair_select_medium'] = Constants.pair_list_medium.copy() 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_medium'] = [x[0] for x in p.participant.vars['pair_select_medium']] 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_medium'] = [x[1] for x in p.participant.vars['pair_select_medium']] p.participant.vars['anagram_select_difficult'] = [x[1] for x in p.participant.vars['pair_select_difficult']] p.participant.vars['choices_made'] = [] def group_players(self): players = self.get_players() firstT = [p for p in players if p.sex == 1] secondT = [p for p in players if p.sex == 2] group_matrix = [] group_matrix.append(firstT) group_matrix.append(secondT) self.set_group_matrix(group_matrix) 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) def sync_team_by_sex(self): players = self.get_players() for p in players: p.team = p.sex p.participant.vars['player_sex'] = p.sex print(p.participant.vars) 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, 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 def set_payoffs(self): self.payoff = self.correct * Constants.bonus self.participant.vars['correct'] = self.correct self.participant.vars['no_anagrams'] = Constants.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()