from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency ) import random, string, json, time doc = """ a.k.a. Keynesian beauty contest. Players all guess a number; whoever guesses closest to 2/3 of the average wins. See https://en.wikipedia.org/wiki/Guess_2/3_of_the_average """ 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 = 3 seconds_per_period = 60 class Subsession(BaseSubsession): dictionary = models.StringField() def creating_session(self): self.group_randomly(fixed_id_in_group=True) # 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] if self.round_number==1: if performances[0]>performances[1]: p1.payoff = float(p1.performance*fixed_a) p2.payoff = 0 elif performances[1]>performances[0]: p2.payoff = float(p2.performance*fixed_a) p1.payoff = 0 else: p1.payoff = float(p1.performance*fixed_a) p2.payoff = float(p2.performance*fixed_a) if performances[2]>performances[3]: p3.payoff = float(p3.performance*fixed_a) p4.payoff = 0 elif performances[3]>performances[2]: p4.payoff = float(p4.performance*fixed_a) p3.payoff = 0 else: p3.payoff = float(p3.performance*fixed_a) p4.payoff = float(p4.performance*fixed_a) elif ((self.round_number==2) or (self.round_number==3)): if ((performances[0]+performances[1])>(performances[2]+performances[3])): p1.payoff = float(p1.performance*fixed_a) p2.payoff = float(p2.performance*fixed_a) p3.payoff = 0 p4.payoff = 0 elif ((performances[0]+performances[1])<(performances[2]+performances[3])): p1.payoff = 0 p2.payoff = 0 p3.payoff = float(p3.performance*fixed_a) p4.payoff = float(p4.performance*fixed_a) else: p1.payoff = float(p1.performance*fixed_a) p2.payoff = float(p2.performance*fixed_a) p3.payoff = float(p3.performance*fixed_a) p4.payoff = float(p4.performance*fixed_a) 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) 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 Literature"],[2,"Economics"],[3,"Sciences"],[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 more than 15 percents"],[2,"Between 15 and 5 percents lower"],[3,"Between -5 percents and 5 percents"],[4,"Between 5 and 15 percents higher"],[5,"Above 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 more than 15 percents"],[2,"Between 15 and 5 percents lower"],[3,"Between -5 percents and 5 percents"],[4,"Between 5 and 15 percents higher"],[5,"Above 15 percents"]])