from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'pgg_simple' players_per_group = 3 num_rounds = 4 #number of rounds endowment = c(100) multiplier1 = 1.8 multiplier2 = 2.5 class Subsession(BaseSubsession): def creating_session(self): self.group_randomly() ##stranger matching class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() def set_pggpayoffs(self): players = self.get_players() contributions = [p.contribution for p in players] self.total_contribution = sum(contributions) if self.round_number <= 2: self.individual_share = self.total_contribution * Constants.multiplier1 / Constants.players_per_group else: self.individual_share = self.total_contribution * Constants.multiplier2 / Constants.players_per_group for p in players: p.payoff_pgg = Constants.endowment - p.contribution + self.individual_share def set_payoffs(self): players = self.get_players() for p in players: others = p.get_others_in_group() punishments = [o.punishment for o in others] p.total_punishment = sum(punishments)*2 p.payoff = p.payoff_pgg - p.total_punishment class Player(BasePlayer): contribution = models.CurrencyField(min=0, max=Constants.endowment) punishment = models.BooleanField(choices=[(1, 'Yes'), (0, 'No')]) total_punishment = models.IntegerField() payoff_pgg = models.CurrencyField()