from otree.api import * import time from datetime import datetime doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'our_experiment_final_newtry' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 #ENDOWMENT = 100 #HOT_TREATMENT = 1 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): subject_id = models.StringField(label="你的ID:") test1_ans = models.IntegerField(label="你的回答:", required=True) test2_ans1 = models.IntegerField(label="你的回答:") test2_ans2 = models.IntegerField(label="你的回答:") count_test1_ans = models.IntegerField(initial=0) count_test2_ans1 = models.IntegerField(initial=0) count_test2_ans2 = models.IntegerField(initial=0) test1_ans_choice_record = models.StringField() test2_ans1_choice_record = models.StringField() test2_ans2_choice_record = models.StringField() page_number = models.IntegerField(initial=0) dropout = models.BooleanField(initial=0) # PAGES class subject_id(Page): form_model = 'player' form_fields = ['subject_id'] # timeout_submission = {'subject_id': 'fail'} @staticmethod def vars_for_template(player): session = player.session experiment_time = session.config['experiment_time'] return dict( experiment_time = experiment_time, ) @staticmethod def before_next_page(player, timeout_happened): if player.subject_id == "fail": participant = player.participant #participant.subject_id = player.subject_id participant.drop = True player.dropout = participant.drop # @staticmethod # def get_timeout_seconds(self): # experiment_exact_time = self.session.config['experiment_exact_time'] # experiment_start_timestamp = datetime.strptime(experiment_exact_time, '%Y-%m-%d %H:%M:%S').timestamp() # return experiment_start_timestamp + 60 * 5 - time.time() # @staticmethod # def is_displayed(player: Player): # experiment_exact_time = player.session.config['experiment_exact_time'] # experiment_start_timestamp = datetime.strptime(experiment_exact_time, '%Y-%m-%d %H:%M:%S').timestamp() # return experiment_start_timestamp + 60 * 5 > time.time() #def waiting_too_long(player): #participant = player.participant #import time # assumes you set wait_page_arrival in PARTICIPANT_FIELDS. #return time.time() - participant.wait_page_arrival > 60 #def group_by_arrival_time_method(subsession, waiting_players): #all_players = [p for p in waiting_players] #if len(all_players) >= 2: # print('about to create a group') #return [all_players[0], all_players[1]] #for player in waiting_players: #if waiting_too_long(player): # make a single-player group. #return [player] #class wait_page_group(WaitPage): #group_by_arrival_time = True #def before_next_page(player, timeout_happened): #player.participant.id_in_group = player.id_in_group #title_text = "敬請稍待" #body_text = "系統正在為你分組" #class role_result(Page): #@staticmethod #def vars_for_template(player): #number_of_player = len(player.get_others_in_group()) + 1 #return dict(number_of_player = number_of_player) #@staticmethod #def app_after_this_page(player, upcoming_apps): #player_list = [p for p in player.get_others_in_group()] #if len(player_list) == 0: #return 'survey' class MyWaitPage(WaitPage): wait_for_all_groups = True title_text = "敬請稍待" body_text = "正在等待所有玩家進入本場實驗,你至多需要等待五分鐘。" class Payoff(Page): form_model = 'player' form_fields = ['test1_ans', 'count_test1_ans', 'test1_ans_choice_record'] #def is_displayed(player): #return player.participant.filter == True timeout_seconds = 10*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" @staticmethod def before_next_page(player, timeout_happened): player.page_number = 3 participant = player.participant participant.page_number = player.page_number if timeout_happened: participant = player.participant participant.drop = True player.dropout = participant.drop @staticmethod def app_after_this_page(player, upcoming_apps): participant = player.participant if participant.drop == True: session = player.session hot_treatment = session.config['hot_treatment'] if hot_treatment == 1: return 'H_newtry' else: return 'HLC_newtry' class Payoff_ans(Page): pass #def is_displayed(player): #return player.participant.filter == True #pass #@staticmethod #def error_message(player: Player, values): # if player.test1_ans != 10: # return "Game not finished yet" class Instruction(Page): form_model = 'player' form_fields = ['test2_ans1', 'count_test2_ans1', 'test2_ans1_choice_record'] #def is_displayed(player): #return player.participant.filter == True @staticmethod def vars_for_template(player): session = player.session hot_treatment = session.config['hot_treatment'] return dict( hot_treatment = hot_treatment, ) timeout_seconds = 15*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" @staticmethod def before_next_page(player, timeout_happened): player.page_number = 4 participant = player.participant participant.page_number = player.page_number if timeout_happened: participant = player.participant participant.drop = True player.dropout = participant.drop @staticmethod def app_after_this_page(player, upcoming_apps): participant = player.participant if participant.drop == True: session = player.session hot_treatment = session.config['hot_treatment'] if hot_treatment == 1: return 'H_newtry' else: return 'HLC_newtry' class Instruction_ans(Page): #def is_displayed(player): #return player.participant.filter == True pass class Instruction2(Page): form_model = 'player' form_fields = ['test2_ans2', 'count_test2_ans2', 'test2_ans2_choice_record'] #def is_displayed(player): #return player.participant.filter == True @staticmethod def vars_for_template(player): session = player.session hot_treatment = session.config['hot_treatment'] return dict( hot_treatment = hot_treatment, ) timeout_seconds = 10*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" @staticmethod def before_next_page(player, timeout_happened): player.page_number = 5 participant = player.participant participant.page_number = player.page_number if timeout_happened: participant.drop = True player.dropout = participant.drop @staticmethod def app_after_this_page(player, upcoming_apps): participant = player.participant if participant.drop == True: session = player.session hot_treatment = session.config['hot_treatment'] if hot_treatment == 1: return 'H_newtry' else: return 'HLC_newtry' class Instruction_ans2(Page): pass #def is_displayed(player): #return player.participant.filter == True #@staticmethod #def before_next_page(player, timeout_happened): # player.participant.id_in_group = player.id_in_group #class End(Page): #pass def creating_session(subsession: Subsession): for player in subsession.get_players(): participant = player.participant participant.drop = False participant.single = False participant.page_number = 0 page_sequence = [ subject_id, MyWaitPage, Payoff, Instruction, Instruction2, ]