from random import random from sqlite3 import complete_statement from otree.api import * from otree.models import subsession doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'monty_hall_t_guide' players_per_group = None num_rounds = 2 num_doors = 3 num_rows = 1 win_payoff = cu(10) commit_payoff = cu(1) lose_payoff = cu(0) class Subsession(BaseSubsession): answer = models.IntegerField() class Group(BaseGroup): pass class Player(BasePlayer): is_winner = models.BooleanField(initial=False) first_choice = models.IntegerField(initial=101) old_choice = models.IntegerField(initial=101) final_choice = models.IntegerField(initial=101) switch = models.BooleanField(initial=False) confirm = models.BooleanField(initial=False) game_over = models.BooleanField(initial=False) open = models.BooleanField(initial=False) second_stage = models.BooleanField(initial=False) first_stage_correct = models.BooleanField(initial=False) door_keep = models.IntegerField(initial=101) commit = models.IntegerField(blank=True, choices=[-1, 0, 1, 2], widget=widgets.RadioSelect) temp_payoff = models.CurrencyField(initial=cu(0)) hollow = models.BooleanField(initial=True) another_door = models.IntegerField(initial=101) unswitchable = models.BooleanField(initial=False) guide_step = models.IntegerField(initial=0) guide_error = models.StringField(initial='') quiz_one = models.IntegerField(blank=True, choices=[1,2,3,4], widget=widgets.RadioSelect) quiz_two = models.IntegerField(blank=True, choices=[1,2,3,4], widget=widgets.RadioSelect) quiz_three = models.IntegerField(blank=True, choices=[1,2,3,4], widget=widgets.RadioSelect) quiz_four = models.IntegerField(blank=True, choices=[1,2,3,4], widget=widgets.RadioSelect) Pass = models.BooleanField(initial=False) count = models.IntegerField(initial=0) def creating_session(subsession: Subsession): # import random # # subsession.answer = random.randint(1, Constants.num_doors) # subsession.answer = 1 pass class Instruction(Page): def is_displayed(player): return player.round_number == 1 class Game(Page): def is_displayed(player): import random another_door = 101 # load commit from participant field participant = player.participant if player.round_number == 1 and player.game_over == False: participant.vars['monty_hall_trans_cmt'] = 0 # player.another_door = another_door player.another_door = 2 player.commit = participant.vars['monty_hall_trans_cmt'] return player.round_number == 1 @staticmethod def js_vars(player: Player): import random return dict(num_doors = Constants.num_doors, num_rows = Constants.num_rows, answer = 1, another_door = player.another_door, commit=player.commit) def live_method(player: Player, data): import random participant = player.participant player.guide_error = '' print('data:', data) if (data and data == 'got_it' and player.guide_step == 0): player.guide_step += 1 player.count += 1 if (data and data == 'got_it' and player.guide_step == 1): player.count += 1 if (player.count > 2): if (player.first_choice != 2): player.guide_error = '請選擇中間的門。' player.old_choice = 2 else: player.guide_step += 1 if (data and data == 'got_it' and player.guide_step == 3): player.guide_step += 1 if (data and data in range(1,101)): if (player.second_stage == False): player.old_choice = player.first_choice player.first_choice = data else: player.old_choice = player.final_choice player.final_choice = data player.confirm = True player.hollow = False if (data and data == 'open'): if (player.first_choice != 2): player.guide_error = '請選擇中間的門並按下"揭露一扇有山羊的門"。' else: player.guide_step += 1 player.open = True # temporarily set final_choice as first_choice, player can switch later player.final_choice = player.first_choice player.old_choice = player.first_choice player.door_keep = 1 if (data and data == 'opened'): player.second_stage = True if (data and data == 'final_choice'): if player.final_choice == 1: player.is_winner = True player.temp_payoff = Constants.win_payoff if player.commit != 0: player.temp_payoff += Constants.commit_payoff player.game_over = True # Record whether the player switched if player.first_choice == player.final_choice: player.switch = False else: player.switch = True print("Id:", player.id_in_group) print("answer:", 1) print("door keep:", player.door_keep) print("old choice:", player.old_choice) print("first choice:", player.first_choice) print("final choice:", player.final_choice) print("old choice:", player.old_choice) print("second stage:", player.second_stage) print("commit:", player.commit) print("another door:", player.another_door) print("unswitchable:", player.unswitchable) print("guide step:", player.guide_step) print("guide error:", player.guide_error) print("----------------") return { player.id_in_group: dict( game_over=player.game_over, first_choice = player.first_choice, final_choice = player.final_choice, old_choice = player.old_choice, open = player.open, second_stage = player.second_stage, first_stage_correct = player.first_stage_correct, door_keep = player.door_keep, # commit = player.commit, commit = participant.monty_hall_trans_cmt, confirm = player.confirm, hollow = player.hollow, unswitchable = player.unswitchable, guide_step = player.guide_step, guide_error = player.guide_error ) } class Commit(Page): form_model = 'player' form_fields = ['commit'] def is_displayed(player): return (player.commit == 0 and player.unswitchable == False) def before_next_page(player: Player, timeout_happened): participant = player.participant participant.monty_hall_trans_cmt = player.commit @staticmethod def error_message(player, values): if values['commit'] == 0: return '請回答你是否選擇鎖定。' def js_vars(player: Player): return dict(switch = player.switch) class Game_auto(Page): def is_displayed(player): import random another_door = 101 # load commit from participant field participant = player.participant if player.round_number == 1 and player.game_over == False: participant.vars['monty_hall_trans_cmt'] = 0 player.another_door = 1 player.commit = participant.vars['monty_hall_trans_cmt'] return player.round_number == 2 @staticmethod def js_vars(player: Player): import random return dict(num_doors = Constants.num_doors, num_rows = Constants.num_rows, answer = 2, another_door = player.another_door) def live_method(player: Player, data): import random participant = player.participant player.guide_error = '' print('data:', data) if (data and data == 'got_it'): player.guide_step += 1 if (data and data in range(1,101)): if (player.second_stage == False): player.old_choice = player.first_choice player.first_choice = data else: player.old_choice = player.final_choice player.final_choice = data player.confirm = True player.hollow = False if (data and data == 'open'): if (player.first_choice != 2): player.guide_error = '請選擇中間的門。.' else: player.guide_step += 1 player.open = True # temporarily set final_choice as first_choice, player can switch later player.final_choice = player.first_choice player.old_choice = player.first_choice if player.first_choice == 2: player.first_stage_correct = True player.door_keep = 3 if player.door_keep != player.another_door: player.unswitchable = True if player.final_choice == 2: player.is_winner = True player.temp_payoff = Constants.win_payoff if player.commit == 1: player.temp_payoff += Constants.commit_payoff else: player.door_keep = 2 if (data and data == 'opened'): player.second_stage = True if (data and data == 'final_choice'): if player.final_choice == 2: player.is_winner = True if player.commit == 0: player.temp_payoff = Constants.win_payoff if player.commit == 1: player.temp_payoff += Constants.commit_payoff player.game_over = True # Record whether the player switched if player.first_choice == player.final_choice: player.switch = False else: player.switch = True print("Id:", player.id_in_group) print("answer:", 2) print("door keep:", player.door_keep) print("old choice:", player.old_choice) print("first choice:", player.first_choice) print("final choice:", player.final_choice) print("old choice:", player.old_choice) print("second stage:", player.second_stage) print("another door:", player.another_door) print("unswitchable:", player.unswitchable) print("guide step:", player.guide_step) print("guide error:", player.guide_error) print("commit:", player.commit) print("----------------") return { player.id_in_group: dict( game_over=player.game_over, first_choice = player.first_choice, final_choice = player.final_choice, old_choice = player.old_choice, open = player.open, second_stage = player.second_stage, first_stage_correct = player.first_stage_correct, door_keep = player.door_keep, # commit = player.commit, commit = participant.monty_hall_trans_cmt, confirm = player.confirm, hollow = player.hollow, unswitchable = player.unswitchable, guide_step = player.guide_step, guide_error = player.guide_error ) } class Results(Page): pass class Quiz(Page): form_model = 'player' form_fields = ['quiz_one', 'quiz_two', 'quiz_three', 'quiz_four'] def is_displayed(player): return player.round_number == 2 @staticmethod def vars_for_template(player): return dict( num_doors = Constants.num_doors ) def error_message(player, values): for q in ['quiz_one', 'quiz_two', 'quiz_three', 'quiz_four']: if values[q] == None: return '請回答所有測驗問題。' def before_next_page(player, timeout_happened): if player.quiz_one == 3 and player.quiz_two == 4 and player.quiz_three == 1 and player.quiz_four == 1: player.Pass = True class Quiz_result(Page): def is_displayed(player): return player.round_number == 2 def before_next_page(player: Player, timeout_happened): participant = player.participant participant.monty_hall_t_pass = player.Pass @staticmethod def app_after_this_page(player, upcoming_apps): if player.Pass == True: return upcoming_apps[0] else: return upcoming_apps[1] page_sequence = [Instruction, Game, Game_auto, Results, Commit, Quiz, Quiz_result] # page_sequence = [Quiz, Quiz_result]