import random from otree.api import * doc = """ This is an information transmission game inspired by... """ class Constants(BaseConstants): name_in_url = 'information_transmission' players_per_group = 2 num_rounds = 8 instructions_template1 = 'information_transmission/Instructions.html' instructions_template2 = 'information_transmission/Instructions2.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): message = models.IntegerField( doc="""Message sent by Player 1""", label="Please enter a message from 1 to 9:", choices=[1, 3, 5, 7, 9], ) state = models.IntegerField() action = models.IntegerField(doc="""Action taken by Player 2""", min=1, max=9) class Player(BasePlayer): pass # FUNCTIONS def set_payoffs(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) if group.round_number < 5: adjustment = 0 else: adjustment = 1.2 p1.payoff = 110 - 10 * (abs(group.state - group.action + adjustment)) ** 1.4 p2.payoff = 110 - 10 * (abs(group.state - group.action)) ** 1.4 # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 or player.round_number == 5 class Send(Page): """This page is only for P1 P1 sends amount (all, some, or none) to P2 This amount is tripled by experimenter, i.e if sent amount by P1 is 5, amount received by P2 is 15""" form_model = 'group' form_fields = ['message'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 @staticmethod def vars_for_template(player: Player): group = player.group rand_number = random.randint(1, 5) group.state = rand_number * 2 - 1 return { "rand_number": rand_number, } class SendBackWaitPage(WaitPage): pass class SendBack(Page): """This page is only for P2 P2 sends back some amount (of the tripled amount received) to P1""" form_model = 'group' form_fields = ['action'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 2 class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): """This page displays the earnings of each player""" page_sequence = [ Introduction, Send, SendBackWaitPage, SendBack, ResultsWaitPage, Results, ]