from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import numpy as np author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'turnaround' players_per_group = 4 num_rounds = 30 # treatment = 4 class Subsession(BaseSubsession): def creating_session(self): players = self.get_players() if self.round_number == 1: for p in players: p.set_payoff_space() paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = paying_round if self.round_number == 11 or self.round_number == 21: self.group_randomly() class Group(BaseGroup): bonus = models.IntegerField() groupEffort = models.IntegerField() misreportPayoff = models.FloatField() group_payoff = models.FloatField(initial=5) 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) 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 tullock(self): players = self.get_players() player_bids = [] for p in players: player_bids.append(p.tickets) p.participant.vars['payoffs'].append(2*(100-p.tickets)) ticket_sum = sum(player_bids) if ticket_sum > 0: p1_prob = player_bids[0]/ticket_sum p2_prob = player_bids[1]/ticket_sum p3_prob = player_bids[2]/ticket_sum p4_prob = player_bids[3]/ticket_sum tullock_winner = np.random.choice([1,2,3,4], p=[p1_prob,p2_prob,p3_prob,p4_prob]) else: tullock_winner = random.choice([1, 2, 3, 4]) for p in players: if p.id_in_group == tullock_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.averageEffort 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' class Player(BasePlayer): def set_payoff_space(self): self.participant.vars['payoffs'] = [] effort = models.IntegerField( choices=[0, 10, 20, 30, 40], doc="""Effort Level""" ) other_effort = models.IntegerField( choices=[0, 10, 20, 30, 40], doc="""Effort Level""" ) tickets = models.IntegerField( widget=widgets.Slider(attrs={'step':1, 'min':0, 'max':100}), doc="""number of tickets""" ) roundPayoff = models.IntegerField() ambPayoff = models.FloatField(initial=0) riskPayoff = models.FloatField(initial=0) current_role = models.StringField(initial='employee') leaderPayoff = models.FloatField(initial=0) quizScore = models.IntegerField(initial=0) averageEffort = models.FloatField(initial=0) vote_choice = models.StringField( choices=['Employee 1', 'Employee 2', 'Employee 3', 'Employee 4'], widget=widgets.RadioSelect, doc="""Group voting choices""" ) def gradeQuiz(self): qnumber = random.randint(1,5) if self.quizQuestion1 == 'Botswana': self.quizScore += 1 if qnumber == 1: self.participant.vars['payoffs'].append(80) else: pass else: pass if self.quizQuestion2 == 'Delaware': self.quizScore += 1 if qnumber == 2: self.participant.vars['payoffs'].append(80) else: pass else: pass if self.quizQuestion3 == 'Dublin': self.quizScore += 1 if qnumber == 3: self.participant.vars['payoffs'].append(80) else: pass else: pass if self.quizQuestion4 == 'Neptune': self.quizScore += 1 if qnumber == 4: self.participant.vars['payoffs'].append(80) else: pass else: pass if self.quizQuestion5 == '2.71': self.quizScore += 1 if qnumber == 5: self.participant.vars['payoffs'].append(80) else: pass else: pass def set_payoffs(self): if self.subsession.round_number == self.session.vars['paying_round']: self.participant.vars['payoffs'].append(self.roundPayoff) else: self.payoff += c(0) def get_final_payoffs(self): payoff = random.choice(self.participant.vars['payoffs']) self.payoff += payoff def getLeaderPayoff(self): if self.current_role == 'manager': taskPayoff = random.randint(1, 5) if taskPayoff == 1: if self.reportChoice1 == 'A': self.leaderPayoff = 32 if self.reportChoice1 == 'B': self.leaderPayoff = 160 elif taskPayoff == 2: if self.reportChoice2 == 'A': self.leaderPayoff = 64 if self.reportChoice2 == 'B': self.leaderPayoff = 160 elif taskPayoff == 3: if self.reportChoice3 == 'A': self.leaderPayoff = 96 if self.reportChoice3 == 'B': self.leaderPayoff = 160 elif taskPayoff == 4: if self.reportChoice4 == 'A': self.leaderPayoff = 128 if self.reportChoice4 == 'B': self.leaderPayoff = 160 elif taskPayoff == 5: if self.reportChoice5 == 'A': self.leaderPayoff = 160 if self.reportChoice5 == 'B': self.leaderPayoff = 160 self.participant.vars['payoffs'].append(self.leaderPayoff) 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 = 160*coinflip else: self.riskPayoff = risknumber*8 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 = 160*ambcoin else: self.ambPayoff = 8*ambnumber 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.StringField( choices=['1.72', '2.71', '1.27', '2.17'], widget=widgets.RadioSelect, ) styleQuestion1 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion2 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion3 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion4 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion5 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion6 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion7 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion8 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion9 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) styleQuestion10 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion1 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion2 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion3 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion4 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion5 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion6 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion7 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion8 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion9 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion10 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion11 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion12 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion13 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion14 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) groupQuestion15 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) teamQuestion1 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) teamQuestion2 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) teamQuestion3 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) teamQuestion4 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) leaderQuestion1 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) leaderQuestion2 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], widget=widgets.RadioSelectHorizontal, ) leaderQuestion3 = models.StringField( choices=['1', '2', '3', '4', '5', 'Prefer not to answer'], 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, )