from otree.api import * from random import choice, shuffle 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 = 1 instructions_template = 'dictator/instructions.html' # random endowment is put here to make it persistent regardless of reloads random_endowment = cu(choice([10, 40])) class Subsession(BaseSubsession): pass class Group(BaseGroup): kept = models.CurrencyField( doc="""Amount dictator decided to keep for himself""", min=0, label="I will keep", ) class Player(BasePlayer): pass # FUNCTIONS def kept_max(group: Group): dictator = group.get_player_by_id(1).participant if dictator.earning: score = group.get_player_by_id(1).participant.score return cu(40) if score >= 10 else cu(10) else: return Constants.random_endowment def creating_session(subsession: Subsession): matrix = [] player_type = [[[], []], [[], []]] # print(subsession.get_players()) players = subsession.get_players() shuffle(players) for player in players: earning = int(player.participant.earning) dictator = int(player.participant.role == 'dictator') player_type[earning][dictator].append(player) for i in range(2): for j in range(len(player_type[i][0])): # dictator has ID 1 matrix.append([player_type[i][1][j], player_type[i][0][j]]) subsession.set_group_matrix(matrix) 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 = kept_max(group) - group.kept # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): participant = player.participant return (not participant.earning) or (participant.role == 'dictator') @staticmethod def vars_for_template(player: Player): score = None if not player.participant.earning else player.participant.score return dict( score=score, endowment=kept_max(player.group) ) class Offer(Page): form_model = 'group' form_fields = ['kept'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 @staticmethod def vars_for_template(player: Player): return dict( endowment=kept_max(player.group) ) class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): pass page_sequence = [Introduction, Offer, ResultsWaitPage, Results]