from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ This is a one-period public goods game with 3 players. """ class Constants(BaseConstants): name_in_url = 'public_goods_step_level' num_rounds = 5 bonus_per_person = c(500) step_per_person = c(350) players_per_group = None instructions_template = 'public_goods_step_level/Instructions.html' class Subsession(BaseSubsession): def creating_session(self): group_matrix = [] players = self.get_players() ppg = self.session.config['players_per_group'] for i in range(0, len(players), ppg): group_matrix.append(players[i:i+ppg]) self.set_group_matrix(group_matrix) class Group(BaseGroup): total_contribution = models.CurrencyField() step = models.CurrencyField() def set_payoffs(self): self.total_contribution = sum([p.contribution for p in self.get_players()]) self.step = (self.session.config['players_per_group'] * Constants.step_per_person) if self.total_contribution >= self.step: for p in self.get_players(): p.payoff = (Constants.bonus_per_person - p.contribution) p.bonus = Constants.bonus_per_person p.threshold_reached = True if self.total_contribution < self.step: for p in self.get_players(): p.payoff = (c(0) - p.contribution) p.bonus = c(0) p.threshold_reached = False class Player(BasePlayer): contribution = models.CurrencyField( min=0, doc="""The amount contributed by the player""", ) bonus = models.CurrencyField() threshold_reached = models.BooleanField() secretword = models.StringField()