from random import shuffle from otree.api import * from prisoner import NextGameWaitPage 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 Constants(BaseConstants): name_in_url = 'dictator' players_per_group = 2 num_rounds = 2 instructions_template = 'dictator/instructions.html' # Initial amount allocated to the dictator endowment = cu(100) class Subsession(BaseSubsession): pass class Group(BaseGroup): kept = models.CurrencyField( doc="""Amount dictator decided to keep for himself""", min=0, max=Constants.endowment, label="I will keep", ) given = models.CurrencyField( doc="""Amount dictator decided to give the other player""", min=0, max=Constants.endowment, label="I will give", ) class Player(BasePlayer): pass # FUNCTIONS def creating_session(subsession: Subsession): subsession.group_randomly(fixed_id_in_group=True) if subsession.round_number == 2: mat = subsession.get_group_matrix() for i in range(len(mat)): mat[i] = mat[i][::-1] # reverse the roles subsession.set_group_matrix(mat) def set_payoffs(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p1.payoff = group.kept p2.payoff = Constants.endowment - group.kept # PAGES class Introduction(Page): timeout_seconds = 180 @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Offer(Page): timeout_seconds = 90 form_model = 'group' form_fields = ['kept', 'given'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 @staticmethod def error_message(player: Player, values): if values['kept'] + values['given'] != Constants.endowment: return f'The numbers must add up to {Constants.endowment}' class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs body_text = 'In this round you are a Red person. You have been randomly paired with a Blue person who is now making their decision.' class Results(Page): timeout_seconds = 30 @staticmethod def vars_for_template(player: Player): group = player.group return dict(offer=Constants.endowment - group.kept) # class NextGameWaitPage(WaitPage): # wait_for_all_groups = True # body_text = 'Please wait for other groups to finish before moving to the next round.' # @staticmethod # def is_displayed(player: Player): # return player.round_number == 2 page_sequence = [Introduction, Offer, ResultsWaitPage, Results, NextGameWaitPage]