from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) doc = '' class Constants(BaseConstants): name_in_url = 'PublicGoodGame4players' players_per_group = 4 num_rounds = 2 multiplier = 2 endowment = 14 instructions_template = 'PublicGoodGame4players/instructions.html' class Subsession(BaseSubsession): def creating_session(self): self.group_randomly(fixed_id_in_group = False) class Group(BaseGroup): individual_share = models.CurrencyField() total_contribution = models.CurrencyField() def set_payoff(self): contributions=0 partecipanti=0 for p in self.get_players(): if p.contribution == 99: pass else: partecipanti=partecipanti+1 contributions=contributions+p.contribution if partecipanti > 1: self.total_contribution = contributions * 2 self.individual_share= self.total_contribution/partecipanti if partecipanti == 1: self.total_contribution = 0 self.individual_share = 0 if partecipanti == 0: self.total_contribution = 0 self.individual_share = 0 for p in self.get_players(): if partecipanti > 1: if p.contribution == 99: p.payoff = Constants.endowment else: p.payoff = Constants.endowment - p.contribution + self.individual_share else: p.payoff = Constants.endowment p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p3 = self.get_player_by_id(3) p4 = self.get_player_by_id(4) p1.partecipantipg = partecipanti p2.partecipantipg = partecipanti p3.partecipantipg = partecipanti p4.partecipantipg = partecipanti def stringhe(self): for p in self.get_players(): p.progettocomunepl = None if p.contribution == 99: p.progettocomunepl = "Exit" if p.contribution == 0: p.progettocomunepl = "0 points" if p.contribution == 3: p.progettocomunepl = "3 points" if p.contribution == 6: p.progettocomunepl = "6 points" if p.contribution == 9: p.progettocomunepl = "9 points" class Player(BasePlayer): contribution = models.IntegerField(choices=[[0, '0'], [3, '3'], [6, '6'], [9, '9'], [99, 'Exit']], label='How much do you want to put in the common project?', widget=widgets.RadioSelect) pg = models.IntegerField(choices=[[18, '0 + 3 + 6 = 9 points. 9 x 2 = 18 points'], [9, '0 + 6 + 3 = 9 points']], label='1) What is the value of the common project?', widget=widgets.RadioSelect) miopayoff = models.IntegerField(choices=[[17, '18 : 3 = 6 points. 14 - 3 = 11 points. 11 + 6 = 17 points'], [20, ' 18 : 3 = 6 points. 14 + 6 = 20 points']], label='2) What is your payoff?', widget=widgets.RadioSelect) progettocomunepl = models.StringField() partecipantipg = models.IntegerField() def setglobals(self): self.participant.vars['OCIY_payoff'] = self.participant.payoff return(self.participant.vars)