from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) doc = """ This is a one-period public goods game with 2 players. """ class Constants(BaseConstants): name_in_url = 'public_goods' players_per_group = 2 num_rounds = 1 instructions_template = 'public_goods/Instructions.html' # """Amount allocated to each player""" endowment = c(10) multiplier = 1 class Subsession(BaseSubsession): def vars_for_admin_report(self): contributions = [p.contribution for p in self.get_players() if p.contribution != None] if contributions !=None: return { 'avg_contribution': sum(contributions)/len(contributions), 'min_contribution': min(contributions), 'max_contribution': max(contributions), } else: return { 'avg_contribution': '(no data)', 'min_contribution': '(no data)', 'max_contribution': '(no data)', } class Group(BaseGroup): min_contribution = models.CurrencyField() max_contribution = models.CurrencyField() total_contribution = models.CurrencyField() individual_share = models.CurrencyField() roll_dice= models.IntegerField() set_payoffs = models.CurrencyField() factor = models.FloatField() belief_roll = models.StringField( choices=['Good Luck', 'Bad Luck'], verbose_name='What do you think your partner will have?', widget=widgets.RadioSelect) belief = models.FloatField() extra_payment = models.IntegerField() def set_payoffs(self): contributions = [p.contribution for p in self.get_players() if p.contribution != None] self.min_contribution = min(contributions) self.max_contribution = max(contributions) if self.min_contribution == self.max_contribution: self.total_contribution=sum([p.contribution for p in self.get_players()]) else: self.total_contribution =self.min_contribution*2 self.individual_share = (self.total_contribution * Constants.multiplier)* self.factor / Constants.players_per_group for p in self.get_players(): p.payoff = ((Constants.endowment - self.min_contribution) + self.individual_share) class Player(BasePlayer): contribution = models.CurrencyField( min=0, max=Constants.endowment, doc="""The amount contributed by the player""", ) sent_back= models.CurrencyField()