from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'William Brown' doc = """ This is a search task experiment that uses leadership roles as a coordination mechanism """ class Constants(BaseConstants): name_in_url = 'lemonade_stand' players_per_group = 4 num_rounds = 3 class Subsession(BaseSubsession): def creating_session(self): self.group_randomly() if self.round_number == 1: paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round class Group(BaseGroup): group_payoff = models.FloatField() total_payoff = models.FloatField() misreportPayoff = models.FloatField() total_1 = models.IntegerField(initial=0, min=0, max=Constants.players_per_group) total_2 = models.IntegerField(initial=0, min=0, max=Constants.players_per_group) total_3 = models.IntegerField(initial=0, min=0, max=Constants.players_per_group) total_4 = models.IntegerField(initial=0, min=0, max=Constants.players_per_group) optimum1_1 = models.FloatField() optimum1_2 = models.FloatField() optimum1_3 = models.FloatField() punishment1 = models.FloatField() optimum2_1 = models.FloatField() optimum2_2 = models.FloatField() optimum2_3 = models.FloatField() punishment2 = models.FloatField() optimum3_1 = models.FloatField() optimum3_2 = models.FloatField() optimum3_3 = models.FloatField() punishment3 = models.FloatField() r1_payoff = models.FloatField(initial=0) r2_payoff = models.FloatField(initial=0) r3_payoff = models.FloatField(initial=0) r4_payoff = models.FloatField(initial=0) r5_payoff = models.FloatField(initial=0) subPeriod = models.IntegerField(initial=1) def getRandomOptima(self): locations = [200, 100, 60] random.shuffle(locations) self.optimum1_1 = locations[0] self.optimum1_2 = random.randrange(0, 100, 1)/10 self.optimum1_3 = random.randrange(0, 100, 1)/10 self.optimum2_1 = locations[1] self.optimum2_2 = random.randrange(0, 100, 1)/10 self.optimum2_3 = random.randrange(0, 100, 1)/10 self.optimum3_1 = locations[2] self.optimum3_2 = random.randrange(0, 100, 1)/10 self.optimum3_3 = random.randrange(0, 100, 1)/10 if self.optimum1_1 == 200: self.punishment1 = 60 elif self.optimum1_1 == 100: self.punishment1 = 50 elif self.optimum1_1 == 60: self.punishment1 = 5 if self.optimum2_1 == 200: self.punishment2 = 60 elif self.optimum2_1 == 100: self.punishment2 = 50 elif self.optimum2_1 == 60: self.punishment2 = 5 if self.optimum3_1 == 200: self.punishment3 = 60 elif self.optimum3_1 == 100: self.punishment3 = 50 elif self.optimum3_1 == 60: self.punishment3 = 5 print([self.optimum1_1, self.optimum1_2, self.optimum1_3]) def get_vote_totals(self): players = self.get_players() for p in players: if p.vote_choice == 'Employee 1': self.total_1 += 1 elif p.vote_choice == 'Employee 2': self.total_2 += 1 elif p.vote_choice == 'Employee 3': self.total_3 += 1 elif p.vote_choice == 'Employee 4': self.total_4 += 1 def get_random_leader(self): leader_num = random.randint(1, 4) players = self.get_players() for p in players: if p.id_in_group == leader_num: p.current_role = 'manager' else: p.current_role = 'employee' def get_elected_leader(self): self.get_vote_totals() players = self.get_players() sysRandom = random.SystemRandom() candidates = { 1: self.total_1, 2: self.total_2, 3: self.total_3, 4: self.total_4, } winning_candidate = [] maximum = 0 for name, votes in candidates.items(): if votes == maximum: winning_candidate.append(name) if votes > maximum: winning_candidate = [] winning_candidate.append(name) maximum = votes winner = sysRandom.choice(winning_candidate) for p in players: if p.id_in_group == winner: p.current_role = 'manager' else: p.current_role = 'employee' def get_contest_leader(self): sysRandom = random.SystemRandom() players = self.get_players() player_scores = {} keys = [p.id_in_group-1 for p in players] scores = [p.current_payoff for p in players] for i in keys: player_scores[i] = scores[i] contest_winner = [] maximum = 0 for player, score in player_scores.items(): if score == maximum: contest_winner.append(player) if score > maximum: contest_winner = [] contest_winner.append(player) maximum = score winner = sysRandom.choice(contest_winner) winner = winner + 1 for p in players: if p.id_in_group == winner: p.current_role = 'manager' else: p.current_role = 'employee' def reset(self): players = self.get_players() for p in players: p.current_role = 'employee' p.numberOfClicks = 0 def set_payoffs(self): players = self.get_players() for p in players: p.getLeaderPayoff() if self.subsession.round_number == self.session.vars['paying_round']: p.payoff = p.individual_payoff + p.leaderPayoff + self.group_payoff else: p.payoff = c(0) class Player(BasePlayer): current_payoff = models.FloatField(initial=0) subPeriod = models.IntegerField(initial=1) individual_payoff = models.FloatField() current_role = models.StringField(initial='employee') leaderPayoff = models.FloatField(initial=0) ambPayoff = models.FloatField(initial=0) riskPayoff = models.FloatField(initial=0) quizScore = models.IntegerField(initial=0) maxLocation = models.StringField(initial='None') maxSugar = models.StringField(initial='None') maxLemon = models.StringField(initial='None') treatment = models.IntegerField() r1_payoff = models.FloatField(initial=0) r2_payoff = models.FloatField(initial=0) r3_payoff = models.FloatField(initial=0) r4_payoff = models.FloatField(initial=0) r5_payoff = models.FloatField(initial=0) def getLeaderPayoff(self): if self.current_role == 'manager': taskPayoff = random.randint(1, 5) if taskPayoff == 1: if self.reportChoice1 == 'A': self.leaderPayoff = 0.30 if self.reportChoice1 == 'B': self.leaderPayoff = 1.50 elif taskPayoff == 2: if self.reportChoice2 == 'A': self.leaderPayoff = 0.60 if self.reportChoice2 == 'B': self.leaderPayoff = 1.50 elif taskPayoff == 3: if self.reportChoice3 == 'A': self.leaderPayoff = 0.90 if self.reportChoice3 == 'B': self.leaderPayoff = 1.50 elif taskPayoff == 4: if self.reportChoice4 == 'A': self.leaderPayoff = 1.20 if self.reportChoice4 == 'B': self.leaderPayoff = 1.50 elif taskPayoff == 5: if self.reportChoice5 == 'A': self.leaderPayoff = 1.50 if self.reportChoice5 == 'B': self.leaderPayoff = 1.50 else: pass def getRandomPayoffs(self): risknumber = random.randint(1, 20) ambnumber = random.randint(1, 20) coinflip = random.randint(0, 1) ambprob = random.randrange(0, 100, 1)/100 ambuni = random.uniform(0,1) if ambuni < ambprob: ambcoin = 0 else: ambcoin = 1 riskquestions = { 1: self.riskChoice1, 2: self.riskChoice2, 3: self.riskChoice3, 4: self.riskChoice4, 5: self.riskChoice5, 6: self.riskChoice6, 7: self.riskChoice7, 8: self.riskChoice8, 9: self.riskChoice9, 10: self.riskChoice10, 11: self.riskChoice11, 12: self.riskChoice12, 13: self.riskChoice13, 14: self.riskChoice14, 15: self.riskChoice15, 16: self.riskChoice16, 17: self.riskChoice17, 18: self.riskChoice18, 19: self.riskChoice19, 20: self.riskChoice20, } chosenRisk = riskquestions[risknumber] if chosenRisk == 'A': self.riskPayoff = 50*coinflip else: self.riskPayoff = risknumber*2.5 ambquestions = { 1: self.ambChoice1, 2: self.ambChoice2, 3: self.ambChoice3, 4: self.ambChoice4, 5: self.ambChoice5, 6: self.ambChoice6, 7: self.ambChoice7, 8: self.ambChoice8, 9: self.ambChoice9, 10: self.ambChoice10, 11: self.ambChoice11, 12: self.ambChoice12, 13: self.ambChoice13, 14: self.ambChoice14, 15: self.ambChoice15, 16: self.ambChoice16, 17: self.ambChoice17, 18: self.ambChoice18, 19: self.ambChoice19, 20: self.ambChoice20, } chosenAmb = ambquestions[ambnumber] if chosenAmb == 'A': self.ambPayoff = 50*ambcoin else: self.ambPayoff = 2.5*ambnumber def chat_nickname(self): return 'Player {}'.format(self.id_in_group) def chat_configs(self): configs = [] for other in self.get_others_in_group(): if other.id_in_group < self.id_in_group: lower_id, higher_id = other.id_in_group, self.id_in_group else: lower_id, higher_id = self.id_in_group, other.id_in_group configs.append({ # make a name for the channel that is the same for all # channel members. That's why we order it (lower, higher) 'channel': '{}-{}-{}'.format(self.group.id, lower_id, higher_id), 'label': 'Chat with {}'.format(other.chat_nickname()) }) return configs def get_leader_id(self): for other in self.get_others_in_group(): if other.current_role == 'manager': return other.id_in_group else: pass def leader_chat(self): configs = [] leader_id = self.get_leader_id() if leader_id < self.id_in_group: lower_id, higher_id = leader_id, self.id_in_group else: lower_id, higher_id = self.id_in_group, leader_id configs.append({ # make a name for the channel that is the same for all # channel members. That's why we order it (lower, higher) 'channel': '{}-{}-{}'.format(self.group.id, lower_id, higher_id), 'label': 'Chat with manager' }) return configs def role(self): if self.current_role == 'employee': return 'employee' else: return 'manager' def gradeQuiz(self): qnumber = random.randint(1,5) if self.quizQuestion1 == 'Botswana': self.quizScore += 1 if qnumber == 1: self.payoff += 1000 else: pass else: pass if self.quizQuestion2 == 'Delaware': self.quizScore += 1 if qnumber == 2: self.payoff += 1000 else: pass else: pass if self.quizQuestion3 == 'Dublin': self.quizScore += 1 if qnumber == 3: self.payoff += 1000 else: pass else: pass if self.quizQuestion4 == 'Neptune': self.quizScore += 1 if qnumber == 4: self.payoff += 1000 else: pass else: pass if self.quizQuestion5 == 2.71: self.quizScore += 1 if qnumber == 5: self.payoff += 1000 else: pass else: pass vote_choice = models.StringField( choices = ['Employee 1', 'Employee 2', 'Employee 3', 'Employee 4'], widget=widgets.RadioSelect, doc="""Group voting choices""" ) dimension_1 = models.StringField( choices=['North', 'Central', 'South'], doc="""Location""" ) dimension_2 = models.FloatField( widget=widgets.Slider(attrs={'step':0.1, 'min':0, 'max':10}), doc="""Sugar Content""" ) dimension_3 = models.FloatField( widget=widgets.Slider(attrs={'step':0.1,'min':0,'max':10}), doc="""Lemon Content""" ) value = models.FloatField( doc="""Value""" ) numberOfClicks = models.IntegerField(initial=0) quizQuestion1 = models.StringField( choices=['Sudan', 'Gabon', 'Botswana', 'Mauritania'], widget=widgets.RadioSelect, ) quizQuestion2 = models.StringField( choices=['New Jersey', 'Georgia', 'Pennsylvania', 'Delaware'], widget=widgets.RadioSelect, ) quizQuestion3 = models.StringField( choices=['Dublin', 'Blackburn', 'Belfast', 'Cobb'], widget=widgets.RadioSelect, ) quizQuestion4 = models.StringField( choices=['Jupiter', 'Mars', 'Saturn', 'Neptune'], widget=widgets.RadioSelect, ) quizQuestion5 = models.FloatField( choices=[1.72, 2.71, 1.27, 2.17], widget=widgets.RadioSelect, ) styleQuestion1 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion2 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion3 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion4 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion5 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion6 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion7 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion8 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion9 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) styleQuestion10 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion1 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion2 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion3 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion4 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion5 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion6 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion7 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion8 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion9 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion10 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion11 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion12 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion13 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion14 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) groupQuestion15 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) teamQuestion1 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) teamQuestion2 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) teamQuestion3 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) teamQuestion4 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) leaderQuestion1 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) leaderQuestion2 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) leaderQuestion3 = models.IntegerField( choices=[1, 2, 3, 4, 5], widget=widgets.RadioSelectHorizontal, ) riskChoice1 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice2 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice3 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice4 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice5 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice6 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice7 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice8 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice9 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice10 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice11 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice12 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice13 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice14 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice15 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice16 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice17 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice18 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice19 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) riskChoice20 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice1 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice2 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice3 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice4 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice5 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice6 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice7 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice8 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice9 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice10 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice11 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice12 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice13 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice14 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice15 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice16 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice17 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice18 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice19 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) ambChoice20 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) reportChoice1 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) reportChoice2 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) reportChoice3 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) reportChoice4 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, ) reportChoice5 = models.StringField( choices=['A', 'B'], widget=widgets.RadioSelectHorizontal, )