from otree.api import * from random import * doc = """ Public goods with punishment, roughly based on Fehr & Gaechter 2000. """ class C(BaseConstants): NAME_IN_URL = 'punishment_2' PLAYERS_PER_GROUP = 5 NUM_ROUNDS = 5 ENDOWMENT = cu(100) MULTIPLIER = 1.6 #MAX_PUNISHMENT = 150 class Subsession(BaseSubsession): pass class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() class Player(BasePlayer): contribution = models.CurrencyField( min=0, max=100, label="How much will you contribute?" ) punishment_received = models.CurrencyField() payoff_before_punishment = models.CurrencyField() total = models.CurrencyField() bank_account = models.CurrencyField() name = models.StringField( label="Please insert your personal identification name as stated above." ) gender = models.StringField( label="Please state your gender. You can skip this step, if you dont want to." ) age = models.StringField( label="Please state your age." ) def set_payoffs(group: Group): players = group.get_players() contributions = [p.contribution for p in players] group.total_contribution = sum(contributions) group.individual_share = group.total_contribution * C.MULTIPLIER / C.PLAYERS_PER_GROUP for p in players: p.payoff_before_punishment = C.ENDOWMENT - p.contribution + group.individual_share p.payoff = p.payoff_before_punishment p.punishment_received = 0 p.total = 0 p.bank_account = 0 value = (randint(0,100)+(((-90/70)*p.contribution)+90)) if value>100: p.punishment_received = (70 - p.contribution) * 1.06 for p in players: print(p.punishment_received) for p in players: p.total = p.payoff - p.punishment_received pall = p.in_all_rounds() totals = [i.total for i in pall] p.bank_account = sum(totals) # PAGES class Intro(Page): form_model = 'player' form_fields = ['name','gender','age'] @staticmethod def is_displayed(player): return player.round_number == 1 class Contribute(Page): form_model = 'player' form_fields = ['contribution'] class WaitPage1(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): pass class Results_outro(Page): @staticmethod def is_displayed(player): return player.round_number == 5 page_sequence = [ Intro, Contribute, WaitPage1, Results, Results_outro, ]