from otree.api import * c = cu doc = '' def make_image_data(image_names): return [dict(name=name, path='shapes/{}'.format(name)) for name in image_names] class C(BaseConstants): NAME_IN_URL = 'HawkDove' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 15 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): subsession.group_randomly() class Group(BaseGroup): pass def set_payoff(group: Group): import random player1 = group.get_player_by_id(1) player2 = group.get_player_by_id(2) random_round = random.randint(1, 15) if group.round_number == random_round: if player1.choice == 'A' and player2.choice == 'A': player1.payoff = 0 player2.payoff = 0 elif player1.choice == 'A' and player2.choice == 'B': player1.payoff = 10 player2.payoff = 5 elif player1.choice == 'B' and player2.choice == 'A': player1.payoff = 5 player2.payoff = 10 else: player1.payoff = 5 player2.payoff = 5 else: if player1.choice == 'A' and player2.choice == 'A': player1.payoff = 0 player2.payoff = 0 elif player1.choice == 'A' and player2.choice == 'B': player1.payoff = 10 player2.payoff = 5 elif player1.choice == 'B' and player2.choice == 'A': player1.payoff = 5 player2.payoff = 10 else: player1.payoff = 5 player2.payoff = 5 for player in group.get_players(): participant = player.participant participant.HD_payoff = player.payoff class Player(BasePlayer): choice = models.StringField(choices=[['A', 'A'], ['B', 'B']], widget=widgets.RadioSelect, label='What do you want to play?') chosen_avatar = models.StringField() class Instructions(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 return True class Avatar(Page): form_model = 'player' form_fields = ['chosen_avatar'] @staticmethod def vars_for_template(player: Player): import random image_names = [ '217.jpg', '728.jpg', '736.jpg', '791.jpg', ] random.shuffle(image_names) return dict(image_data=make_image_data(image_names)) class WaitForAvatarSelection(WaitPage): wait_for_all_groups = True title_text = "Waiting Page" body_text = "We are waiting for the other players’ choice. When the other player has chosen, the system will automatically go forward." class Game(Page): form_model = 'player' form_fields = ['choice'] @staticmethod def vars_for_template(player: Player): group = player.group player1 = player.get_others_in_group()[0] chosen_avatar_player1 = player1.chosen_avatar if player.id_in_group == 2 else "" image_names = [ '217.jpg', '728.jpg', '736.jpg', '791.jpg', ] return { 'chosen_avatar_player1': chosen_avatar_player1, 'image_data': make_image_data(image_names) } class MyWaitPage(WaitPage): after_all_players_arrive = set_payoff title_text = "Waiting Page" body_text = "We are waiting for the other players’ choice. When the other player has chosen, the system will automatically go forward." class Results(Page): form_model = 'player' page_sequence = [Instructions, Avatar, WaitForAvatarSelection, Game, MyWaitPage, Results]