from otree.api import * import time from datetime import datetime, timedelta from csv import reader import pytz doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'our_experiment_final_newtry' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 #list_empty = [] PAPERCUPS_TEMPLATE = '_templates/global/papercups.html' INSTRUCTION_TEMPLATE = 'instruction_combine_newtry/templates/instruction.html' TEST_ONE_TEMPLATE = 'instruction_combine_newtry/templates/test_one.html' TEST_TWO_TEMPLATE = 'instruction_combine_newtry/templates/test_two.html' #ENDOWMENT = 100 #HOT_TREATMENT = 1 class Subsession(BaseSubsession): count_drop_prior = models.IntegerField(initial=0) latecomer = models.IntegerField(initial=0) class Group(BaseGroup): pass class Player(BasePlayer): subject_id = models.StringField(label="你的ID:", initial="") 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) single_player = 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 exp_exact_time = session.config['experiment_exact_time'] exp_datetime = datetime.strptime(exp_exact_time, '%Y-%m-%d %H:%M:%S') expired = exp_datetime + timedelta(minutes=5) expired = expired.strftime("%H:%M") return dict( experiment_time = expired, real_subject_id = player.participant.code, ) @staticmethod def before_next_page(player, timeout_happened): if timeout_happened: participant = player.participant participant.drop = True player.dropout = participant.drop @staticmethod def get_timeout_seconds(self): exp_exact_time = self.session.config['experiment_exact_time'] exp_datetime = datetime.strptime(exp_exact_time, '%Y-%m-%d %H:%M:%S') # timezone tw = pytz.timezone('Asia/Taipei') tw_dt = tw.localize(exp_datetime) utc_dt = tw_dt.astimezone(pytz.utc) ts = utc_dt.timestamp() return ts + 60 * 5 - time.time() # return 30 @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' #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' # def group_by_arrival_time_method(subsession, waiting_players): # all_players = [p for p in waiting_players] # for p in waiting_players: # if p.dropout == 1: # subsession.count_drop_prior += 1 # if len(all_players) == len(p.session.num_participants) - subsession.count_drop_prior: # return [all_players] # for p in p.group.get_players(): # if p.dropout != 1: # subsession.latecomer = max(p.id_in_group()) # if p.id_in_group() == subsession.latecomer: # participant = p.participant # participant.single = True # p.single_player = participant.single class MyWaitPage(WaitPage): #group_by_arrival_time = True template_name = 'instruction_combine_newtry/MyWaitPage.html' wait_for_all_groups = True title_text = "敬請稍待" #body_text = "正在等待所有玩家進入本場實驗,你至多需要等待五分鐘。" @staticmethod def vars_for_template(player): session = player.session exp_exact_time = session.config['experiment_exact_time'] exp_datetime = datetime.strptime(exp_exact_time, '%Y-%m-%d %H:%M:%S') expired = exp_datetime + timedelta(minutes=5) expired = expired.strftime("%H:%M") return dict( experiment_time = expired, ) 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 = 15*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" and player.single_player == False @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" and player.single_player == False @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 = 15*60 timer_text = '警告!你需要在以下時間內提交本頁面,否則系統將判定已離線,你在本實驗中視同棄權:' @staticmethod def is_displayed(player:Player): return player.subject_id != "fail" and player.single_player == False @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 = [ SubjectId, MyWaitPage, Payoff, Instruction, Instruction2, ]