from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'rockpaperscissorsVHB' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 5 SYMBOLS = ['Rock', 'Paper', 'Scissors'] class Subsession(BaseSubsession): pass class Group(BaseGroup): is_draw = models.BooleanField(initial= False) class Player(BasePlayer): hand = models.StringField(choices=C.SYMBOLS, label="Please choose your hand") result= models.StringField() win = models.BooleanField(initial=False) num_wins = models.IntegerField() num_wins_opp = models.IntegerField() ### Method def calculate_group_winner(group: Group): ### player1 = group.get_players()[0] player2 = group.get_players()[1] print(player1.hand, player2.hand) if player1.hand == player2.hand: player1.result = 'Draw' player2.result = 'Draw' group.is_draw = True elif player1.hand + player2.hand in 'ScissorsPaperRockScissors': player1.result = 'Win' player2.result = 'Loss' player1.win = True else: player2.result = 'Win' player2.win = True player1.result = 'Loss' def calculate_group_result(group: Group): ### player1 = group.get_players()[0] player2 = group.get_players()[1] player1.num_wins = sum([p.win for p in player1.in_all_rounds()]) player2.num_wins = sum([p.win for p in player2.in_all_rounds()]) player1.num_wins_opp = player2.num_wins player2.num_wins_opp = player1.num_wins # PAGES class GameRound(Page): form_model = 'player' form_fields = ['hand'] class WaitForOther(WaitPage): @staticmethod def after_all_players_arrive(group): calculate_group_winner(group) class Results(Page): @staticmethod def vars_for_template(player): return dict( outcome= player.result ) class WaitForOtherFinal(WaitPage): @staticmethod def is_displayed(player: Player): return player.round_number==C.NUM_ROUNDS @staticmethod def after_all_players_arrive(group): calculate_group_result(group) class FinalResults(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player): return dict( outcome_self=player.num_wins, outcome_other=player.num_wins_opp, num_draws = C.NUM_ROUNDS - player.num_wins - player.num_wins_opp ) page_sequence = [GameRound, WaitForOther, Results, WaitForOtherFinal, FinalResults]