from otree.api import * c = cu doc = '' class Constants(BaseConstants): name_in_url = 'tutorial_02_public_goods' players_per_group = None num_rounds = 2 endowment = cu(1000) multiplier = 2 class Subsession(BaseSubsession): pass def set_payoffs(group): players = group.get_players() contributions = [p.contribution for p in players] print('contributions:', contributions) group.total_contribution = sum(contributions) group.individual_share = group.total_contribution * Constants.multiplier / Constants.players_per_group print('individual share:', group.individual_share) if group.round_number == 1: print("round 1") else: print(group.round_number) for p in players: if group.round_number == 1: p.payoff = Constants.endowment - p.contribution + group.individual_share print('payoff for round', group.round_number, ":" , p.payoff) else: p.payoff = p.in_round(group.round_number - 1).payoff - p.contribution + group.individual_share print('payoff for round', group.round_number, ":" , p.payoff) class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() set_payoffs = set_payoffs class Player(BasePlayer): contribution = models.CurrencyField(label='How much will you contribute?', max=Constants.endowment, min=0) class Contribute(Page): form_model = 'player' form_fields = ['contribution'] class ResultsWaitPage(WaitPage): after_all_players_arrive = 'set_payoffs' class Results(Page): form_model = 'player' page_sequence = [Contribute, ResultsWaitPage, Results]