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_mod' 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() 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) first_door_timestamp = models.FloatField() reveal_timestamp = models.FloatField() submit_timestamp = models.FloatField() def creating_session(subsession: Subsession): import random subsession.answer = random.randint(1, Constants.num_doors) class Game(Page): def is_displayed(player): # load commit from participant field participant = player.participant if player.round_number == 1 and player.game_over == False: participant.vars['monty_hall_cmt'] = 0 player.commit = participant.vars['monty_hall_cmt'] return player @staticmethod def js_vars(player: Player): subsession = player.subsession return dict(num_doors = Constants.num_doors, num_rows = Constants.num_rows, answer = subsession.answer, commit = player.commit) def live_method(player: Player, data): import random subsession = player.subsession participant = player.participant print('data:', data) if data: # if ('door' in data.keys()): # if (data and data['door'] in range(1,101)): # if (player.second_stage == False): # player.old_choice = player.first_choice # player.first_choice = data['door'] # else: # player.old_choice = player.final_choice # player.final_choice = data['door'] # player.confirm = True # player.hollow = False if ('status' in data.keys()): if (data and data['status'] == 'open'): player.first_choice = data['door'] 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) else: player.door_keep = subsession.answer if (data and data['status'] == 'opened'): player.second_stage = True if (data and data['status'] == 'final_choice'): player.final_choice = data['door'] 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.first_door_timestamp = data['first_door_timestamp'] player.reveal_timestamp = data['reveal_timestamp'] player.submit_timestamp = data['submit_timestamp'] player.game_over = True # Record whether the player switched if player.first_choice == player.final_choice: player.switch = False else: player.switch = True 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("----------------") return { player.id_in_group: dict( game_over=player.game_over, open = player.open, first_choice = player.final_choice, second_stage = player.second_stage, first_stage_correct = player.first_stage_correct, door_keep = player.door_keep, # commit = player.commit, commit = participant.monty_hall_cmt, ) } class ResultsWaitPage(WaitPage): pass class Commit(Page): form_model = 'player' form_fields = ['commit'] def is_displayed(player): return player.commit == 0 def before_next_page(player: Player, timeout_happened): if player.commit in [1,2]: participant = player.participant participant.monty_hall_cmt = player.commit @staticmethod def error_message(player, values): if values['commit'] == 0: return 'Please answer whether you want to commit.' def js_vars(player: Player): return dict(switch = player.switch) class Results(Page): pass page_sequence = [Game, Results, Commit]