from math import floor from random import random, randint from otree.api import * class C(BaseConstants): NAME_IN_URL = 'public_goods_simple' PLAYERS_PER_GROUP = 3 NUM_ROUNDS = 5 ENDOWMENT = cu(100) MULTIPLIER = 1.8 class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() class Player(BasePlayer): contribution = models.CurrencyField( min=0, max=C.ENDOWMENT, label="How much will you contribute of your 100 points?" ) chocolate_balls = models.IntegerField() good_feedback = models.LongStringField(label="What did you like about this class?") bad_feedback = models.LongStringField(label="What did you NOT like about this class, and what could be done better?") # FUNCTIONS def set_payoffs(group: Group): players = group.get_players() contributions = [p.contribution for p in players] group.total_contribution = sum(contributions) group.individual_share = ( group.total_contribution * C.MULTIPLIER / C.PLAYERS_PER_GROUP ) for p in players: p.payoff = C.ENDOWMENT - p.contribution + group.individual_share p.chocolate_balls = floor(p.payoff/40)-1 # PAGES class Contribute(Page): form_model = 'player' form_fields = ['contribution'] class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): pass class FinalResults(Page): @staticmethod def is_displayed(player): return player.round_number == 5 @staticmethod def vars_for_template(player): a = randint(1, 5) p = player.in_round(a) chocolate_balls = p.chocolate_balls return dict( chocolate_balls = chocolate_balls, a = a ) page_sequence = [Contribute, ResultsWaitPage, Results, FinalResults]