from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from time import time # class MyPage(Page): # pass # # # class ResultsWaitPage(WaitPage): # pass class AllInstructions(Page): # timer_text = 'Time left to go through the instructions:' def is_displayed(self): # print(self.player.participant.vars['inactive']) return self.round_number == 1 #and not self.player.participant.vars['inactive'] def vars_for_template(self): group = self.session.config['group'] reputation = self.subsession.reputation reward = self.session.config['reward'] punishment = self.session.config['punishment'] lose = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) highpayoff = self.subsession.value + self.subsession.endowment # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_g.png' else: image_path = 'high.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low.png' # Decision A if not group: if not reputation: decA = 'DECISION_T1_A.png' else: if self.session.config['vc_ratio'] == 'high': decA = 'DECISION_T2_A.png' else: decA = 'DECISION_T3_A.png' else: decA = 'DECISION_T456_A.png' # Decision B if not group: if self.session.config['vc_ratio'] == 'high': decB = 'DECISION_T12_B.png' else: decB = 'DECISION_T3_B.png' else: decB = 'DECISION_T456_B.png' # Results A if not group: if self.session.config['vc_ratio'] == 'high': resA = 'RESULTS_T12_A.png' else: resA = 'RESULTS_T3_A.png' else: resA = 'RESULTS_T456_A.png' # Results B if not group: if self.session.config['vc_ratio'] == 'high': resB = 'RESULTS_T12_B.png' else: resB = 'RESULTS_T3_B.png' else: if not reward: resB = 'RESULTS_T4_B.png' else: if not punishment: resB = 'RESULTS_T5_B.png' else: resB = 'RESULTS_T6_B.png' return dict( group=group, reputation=reputation, reward=reward, punishment=punishment, lose=lose, highpayoff=highpayoff, image_path=image_path, decA=decA, decB=decB, resA=resA, resB=resB, ) # def get_timeout_seconds(self): # return self.player.timeout_seconds() # def before_next_page(self): # # Register timeouts # if self.timeout_happened: # self.player.participant.vars['inactive'] = True class Questions(Page): form_model = 'player' form_fields = ['q1','q2','q3','q4','q5a','q5b','q6a','q6b','q7a','q7b','q8'] # timer_text = 'Time left to go through the instructions:' def is_displayed(self): return self.round_number == 1 #and not self.player.participant.vars['inactive'] def vars_for_template(self): group = self.session.config['group'] reputation = self.subsession.reputation reward = self.session.config['reward'] punishment = self.session.config['punishment'] lose = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) highpayoff = self.subsession.value + self.subsession.endowment # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_g.png' else: image_path = 'high.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low.png' # Decision A if not group: if not reputation: decA = 'DECISION_T1_A.png' else: if self.session.config['vc_ratio'] == 'high': decA = 'DECISION_T2_A.png' else: decA = 'DECISION_T3_A.png' else: decA = 'DECISION_T456_A.png' # Decision B if not group: if self.session.config['vc_ratio'] == 'high': decB = 'DECISION_T12_B.png' else: decB = 'DECISION_T3_B.png' else: decB = 'DECISION_T456_B.png' # Results A if not group: if self.session.config['vc_ratio'] == 'high': resA = 'RESULTS_T12_A.png' else: resA = 'RESULTS_T3_A.png' else: resA = 'RESULTS_T456_A.png' # Results B if not group: if self.session.config['vc_ratio'] == 'high': resB = 'RESULTS_T12_B.png' else: resB = 'RESULTS_T3_B.png' else: if not reward: resB = 'RESULTS_T4_B.png' else: if not punishment: resB = 'RESULTS_T5_B.png' else: resB = 'RESULTS_T6_B.png' return dict( group=group, reputation=reputation, reward=reward, punishment=punishment, lose=lose, highpayoff=highpayoff, image_path=image_path, decA=decA, decB=decB, resA=resA, resB=resB, ) def before_next_page(self): # # Register timeouts # if self.timeout_happened: # self.player.participant.vars['inactive'] = True # self.player.q1 = None # self.player.q2 = None # self.player.q3 = None # self.player.q4 = None # self.player.q5a = None # #self.player.q6 = None # self.player.q7a = None # self.player.q7b = None # self.player.q8 = None # if not self.timeout_happened: self.player.check_answers() class Answers(Page): form_model = 'player' # timer_text = 'Time left to go through the instructions:' def is_displayed(self): return self.round_number == 1 #and not self.player.participant.vars['inactive'] def vars_for_template(self): group = self.session.config['group'] reputation = self.subsession.reputation reward = self.session.config['reward'] punishment = self.session.config['punishment'] lose = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) highpayoff = self.subsession.value + self.subsession.endowment # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_g.png' else: image_path = 'high.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low.png' # Decision A if not group: if not reputation: decA = 'DECISION_T1_A.png' else: if self.session.config['vc_ratio'] == 'high': decA = 'DECISION_T2_A.png' else: decA = 'DECISION_T3_A.png' else: decA = 'DECISION_T456_A.png' # Decision B if not group: if self.session.config['vc_ratio'] == 'high': decB = 'DECISION_T12_B.png' else: decB = 'DECISION_T3_B.png' else: decB = 'DECISION_T456_B.png' # Results A if not group: if self.session.config['vc_ratio'] == 'high': resA = 'RESULTS_T12_A.png' else: resA = 'RESULTS_T3_A.png' else: resA = 'RESULTS_T456_A.png' # Results B if not group: if self.session.config['vc_ratio'] == 'high': resB = 'RESULTS_T12_B.png' else: resB = 'RESULTS_T3_B.png' else: if not reward: resB = 'RESULTS_T4_B.png' else: if not punishment: resB = 'RESULTS_T5_B.png' else: resB = 'RESULTS_T6_B.png' value = self.subsession.endowment + self.subsession.value payoff = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) if self.player.q1 == 1: q1 = True else: q1 = False if self.player.q2 == 1: q2 = True else: q2 = False if self.player.q3 == 1: q3 = True else: q3 = False if self.player.q4 == 1: q4 = True else: q4 = False if self.player.q8 == 1: q8_answer = "Neither Person A nor Person B is informed about the decisions made by other persons in earlier rounds." elif self.player.q8 == 2: q8_answer = "Only Persons B receive information about the decisions made by Persons A in earlier rounds." elif self.player.q8 == 3: q8_answer = "Only Persons A receive information about the decisions made by Persons B in earlier rounds." elif self.player.q8 == 4: q8_answer = "Both Person A and Person B receive information about the decisions made by Persons A in earlier rounds." elif self.player.q8 == 5: q8_answer = "Both Person A and Person B receive information about the decisions made by Persons B in earlier rounds." else: q8_answer = "" if not self.subsession.reputation: q8_exp = "Neither Person A nor Person B is informed about the decisions made by other persons in earlier rounds." else: q8_exp = "Both Person A and Person B receive information about the decisions made by Persons B in earlier rounds." return dict( # Instructions group=group, reputation=reputation, reward=reward, punishment=punishment, lose=lose, highpayoff=highpayoff, image_path=image_path, decA=decA, decB=decB, resA=resA, resB=resB, # Answers value=value, payoff=payoff, q1=q1, q2=q2, q3=q3, q4=q4, q8_answer=q8_answer, q8_exp=q8_exp, ) class PracticeDecisions(Page): form_model = 'player' # timer_text = 'Time left to go through the instructions:' def is_displayed(self): return self.round_number == 1 #and not self.player.participant.vars['inactive'] class DecisionsAttacker(Page): # template_name = 't4_seq_hawk_dove/DecisionsAttacker.html' form_model = 'player' form_fields = ['pract_choice_a'] timer_text = 'Time left to go through the instructions:' def get_timeout_seconds(self): if self.round_number == 1: timeout = 60 else: timeout = 40 return timeout def vars_for_template(self): group = self.session.config['group'] if self.round_number == 2: prac_round = 14 up = 1 down = 6 total = 7 elif self.round_number == 3: prac_round = 22 up = 5 down = 2 total = 7 else: prac_round = 1 up = 0 down = 0 total = 0 cost_fight = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) highpayoff = self.subsession.endowment + self.subsession.value # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_g.png' else: image_path = 'high.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low.png' return dict( # Practice round prac_round=prac_round, # Treatment type group=group, cost_fight=cost_fight, highpayoff=highpayoff, # Decision tree image_path=image_path, # Reputation table up=up, down=down, total=total, ) # def get_timeout_seconds(self): # return self.player.timeout_seconds() def before_next_page(self): # Register timeouts if self.timeout_happened: # self.player.participant.vars['inactive'] = True self.player.pract_choice_a = 2 self.player.pract_payoff_a = 0 self.player.participant.vars['prac_missed'] += 1 print('Player missed {} choices'.format(self.player.participant.vars['prac_missed'])) class DecisionDefender(Page): form_model = 'player' form_fields = ['pract_choice_d'] def get_timeout_seconds(self): if self.round_number == 1: timeout = 60 else: timeout = 40 return timeout # timer_text = 'Time left to go through the instructions:' # def is_displayed(self): # return not self.player.participant.vars['inactive'] def vars_for_template(self): group = self.session.config['group'] if self.round_number == 2: prac_round = 14 up = 1 down = 6 total = 7 elif self.round_number == 3: prac_round = 22 up = 5 down = 2 total = 7 else: prac_round = 1 up = 0 down = 0 total = 0 cost_fight = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) highpayoff = self.subsession.endowment + self.subsession.value if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_g.png' else: image_path = 'high.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low.png' return dict( # Practice round prac_round=prac_round, # Treatment type group=group, cost_fight=cost_fight, highpayoff=highpayoff, # Decision tree image_path=image_path, # Reputation table up=up, down=down, total=total, ) def before_next_page(self): # Register timeouts if self.timeout_happened: # self.player.participant.vars['inactive'] = True self.player.pract_choice_d = 2 self.player.pract_payoff_b = 0 self.player.participant.vars['prac_missed'] += 1 print('Player missed {} choices'.format(self.player.participant.vars['prac_missed'])) else: if self.player.pract_choice_a == 0: self.player.pract_payoff_a = self.subsession.endowment self.player.pract_payoff_b = self.subsession.endowment + self.subsession.value elif self.player.pract_choice_a == 1: if self.player.pract_choice_d == 1: self.player.pract_payoff_a = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) self.player.pract_payoff_b = int(self.subsession.endowment + (self.subsession.value - self.subsession.cost) / 2) elif self.player.pract_choice_d == 0: self.player.pract_payoff_a = self.subsession.endowment + self.subsession.value self.player.pract_payoff_b = self.subsession.endowment elif self.player.pract_choice_a == 2: self.player.pract_payoff_a = 0 self.player.pract_payoff_b = 0 class Results(Page): template_name = 't4_instr/Results.html' form_model = 'player' form_fields = ['pract_rewpun'] timeout_seconds = 40 # timer_text = 'Time left to go through the instructions:' # def is_displayed(self): # return not self.player.participant.vars['inactive'] def vars_for_template(self): #role = 'defender' group = self.session.config['group'] reward_tm = self.session.config['reward'] punish_tm = self.session.config['punishment'] #choice_a = 1 #choice_d = self.player.pract_choice_d #pract_payoff = self.player.pract_payoff if self.round_number == 2: prac_round = 14 sel_defender = 4 pract_choice_d = self.player.pract_choice_d elif self.round_number == 3: prac_round = 22 sel_defender = 1 pract_choice_d = self.player.pract_choice_d else: prac_round = 1 sel_defender = 3 pract_choice_d = self.player.pract_choice_d defenders = [] if group: if self.round_number == 1: defenders.append( {'id': 1, 'total': 0, 'num_up': 0, 'num_down': 0}) defenders.append( {'id': 2, 'total': 0, 'num_up': 0, 'num_down': 0}) if self.player.pract_choice_a == 0: defenders.append( {'id': 3, 'total': 0, 'num_up': 0, 'num_down': 0}) else: if self.player.pract_choice_d == 1: defenders.append( {'id': 3, 'total': 1, 'num_up': 0, 'num_down': 1}) elif self.player.pract_choice_d == 0: defenders.append( {'id': 3, 'total': 1, 'num_up': 1, 'num_down': 0}) defenders.append( {'id': 4, 'total': 0, 'num_up': 0, 'num_down': 0}) elif self.round_number == 2: defenders.append( {'id': 1, 'total': 2, 'num_up': 2, 'num_down': 0}) defenders.append( {'id': 2, 'total': 3, 'num_up': 0, 'num_down': 3}) if self.player.pract_choice_a == 0: defenders.append( {'id': 3, 'total': 3, 'num_up': 1, 'num_down': 2}) else: if self.player.pract_choice_d == 1: defenders.append( {'id': 3, 'total': 2, 'num_up': 0, 'num_down': 2}) elif self.player.pract_choice_d == 0: defenders.append( {'id': 3, 'total': 2, 'num_up': 1, 'num_down': 1}) defenders.append( {'id': 4, 'total': 2, 'num_up': 0, 'num_down': 2}) elif self.round_number == 3: defenders.append( {'id': 1, 'total': 5, 'num_up': 3, 'num_down': 2}) defenders.append( {'id': 2, 'total': 4, 'num_up': 4, 'num_down': 0}) if self.player.pract_choice_a == 0: defenders.append( {'id': 3, 'total': 4, 'num_up': 2, 'num_down': 2}) else: if self.player.pract_choice_d == 1: defenders.append( {'id': 3, 'total': 5, 'num_up': 2, 'num_down': 3}) elif self.player.pract_choice_d == 0: defenders.append( {'id': 3, 'total': 5, 'num_up': 3, 'num_down': 2}) defenders.append( {'id': 4, 'total': 5, 'num_up': 4, 'num_down': 1}) print(defenders) if reward_tm: for d in defenders: if self.round_number == 1: if d['id'] == 1: d["num_likes"] = 0 if d['id'] == 2: d["num_likes"] = 0 if d['id'] == 3: d["num_likes"] = 0 if d['id'] == 4: d["num_likes"] = 0 elif self.round_number == 2: if d['id'] == 1: d["num_likes"] = 0 if d['id'] == 2: d["num_likes"] = 7 if d['id'] == 3: d["num_likes"] = 2 if d['id'] == 4: d["num_likes"] = 4 elif self.round_number == 3: if d['id'] == 1: d["num_likes"] = 3 if d['id'] == 2: d["num_likes"] = 1 if d['id'] == 3: d["num_likes"] = 4 if d['id'] == 4: d["num_likes"] = 1 return dict( #role=role, group=group, sel_defender=sel_defender, reward_tm=reward_tm, punish_tm=punish_tm, prac_round=prac_round, #choice_a=choice_a, #choice_d=choice_d, pract_choice_d = pract_choice_d, #pract_payoff=pract_payoff, defenders=defenders, ) # def get_timeout_seconds(self): # return self.player.timeout_seconds() def before_next_page(self): # Register timeouts # if self.timeout_happened: # self.player.participant.vars['inactive'] = True if self.session.config['reward'] and self.player.pract_choice_a == 1 and self.player.pract_choice_d < 2: if self.player.pract_choice_d == 1: self.player.pract_numlikes += 1 if self.player.pract_rewpun == 1: self.player.pract_numlikes += 1 if self.session.config['punishment'] and self.player.pract_choice_a == 1 and self.player.pract_choice_d < 2: if self.player.pract_rewpun == 3: self.player.pract_numpunishers += 1 self.player.pract_payoff_b -= 3 # elif self.player.pract_choice_d == 0: # self.player.pract_numpunishers += 1 # self.player.pract_payoff_b -= 3 class RewardPunishment(Page): def is_displayed(self): return (self.session.config['reward'] or self.session.config['punishment']) \ and self.player.pract_choice_a == 1 and self.player.pract_choice_d < 2 # and not self.player.participant.vars['inactive'] timeout_seconds = 20 # timer_text = 'Time left to go through the instructions:' def vars_for_template(self): reward_tm = self.session.config['reward'] punish_tm = self.session.config['punishment'] points_lost = self.player.pract_numpunishers * 3 if self.round_number == 2: pract_round = 14 elif self.round_number == 3: pract_round = 22 else: pract_round = 1 if self.player.pract_rewpun == 3: pun_payoff = 0 else: pun_payoff = 1 return dict( reward_tm=reward_tm, punish_tm=punish_tm, points_lost=points_lost, pract_round=pract_round, pun_payoff=pun_payoff, ) # def get_timeout_seconds(self): # return self.player.timeout_seconds() # def before_next_page(self): # Register timeouts # if self.timeout_happened: # self.player.participant.vars['inactive'] = True class ReadyToStart(Page): # timeout_seconds = 2 # timer_text = 'Time left to go through the instructions:' def is_displayed(self): return self.round_number == Constants.num_rounds #and not self.player.participant.vars['inactive'] # def get_timeout_seconds(self): # return self.player.timeout_seconds() # def before_next_page(self): # # Register timeouts # if self.timeout_happened: # self.player.participant.vars['inactive'] = True # else: # self.player.timestamp_start = time() # self.participant.vars['instr_timestamp'] = self.player.timestamp_start page_sequence = [ AllInstructions, Questions, Answers, PracticeDecisions,DecisionsAttacker,DecisionDefender, Results,RewardPunishment, ReadyToStart] # page_sequence = [ReadyToStart]