import random from otree.api import * """ Sim for O'Neill game. """ class Constants(BaseConstants): name_in_url = 'oneill' players_per_group = 2 num_rounds = 5 instructions_template = 'oneill/instructions.html' row_role = 'Row' column_role = 'Column' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): choice = models.StringField( choices=['Joker', 'Ace', 'Two', 'Three'], widget=widgets.RadioSelect, label="Please choose your card.", ) def other_player(player: Player): return player.get_others_in_group()[0] def set_payoff(player: Player): matrix = { 'Joker': {'Joker': 5, 'Ace': -5, 'Two': -5, 'Three': -5}, 'Ace': {'Joker': -5, 'Ace': -5, 'Two': 5, 'Three': 5}, 'Two': {'Joker': -5, 'Ace': 5, 'Two': -5, 'Three': 5}, 'Three': {'Joker': -5, 'Ace': 5, 'Two': 5, 'Three': -5}, } payoff = matrix[player.choice][other_player(player).choice] if player.role == Constants.column_role: payoff = -payoff player.payoff = payoff # 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 { 'player_payoff': int(player.payoff), 'opponent_choice': opponent.choice, 'opponent_payoff': int(opponent.payoff), } class Final(Page): @staticmethod def is_displayed(player: Player): return player.round_number == 5 @staticmethod def vars_for_template(player: Player): return {'my_payoff': int(player.participant.payoff)} page_sequence = [Introduction, Main, ResultsWaitPage, Results, Final]