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. Penalty shoot. """ class Constants(BaseConstants): name_in_url = 'SI_exp07' players_per_group = None num_rounds = 1 payoff_loser = c(0) payoff_shooter = c(10) payoff_goalie = c(15) class Subsession(BaseSubsession): def creating_session(self): # self.group_randomly() import itertools roles = itertools.cycle(['Shooter', 'Goalie']) for p in self.get_players(): p.custom_role = next(roles) print(p.custom_role) # 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: # pass def set_payoffs(self): print("Setting payoffs: Exp7") # Filter by role shooters = [] goalies = [] for p in self.get_players(): if p.custom_role == "Shooter": shooters.append(p) elif p.custom_role == "Goalie": goalies.append(p) else: print("This should never happen") # Select two random players shooter = random.choice(shooters) goalie = random.choice(goalies) if shooter.choice == goalie.choice: shooter.payoff = Constants.payoff_loser goalie.payoff = Constants.payoff_goalie else: shooter.payoff = Constants.payoff_shooter goalie.payoff = Constants.payoff_loser # for player in self.get_players(): # player.participant.vars['exp07_my_choice'] = player.choice # player.participant.vars['exp07_my_payoff'] = player.payoff # player.participant.vars['exp07_my_role'] = 'Torwart' if player.role() == 'Goalie' else 'Schütze', # player.participant.vars['exp07_others_name'] = self.get_player_by_id(player.id_in_group % 2 + 1).participant.label # player.participant.vars['exp07_others_choice'] = self.get_player_by_id(player.id_in_group % 2 + 1).choice # player.participant.vars['exp07_others_role'] = 'Schütze' if player.role() == 'Goalie' else 'Schütze', # player.participant.vars['exp07_others_payoff'] = self.get_player_by_id(player.id_in_group % 2 + 1).payoff, # Save payoffs for live payout self.session.vars['exp07_paying_players_payoff'] = [ shooter.payoff, goalie.payoff ] self.session.vars['exp07_paying_players_labels'] = [ shooter.participant.label, goalie.participant.label ] def vars_for_admin_report(self): # Filter by role shooters = [] goalies = [] for p in self.get_players(): if p.custom_role == "Shooter": shooters.append(p) if p.custom_role == "Goalie": goalies.append(p) else: print("This should never happen") all_choices_shooter_ind = sorted([s.choice for s in shooters]) count_shooter_L = all_choices_shooter_ind.count('Links') count_shooter_M = all_choices_shooter_ind.count('Mitte') count_shooter_R = all_choices_shooter_ind.count('Rechts') choices_shooter_ind = [count_shooter_L, count_shooter_M, count_shooter_R] all_choices_goalie_ind = sorted([g.choice for g in goalies]) count_goalie_L = all_choices_goalie_ind.count('Links') count_goalie_M = all_choices_goalie_ind.count('Mitte') count_goalie_R = all_choices_goalie_ind.count('Rechts') choices_goalie_ind = [count_goalie_L, count_goalie_M, count_goalie_R] # count_LL = count_MM = count_RR = count_GOAL = 0 # for g in self.get_groups(): # if g.get_player_by_role('Shooter').choice == g.get_player_by_role('Goalie').choice == 'Links': # count_LL = count_LL + 1 # elif g.get_player_by_role('Shooter').choice == g.get_player_by_role('Goalie').choice == 'Mitte': # count_MM = count_MM + 1 # elif g.get_player_by_role('Shooter').choice == g.get_player_by_role('Goalie').choice == 'Rechts': # count_RR = count_RR + 1 # else: # count_GOAL = count_GOAL + 1 # # choices_grp = [count_LL, count_MM, count_RR, count_GOAL] return {'choices_shooter_ind': choices_shooter_ind, 'choices_goalie_ind': choices_goalie_ind, 'choices_shooter_ind_p': [100*item/sum(choices_shooter_ind) for item in choices_shooter_ind], 'choices_goalie_ind_p': [100*item/sum(choices_goalie_ind) for item in choices_goalie_ind], 'paying_players': [ self.session.vars['exp07_paying_players_labels'][0], self.session.vars['exp07_paying_players_payoff'][0], self.session.vars['exp07_paying_players_labels'][1], self.session.vars['exp07_paying_players_payoff'][1], ] } class Group(BaseGroup): pass class Player(BasePlayer): choice = models.StringField( choices=['Links', 'Mitte', 'Rechts'], label = '', widget=widgets.RadioSelect ) custom_role = models.StringField() # def role(self): # if self.id_in_group == 1: # return 'Shooter' # if self.id_in_group == 2: # return 'Goalie'