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 - 2 people. """ class Constants(BaseConstants): name_in_url = 'SI_exp09' players_per_group = None 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): self.group_randomly() # 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['exp09_paying_players'] = random.sample(range(0,self.session.num_participants,1), 2) def set_payoffs(self): for p in self.get_players(): p.payoff = 0 p1 = self.get_players()[self.session.vars['exp09_paying_players'][0]] p2 = self.get_players()[self.session.vars['exp09_paying_players'][1]] players = [p1,p2] for p in players: if p.choice == 1: p.payoff = Constants.payoff_I_call elif 1 in [p.choice for p in players]: p.payoff = Constants.payoff_others_call else: p.payoff = Constants.payoff_no_call # for player in players: # player.participant.vars['exp09_my_choice'] = player.choice # player.participant.vars['exp09_others_choice'] = [p.choice for p in player.get_others_in_group()] # player.participant.vars['exp09_my_payoff'] = player.payoff # player.participant.vars['exp09_others_name'] = [p.participant.label for p in player.get_others_in_group()], # player.participant.vars['exp09_others_payoff'] = [p.payoff for p in player.get_others_in_group()], # Save payoffs for live payout self.session.vars['exp09_paying_players_payoff'] = [ players[0].payoff, players[1].payoff ] self.session.vars['exp09_paying_players_labels'] = [ players[0].participant.label, players[1].participant.label ] 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['exp09_paying_players_labels'][0], self.session.vars['exp09_paying_players_payoff'][0], self.session.vars['exp09_paying_players_labels'][1], self.session.vars['exp09_paying_players_payoff'][1], ] } 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 )