from otree.api import * # from .admin_report_functions import * doc = """ One player decides how to divide a certain amount between himself and the other player. See: Kahneman, Daniel, Jack L. Knetsch, and Richard H. Thaler. "Fairness and the assumptions of economics." Journal of business (1986): S285-S300. """ class C(BaseConstants): NAME_IN_URL = 'dictator_punishment' PLAYERS_PER_GROUP = 3 NUM_ROUNDS = 5 INSTRUCTIONS_TEMPLATE = 'dictator_punishment/instructions.html' # Initial amount allocated to the dictator ENDOWMENT = cu(100) PUNISHMENT_ENDOWMENT = cu(50) class Subsession(BaseSubsession): pass class Group(BaseGroup): kept = models.CurrencyField( doc="""Amount dictator decided to keep for himself""", min=0, max=C.ENDOWMENT, label="I will keep", ) punishment = models.CurrencyField( doc="""Amount the third party decided to use for punishment""", min=0, max=C.PUNISHMENT_ENDOWMENT, label="I will punish the dictator by" ) class Player(BasePlayer): pass # FUNCTIONS def creating_session(subsession): subsession.group_randomly() def custom_export(players): # header row yield ['session', 'participant_code', 'round_number', 'id_in_group', 'payoff', 'participant_label'] for p in players: participant = p.participant session = p.session yield [session.code, participant.code, p.round_number, p.id_in_group, p.payoff, participant.label] def set_payoffs(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p3 = group.get_player_by_id(3) p1.payoff = group.kept - group.punishment p2.payoff = C.ENDOWMENT - group.kept p3.payoff = C.PUNISHMENT_ENDOWMENT - group.punishment p1.participant.vars['earning_1'] = p1.payoff p2.participant.vars['earning_1'] = p2.payoff def get_players(player: Player): participant_name = player.participant return dict(participant_name=participant_name) def vars_for_admin_report(subsession): all_offers = [] all_punishments = [] for subsession in subsession.in_all_rounds(): for group in subsession.get_groups(): kept = group.field_maybe_none('kept') punishment = group.field_maybe_none('punishment') if kept is not None: offer = C.ENDOWMENT - kept all_offers.append(offer) if punishment is not None: punishment = punishment all_punishments.append(punishment) payoffs = sorted([p.payoff for p in subsession.get_players()]) return dict(payoffs=payoffs, all_offers=all_offers, all_punishments=all_punishments) def js_vars(player: Player): group = player.group if group.kept is not None: return dict( taken=group.kept, ) # PAGES class Introduction(Page): pass class Offer(Page): form_model = 'group' form_fields = ['kept'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 class WaitForOffer(WaitPage): @staticmethod def is_displayed(player: Player): return player.id_in_group == 2 class WaitForOffer2(WaitPage): @staticmethod def is_displayed(player: Player): return player.id_in_group == 3 class Punishment(Page): form_model = 'group' form_fields = ['punishment'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 3 @staticmethod def vars_for_template(player: Player): group = player.group if group.kept is not None: return dict(offer=C.ENDOWMENT - group.kept) class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): @staticmethod def vars_for_template(player: Player): group = player.group if group.kept is not None: return dict(offer=C.ENDOWMENT - group.kept, dictator_payoff=group.kept - group.punishment, tp_payoff=C.PUNISHMENT_ENDOWMENT - group.punishment) @staticmethod def js_vars(player: Player): group = player.group if group.kept is not None: return dict( taken=group.kept ) @staticmethod def vars_for_admin_report(subsession): payoffs = sorted([p.payoff for p in subsession.get_players()]) return dict(payoffs=payoffs) page_sequence = [Introduction, Offer, WaitForOffer, WaitForOffer2, Punishment, ResultsWaitPage, Results]