from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from ._builtin import Page, WaitPage from statistics import mean class Constants(BaseConstants): name_in_url = 'public_goods_simple' players_per_group = 3 num_rounds = 5 endowment = c(10) multiplier = 2 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: self.group_randomly() class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() def set_payoffs(self): self.total_contribution = sum([p.contribution for p in self.get_players()]) self.individual_share = self.total_contribution * 2 / Constants.players_per_group for p in self.get_players(): p.payoff = p.contribution + self.individual_share p.contributions.append(p.contribution) average_contribution = self.total_contribution / Constants.players_per_group class Player(BasePlayer): question1 = models.IntegerField(label="How many rounds are there in this game?") question2 = models.IntegerField(label=" What is the multiplier used in this game?") question3 = models.IntegerField(label="What is an endowment in this game?") contribution = models.CurrencyField(min=0, max=Constants.endowment, label='How much do you want to contribute?') contributions = models.CurrencyField() def average_contribution(self): return sum(self.total_contribution) / 3 class Instructions(Page): pass class Contribute(Page): form_model = 'player' form_fields = ['contribution'] class UnderstandingCheck(Page): form_model = 'player' form_fields = ['question1', 'question2', 'question3'] def error_message(self, values): if values['question1'] != 5 or values['question2'] != 2 or values[ 'question3'] != 10: return 'One or more of your answers is incorrect. Please make sure you understand the instructions before proceeding.' class ResultsWaitPage (WaitPage): pass class Results(Page): pass class Survey(Page): q1 = models.TextField() q2 = models.TextField() q3 = models.CharField(max_length=1) q4 = models.CharField(max_length=3) q5 = models.CharField(max_length=3) page_sequence = [ Instructions, UnderstandingCheck, Contribute, ResultsWaitPage, Results, Survey ]