from otree.api import * import random doc = """ Game """ class C(BaseConstants): NAME_IN_URL = 'Employee_Game' PLAYERS_PER_GROUP = 2 NUM_ROUNDS = 3 player_roles = ['employee', 'employer'] class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): for group in subsession.get_groups(): players = group.get_players() random.shuffle(players) for i, role in enumerate(C.player_roles): players[i].User_role = role class Group(BaseGroup): pass class Player(BasePlayer): User_role = models.StringField(choices=C.player_roles) correct_answer = models.IntegerField(initial=0) User_input = models.IntegerField() total_correct = models.IntegerField() amount = models.IntegerField() numberInput = models.IntegerField() split_amount = models.IntegerField total_amount =models.IntegerField() class MyWaitPage(WaitPage): def vars_for_template(player: Player): return dict( body_text="Bitte warten Sie." # change the text of the waitpage ) class IntroPart1(Page): def is_displayed(player): return player.round_number == 1 class IntroPart2_Role(Page): def is_displayed(player): return player.round_number == 1 class IntroPart3(Page): def is_displayed(player): return player.round_number == 1 class IntroPart4(Page): def is_displayed(player): return player.round_number == 1 timeout_seconds = 60 timer_text = "Verbleibende Zeit:" class IntroPart5(Page): def is_displayed(player): return player.round_number == 1 class IntroPart6(Page): def is_displayed(player): return player.round_number == 1 timeout_seconds = 60 timer_text = "Verbleibende Zeit:" class IntroPart7(Page): def is_displayed(player): return player.round_number == 1 timeout_seconds = 300 timer_text = "Verbleibende Zeit:" class IntroPart8(Page): def is_displayed(player): return player.round_number == 1 class Instruction(Page): pass class Contract_Instruction(Page): pass class Contract_Instruction2(Page): pass class StartNextRound(Page): @staticmethod def is_displayed(player: Player): return player.round_number >=2 timeout_seconds = 5 timer_text = "Verbleibende Zeit:" def vars_for_template(player: Player): player.User_role = player.participant.vars.get('User_role') class Task_Employer(Page): form_model = 'player' form_fields = ['User_input'] def is_displayed(player): return player.User_role == 'employer' timeout_seconds = 10 timer_text = "Verbleibende Zeit:" def vars_for_template(player: Player): # Check if numbers are already generated and stored in participant.vars if 'numbers' not in player.participant.vars: numbers, solution = Task_Employer.generate_numbers1(Player) player.participant.vars['numbers'] = numbers player.participant.vars['solution'] = solution # for the second set of numhbers numbers2, solution2 = Task_Employer.generate_numbers2(player) player.participant.vars['numbers2'] = numbers2 player.participant.vars['solution2'] = solution2 else: numbers = player.participant.vars['numbers'] solution = player.participant.vars['solution'] numbers2 = player.participant.vars['numbers2'] solution2 = player.participant.vars['solution2'] return { 'Numbers': numbers, 'solution': solution, 'Numbers2': numbers2, 'solution2': solution2, 'Answer': solution + solution2 } def generate_numbers1(player): solution = 0 counts_7 = [0] * 8 LINE_LENGTH = 15 numbers = [] for _ in range(8): counts_7_i = 0 innerlist = [] for _ in range(LINE_LENGTH): val = random.randint(0, 9) innerlist.append(str(val)) if val == 7: counts_7_i += 1 numbers.append(''.join(innerlist)) counts_7.append(counts_7_i) solution += counts_7_i # Remove the first element of counts_7 since it was initialized with zeros counts_7.pop(0) return numbers, solution def generate_numbers2(player): solution2 = 0 counts_7 = [0] * 8 LINE_LENGTH = 15 numbers2 = [] for _ in range(8): counts_7_i = 0 innerlist = [] for _ in range(LINE_LENGTH): val = random.randint(0, 9) innerlist.append(str(val)) if val == 7: counts_7_i += 1 numbers2.append(''.join(innerlist)) counts_7.append(counts_7_i) solution2 += counts_7_i # Remove the first element of counts_7 since it was initialized with zeros counts_7.pop(0) return numbers2, solution2 def before_next_page(player: Player, timeout_happened): # Clear stored values in participant.vars for the next round if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2']: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 1: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 1: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 2: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 2: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 3: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 3: player.correct_answer = 1; if 'numbers' in player.participant.vars: del player.participant.vars['numbers'] if 'solution' in player.participant.vars: del player.participant.vars['solution'] if 'numbers2' in player.participant.vars: del player.participant.vars['numbers2'] if 'solution2' in player.participant.vars: del player.participant.vars['solution2'] player.participant.vars['User_role'] = player.User_role print('role',player.participant.vars['User_role']) class Task_Employee(Page): form_model = 'player' form_fields = ['User_input'] def is_displayed(player): return player.User_role == 'employee' timeout_seconds = 10 timer_text = "Verbleibende Zeit:" def vars_for_template(player: Player): # Check if numbers are already generated and stored in participant.vars if 'numbers' not in player.participant.vars: numbers, solution = Task_Employee.generate_numbers1(Player) player.participant.vars['numbers'] = numbers player.participant.vars['solution'] = solution # for the second set of numhbers numbers2, solution2 = Task_Employee.generate_numbers2(player) player.participant.vars['numbers2'] = numbers2 player.participant.vars['solution2'] = solution2 else: numbers = player.participant.vars['numbers'] solution = player.participant.vars['solution'] numbers2 = player.participant.vars['numbers2'] solution2 = player.participant.vars['solution2'] return { 'Numbers': numbers, 'solution': solution, 'Numbers2': numbers2, 'solution2': solution2, 'Answer': solution + solution2 } def generate_numbers1(player): solution = 0 counts_7 = [0] * 8 LINE_LENGTH = 15 numbers = [] for _ in range(8): counts_7_i = 0 innerlist = [] for _ in range(LINE_LENGTH): val = random.randint(0, 9) innerlist.append(str(val)) if val == 7: counts_7_i += 1 numbers.append(''.join(innerlist)) counts_7.append(counts_7_i) solution += counts_7_i # Remove the first element of counts_7 since it was initialized with zeros counts_7.pop(0) return numbers, solution def generate_numbers2(player): solution2 = 0 counts_7 = [0] * 8 LINE_LENGTH = 15 numbers2 = [] for _ in range(8): counts_7_i = 0 innerlist = [] for _ in range(LINE_LENGTH): val = random.randint(0, 9) innerlist.append(str(val)) if val == 7: counts_7_i += 1 numbers2.append(''.join(innerlist)) counts_7.append(counts_7_i) solution2 += counts_7_i # Remove the first element of counts_7 since it was initialized with zeros counts_7.pop(0) return numbers2, solution2 def before_next_page(player: Player, timeout_happened): # Clear stored values in participant.vars for the next round if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2']: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 1: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 1: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 2: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 2: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] + 3: player.correct_answer = 1; if player.User_input == player.participant.vars['solution'] + player.participant.vars['solution2'] - 3: player.correct_answer = 1; if 'numbers' in player.participant.vars: del player.participant.vars['numbers'] if 'solution' in player.participant.vars: del player.participant.vars['solution'] if 'numbers2' in player.participant.vars: del player.participant.vars['numbers2'] if 'solution2' in player.participant.vars: del player.participant.vars['solution2'] player.participant.vars['User_role'] = player.User_role print('role',player.participant.vars['User_role']) class RoundResult(Page): @staticmethod def is_displayed(player: Player): # Check if the player's User_role is 'employer' return player.User_role == 'employer' @staticmethod def vars_for_template(player: Player): # Get the other player in the same group other_player = player.get_others_in_group()[0] # Check if both players in the group answered correctly in the current round both_players_correct = player.in_round(player.round_number).correct_answer == 1 and \ other_player.in_round(player.round_number).correct_answer == 1 # Return True if both players answered correctly, else False return { "Correct": both_players_correct } class Result(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS and player.User_role == 'employer' def vars_for_template(player: Player): sum_correct = 0 other_players = player.get_others_in_group() # Get other players in the same group for round_number in range(1, C.NUM_ROUNDS + 1): both_players_correct = all(p.in_round(round_number).correct_answer == 1 for p in [player] + other_players) print('test',both_players_correct) if both_players_correct: sum_correct += 1 print('c',sum_correct) player.total_correct = sum_correct player.amount = player.total_correct * 1000 print("total correct", player.total_correct) # Save total_correct and amount to other players in the group for p in other_players: p.total_correct = player.total_correct return { "total": player.total_correct, "amount": player.amount } class Reporting(Page): form_model = 'player' form_fields = ['numberInput' ] @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS and player.User_role == 'employer' @staticmethod def before_next_page(player: Player, timeout_happened): other_players = player.get_others_in_group() # Get other players in the same group if player.numberInput ==1: player.split_amount = 1000//2; player.total_amount = player.amount - player.split_amount if player.numberInput == 2: player.split_amount = 2000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput == 3: player.split_amount = 3000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput == 4: player.split_amount = 4000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput ==5: player.split_amount = 5000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput == 6: player.split_amount = 6000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput ==7: player.split_amount = 7000 // 2; player.total_amount = player.amount - player.split_amount if player.numberInput == 8: player.split_amount = 8000 // 2; player.total_amount = player.amount - player.split_amount # Assign the total_amount to other players in the same group for p in other_players: p.total_amount = player.split_amount class Finish(Page): @staticmethod def is_displayed(player: Player): return player.round_number == C.NUM_ROUNDS page_sequence = [MyWaitPage,IntroPart1, IntroPart2_Role,IntroPart3,IntroPart4,IntroPart5,IntroPart6,IntroPart7,IntroPart8, StartNextRound,Task_Employee,Task_Employer,MyWaitPage,RoundResult,Result,Reporting,MyWaitPage,Finish]