from otree.api import * c = cu doc = '- Choose one round for player\n- Add a chat\n- Randomly reshuffle the group composition' class C(BaseConstants): NAME_IN_URL = 'public_good_game' PLAYERS_PER_GROUP = 3 NUM_ROUNDS = 3 MULTIPLIER = 1.2 ENDOWMENT = cu(1000) class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session # Choose random the round to use to pay if subsession.round_number == 1 : # Will be run 3 times in a row otherwise. import random players = subsession.get_players() for p in players : p.participant.pgg_round_to_pay = random.randint(1, C.NUM_ROUNDS) #Store the round in a participant field (See Group) # Reshuffle : subsession.group_randomly() class Group(BaseGroup): total_contribution = models.CurrencyField() individual_share = models.CurrencyField() 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) #rank = sorted([p.decision for p in players]) #for p in players : #p_rank = rank.index(p.decision)+1 # rank = sorted(players, key = lamdbsa x:x.decision) #Compute the payoff for p in players: p.payoff = C.ENDOWMENT - p.contribution + group.individual_share if p.round_number == C.NUM_ROUNDS : p.participant.pgg_payoff = p.in_round(p.participant.pgg_round_to_pay).payoff class Player(BasePlayer): contribution = models.CurrencyField(label='How much will you contribute?', max=C.ENDOWMENT, min=0) class Introduction(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Contribute(Page): form_model = 'player' form_fields = ['contribution'] timeout_seconds = 60 class WaitResult(WaitPage): after_all_players_arrive = set_payoffs class Results(Page): form_model = 'player' page_sequence = [Introduction, Contribute, WaitResult, Results]