from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import statistics from random import * import itertools author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'group_creation' players_per_group = None num_rounds = 1 class Group(BaseGroup): pass class Player(BasePlayer): level1 = models.IntegerField(widget=widgets.Slider(show_value=False)) level2 = models.IntegerField(widget=widgets.Slider(show_value=False)) level3 = models.IntegerField(widget=widgets.Slider(show_value=False)) color = models.CharField() situation = models.CharField() # role = models.CharField() class Subsession(BaseSubsession): def making_dictators(self,players): #Shuffles the players, then marks 2/3rds of total participants shuffle(players) #as dictators total_players = len(players) dictators_each_group = int((2/3)*total_players)/2 blue_dictator_count=red_dictator_count=dictators_each_group for player in players: if(player.participant.vars['color']=='blue' and blue_dictator_count>0): player.participant.vars['dictator'] = True blue_dictator_count -= 1 player.situation = "dictator" elif(player.participant.vars['color']=='red' and red_dictator_count>0): player.participant.vars['dictator'] = True player.situation = "dictator" red_dictator_count -= 1 else: player.participant.vars['dictator'] = False player.situation = "receiver" def dictator_sequences(self,players): #Assigns each dictator an order to go through the 5 treatments dictator_sequences = [[1,3,2,4,5],[2,3,1,4,5],[3,1,4,2,5],[4,1,3,2,5],[5,1,3,2,4], [1,4,2,3,5],[2,4,1,3,5],[3,1,5,2,4],[4,1,5,2,3],[5,1,4,2,3], [1,5,2,3,4],[2,5,1,3,4],[3,2,4,1,5],[4,2,3,1,5],[5,2,3,1,4], [1,4,2,5,3],[2,4,1,5,3],[3,2,5,1,4],[4,2,5,1,3],[5,2,4,1,3]] shuffle(dictator_sequences) #Confirmed with Kavya, shuffling needs to happen for player in players: if(player.participant.vars['dictator']==True): player.participant.vars['dictator_sequence'] = dictator_sequences.pop() def print_player_vars(self,players): #Just for the convenience of printing vars to verify functions for player in players: #Can delete it whenever print(player.participant.vars) def group_member_allocation(self,player,total_no_of_players): group_id = player.id_in_group if(group_id>(total_no_of_players//2)): group_id -= total_no_of_players//2 return group_id def grouping_players(self,players): scores = [p.level1 + p.level2 + p.level3 for p in players] # gets players scores sorted_players = [X for _, X in sorted(zip(scores, players), key=lambda pair: pair[0])] R_players = [] B_players = [] # this sorts the players from lowest to highest score for p in sorted_players[:int(len(sorted_players) / 2)]: p.participant.vars['group_member'] = 'Group Member ' + str(self.group_member_allocation(p,len(sorted_players))) p.participant.vars['color'] = 'red' p.color = 'red' R_players.append(p) #print(p, 'variable is', p.participant.vars) for p in sorted_players[int(len(sorted_players) / 2):]: p.participant.vars['group_member'] = 'Group Member ' + str(self.group_member_allocation(p,len(sorted_players))) B_players.append(p) p.participant.vars['color'] = 'blue' p.color = 'blue' #print(p, 'vars is', p.participant.vars) #self.print_player_vars(players) def match(self,players): reciever_ids = [] word1 = ["A","I"] word2 = ["A","I"] #word1 = ["U","N","C","O","P","Y","R","I","G","H","T","A","B","L","E"] #word2 = ["D","E","R","M","A","T","O","G","L","Y","P","H","I","C","S"] player_id = 1 for player in players: player.participant.vars['player_id'] = player_id #Added player id that stays persistent through apps. player_id +=1 # Needed for keeping tracks of dictators and receivers. player.participant.vars['letters'] = [] player.participant.vars['letters'].append(word1[randint(0,len(word1)-1)]) player.participant.vars['letters'].append(word2[randint(0,len(word2)-1)]) blue_receiver_ids = [] red_receiver_ids = [] for player in players: player.participant.vars['group_enhancing_payoff'] = [] player.participant.vars['payoff_array'] = [] if not player.participant.vars["dictator"]: if player.participant.vars["color"] == 'blue': blue_receiver_ids.append(player.participant.vars['player_id']) else: red_receiver_ids.append(player.participant.vars['player_id']) else: player.participant.vars['blue_receivers_list'] = [] player.participant.vars['red_receivers_list'] = [] player.participant.vars['active_treatment'] = -1 for i in range(5): for count,player in enumerate(players): if player.participant.vars['dictator']: player.participant.vars['blue_receivers_list'].append(blue_receiver_ids[count%(len(blue_receiver_ids))]) player.participant.vars['red_receivers_list'].append(red_receiver_ids[count%(len(blue_receiver_ids))]) blue_receiver_ids.append(blue_receiver_ids.pop(0)) red_receiver_ids.append(red_receiver_ids.pop(0))