from otree.api import * """ Sim for 3x3 'Price Competition' game. Each player is matched with a partner and """ class Constants(BaseConstants): name_in_url = 'priceComp' players_per_group = 2 num_rounds = 5 instructions_template = 'priceComp/instructions.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): choice = models.IntegerField( choices=[ [1, cu(1)], [2, cu(2)], [3, cu(3)], ], widget=widgets.RadioSelect, ) # FUNCTIONS def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff = {1: {1: 6, 2: 15, 3: 15}, 2: {1: 0, 2: 9, 3: 18}, 3: {1: 0, 2: 0, 3: 12}} player.payoff = cu(payoff[player.choice][other_player(player).choice]) # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Main(Page): form_model = 'player' form_fields = ['choice'] class ResultsWaitPage(WaitPage): @staticmethod def after_all_players_arrive(group: Group): for p in group.get_players(): set_payoff(p) class Results(Page): @staticmethod def vars_for_template(player: Player): opponent = other_player(player) return { 'opponent_price': opponent.choice, 'opponent_payoff': opponent.payoff, 'same_price': player.choice == opponent.choice, } class Final(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds @staticmethod def vars_for_template(player: Player): opponent = other_player(player) my_total = sum([p.payoff for p in player.in_all_rounds()]) opponent_total = sum([p.payoff for p in opponent.in_all_rounds()]) return {'my_payoff': my_total, 'opponent_payoff': opponent_total} page_sequence = [Introduction, Main, ResultsWaitPage, Results, Final]