from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Asri' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'pgg' players_per_group = 3 num_rounds = 10 efficiency_factor = 2 endowment = c(100) switching_rounds = [1] # add more figures if you like and adjust Intro(Page) in Intro html file lower_bound = 50 upper_bound = 150 class Subsession(BaseSubsession): def creating_session(self): for i in self.get_players(): if self.round_number ==1: if self.session.config['hetero_endowment']: i.endowment = random.randint(Constants.lower_bound, Constants.upper_bound) # generating random endowment or use (next line) else: i.endowment = Constants.endowment else: i.endowment = i.in_round(1).endowment class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() average_contribution = models.CurrencyField() def set_payoffs(self): self.total_contribution = sum([p.contribution for p in self.get_players()]) # p = i self.average_contribution = self.total_contribution / Constants.players_per_group self.individual_share = self.average_contribution * Constants.efficiency_factor # no need for brackets since python works from left to right for p in self.get_players(): p.payoff = p.endowment - p.contribution + self.individual_share class Player(BasePlayer): endowment = models.CurrencyField() contribution = models.CurrencyField(min=0)