from random import choice from otree.api import * """ Sim for Repeated Normal Form game """ class Constants(BaseConstants): name_in_url = 'normalForm' players_per_group = 2 num_rounds = 100 instructions_template = 'normalForm/instructions.html' history_table_template = 'normalForm/history_table.html' row_role = 'Row' column_role = 'Column' class Subsession(BaseSubsession): end_round = models.BooleanField() class Group(BaseGroup): pass class Player(BasePlayer): choice = models.StringField( widget=widgets.RadioSelect, label='Please make your choice.' ) def choice_choices(player: Player): return ['One', 'Two'] if player.role == Constants.row_role else ['Aay', 'Bee'] def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): payoff = { 'One': {'Aay': [6, 2], 'Bee': [3, 5]}, 'Two': {'Aay': [3, 5], 'Bee': [5, 3]}, } i = 0 if player.role == Constants.row_role else 1 player.payoff = payoff[player.group.get_player_by_role('Row').choice][ player.group.get_player_by_role('Column').choice ][i] def vars_for_template(player: Player, show_current_round): session = player.session round_number = player.round_number other = other_player(player) if round_number == 1 and not show_current_round: players = [] else: if show_current_round: last_round = round_number else: last_round = round_number - 1 if session.config['display_all_history'] or round_number == 1: first_round = 1 else: first_round = round_number - 1 me_all = player.in_rounds(first_round, last_round) other_all = other.in_rounds(first_round, last_round) players = zip(me_all, other_all) return dict(other=other, players=players) # PAGES class Introduction(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 1 class Main(Page): form_model = 'player' form_fields = ['choice'] @staticmethod def vars_for_template(player: Player): return vars_for_template(player, False) 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): return vars_for_template(player, True) page_sequence = [Introduction, Main, ResultsWaitPage, Results]