from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Maxim Ott' doc = """ Experiment for the lecture "Strategische Interaktion" by Prof. Dr. Sandra Ludwig. Call 911 - 6 people. """ class Constants(BaseConstants): name_in_url = 'SI_exp08' players_per_group = None ppg = 6 num_rounds = 1 payoff_no_call = c(0) payoff_I_call = c(5) payoff_others_call = c(10) class Subsession(BaseSubsession): # def creating_session(self): # # # This code defines a cutoff number # # If after pairing people in teams of # # there are more people left over than , but fewer than ppg # # then an extra team is created with them # # Otherwise they are added onto a team # # cutoff = 0 # # list_of_players = random.sample(list(range(1,len(self.get_players())+1)),len(self.get_players())) # # if len(list_of_players) <= cutoff: # self.set_group_matrix([list_of_players]) # else: # leftovers = len(self.get_players()) % 6 # # if leftovers == 0: # new_structure = [[]] # new_structure[0] = list_of_players[0:Constants.ppg] # list_of_players = list_of_players[Constants.ppg:] # while list_of_players != []: # new_structure.append(list_of_players[0:Constants.ppg]) # list_of_players = list_of_players[Constants.ppg:] # print(new_structure) # # elif leftovers > cutoff: # new_structure = [[]] # new_structure[0] = list_of_players[0:leftovers] # list_of_players = list_of_players[leftovers:] # while list_of_players != []: # new_structure.append(list_of_players[0:Constants.ppg]) # list_of_players = list_of_players[Constants.ppg:] # print(new_structure) # # else: # new_structure = [[]] # new_structure[0] = list_of_players[0:Constants.ppg+leftovers] # list_of_players = list_of_players[Constants.ppg+leftovers:] # while list_of_players != []: # new_structure.append(list_of_players[0:Constants.ppg]) # list_of_players = list_of_players[Constants.ppg:] # print(new_structure) # # self.set_group_matrix(new_structure) # # print(self.get_group_matrix()) # # Note: Stupid python list indexing[start=zero, end=exclusive!] # # Only needed if this experiment is done as a stand-alone experiment # if self.round_number == 1: # self.session.vars['exp08_paying_players'] = random.sample(range(0,self.session.num_participants,1), 2) def creating_session(self): if self.round_number == 1: self.session.vars['exp08_paying_players'] = random.sample(range(0,self.session.num_participants,1), 6) def set_payoffs(self): # # 'players_per_group = None' schaltet nur die automatische Gruppierung aus # # Ich habe vorher manuell Gruppen gemacht # # Dieser Code wird ein Mal PRO *ECHTE* GRUPPE ausgeführt. # if len(self.get_players()) < Constants.ppg: # print('p in small group') # # Eine 'zu kleine' Gruppe. Hole 'Phantomspieler' dazu # fake_group = self.get_players() # print(fake_group) # all_players = self.subsession.get_players() # print(all_players) # other_players = [] # for i in range(0,len(all_players)): # if all_players[i].id_in_subsession not in [p.id_in_subsession for p in fake_group]: # other_players.append(all_players[i]) # print(other_players) # # fake_group.extend(other_players[i] for i in random.sample(list(range(0,len(other_players))),Constants.ppg-len(fake_group))) # # print(fake_group) # # this_group = fake_group # else: # # 'Normal' große Gruppe, Standardfall (oder zu große) # this_group = self.get_players() all_players = self.get_players() this_group = [] for i in self.session.vars['exp08_paying_players']: this_group.append(all_players[i]) for p in this_group: if p.choice == 1: p.payoff = Constants.payoff_I_call elif 1 in [p.choice for p in this_group]: p.payoff = Constants.payoff_others_call else: p.payoff = Constants.payoff_no_call # Save payoffs for live payout self.session.vars['exp08_paying_players_payoff'] = [ this_group[0].payoff, this_group[1].payoff, this_group[2].payoff, this_group[3].payoff, this_group[4].payoff, this_group[5].payoff ] self.session.vars['exp08_paying_players_labels'] = [ this_group[0].participant.label, this_group[1].participant.label, this_group[2].participant.label, this_group[3].participant.label, this_group[4].participant.label, this_group[5].participant.label ] # for player in this_group: # player.participant.vars['exp08_my_choice'] = player.choice # player.participant.vars['exp08_others_choice'] = [p.choice for p in this_group] # player.participant.vars['exp08_my_payoff'] = player.payoff # player.participant.vars['exp08_others_payoff'] = [p.payoff for p in this_group], # print('###') # print([p.payoff for p in this_group]) # print([p.id_in_subsession for p in this_group]) # print([p.choice for p in this_group]) # print(player.participant.vars['exp08_others_choice']) # print(player.participant.vars['exp08_others_payoff']) # player.participant.vars['exp08_others_names'] = [p.participant.label for p in this_group], def vars_for_admin_report(self): all_choices_ind = sorted([p.choice for p in self.get_players()]) count_call = all_choices_ind.count(1) count_dontcall = all_choices_ind.count(0) choices_ind = [count_call, count_dontcall] count_groupcall = count_groupnocall = 0 for g in self.get_groups(): if 1 in [p.choice for p in g.get_players()]: count_groupcall = count_groupcall + 1 else: count_groupnocall = count_groupnocall + 1 choices_grp = [count_groupcall, count_groupnocall] return {'choices_ind': choices_ind, 'choices_ind_p': [100*item/sum(choices_ind) for item in choices_ind], 'choices_grp': choices_grp, 'paying_players': [ self.session.vars['exp08_paying_players_labels'][0], self.session.vars['exp08_paying_players_payoff'][0], self.session.vars['exp08_paying_players_labels'][1], self.session.vars['exp08_paying_players_payoff'][1], self.session.vars['exp08_paying_players_labels'][2], self.session.vars['exp08_paying_players_payoff'][2], self.session.vars['exp08_paying_players_labels'][3], self.session.vars['exp08_paying_players_payoff'][3], self.session.vars['exp08_paying_players_labels'][4], self.session.vars['exp08_paying_players_payoff'][4], self.session.vars['exp08_paying_players_labels'][5], self.session.vars['exp08_paying_players_payoff'][5], ] } class Group(BaseGroup): pass class Player(BasePlayer): choice = models.IntegerField( choices=[ [1, 'Ich rufe die Polizei an.'], [0, 'Ich rufe die Polizei nicht an.'], ], label = 'Wie entscheiden Sie sich?', widget=widgets.RadioSelect )