from otree.api import * import random doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'my_public_goods_communication' players_per_group = 4 num_rounds = 1 endowment = 10 MPCR = 0.5 list = [1,2,3,4,5] class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution = models.IntegerField() total_contribution_1 = models.IntegerField() total_contribution_2 = models.IntegerField() total_contribution_3 = models.IntegerField() total_contribution_4 = models.IntegerField() individual_share = models.FloatField() individual_share_1 = models.FloatField() individual_share_2 = models.FloatField() individual_share_3 = models.FloatField() individual_share_4 = models.FloatField() message = models.LongStringField(blank=True, label="Please write down the message you would like to send to other players:") message_1 = models.LongStringField(blank=True, label="Please write down the message you would like to send to other players:") message_2 = models.LongStringField(blank=True, label="Please write down the message you would like to send to other players:") message_3 = models.LongStringField(blank=True, label="Please write down the message you would like to send to other players:") message_4 = models.LongStringField(blank=True, label="Please write down the message you would like to send to other players:") def set_payoffs(group): players = group.get_players() contributions = [p.contribution for p in players] group.total_contribution = sum(contributions) group.individual_share = group.total_contribution * Constants.MPCR for player in players: player.payoff_0 = Constants.endowment - player.contribution + group.individual_share def set_payoffs_1(group): players = group.get_players() contributions_1 = [p.contribution_1 for p in players] group.total_contribution_1 = sum(contributions_1) group.individual_share_1 = group.total_contribution_1 * Constants.MPCR for player in players: player.payoff_1 = Constants.endowment - player.contribution_1 + group.individual_share_1 def set_payoffs_2(group): players = group.get_players() contributions_2 = [p.contribution_2 for p in players] group.total_contribution_2 = sum(contributions_2) group.individual_share_2 = group.total_contribution_2 * Constants.MPCR for player in players: player.payoff_2 = Constants.endowment - player.contribution_2 + group.individual_share_2 def set_payoffs_3(group): players = group.get_players() contributions_3 = [p.contribution_3 for p in players] group.total_contribution_3 = sum(contributions_3) group.individual_share_3 = group.total_contribution_3 * Constants.MPCR for player in players: player.payoff_3 = Constants.endowment - player.contribution_3 + group.individual_share_3 def set_payoffs_4(group): players = group.get_players() contributions_4 = [p.contribution_4 for p in players] group.total_contribution_4 = sum(contributions_4) group.individual_share_4 = group.total_contribution_4 * Constants.MPCR for player in players: player.payoff_4 = Constants.endowment - player.contribution_4 + group.individual_share_4 class Player(BasePlayer): reward_round = models.IntegerField() contribution = models.IntegerField( min=0, max=Constants.endowment, label="How much will you contribute?" ) contribution_1 = models.IntegerField( min=0, max=Constants.endowment, label="How much will you contribute?" ) contribution_2 = models.IntegerField( min=0, max=Constants.endowment, label="How much will you contribute?" ) contribution_3 = models.IntegerField( min=0, max=Constants.endowment, label="How much will you contribute?" ) contribution_4 = models.IntegerField( min=0, max=Constants.endowment, label="How much will you contribute?" ) student_ID = models.IntegerField(label="Please enter your student ID to proceed") payoff_0 = models.FloatField() payoff_1 = models.FloatField() payoff_2 = models.FloatField() payoff_3 = models.FloatField() payoff_4 = models.FloatField() final_payoff = models.FloatField() # PAGES class ID_Entry(Page): form_model = 'player' form_fields = ['student_ID'] class Leader_Assignment(Page): form_model = 'group' form_fields = ['message'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitforP1(WaitPage): pass class Contribute(Page): form_model = 'player' form_fields = ['contribution'] class MyPage(Page): pass class ResultsWaitPage(WaitPage): after_all_players_arrive = 'set_payoffs' class Results(Page): pass class Leader_Assignment_1(Page): form_model = 'group' form_fields = ['message_1'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitforP1_1(WaitPage): pass class Contribute_1(Page): form_model = 'player' form_fields = ['contribution_1'] class MyPage_1(Page): pass class ResultsWaitPage_1(WaitPage): after_all_players_arrive = 'set_payoffs_1' class Results_1(Page): pass class Leader_Assignment_2(Page): form_model = 'group' form_fields = ['message_2'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitforP1_2(WaitPage): pass class Contribute_2(Page): form_model = 'player' form_fields = ['contribution_2'] class MyPage_2(Page): pass class ResultsWaitPage_2(WaitPage): after_all_players_arrive = 'set_payoffs_2' class Results_2(Page): pass class Leader_Assignment_3(Page): form_model = 'group' form_fields = ['message_3'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitforP1_3(WaitPage): pass class Contribute_3(Page): form_model = 'player' form_fields = ['contribution_3'] class MyPage_3(Page): pass class ResultsWaitPage_3(WaitPage): after_all_players_arrive = 'set_payoffs_3' class Results_3(Page): pass class Leader_Assignment_4(Page): form_model = 'group' form_fields = ['message_4'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitforP1_4(WaitPage): pass class Contribute_4(Page): form_model = 'player' form_fields = ['contribution_4'] class MyPage_4(Page): pass class ResultsWaitPage_4(WaitPage): after_all_players_arrive = 'set_payoffs_4' class Results_4(Page): @staticmethod def before_next_page(player: Player, timeout_happened): player.reward_round = random.choice(Constants.list) if player.reward_round == 1: player.final_payoff = player.payoff_0 elif player.reward_round == 2: player.final_payoff = player.payoff_1 elif player.reward_round == 3: player.final_payoff = player.payoff_2 elif player.reward_round == 4: player.final_payoff = player.payoff_3 else: player.final_payoff = player.payoff_4 class Final_payoff(Page): pass page_sequence = [ID_Entry, Leader_Assignment, WaitforP1, Contribute, ResultsWaitPage, Results, Leader_Assignment_1, WaitforP1_1, Contribute_1, ResultsWaitPage_1, Results_1, Leader_Assignment_2, WaitforP1_2, Contribute_2, ResultsWaitPage_2, Results_2, Leader_Assignment_3, WaitforP1_3, Contribute_3, ResultsWaitPage_3, Results_3, Leader_Assignment_4, WaitforP1_4, Contribute_4, ResultsWaitPage_4, Results_4, Final_payoff, ]