from otree.api import ( models, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Page, WaitPage, ) doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'my_pub_goods' players_per_group = 2 num_rounds = 2 MPCR1 = 0.5 MPCR2 = 0.7 endowment = 100 class Subsession(BaseSubsession): pass class Group(BaseGroup): MPCR = models.FloatField() pass class Player(BasePlayer): contribution = models.IntegerField(min=0, max=Constants.endowment, label="How much will you contribute?") pass # FUNCTIONS def creating_session(subsession): print('in create session') # Establish a total earnings variables for each participant # initiate to 0 at beginning of session. for p in subsession.get_players(): if subsession.round_number == 1: p.participant.vars['totalEarnings'] = 0 # Assign varying mpcr (first half of periods one value and second half of periods another value) for g in subsession.get_groups(): print('round ', subsession.round_number) print('num_rounds/2 ', int(Constants.num_rounds/2)) if subsession.round_number <= int(Constants.num_rounds/2): g.MPCR = Constants.MPCR1 else: g.MPCR = Constants.MPCR2 def setpayoffs(g: Group): # First we need to calculate the total contributed by all group members total_contribution = 0 for p in g.get_players(): total_contribution += p.contribution # Second we need to calculate the earnings for each group member for p in g.get_players(): print('payoff ', p.participant.payoff) print('endowment ', Constants.endowment) print('contribution ', total_contribution) print('MPCR ', g.MPCR) p.participant.payoff = float(Constants.endowment - p.contribution + total_contribution * g.MPCR) p.participant.vars['totalEarnings'] += p.participant.payoff # PAGES class Action(Page): form_model = 'player' form_fields = ['contribution'] pass class ResultsWaitPage(WaitPage): after_all_players_arrive = 'setpayoffs' body_text = "Please wait for everyone to make decisions." pass class Results(Page): pass page_sequence = [Action, ResultsWaitPage, Results]