from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from django.views.decorators.csrf import csrf_exempt from django.utils.decorators import method_decorator import numpy as np import time def vars_for_all_templates(self): return { 'participation_fee': c(self.session.config['participation_fee']), 'round': self.round_number } @method_decorator(csrf_exempt, name='dispatch') class GroupWaitPage(WaitPage): def is_displayed(self): return self.round_number not in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] group_by_arrival_time = True title_text = "Please Wait" body_text = "It may take a few seconds to wait for other people in your group before you can start the game. " \ "The maximum waiting time is 5 minutes." class AuctionInstructions(Page): def is_displayed(self): return self.round_number in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] class ComprehensionAuction(Page): def is_displayed(self): return self.round_number in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] form_model = 'player' form_fields = ['qn_win', 'qn_pay'] def qn_win_error_message(self, value): if self.player.current_play != "aa" and value != 3: self.player.qn_win_got_wrong = True self.player.failed_comprehension_auction = True self.player.participant.vars['correct_num'] -= 1 if self.player.current_play == "aa" and value != 2: self.player.qn_win_got_wrong = True self.player.failed_comprehension_auction = True self.player.participant.vars['correct_num'] -= 1 def qn_pay_error_message(self, value): if self.player.current_play == "fp" and value != 2: self.player.qn_pay_got_wrong = True self.player.failed_comprehension_auction = True self.player.participant.vars['correct_num'] -= 1 if self.player.current_play == "sp" and value != 3: self.player.qn_pay_got_wrong = True self.player.failed_comprehension_auction = True self.player.participant.vars['correct_num'] -= 1 if self.player.current_play == "aa" and value != 4: self.player.qn_pay_got_wrong = True self.player.failed_comprehension_auction = True self.player.participant.vars['correct_num'] -= 1 def before_next_page(self): if self.player.failed_comprehension_auction and self.round_number == 1: self.participant.vars['failed_first_comprehension'] = True self.participant.vars['total_payment'] = c(2) class ComprehensionAuctionCorrect(Page): def is_displayed(self): return self.round_number in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] def js_vars(self): return { 'play': self.player.current_play, } class BeginStudy(Page): def is_displayed(self): return self.round_number in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] def before_next_page(self): self.participant.vars['wait_page_arrival'] = time.time() class Auction(Page): live_method = 'live_aa' def is_displayed(self): return self.round_number not in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and self.player.group_size != 1 and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] form_model = 'player' form_fields = ['bid', 'stop_help'] def js_vars(self): return { 'id': self.player.id_in_group, 'current': self.player.current_play } def get_timeout_seconds(self): if self.player.current_play == "aa": return None else: return 90 timer_text = 'Time Remaining:' def before_next_page(self): self.player.group.current_play = self.player.current_play if self.player.current_play == "aa": self.group.set_payoffs() class ResultWaitPage(WaitPage): def is_displayed(self): return self.group.current_play != "aa" and self.round_number not in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and self.player.group_size != 1 and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] after_all_players_arrive = 'set_payoffs' title_text = "Please Wait" body_text = "It may take a few seconds to wait for other people in your group to submit their bids. " \ "The maximum waiting time is 90 seconds." class Result(Page): def is_displayed(self): return self.round_number not in self.participant.vars['instruction_round'] and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] and not self.participant.vars['skip'] def vars_for_template(self): if self.player.round_payoff > 0: abs_payoff = self.player.round_payoff positive = 1 elif self.player.round_payoff < 0: abs_payoff = -1 * self.player.round_payoff positive = -1 else: abs_payoff = 0 positive = 0 total_payoff = self.player.round_payoff + 20 return { 'abs_payoff': c(abs_payoff), 'total_payoff': c(total_payoff), 'positive': positive } def before_next_page(self): self.participant.vars['wait_page_arrival'] = time.time() timeout_seconds = 45 timer_text = 'Time Remaining on this Page:' class FinishedTasks(Page): def is_displayed(self): return self.round_number == Constants.num_rounds and not self.participant.vars['attention_got_wrong'] and not self.participant.vars['failed_first_comprehension'] def before_next_page(self): if not self.participant.vars['skip']: self.participant.vars['correct_payment'] = c(self.participant.vars['correct_num']*0.2) self.participant.vars['total_payment'] += self.participant.vars['correct_payment'] self.player.total_payment = self.participant.vars['total_payment'] class PayoffandGoodbye(Page): def is_displayed(self): return self.round_number == Constants.num_rounds and not self.participant.vars['attention_got_wrong'] def before_next_page(self): self.player.total_payment = self.participant.vars['total_payment'] self.player.skip = self.participant.vars['skip'] class ToProlific(Page): def is_displayed(self): return self.round_number == Constants.num_rounds page_sequence = [ GroupWaitPage, AuctionInstructions, ComprehensionAuction, ComprehensionAuctionCorrect, BeginStudy, Auction, ResultWaitPage, Result, FinishedTasks, PayoffandGoodbye, ToProlific ]