from random import random from otree.api import * from otree.models import subsession doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'monty_hall_transparent' players_per_group = None num_rounds = 10 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) first_door_timestamp = models.FloatField(initial=0.01) submit_timestamp = models.FloatField(initial=0.01) def creating_session(subsession: Subsession): import random subsession.answer = random.randint(1, Constants.num_doors) class Game(Page): form_model = 'player' form_fields = ['first_door_timestamp','submit_timestamp'] def is_displayed(player): import random another_door = 101 subsession = player.subsession # 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 if player.game_over == False: another_door = random.randint(1, Constants.num_doors) while another_door == subsession.answer: another_door = random.randint(1, Constants.num_doors) player.another_door = another_door player.commit = participant.vars['monty_hall_trans_cmt'] return player @staticmethod def js_vars(player: Player): import random subsession = player.subsession return dict(num_doors = Constants.num_doors, num_rows = Constants.num_rows, answer = subsession.answer, another_door = player.another_door, commit = player.commit) def live_method(player: Player, data): import random subsession = player.subsession participant = player.participant print('data:', data) 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'): 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 == subsession.answer: player.first_stage_correct = True player.door_keep = player.first_choice while player.door_keep == player.first_choice: player.door_keep = random.randint(1, Constants.num_doors) if player.door_keep != player.another_door: player.unswitchable = True if player.final_choice == subsession.answer: player.is_winner = True player.payoff = Constants.win_payoff if player.commit != 0: player.payoff += Constants.commit_payoff else: player.door_keep = subsession.answer if (data and data == 'opened'): player.second_stage = True if (data and data == 'final_choice'): if player.final_choice == subsession.answer: player.is_winner = True player.payoff = Constants.win_payoff if player.commit != 0: player.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:", subsession.answer) 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("gameover:", player.game_over) print("round:", player.round_number) print("firstdoortime:", player.first_door_timestamp) 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 ) } class ResultsWaitPage(WaitPage): pass class Commit(Page): form_model = 'player' form_fields = ['commit'] def is_displayed(player): if player.round_number >= 3: return (player.commit == 0 and player.unswitchable == False) def before_next_page(player: Player, timeout_happened): if player.commit in [1,2]: 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 Results(Page): pass page_sequence = [Game, Results, Commit]