from otree.api import Currency as c, currency_range from . import models from ._builtin import Page, WaitPage from .models import Constants, Question import random import time class StartPage(Page): def is_displayed(self): if self.round_number == 1: print('This is the start of Ravens tests') return (self.round_number == 1) and (not self.participant.vars.get('finished', 0)) and (not self.session.config['debug']) class Introduction(Page): timeout_seconds = 120 def is_displayed(self): return (self.round_number == 1) and (not self.participant.vars.get('finished', 0)) def before_next_page(self): self.participant.vars['decision_start_time'] = time.time() self.participant.vars['time_left'] = Constants.minutes_given*60 self.participant.vars['time_used'] = 0 self.participant.vars['loaded'] = 0 ## indicate whether images have already loaded def vars_for_template(self): return { 'ravens_payoff_per_question': self.session.config.get('ravens_payoff_per_question',0), 'image_path': Constants.name_in_url+'/example.png' } class QuestionPage(Page): live_method = 'live_method' # form_model = 'player' # form_fields = ['answer'] # timeout_seconds = Constants.minutes_given*60 + 15 def js_vars(self): return dict(num_questions=Constants.num_questions, images=['/static/'+Constants.name_in_url+'/{}.png'.format(i+1) for i in range(Constants.num_questions)]) def is_displayed(self): return (not self.participant.vars.get('finished', 0)) def vars_for_template(self): return { 'ravens_payoff_per_question': self.session.config.get('ravens_payoff_per_question',0), # 'image_paths': [Constants.name_in_url+'/{}.png'.format(i+1) for i in range(Constants.num_questions)], # 'image_path': Constants.name_in_url+'/example.png', } def before_next_page(self): questions = Question.objects.filter(player=self.player) self.player.num_correct = sum([q.ans_correct for q in questions if q.ans_correct]) self.player.decision_time = sum([q.decision_time for q in questions if q.decision_time]) # self.player.ans_correct = self.player.answer == Constants.answer_keys[self.round_number-1] payoff_per_question = self.session.config.get('ravens_payoff_per_question',0) self.player.participant.vars['ravens_payoff'] = self.player.num_correct * payoff_per_question self.player.payoff = self.player.num_correct*payoff_per_question self.player.decision_time = round((time.time() - self.participant.vars['decision_start_time']) * 10) / 10 class Belief(Page): # live_method = 'live_method' form_model = 'player' form_fields = ['belief1','belief2'] def is_displayed(self): return (not self.participant.vars.get('finished', 0)) and (self.round_number == Constants.num_rounds) def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['experiment_ending_time'] = time.time() class Results(Page): # timeout_seconds = 60 def is_displayed(self): return (not self.participant.vars.get('finished', 0)) and (self.round_number == Constants.num_rounds) and (self.session.config.get('ravens_payoff_per_question',0)>0) def vars_for_template(self): return { 'total_correct': sum([p.ans_correct for p in self.player.in_all_rounds()]), 'earnings': sum([p.ans_correct for p in self.player.in_all_rounds()])*self.participant.vars['ravens_payoff_per_question']>0 } def before_next_page(self): # time.time() returns the number of seconds passed since epoch (January 1, 1970, 00:00:00) self.participant.vars['experiment_ending_time'] = time.time() page_sequence = [ Introduction, QuestionPage, Belief, # Results ]