from otree.api import * c = cu import random as rd doc = '\nThis is a one-shot "Prisoner\'s Dilemma". Two players are asked separately\nwhether they want to cooperate or defect. Their choices directly determine the\npayoffs.\n' class C(BaseConstants): NAME_IN_URL = 'prisoner' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 2 PAYOFF_A = 4 PAYOFF_B = 3 PAYOFF_C = 2 PAYOFF_D = 1 INSTRUCTIONS_TEMPLATE = 'prisoner/instructions.html' payoff_round = rd.randint(1, NUM_ROUNDS) class Subsession(BaseSubsession): pass def creating_session(subsession): print(subsession.get_group_matrix()) subsession.group_randomly() print(subsession.get_group_matrix()) print(subsession.get_players()[1]) class Group(BaseGroup): pass class Player(BasePlayer): cooperate = models.BooleanField(choices=[[True, 'Cooperate'], [False, 'Defect']], doc='This player s decision', widget=widgets.RadioSelect) punish = models.BooleanField(choices=[[True, 'Punish'], [False, 'NoPunish']], doc='This player s decision', widget=widgets.RadioSelect) IBAN = models.StringField(label='Your IBAN') final_payoff = models.CurrencyField() #playersforpayoff = get_players() def cooperate_label(player): choice_label = 'cooperated' if player.cooperate else 'defected' punish_label = 'punished' if player.punish else "didn't punish" return 'Round {}: {} and {}'.format(player.round_number, choice_label, punish_label) def get_partner_choices(player: Player): partner = player.get_others_in_group()[0] rounds = partner.in_rounds(max(1, partner.round_number - 4), partner.round_number - 1) prev_choices = [ cooperate_label(p) for p in rounds ] return prev_choices def other_player(player: Player): return player.get_others_in_group()[0] def set_payoffs(group: Group): for p in group.get_players(): set_payoff(p) def set_payoff(player: Player): payoff_matrix = { (False, True): C.PAYOFF_A, (True, True): C.PAYOFF_B, (False, False): C.PAYOFF_C, (True, False): C.PAYOFF_D, } other = other_player(player) player.payoff = payoff_matrix[(player.cooperate, other.cooperate)] def set_final_payoffs(group: Group): for p in group.get_players(): set_final_payoff(p) def set_final_payoff(player: Player): punished = player.punish or player.get_others_in_group()[0].punish if punished: player.payoff = 0 class Hello(Page): form_model = 'player' class Consent(Page): form_model = 'player' class Introduction(Page): form_model = 'player' timeout_seconds = 100 @staticmethod def is_displayed(player): return player.round_number == 1 class Decision(Page): form_model = 'player' form_fields = ['cooperate'] @staticmethod def vars_for_template(player: Player): choices = get_partner_choices(player) return dict( choices=choices, partner=player.get_others_in_group()[0], current_round=player.round_number ) class Punishment(Page): form_model = 'player' form_fields = ['punish'] class PunishWaitPage(WaitPage): after_all_players_arrive = set_payoffs class ResultsWaitPage(WaitPage): wait_for_all_groups = True @staticmethod def after_all_players_arrive(subsession): for group in subsession.get_groups(): set_final_payoffs(group) class Results(Page): form_model = 'player' @staticmethod def vars_for_template(player: Player): opponent = other_player(player) return dict( opponent=opponent, same_choice=player.cooperate == opponent.cooperate, my_decision=player.field_display('cooperate'), opponent_decision=opponent.field_display('cooperate'), same_punishment=player.punish == opponent.punish, my_punish=player.field_display('punish'), opponent_punish_decision=opponent.field_display('punish') ) class Payment(Page): form_model = 'player' form_fields = ['IBAN'] @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player: Player): player.final_payoff = player.in_round(C.payoff_round).payoff round = C.payoff_round #def vars_for_template(player: Player): # player.payoff = player.in_round(1).payoff # player.payoff = iter(player.payoff) # return dict(player.payoff) page_sequence = [Introduction, Decision, PunishWaitPage, Punishment, ResultsWaitPage, Results, Payment] #hello, consent in app sequence