from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Your name here' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'my_public_good_game' players_per_group = 3 num_rounds = 2 efficiency_factor = 2 endowment = c(100) #specifies the currency switching_rounds = [1, ] lb = 50 ub = 150 class Subsession(BaseSubsession): #representations of participants in each round def creating_session(self): #before the session starts it will produce what you will define in the following for i in self.get_players(): if self.round_number == 1: if self.session.config.get('hetero_endowment'): #just a dictionary i.endowment = random.randint(Constants.lb, Constants.ub) else: i.endowment = Constants.endowment else: i.endowment = i.in_round(1).endowment # if it is not round 1, then take the endowment of round 1, so that there are no changes across rounds class Group(BaseGroup): total_contribution = models.CurrencyField() # only fields that will be shown like that, will be shown in the data later individual_share = models.CurrencyField() average_contribution = models.CurrencyField() def set_payoffs(self): self.total_contribution = sum([p.contribution for p in self.get_players()]) #returns the list of the players in the group and sums up their contribution self.average_contribution = self.total_contribution / Constants.players_per_group self.individual_share = self.average_contribution * Constants.efficiency_factor for p in self.get_players(): # we have to define it for every player p.payoff = p.endowment - p.contribution + self.individual_share class Player(BasePlayer): endowment = models.CurrencyField() contribution = models.CurrencyField(min=0)