from otree.api import * import time from datetime import datetime, timedelta from csv import reader import pytz class C(BaseConstants): NAME_IN_URL = 'our_experiment_id_newtry' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 #list_empty = [] PAPERCUPS_TEMPLATE = '_templates/global/papercups.html' #ENDOWMENT = 100 #HOT_TREATMENT = 1 requirements = [ dict(name='first', label="我了解若在本頁面的倒計時結束前未按下「報到」按鈕,將失去參與實驗的資格。"), dict(name='second', label="我已確認使用 Google Chrome 或 Microsoft Edge 瀏覽器開啟實驗連結。"), dict(name='third', label="我了解若於實驗中遇到任何問題,可使用實驗畫面右下角按鈕與實驗者聯絡。"), ] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): dropout = models.BooleanField(initial=0) first = models.BooleanField(initial=False, blank=True) second = models.BooleanField(initial=False, blank=True) third = models.BooleanField(initial=False, blank=True) # 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 Preparation(Page): timer_text = '距離實驗開始所剩的時間:' # timer_text = "本場實驗後本頁面會自動跳轉至實驗畫面的第一頁,請耐心等待:" @staticmethod def is_displayed(player): exp_exact_time = player.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 time.time() < ts @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 expired = expired.strftime("%H:%M") return dict( experiment_time_first = expired, #real_subject_id = player.participant.code, ) @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 - time.time() class SubjectId_simplify(Page): form_model = 'player' # form_fields = ['first','second','third'] # form_fields = ['subject_id'] # timeout_submission = {'subject_id': 'fail'} timer_text = '剩餘時間:' @staticmethod def get_form_fields(player: Player): return [req['name'] for req in C.requirements] @staticmethod def error_message(player: Player, values): num_selected = 0 for req in C.requirements: if values[req['name']]: num_selected += 1 if num_selected != 3: return "報到失敗!請先播放音檔並勾選所有方格!" @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 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 = [ Preparation, SubjectId_simplify, ]