from ._builtin import Page, WaitPage from otree.api import Currency as c, currency_range from .models import Constants import random import time class Introduction(Page): def is_displayed(self): return self.round_number == 1 class Decision(Page): def is_displayed(self): return self.round_number <= 3 form_model = 'player' form_fields = ['decision'] #class ResultsWaitPage(WaitPage): # def after_all_players_arrive(self): # for p in self.group.get_players(): # p.set_payoff() class Results(Page): def is_displayed(self): return self.round_number <= 3 def vars_for_template(self): me = self.player me.set_payoff() if me.decision == 'Cooperate' and me.computer_decision == 'Cooperate': return { 'my_decision': 'go for the tree-fruit', 'opponent_decision': 'go for the tree-fruit', 'same_choice': me.decision == me.computer_decision, 'player_in_all_rounds': self.player.in_all_rounds(), 'total_payoff': self.participant.payoff, } if me.decision == 'Cooperate' and me.computer_decision == 'Defect': return { 'my_decision': 'go for the tree-fruit', 'opponent_decision': 'go for the berries', 'same_choice': me.decision == me.computer_decision, 'player_in_all_rounds': self.player.in_all_rounds(), 'total_payoff': self.participant.payoff, } if me.decision == 'Defect' and me.computer_decision == 'Cooperate': return { 'my_decision': 'go for the berries', 'opponent_decision': 'go for the tree-fruit', 'same_choice': me.decision == me.computer_decision, 'player_in_all_rounds': self.player.in_all_rounds(), 'total_payoff': self.participant.payoff, } if me.decision == 'Defect' and me.computer_decision == 'Defect': return { 'my_decision': 'go for the berries', 'opponent_decision': 'go for the berries', 'same_choice': me.decision == me.computer_decision, 'player_in_all_rounds': self.player.in_all_rounds(), 'total_payoff': self.participant.payoff, } class End(Page): def is_displayed(self): return self.round_number == 3 def vars_for_template(self): if self.round_number == 3: self.participant.payoff = c(0) return { 'experiment_payoff': self.participant.payoff, } else: pass class SkillTest(Page): def is_displayed(self): return self.round_number == 3 def vars_for_template(self): self.participant.vars['error_var1'] = True self.participant.vars['error_var2'] = True self.participant.vars['error_var3'] = True self.participant.vars['attempts_1'] = 0 self.participant.vars['attempts_2'] = 0 self.participant.vars['attempts_3'] = 0 self.participant.vars['failed'] = False self.participant.vars['see_results'] = False class SkillTest1(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is True and self.participant.vars['attempts_1'] < 3 form_model = 'player' form_fields = ['skill_answer1'] def before_next_page(self): #if skill answer is wrong (Player 2), error_var1 = True me = self.player if me.skill_answer1 == 'Player 2': self.participant.vars['error_var1'] = True if me.skill_answer1 == 'Player 1': self.participant.vars['error_var1'] = False if self.participant.vars['attempts_1'] == 2 and self.participant.vars['error_var1'] is True: self.participant.vars['failed'] = True self.participant.vars['attempts_1'] += 1 #def app_after_this_page(self, upcoming_apps): # if self.participant.vars['attempts_1'] == 3 and self.participant.vars['error_var1'] is True: # self.participant.vars['failed'] = True # return upcoming_apps[2] class SkillError1(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is True and self.participant.vars['failed'] is False #AND SOME VARIABLE (error_var1) IS TRUE class SkillTest2(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is False and self.participant.vars['error_var2'] is True and self.participant.vars['attempts_2'] < 3 and self.participant.vars['failed'] is False form_model = 'player' form_fields = ['skill_answer2'] def before_next_page(self): #if skill answer is wrong (Player 2), error_var1 = True me = self.player if me.skill_answer2 == '$0.10': self.participant.vars['error_var2'] = False if me.skill_answer2 != '$0.10': self.participant.vars['error_var2'] = True if self.participant.vars['attempts_2'] == 2 and self.participant.vars['error_var2'] is True: self.participant.vars['failed'] = True self.participant.vars['attempts_2'] += 1 #def app_after_this_page(self, upcoming_apps): # return upcoming_apps[2] class SkillError2(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is False and self.participant.vars['error_var2'] is True and self.participant.vars['failed'] is False class SkillTest3(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is False and self.participant.vars['error_var2'] is False and self.participant.vars['error_var3'] is True and self.participant.vars['attempts_3'] < 3 and self.participant.vars['failed'] is False form_model = 'player' form_fields = ['skill_answer3'] def before_next_page(self): me = self.player if me.skill_answer3 == 'Cooperate': self.participant.vars['error_var3'] = True if me.skill_answer3 == 'Defect': self.participant.vars['start_time'] = time.time() self.participant.vars['error_var3'] = False if self.participant.vars['attempts_3'] == 2 and self.participant.vars['error_var3'] is True: self.participant.vars['failed'] = True self.participant.vars['attempts_3'] += 1 #def app_after_this_page(self, upcoming_apps): # if self.participant.vars['error_var3'] is False: # return upcoming_apps[0] # return upcoming_apps[2] #IF self.session.vars['error_var3'] = FALSE, REDIRECT TO NEXT APP EVEN IF NOT DONE ROUNDS class SkillError3(Page): def is_displayed(self): return self.round_number >= 3 and self.participant.vars['error_var1'] is False and self.participant.vars['error_var2'] is False and self.participant.vars['error_var3'] is True and self.participant.vars['failed'] is False page_sequence = [ Introduction, Decision, #TestResultsPage, #ResultsWaitPage, Results, End, SkillTest, SkillTest1, SkillError1, SkillTest2, SkillError2, SkillTest3, SkillError3, ]