from otree.api import * # ====================================================================================================================== doc = """ Section A (Part I) of the strategy choice and complexity task experiment. This includes a Welcome Page, Instructions, 6 Comprehension questions and a Results page. """ # ====================================================================================================================== # 1. Defining Constants # ====================================================================================================================== class Constants(BaseConstants): name_in_url = 'introduction' players_per_group = None num_rounds = 1 INSTRUCTIONS_TEMPLATE = __name__ + '/instructions.html' PAYOFF_TABLE = __name__ + '/payoff_table.html' d_description = { 3: 'Choose A in the first game. In subsequent games, choose A if in the last game your counterpart played A. Otherwise, choose B if in the last game your counterpart played B.', 4: 'Choose A in the first game. In subsequent games, choose A if in the last game your counterpart played A. Otherwise, choose B if in the last game your counterpart played B.', 5: 'Choose A in the first two games. In subsequent games, choose A if in either of the last two games your counterpart played A. Otherwise, choose B if in both of the last two games your counterpart played B.', 6: 'Choose A in the first two games. In subsequent games, choose A if in either of the last two games your counterpart played A. Otherwise, choose B if in both of the last two games your counterpart played B.', } # Cerate an image dictionary d_image = { 3: 'strategies/instructions/comp_tft.jpg', 4: 'strategies/instructions/comp_tft.jpg', 5: 'strategies/instructions/comp_tf2t.jpg', 6: 'strategies/instructions/comp_tf2t.jpg', } # Create error messages d_error = { 3: 'Incorrect. This is the first game in the set. Please refer to your plan-of-action on what to choose in the first game, and try again.', 4: 'Incorrect. The last game outcome was AB. Please refer to your plan-of-action on what to choose if your counterpart played B last game and try again.', 5: 'Incorrect. This is the first game in the set. Please refer to your plan-of-action on what to choose in the first game, and try again.', 6: 'Incorrect. The last game outcome was AA. Please refer to your plan-of-action on what to choose if your counterpart played A last game and try again.', } class Player(BasePlayer): # Create a counter quiz_counter = models.IntegerField(initial=3) # Create a generic form field correct = models.BooleanField(initial=False) show_q1 = models.BooleanField(initial=False) show_q2 = models.BooleanField(initial=False) # Create variables to store correct_q1 = models.BooleanField(initial=False) correct_q2 = models.BooleanField(initial=False) correct_q3 = models.BooleanField(initial=False) correct_q4 = models.BooleanField(initial=False) correct_q5 = models.BooleanField(initial=False) correct_q6 = models.BooleanField(initial=False) count_mistakes = models.IntegerField(initial=0) # treatment_manu = models.BooleanField(initial=False) self_directed = models.BooleanField(initial=False) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass # ====================================================================================================================== # 3. Defining Pages # ====================================================================================================================== class aBegin(Page): pass class aHello(Page): @staticmethod def before_next_page(player: Player, timeout_happened): player.participant.debug_box = False # Initialise each counter player.participant.count_c = 0 player.participant.implement_counter = 0 player.participant.practice_counter = 0 # SET THE VARIABLES THAT CHANGE THE SESSION TYPE -------------------------------------------------------------- player.participant.manual_first = True # Set manual or automatic player.participant.id_set = 3 # Set the id_set player.treatment_manu = player.participant.manual_first class aInstructions(Page): pass class aIntermediate(Page): pass class aQuizOne(Page): @staticmethod def live_method(player: Player, data): # Check if the first question is correct print('the player id in group is', player.id_in_group) if data['question'] == 'question1': player.correct_q1 = data['answer'] == '5' # Add any mistakes to the mistake counter if not player.correct_q1: player.count_mistakes += 1 # Conditional for showing results in the html page player.show_q1 = True player.show_q2 = False if data['question'] == 'question2': player.correct_q2 = data['answer'] == '0' # Add any mistakes to the mistake counter if not player.correct_q2: player.count_mistakes += 1 # Conditional for showing results in the html page player.show_q1 = False player.show_q2 = True print('Mistakes made total:', player.count_mistakes) # Return return {player.id_in_group: dict(answerQ1=player.correct_q1, showQ1=player.show_q1, answerQ2=player.correct_q2, showQ2=player.show_q2, gameEnd=player.correct_q1 and player.correct_q2)} class aQuizTwo(Page): @staticmethod def vars_for_template(player: Player): # Need to return image, description, return dict(description=Constants.d_description[player.quiz_counter], image=Constants.d_image[player.quiz_counter]) @staticmethod def live_method(player: Player, data): # Check if the first question is correct if player.quiz_counter in [3, 5, 6]: player.correct = data['coop'] elif player.quiz_counter == 4: player.correct = not data['coop'] # Set values to return for player histories history_player = 'A' history_opponent = 'B' if player.correct: if player.quiz_counter == 4: history_player = 'AB' history_opponent = 'BB' if player.quiz_counter == 5: history_opponent = 'A' if player.quiz_counter == 6: history_player = 'AA' history_opponent = 'AA' # Add any mistakes to the mistake counter if not player.correct: player.count_mistakes += 1 print('Mistakes made total:', player.count_mistakes) return{player.id_in_group: dict(answer=player.correct, error_message=Constants.d_error[player.quiz_counter], history_player=history_player, history_opponent=history_opponent)} @staticmethod def before_next_page(player: Player, timeout_happened): # Store the result if player.quiz_counter == 3: player.correct_q3 = player.correct elif player.quiz_counter == 4: player.correct_q4 = player.correct elif player.quiz_counter == 5: player.correct_q5 = player.correct elif player.quiz_counter == 6: player.correct_q6 = player.correct # Move the counter ahead one player.quiz_counter += 1 class aResults(Page): @staticmethod def before_next_page(player: Player, timeout_happened): results = dict(correct_q1=player.correct_q1, correct_q2=player.correct_q2, correct_q3=player.correct_q3, correct_q4=player.correct_q4, correct_q5=player.correct_q5, correct_q6=player.correct_q6, count_mistakes=player.count_mistakes) # Set the mistakes to a participant variable player.participant.questions = player.count_mistakes # Append results onto participant variable player.participant.d_payoff = results class bStart(Page): pass page_sequence = [aBegin, aHello, aInstructions, aIntermediate, aQuizOne, aQuizTwo, aQuizTwo, aQuizTwo, aQuizTwo, aResults, bStart]