from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency, ) import random, string, json, time from statistics import mean doc = """ a.k.a. Encryption Game """ class Constants(BaseConstants): players_per_group = 4 num_rounds = 3 name_in_url = "encrypt_js" fixed_amount = 0.5 letters_per_word = 3 use_timeout = True seconds_per_entr = 120 seconds_per_period = 300 picked_round=random.randrange(1,3,1) class Subsession(BaseSubsession): dictionary = models.StringField() def creating_session(self): self.group_randomly(fixed_id_in_group=True) play = self.get_players() performancestot = [p.performance for p in play] average_perf=mean(performancestot) # Create dictionary letters = list(string.ascii_uppercase) random.shuffle(letters) numbers = [] N = list(range(100, 1000)) for i in range(27): choice = random.choice(N) N.remove(choice) numbers.append(choice) d = [letters, numbers] dictionary = dict([(d[0][i], d[1][i]) for i in range(26)]) self.dictionary = json.dumps(dictionary) class Group(BaseGroup): def set_payoffs(self): p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p3 = self.get_player_by_id(3) p4 = self.get_player_by_id(4) fixed_a = Constants.fixed_amount players = self.get_players() performances = [p.performance for p in players] average_perf = sum(performances)/len(performances) if self.round_number == 1: if performances[0] > performances[1]: p1.earnings = float(p1.performance * fixed_a) p2.earnings = 0 elif performances[1] > performances[0]: p2.earnings = float(p2.performance * fixed_a) p1.earnings = 0 else: p1.earnings = float(p1.performance * fixed_a) p2.earnings = float(p2.performance * fixed_a) if performances[2] > performances[3]: p3.earnings = float(p3.performance * fixed_a) p4.earnings = 0 elif performances[3] > performances[2]: p4.earnings = float(p4.performance * fixed_a) p3.earnings = 0 else: p3.earnings = float(p3.performance * fixed_a) p4.earnings = float(p4.performance * fixed_a) for p in players: if p.self_eval_perf_t1==1: if p.earnings==0: p.bonus_question=0 else: p.bonus_question=0.5 else: if p.earnings==0: p.bonus_question=0.5 else: p.bonus_question=0 if p.performance < 0.85*average_perf: if p.self_eval_perf_groupe_t1==1: p.bonus_question=p.bonus_question+0.5 elif p.performance < 0.95*average_perf: if p.self_eval_perf_groupe_t1==2: p.bonus_question=p.bonus_question+0.5 elif p.performance <1.05*average_perf: if p.self_eval_perf_groupe_t1==3: p.bonus_question=p.bonus_question+0.5 elif p.performance < 1.15*average_perf: if p.self_eval_perf_groupe_t1==4: p.bonus_question=p.bonus_question+0.5 else: if p.self_eval_perf_groupe_t1==5: p.bonus_question=p.bonus_question+0.5 elif (self.round_number == 2): if (performances[0] + performances[1]) > ( performances[2] + performances[3] ): p1.earnings = float(p1.performance * fixed_a) p2.earnings = float(p2.performance * fixed_a) p3.earnings= 0 p4.earnings = 0 elif (performances[0] + performances[1]) < ( performances[2] + performances[3] ): p1.earnings = 0 p2.earnings = 0 p3.earnings = float(p3.performance * fixed_a) p4.earnings = float(p4.performance * fixed_a) else: p1.earnings = float(p1.performance * fixed_a) p2.earnings= float(p2.performance * fixed_a) p3.earnings = float(p3.performance * fixed_a) p4.earnings = float(p4.performance * fixed_a) else: genders=[p.in_round(1).gender for p in players] tresholds=[p.gender_pair for p in players] valbonus_female=[10,9,8,7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0] valbonus_male=[0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10] if genders[0]==1: if tresholds[1]==22: p2.bonus=0 else: p2.valeur=random.randrange(tresholds[1]-1,21,1) p2.bonus=valbonus_male[p2.valeur] else: if tresholds[1]==1: p2.bonus=0 else: p2.valeur=random.randrange(0,tresholds[1]-1,1) p2.bonus=valbonus_female[p2.valeur] if genders[1]==1: if tresholds[0]==22: p1.bonus=0 else: p1.valeur=random.randrange(tresholds[0]-1,21,1) p1.bonus=valbonus_male[p1.valeur] else: if tresholds[0]==1: p1.bonus=0 else: p1.valeur=random.randrange(0,tresholds[0]-1,1) p1.bonus=valbonus_female[p1.valeur] if genders[2]==1: if tresholds[3]==22: p4.bonus=0 else: p4.valeur=random.randrange(tresholds[3]-1,21,1) p4.bonus=valbonus_male[p4.valeur] else: if tresholds[3]==1: p4.bonus=0 else: p4.valeur=random.randrange(0,tresholds[3]-1,1) p4.bonus=valbonus_female[p4.valeur] if genders[3]==1: if tresholds[2]==22: p3.bonus=0 else: p3.valeur=random.randrange(tresholds[2]-1,21,1) p3.bonus=valbonus_male[p3.valeur] else: if tresholds[2]==1: p3.bonus=0 else: p3.valeur=random.randrange(0,tresholds[2]-1,1) p3.bonus=valbonus_female[p3.valeur] if (performances[0] + performances[1]) > ( performances[2] + performances[3] ): p1.earnings = float(p1.performance * fixed_a) + p1.bonus p2.earnings = float(p2.performance * fixed_a) + p2.bonus p3.earnings = p3.bonus p4.earnings = p4.bonus elif (performances[0] + performances[1]) < ( performances[2] + performances[3] ): p1.earnings = p1.bonus p2.earnings = p2.bonus p3.earnings = float(p3.performance * fixed_a) + p3.bonus p4.earnings = float(p4.performance * fixed_a) + p4.bonus else: p1.earnings = float(p1.performance * fixed_a) + p1.bonus p2.earnings = float(p2.performance * fixed_a) + p2.bonus p3.earnings = float(p3.performance * fixed_a) + p3.bonus p4.earnings = float(p4.performance * fixed_a) + p4.bonus class Player(BasePlayer): performance_entr = models.IntegerField(initial=0) mistakes_entr = models.IntegerField(initial=0) performance = models.IntegerField(initial=0) mistakes = models.IntegerField(initial=0) age = models.IntegerField(label="Your age", min=15, max=100) valeur=models.IntegerField(initial=0) bonus=models.IntegerField(initial=0) earnings=models.CurrencyField(initial=0) bonus_question=models.CurrencyField(initial=0) final=models.CurrencyField(initial=0) old_participation = models.IntegerField( label="Did you participate in any other online experiment?", widget=widgets.RadioSelect, choices=[[1, "Yes"], [0, "No"]], ) gender = models.IntegerField( label="Your gender", widget=widgets.RadioSelect, choices=[[1, "Male"], [2, "Female"]], ) background = models.IntegerField( label="Your admission track (background)", widget=widgets.RadioSelect, choices=[ [1, "Arts and Letters"], [2, "Economics"], [3, "Science"], [4, "Other"], ], ) risk = models.IntegerField( label="Thinking to yourself, how prepared are you to take risks?", widget=widgets.RadioSelectHorizontal, choices=[ [1, "Not at all"], [2, ""], [3, ""], [4, ""], [5, ""], [6, ""], [7, ""], [8, ""], [9, ""], [10, "Very much"], ], ) compete = models.IntegerField( label="Thinking to yourself, how much do you enjoy to compete in various situations?", widget=widgets.RadioSelectHorizontal, choices=[ [1, "Not at all"], [2, ""], [3, ""], [4, ""], [5, ""], [6, ""], [7, ""], [8, ""], [9, ""], [10, "Very much"], ], ) woman_perf = models.IntegerField( label="Please think to the individual task (Task 1). You would guess that, on average, a woman performance relative to a man performance is:", widget=widgets.RadioSelect, choices=[ [1, "Lower, by less than -15%"], [2, "Lower, between -5% and -15%"], [3, "Between –5% and 5 %"], [4, "Higher, between 5% and 15%"], [5, "Higher, by more than 15%"], ], ) woman_perf_team = models.IntegerField( label="In the pair production setting, women performance relative to men performance is:", widget=widgets.RadioSelect, choices=[ [1, "Lower, by less than -15%"], [2, "Lower, between -5% and -15%"], [3, "Between –5% and 5 %"], [4, "Higher, between 5% and 15%"], [5, "Higher, by more than 15%"], ], ) self_eval_perf_t1 = models.IntegerField( label="Before moving to the next slide, please self-assess your individual performance relativeley to the performance of the other participant.", widget=widgets.RadioSelect, choices=[ [1, "I did better than the other participant."], [2, "The other participant did better than I."], ], ) self_eval_perf_groupe_t1=models.IntegerField( label="Please self-assess your individual performance with respect to the average performance of the group of students who participate to this experiment.", widget=widgets.RadioSelect, choices=[ [1, "Lower, by less than -15%"], [2, "Lower, between -5% and -15%"], [3, "Between –5% and 5 %"], [4, "Higher, between 5% and 15%"], [5, "Higher, by more than 15%"], ], ) gender_pair = models.IntegerField()