from otree.api import * import time from datetime import datetime from csv import reader doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'intstruction' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 #list_empty = [] PAPERCUPS_TEMPLATE = '_templates/global/papercups.html' INSTRUCTION_TEMPLATE = 'instruction/templates/instruction.html' TEST_ONE_TEMPLATE = 'instruction/templates/test_one.html' TEST_TWO_TEMPLATE = 'instruction/templates/test_two.html' #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 # def subject_id_error_message(player, value): # with open('_static/experiment/subject_id.csv', 'r') as csv_file: # csv_reader = reader(csv_file) # # Passing the csv_reader object to list() to get a list of lists # list_of_rows = list(csv_reader) # id_list = C.list_empty.copy() # for i in range(0,len(list_of_rows)): # id_list.append(list_of_rows[i][0]) # if value != player.participant.code or value != 'fail': # return '此ID無效,請重新輸入!' class SubjectId(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, real_subject_id = player.participant.code, ) @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 RoleResult(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): template_name = 'instruction/MyWaitPage.html' wait_for_all_groups = True title_text = "Please wait" #body_text = "正在等待所有玩家進入本場實驗,你至多需要等待五分鐘。" @staticmethod def vars_for_template(player): session = player.session # experiment_time = session.config['experiment_time'] return dict( # experiment_time = experiment_time, ) 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 = 1000*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 = 1000*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 Instruction2(Page): form_model = 'player' form_fields = ['test2_ans1', 'count_test2_ans1', 'test2_ans1_choice_record'] @staticmethod def vars_for_template(player): session = player.session hot_treatment = session.config['hot_treatment'] return dict( hot_treatment = hot_treatment, ) timeout_seconds = 1000*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 Instruction3(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 = 1000*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 payoff_realize(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 = 1000*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" @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 participant.oppo_list = [] participant.payoffs_list = [] participant.oppo_payoffs_list = [] participant.nodes_list = [] participant.oppo_nodes_list = [] participant.takes_list = [] participant.oppo_takes_list = [] # participant.uni_id = 0 page_sequence = [ SubjectId, MyWaitPage, # Payoff, # Instruction, # Instruction2, # Instruction3, # payoff_realize, ]