from otree.api import * c = Currency doc = """ This is a one-period public goods game with 3 players. """ class Constants(BaseConstants): name_in_url = 'public_goods' players_per_group = 3 num_rounds = 1 instructions_template = 'public_goods/instructions.html' # """Amount allocated to each player""" endowment = c(100) multiplier = 2 class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() class Player(BasePlayer): contribution = models.CurrencyField( min=0, max=Constants.endowment, doc="""The amount contributed by the player""", label="How much will you contribute to the project (from 0 to 100)?", ) # FUNCTIONS def vars_for_admin_report(subsession: Subsession): contributions = [p.contribution for p in subsession.get_players() if p.contribution != None] if contributions: return dict( avg_contribution=sum(contributions) / len(contributions), min_contribution=min(contributions), max_contribution=max(contributions), ) else: return dict( avg_contribution='(no data)', min_contribution='(no data)', max_contribution='(no data)', ) def set_payoffs(group: Group): group.total_contribution = sum([p.contribution for p in group.get_players()]) group.individual_share = ( group.total_contribution * Constants.multiplier / Constants.players_per_group ) for p in group.get_players(): p.payoff = (Constants.endowment - p.contribution) + group.individual_share # PAGES class Introduction(Page): """Description of the game: How to play and returns expected""" pass class Contribute(Page): """Player: Choose how much to contribute""" form_model = 'player' form_fields = ['contribution'] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs body_text = "Waiting for other participants to contribute." class Results(Page): """Players payoff: How much each has earned""" @staticmethod def vars_for_template(player: Player): group = player.group return dict(total_earnings=group.total_contribution * Constants.multiplier) page_sequence = [Introduction, Contribute, ResultsWaitPage, Results]