from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'paper_scissor_rock' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 5 SYMBOLS = ['Rock','Paper','Scissors'] class Subsession(BaseSubsession): pass class Group(BaseGroup): ## as the draw would be the same for all group members, save it in group is_draw = models.BooleanField() class Player(BasePlayer): hand = models.StringField(choices=C.SYMBOLS) ## Dont need the choices, only storage opponent_hand = models.StringField() ## can also do it with won & boolean field, but for now thats more straight forward result = models.StringField() def calculate_winner(player: Player): ''' How to check for right combo possibility 1: we check every combination ex. player.hand == 'Scissor' and opp.hand =='Paper' ... (all combinations possibility 2: we use change it to a string method in one line ''' ## Get Opponent [opp] = player.get_others_in_group() ## opponent = player.get_others_in_group()[0] is also possible player.opponent_hand = opp.hand if player.hand == opp.hand: player.result = 'Draw' player.group.is_draw = True ## as im lazy, i use poss.2 elif player.hand + opp.hand in 'ScissorPaperRockScissor': ## the order of the string gives combination- as adjacent combos has the first Word win over the second player.result = 'Win' else: player.result = 'Loss' # PAGES class GameRound(Page): form_model = 'player' form_fields = ['hand'] @staticmethod def vars_for_template(player): return dict( past_players= player.in_all_rounds()[:-1] ##this round is no data in there and would diie ) class WaitForOther(WaitPage): ''' Waitpage : we should wait for both group members possible I'm fast and press directly Opponent needs time We want to calculate the winner here, but as opponent didn't choose yet, the program isnt runnable as we are missing data ''' @staticmethod def after_all_players_arrive(group): for player in group.get_players(): calculate_winner(player) class Results(Page): @staticmethod def is_displayed(player): return player.round_number == C.NUM_ROUNDS @staticmethod def vars_for_template(player): return dict( past_players= player.in_all_rounds() ) page_sequence = [GameRound,WaitForOther, Results]