from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from datetime import datetime def is_displayed(self): return (self.participant.PARTNER_ALIVE == True) def check_timeout(self): if self.timeout_happened: self.participant.PARTNER_ALIVE = False def isnot_displayed(self): return (self.participant.PARTNER_ALIVE == False) class Instructions1(Page): is_displayed = is_displayed timeout_seconds = Constants.time_waiting before_next_page = check_timeout class Instructions2(Page): is_displayed = is_displayed timeout_seconds = Constants.time_waiting before_next_page = check_timeout class Instructions3(Page): is_displayed = is_displayed timeout_seconds = Constants.time_waiting before_next_page = check_timeout class RoleDescriptions(Page): is_displayed = is_displayed timeout_seconds = Constants.time_waiting before_next_page = check_timeout class Wait(WaitPage): is_displayed = is_displayed after_all_players_arrive = 'check_partner_status' class Countdown(Page): timeout_seconds = 5 is_displayed = is_displayed class PuzzleTask(Page): is_displayed = is_displayed form_model = 'player' form_fields = ['puzzle1', 'puzzle2'] timeout_seconds = 420 # use live pages to check answers: # https://otree.readthedocs.io/en/latest/live.html def live_method(player, data): # evaluate the answers, if send data by the client contains a puzzle1_solution key if data.get('puzzle1_solution'): # constants solution_1 = "PLACE" # does it need to be in caps? solution_2 = "3279" EVAL_DEBUG = True answer1 = data['puzzle1_solution'] answer2 = data['puzzle2_solution'] if EVAL_DEBUG: print('received answers from:', player.id_in_group, ':', data) print('answers:', answer1, answer2, 'solutions:', solution_1, solution_2) # check if they are correct correct = False if answer1 == solution_1 and answer2 == solution_2: correct = True # send back to front-end return {player.id_in_group: {"areAnswersCorrect": correct}} # evaluate presence of other player, if send data contains a otherPlayerPresent key if data.get("playerPresent"): # constants TIMEOUT = 5 # in seconds PUZZLE_PAGE_NAME = "PuzzleTask" OTHER_PLAYER_DEBUG = True # save the current date of the player asking about the other player # you can store variables about players in participants.vars, these variables are not stored in database # but they are useful during the experiment player.participant.vars['last_alive'] = datetime.now() other_player_participant = player.get_others_in_group()[ 0].participant # there are only two players in the group # initialize to present, change if conditions bellow are met presence_status = "present" # look how long is other player not on the page if other_player_participant.vars.get('last_alive'): other_player_last_alive = other_player_participant.vars['last_alive'] time_difference = (datetime.now() - other_player_last_alive).seconds if OTHER_PLAYER_DEBUG: print("diff between last presence of other player and curren time is:", time_difference) if time_difference > TIMEOUT: presence_status = "timed-out" #player.participant.PARTNER_ALIVE = False # look if other player left the page other_player_present_page = other_player_participant._current_page_name # if other player is on another page if other_player_present_page != PUZZLE_PAGE_NAME: presence_status = "left the page" if OTHER_PLAYER_DEBUG: print('player :', player.id_in_group, 'asked for other player with result:', presence_status) return {player.id_in_group: {"otherPlayerPresenceStatus": presence_status}} class ResultsWaitPage3(WaitPage): is_displayed = is_displayed after_all_players_arrive = 'set_payoffs' class Results3(Page): is_displayed = is_displayed timeout_seconds = Constants.time_waiting class Survey(Page): is_displayed = is_displayed form_model = 'player' form_fields = ['Question1', 'Question2', 'Question3'] class Survey2(Page): is_displayed = is_displayed form_model = 'player' form_fields = ['OpenEnd'] class Survey3(Page): is_displayed = is_displayed form_model = 'player' form_fields = ['Gender', 'Age', 'Ethnicity', 'Education',"OpenQ3"] class Prolific(Page): is_displayed = is_displayed def before_next_page(self): self.player.participant.finished = True before_next_page = before_next_page class FinalReveal(Page): is_displayed=is_displayed form_model='player' class Failed(Page): is_displayed=isnot_displayed form_model='player' form_fields=['OpenQ3'] class Thanks(Page): is_displayed=isnot_displayed page_sequence = [Instructions1, Instructions2, Instructions3, RoleDescriptions, Wait, Countdown, PuzzleTask, ResultsWaitPage3, Results3, FinalReveal, Survey, Survey2, Survey3, Failed, Thanks, Prolific]