from otree.api import ( Page, WaitPage, models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) doc = """ This is a two-period public goods game with 3 players. """ class Constants(BaseConstants): MR1 = 1.5 MR2 = 2.4 name_in_url = 'RG_public_goods' players_per_group = 3 num_rounds = 2 instructions_template = 'rusPubG2/instructions.html' # """Amount allocated to each player""" endowment = 100 class Subsession(BaseSubsession): pass class Group(BaseGroup): MR = models.FloatField() class Player(BasePlayer): contribution = models.IntegerField( min=0, max=Constants.endowment, doc="""The amount contributed by the player""", label="How much will you contribute to the public account (from 0 to 100)?", ) # FUNCTIONS def creating_session(subsession): print('in creating session') # establish a total earnings variable for each participant & # initialize to 0 at beginning of session for p in subsession.get_players(): if subsession.round_number == 1: p.participant.vars['totalEarnings'] = 0 # p is set at period 1 and fixed 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.MR = Constants.MR1 else: # second half g.MR = Constants.MR2 # First, calculate the total contribution per group def setPayoffs(g: Group): totalContribution = 0 for p in g.get_players(): totalContribution += p.contribution # second, compute earnings for each group member for p in g.get_players(): print('payoff', p.participant.payoff) print('endowment', Constants.endowment) print('contribution', p.contribution) print('total contribution', totalContribution) p.participant.payoff = float(Constants.endowment - p.contribution + totalContribution*g.MR/Constants.players_per_group) p.participant.vars['totalEarnings'] += p.participant.payoff # PAGES class Action(Page): form_model = 'player' form_fields = ['contribution'] class ResultsWaitPage(WaitPage): after_all_players_arrive = 'setPayoffs' body_text = "Please wait for your groupmates to decide" class Results(Page): pass page_sequence = [Action, ResultsWaitPage, Results]