from otree.api import ( Page, WaitPage, models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) doc = """ This is a one-period public goods game with 3 players. """ class Constants(BaseConstants): name_in_url = 'Public_goods' players_per_group = 6 num_rounds = 3 instructions_template = 'public_goods/instructions.html' # """Amount allocated to each player""" h_score = (100,90,75) m_score = (95,80,65) l_score = (85,70,60) class Subsession(BaseSubsession): pass class Group(BaseGroup): highest_score_A = models.IntegerField(blank=True) second_highest_score_A = models.IntegerField(blank=True) highest_score_B = models.IntegerField(blank=True) second_highest_score_B = models.IntegerField(blank=True) class Player(BasePlayer): score_amount = models.IntegerField() is_winner = models.BooleanField(initial=False) school_choice = models.StringField(widget=widgets.RadioSelect, choices=[["A","School A"],["B","School B"]]) # FUNCTIONS import random def creating_session (subsession: Subsession): subsession.group_randomly(fixed_id_in_group=True) h_score = (100,90,75) m_score = (95,80,65) l_score = (85,70,60) for p in subsession.get_players(): if p.id_in_group == 1: p.score_amount = random.h_socre elif p.id_in_group == 2: p.score_amount = random.h_socre elif p.id_in_group == 3: p.score_amount = random.Constants.m_score elif p.id_in_group == 4: p.score_amount = random.Constants.m_score elif p.id_in_group == 5: p.score_amount = random.Constants.l_score else: p.score_amount = random.Constants.l_score def set_winner (group: Group): players_A = group.get_player_by_role('school_A') players_B = group.get_player_by_role('school_B') listA = players_A.sort() listB = players_B.sort() group.highest_score_A = listA[-1] group.second_highest_score_A = listA[-2] group.highest_score_B = listB[-1] group.second_highest_score_B = listB[-2] players_with_highest_score_A = [p for p in players_A if p.score_amount == group.highest_score_A] players_with_second_highest_score_A = [p for p in players_A if p.score_amount == group.second_highest_score_A] if group.highest_score_A == group.second_highest_score_A: winner_A = players_with_highest_score_A else: winner_A = [players_with_highest_score_A, random.choice(players_with_second_highest_score_A)] winner_A.is_winner = True for p in players_A: set_payoff(p) players_with_highest_score_B = [p for p in players_B if p.score_amount == group.highest_score_B] players_with_second_highest_score_B = [p for p in players_B if p.score_amount == group.second_highest_score_B] if group.highest_score_B == group.second_highest_score_B: winner_B = players_with_highest_score_B else: winner_B = [players_with_highest_score_B, random.choice(players_with_second_highest_score_B)] winner_B.is_winner = True for p in players_B: set_payoff(p) def set_payoff(player:Player): if player.is_winner_A: player.payoff = 110 elif player.is_winner_B: player.payoff = 90 else: player.payoff = 0 def choice(player:Player): if player.school_choice == 'A': return 'school_A' if player.school_choice == 'B': return 'school_B' def vars_for_admin_report(subsession: Subsession): contributions = [p.contribution for p in subsession.get_players() if p.contribution != None] if contributions: return dict( avg_contribution=sum(contributions) / len(contributions), min_contribution=min(contributions), max_contribution=max(contributions), ) else: return dict( avg_contribution='(no data)', min_contribution='(no data)', max_contribution='(no data)', ) # PAGES class Introduction(Page): """Description of the game: How to play and returns expected""" pass class Contribute(Page): """Player: Choose how much to contribute""" after_all_players_arrive = 'creating session' form_model = 'player' form_fields = ['school_choice'] class ResultsWaitPage(WaitPage): after_all_players_arrive = 'set_payoff' body_text = "Waiting for other participants to contribute." class Results(Page): """Players payoff: How much each has earned""" @staticmethod def vars_for_template(player: Player): group = player.group return dict(total_earnings=group.total_contribution * Constants.multiplier) page_sequence = [Introduction, Contribute, ResultsWaitPage, Results]