from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants from time import time import datetime import pytz from random import shuffle class Grouping(WaitPage): template_name = 't5_payoff/Wait.html' wait_for_all_groups = True def vars_for_template(self): endexp = self.player.participant.vars['experimentended'] inactive = self.player.participant.vars['inactive'] # if self.round_number == 1: # role = "" # else: role = self.participant.vars['role'] return dict( endexp=endexp, inactive=inactive, role=role ) def after_all_players_arrive(self): self.subsession.create_groups() class StartQuestions(Page): timeout_seconds = 1 def before_next_page(self): self.participant.vars['end_time'] = datetime.datetime.now(tz=pytz.utc) + datetime.timedelta(minutes=2) class BeliefsEE(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['ee'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'ee' inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gD.png' else: image_path = 'high_D.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_gD.png' else: image_path = 'low_D.png' return dict( group=group, page=page, image_path=image_path, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.ee = -99 # No choice # self.participant.vars['ee_l'] = self.player.ee class BeliefsPNB(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['pnb'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'pnb' cost_fight = int(abs((self.subsession.value - self.subsession.cost) / 2)) inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # 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( group=group, page=page, cost_fight=cost_fight, image_path=image_path, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.pnb = -99 # No choice # self.participant.vars['pnb_l'] = self.player.pnb class BeliefsNE(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['ne'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'ne' inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gD.png' else: image_path = 'high_D.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_gD.png' else: image_path = 'low_D.png' return dict( group=group, page=page, image_path=image_path, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.ne = -99 # No choice # self.participant.vars['ne_l'] = self.player.ne class BeliefsHNB(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['hnb'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'hnb' cost_fight = int(abs((self.subsession.value - self.subsession.cost) / 2)) inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # 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( group=group, page=page, cost_fight=cost_fight, image_path=image_path, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.pnb = -99 # No choice # self.participant.vars['hnb_l'] = self.player.pnb class AppropBEH_u(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['beh_u'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'apb' question = "Don't Resist" number = 4 inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gU.png' else: image_path = 'high_U.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_g.png' else: image_path = 'low_U.png' return dict( group=group, page=page, image_path=image_path, question=question, number=number, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.beh_u = -99 # No choice class AppropBEH_d(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['beh_d'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'apb' question = "Resist" number = 5 inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gD.png' else: image_path = 'high_D.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_gD.png' else: image_path = 'low_D.png' return dict( group=group, page=page, image_path=image_path, question=question, number=number, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.beh_d = -99 # No choice class AppropPUN_u(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['pun_u'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'app' question = "Don't Resist" number = 5 inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gU.png' else: image_path = 'high_U.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_gU.png' else: image_path = 'low_U.png' return dict( group=group, page=page, image_path=image_path, question=question, number=number, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.pun_u = -99 # No choice class AppropPUN_d(Page): template_name = 't5_payoff/Beliefs.html' form_model = 'player' form_fields = ['pun_d'] def is_displayed(self): return self.player.participant.vars['role'] == 'defender' and not self.player.participant.vars['inactive'] def get_timeout_seconds(self): return self.player.timeout_norms() def vars_for_template(self): group = self.session.config['group'] page = 'app' question = "Resist" number = 6 inactive = self.player.participant.vars['inactive'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] # Decision tree if self.session.config['vc_ratio'] == 'high': if self.session.config['group']: image_path = 'high_gD.png' else: image_path = 'high_D.png' elif self.session.config['vc_ratio'] == 'low': if self.session.config['group']: image_path = 'low_gD.png' else: image_path = 'low_D.png' return dict( group=group, page=page, image_path=image_path, question=question, number=number, inactive=inactive, last_round=last_round, defender_id=defender_id ) def before_next_page(self): if self.timeout_happened: self.player.pun_d = -99 # No choice class NormCalc(WaitPage): # wait_for_all_groups = True def after_all_players_arrive(self): self.group.norm_calculations() self.group.final_payoff() class Payoff(Page): # def is_displayed(self): # return not self.player.participant.vars['inactive'] # return self.round_number == self.subsession.last_round + 1 and self.player.playing_status < 3 and \ # self.player.participant.vars['num_timeouts'] < Constants.allowed_timeouts and \ # self.player.participant.vars['participates'] def vars_for_template(self): inactive = self.participant.vars['inactive'] endexp = self.player.participant.vars['experimentended'] role = self.participant.vars['role'] group = self.session.config['group'] last_round = self.session.vars['last_round'] defender_id = self.player.participant.vars['defender_id'] punish_tm = self.session.config['punishment'] if punish_tm: pun_payoff = self.participant.vars['pun_payoff'] else: pun_payoff = 0 task123 = "{:0f}".format(self.participant.vars['task123']) total_payoff = "{:0.2f}".format(self.player.total_payoff) sel_round1 = self.player.participant.vars['sel_round1'] sel_round2 = self.player.participant.vars['sel_round2'] sel_round3 = self.player.participant.vars['sel_round3'] sel_round4 = self.player.participant.vars['sel_round4'] payoff1 = self.player.participant.vars['payoff1'] payoff2 = self.player.participant.vars['payoff2'] payoff3 = self.player.participant.vars['payoff3'] payoff4 = self.player.participant.vars['payoff4'] print('role =', role) print('player.sel_round1 2 3 4', sel_round1, sel_round2, sel_round3) # , sel_round4) print('player.payoff1 2 3 4', payoff1, payoff2, payoff3) # , payoff4) if role == 'defender': print('player.norm_payoff', self.player.norm_payoff) print('player.total_payoff', self.player.total_payoff) return dict( inactive=inactive, endexp=endexp, role=role, group=group, last_round=last_round, defender_id=defender_id, punish_tm=punish_tm, pun_payoff=pun_payoff, task123=task123, total_payoff=total_payoff, sel_round1=sel_round1, sel_round2=sel_round2, sel_round3=sel_round3, sel_round4=sel_round4, payoff1=payoff1, payoff2=payoff2, payoff3=payoff3, payoff4=payoff4, ) class Questionnaire(Page): form_model = 'player' form_fields = ['age', 'gender', 'educ', 'student', 'left_right', 'experience', 'purpose', 'unclear', 'didntwork'] # def is_displayed(self): # return not self.player.participant.vars['inactive'] # def before_next_page(self): # self.player.participant.vars['macr'] = shuffle(['macr1', 'macr2', 'macr3', 'macr4', 'macr5', 'macr6', 'macr7', # 'macr8', 'macr9', 'macr10', 'macr11', 'macr12', 'macr13', # 'macr14', 'macr15', 'macr16', 'macr17', 'macr18', 'macr19', # 'macr20', 'macr21']) # class mac1(Page): # form_model = 'player' # form_fields = shuffle() class End(Page): # def is_displayed(self): # return self.round_number == self.subsession.last_round + 1 and self.player.playing_status < 3 and \ # self.player.participant.vars['num_timeouts'] < Constants.allowed_timeouts and \ # self.player.participant.vars['participates'] def vars_for_template(self): group = self.session.config['group'] return dict( group=group ) page_sequence = [ Grouping, StartQuestions, BeliefsEE, BeliefsPNB, BeliefsNE, BeliefsHNB, # AppropBEH_u, AppropBEH_d, AppropPUN_u, AppropPUN_d, NormCalc, Payoff, Questionnaire, End ]