from otree.api import * import random import time doc = """ fight and flee game when c=40 """ class Constants(BaseConstants): name_in_url = 'fight_and_flee_c40' players_per_group = 2 num_rounds = 41 # for real game, that should equal to 41 #endowment = 400 vh = 100 vl = 100 cost = 40 #change the value of waiting costs of different treatments k = 100 table_template = __name__ + '/table.html' class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): initial_period = models.StringField( choices=['Period 1', 'Period 2', 'Period 3'], label="Based on your strength, please indicate which period you plan not to wait (to fight or flee) in this round", widget=widgets.RadioSelect, blank=False) initial_action = models.StringField( choices = ['Flee', 'Fight'], label="Based on your strength, please indicate whether you plan to fight or flee in your selected period above", widget=widgets.RadioSelect, blank=False) period1_choice = models.StringField( choices=['Flee', 'Fight', 'Wait'], label="Please make your decision in period 1", widget=widgets.RadioSelect, blank=True, initial='Wait' ) period2_choice = models.StringField( choices=['Flee', 'Fight', 'Wait'], label="Please make your decision in period 2", widget=widgets.RadioSelect, blank=True, initial='Wait' ) period3_choice = models.StringField( choices=['Flee', 'Fight'], label="Please make your decision in period 3", widget=widgets.RadioSelect, blank=True, initial='Fight' ) ability = models.IntegerField() cost_waiting = models.IntegerField() #indicate waiting costs in this round period1_end = models.BooleanField() period2_end = models.BooleanField() period3_end = models.BooleanField(initial=False) period = models.IntegerField() #indicates which period the game ends choice = models.StringField() #indicates players' choice when game ends round_earning = models.FloatField ()# indicates the round earnings #round_total_earning = models.FloatField() round_result = models.StringField() # indicates whether the period ends with battle or escape part1_currency_payoff = models.CurrencyField() period3_seconds = models.FloatField(initial=10.00) #loss aversion parameters raw_responses = models.LongStringField() chose_lottery = models.BooleanField() won_lottery = models.BooleanField(initial=False) age = models.IntegerField( label='What is your age?', min=18, max=100) gender = models.StringField( choices=['Male', 'Female', 'Other'], label='What is your gender?', widget=widgets.RadioSelect) citizenship = models.StringField( label='What is your country(s) of citizenship?') major = models.StringField( label='What is your major?') year = models.StringField( choices=['Freshman', 'Sophomore', 'Junior', 'Senior', 'Graduate', 'Other'], label='Which year are you in?', widget=widgets.RadioSelect) risk = models.StringField( choices=['Strongly Disagree', 'Disagree', 'Slightly Disagree', 'Slightly Agree', 'Agree', 'Strongly Agree'], label='You are a person who is fully prepared to take risks.', widget=widgets.RadioSelect) decision = models.LongStringField(initial=None, label="How did you make your decisions in Part 1?") comments = models.LongStringField(blank=True, initial=None, label="Do you have any comments, questions, or complaints about today's experiment?") quiz1 = models.StringField( choices=['The same participant', 'A randomly determined participant'], label='1. In each round, you are matched with: ', widget=widgets.RadioSelect) quiz2 = models.StringField( choices=['The same', 'Randomly determined'], label='2. In each round, your strength is: ', widget=widgets.RadioSelect) quiz3 = models.StringField( choices=['an escape', 'a battle'], label='3. If you choose to wait in Period 1 while your opponent chooses to flee in Period 1. This round ends with:', widget=widgets.RadioSelect) quiz4 = models.StringField( choices=['0 points', '40 points'], label='4. Consider the choice that you and your opponent made in question 3. What is the cost you paid for waiting:', widget=widgets.RadioSelect) quiz5 = models.StringField( choices=['100+100=200 points', '100 points'], label='5. Consider the choice that you and your opponent made in question 3. Your payoff in this round is: ', widget=widgets.RadioSelect) quiz6 = models.StringField( choices=['an escape', 'a battle'], label='6. If both of you choose to wait in Period 1, and both of you choose to fight in Period 2, then this round ends with:', widget=widgets.RadioSelect) quiz7 = models.StringField( choices=['0 points', '40 points'], label='7. Consider the choice that you and your opponent made in question 6. What is the cost you paid for waiting:', widget=widgets.RadioSelect) quiz8 = models.StringField( choices=['-100-40=-140 points', '100-40=60 points', '-100 points', '100 points'], label='8. Consider the choice that you and your opponent made in question 6. Your payoff in this round is: ', widget=widgets.RadioSelect) class Trial(ExtraModel): #for loss aversion test player = models.Link(Player) sure_payoff = models.CurrencyField() lottery_win = models.CurrencyField() lottery_lose = models.CurrencyField() probability = models.FloatField() chose_lottery = models.BooleanField() randomly_chosen = models.BooleanField(initial=False) #Functions def read_csv(): import csv f = open(__name__ + '/stimuli.csv', encoding='utf-8-sig') rows = list(csv.DictReader(f)) return rows def creating_session(subsession: Subsession): # print('in creating_session') session = subsession.session subsession.group_randomly() #randomly rematched in each round if subsession.round_number == 1: paying_round1 = random.randint(2, 11) # change those values to [2,11] later paying_round2 = random.randint(12, 21) # change those values to [12,21] later paying_round3 = random.randint(22, 31) paying_round4 = random.randint(32, 41) session.vars['paying_round1'] = paying_round1 session.vars['paying_round2'] = paying_round2 session.vars['paying_round3'] = paying_round3 session.vars['paying_round4'] = paying_round4 for p in subsession.get_players(): p.ability = random.randint(0, 1000) #randomly assign ability at the beginning p.round_chosen1 = session.vars['paying_round1'] p.round_chosen2 = session.vars['paying_round2'] p.round_chosen3 = session.vars['paying_round3'] p.round_chosen4 = session.vars['paying_round4'] stimuli = read_csv() for stim in stimuli: Trial.create(player=p, **stim) def set_payoff(group:Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) if p1.period1_choice == 'Flee' or p2.period1_choice == 'Flee' or p1.period1_choice == 'Fight' or p2.period1_choice=='Fight': for player in [p1, p2]: player.period1_end = True player.period2_end = False player.period3_end = False elif p1.period2_choice == 'Flee' or p2.period2_choice == 'Flee' or p1.period2_choice == 'Fight' or p2.period2_choice == 'Fight': for player in [p1, p2]: player.period1_end = False player.period2_end = True player.period3_end = False elif p1.period3_choice == 'Flee' or p2.period3_choice == 'Flee' or p1.period3_choice == 'Fight' or p2.period3_choice=='Fight': for player in [p1, p2]: player.period1_end = False player.period2_end = False player.period3_end = True else: for player in [p1, p2]: player.period1_end = False player.period2_end = False player.period3_end = False if p1.period1_end: for player in [p1, p2]: player.choice = player.period1_choice player.period = 1 elif p1.period2_end: for player in [p1, p2]: player.choice = player.period2_choice player.period = 2 elif p1.period3_end: for player in [p1, p2]: player.choice = player.period3_choice player.period = 3 if p1.period1_end: #no waiting cost r1 = random.random() r2 = random.random() if p1.period1_choice == 'Flee' and p2.period1_choice == 'Flee': #both flee, share the prize+k for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = (Constants.vh + Constants.k) / 2 p2.round_earning = (Constants.vh + Constants.k) / 2 elif p1.period1_choice == 'Flee' and p2.period1_choice == 'Fight': #one flee, one fight, then has 50% battle and 50% escape. if r1 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh p1.round_earning = 0 - Constants.vl elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl p1.round_earning = Constants.vh else: #ability are the same, randomly assign one winner if r2 > 0.5: p1.round_earning = Constants.vh p2.round_earning = 0-Constants.vl else: p1.round_earning = 0 -Constants.vl p2.round_earning = Constants.vh else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = 0 p2.round_earning = Constants.k + Constants.vh elif p1.period1_choice == 'Fight' and p2.period1_choice == 'Flee': #one flee, one fight, then has 50% battle and 50% escape. if r1 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh p1.round_earning = 0 -Constants.vl elif p2.ability < p1.ability: p2.round_earning = 0 -Constants.vl p1.round_earning = Constants.vh else: #two players have same ability if r2 > 0.5: p1.round_earning = Constants.vh p2.round_earning = 0 -Constants.vl else: p1.round_earning = 0 -Constants.vl p2.round_earning = Constants.vh else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = Constants.k + Constants.vh p2.round_earning = 0 elif p1.period1_choice == 'Fight' and p2.period1_choice == 'Fight': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh p1.round_earning = 0 -Constants.vl elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl p1.round_earning = Constants.vh else: if r2 > 0.5: p1.round_earning = Constants.vh p2.round_earning = 0 - Constants.vl else: p1.round_earning = 0 - Constants.vl p2.round_earning = Constants.vh elif p1.period1_choice == 'Fight' and p2.period1_choice == 'Wait': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh p1.round_earning = 0 - Constants.vl elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl p1.round_earning = Constants.vh else: #same ability case if r2 > 0.5: p1.round_earning = Constants.vh p2.round_earning = 0 - Constants.vl else: p1.round_earning = 0 - Constants.vl p2.round_earning = Constants.vh elif p1.period1_choice == 'Flee' and p2.period1_choice == 'Wait': #the one who flee gets 0, and not flee get (vh+k) for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = 0 p2.round_earning = Constants.vh + Constants.k elif p1.period1_choice == 'Wait' and p2.period1_choice == 'Fight': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh p1.round_earning = 0 - Constants.vl elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl p1.round_earning = Constants.vh else: #same ability case if r2 > 0.5: p1.round_earning = Constants.vh p2.round_earning = 0 - Constants.vl else: p1.round_earning = 0 - Constants.vl p2.round_earning = Constants.vh elif p1.period1_choice == 'Wait' and p2.period1_choice == 'Flee': #the one who flee gets 0, and not flee get (vh+k) for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = Constants.vh + Constants.k p2.round_earning = 0 elif p1.period2_end: #incur one unit of waiting cost r3 = random.random() r4 = random.random() if p1.period2_choice == 'Flee' and p2.period2_choice == 'Flee': # both flee, share the prize+k for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = (Constants.vh + Constants.k) / 2 - Constants.cost p2.round_earning = (Constants.vh + Constants.k) / 2 - Constants.cost elif p1.period2_choice == 'Flee' and p2.period2_choice == 'Fight': # one flee, one fight, then has 50% battle and 50% escape. if r3 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - Constants.cost p1.round_earning = 0 - Constants.vl - Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - Constants.cost p1.round_earning = Constants.vh - Constants.cost else: # ability are the same, randomly assign one winner if r4 > 0.5: p1.round_earning = Constants.vh - Constants.cost p2.round_earning = 0 - Constants.vl - Constants.cost else: p1.round_earning = 0 - Constants.vl - Constants.cost p2.round_earning = Constants.vh - Constants.cost else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = 0 - Constants.cost p2.round_earning = Constants.k + Constants.vh - Constants.cost elif p1.period2_choice == 'Fight' and p2.period2_choice == 'Flee': # one flee, one fight, then has 50% battle and 50% escape. if r3 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - Constants.cost p1.round_earning = 0 - Constants.vl - Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - Constants.cost p1.round_earning = Constants.vh - Constants.cost else: # two players have same ability if r4 > 0.5: p1.round_earning = Constants.vh - Constants.cost p2.round_earning = 0 - Constants.vl - Constants.cost else: p1.round_earning = 0 - Constants.vl - Constants.cost p2.round_earning = Constants.vh - Constants.cost else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = Constants.k + Constants.vh - Constants.cost p2.round_earning = 0 - Constants.cost elif p1.period2_choice == 'Fight' and p2.period2_choice == 'Fight': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - Constants.cost p1.round_earning = 0 - Constants.vl - Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - Constants.cost p1.round_earning = Constants.vh - Constants.cost else: if r4 > 0.5: p1.round_earning = Constants.vh - Constants.cost p2.round_earning = 0 - Constants.vl - Constants.cost else: p1.round_earning = 0 - Constants.vl - Constants.cost p2.round_earning = Constants.vh - Constants.cost elif p1.period2_choice == 'Fight' and p2.period2_choice == 'Wait': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - Constants.cost p1.round_earning = 0 - Constants.vl - Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - Constants.cost p1.round_earning = Constants.vh - Constants.cost else: # same ability case if r4 > 0.5: p1.round_earning = Constants.vh - Constants.cost p2.round_earning = 0 - Constants.vl - Constants.cost else: p1.round_earning = 0 - Constants.vl - Constants.cost p2.round_earning = Constants.vh - Constants.cost elif p1.period2_choice == 'Flee' and p2.period2_choice == 'Wait': # the one who flee gets 0, and not flee get (vh+k) for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = 0 - Constants.cost p2.round_earning = Constants.vh + Constants.k - Constants.cost elif p1.period2_choice == 'Wait' and p2.period2_choice == 'Fight': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - Constants.cost p1.round_earning = 0 - Constants.vl - Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - Constants.cost p1.round_earning = Constants.vh - Constants.cost else: # same ability case if r4 > 0.5: p1.round_earning = Constants.vh - Constants.cost p2.round_earning = 0 - Constants.vl - Constants.cost else: p1.round_earning = 0 - Constants.vl - Constants.cost p2.round_earning = Constants.vh - Constants.cost elif p1.period2_choice == 'Wait' and p2.period2_choice == 'Flee': # the one who flee gets 0, and not flee get (vh+k) for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = Constants.vh + Constants.k - Constants.cost p2.round_earning = 0 - Constants.cost elif p1.period3_end: r5 = random.random() r6 = random.random() if p1.period3_choice == 'Flee' and p2.period3_choice == 'Flee': for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = (Constants.vh + Constants.k) / 2 - 2 * Constants.cost p2.round_earning = (Constants.vh + Constants.k) / 2 - 2 * Constants.cost elif p1.period3_choice == 'Flee' and p2.period3_choice == 'Fight': if r5 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - 2 * Constants.cost p1.round_earning = 0 - Constants.vl - 2 * Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - 2 * Constants.cost p1.round_earning = Constants.vh - 2 * Constants.cost else: #have the same ability if r6 > 0.5: p1.round_earning = Constants.vh - 2 * Constants.cost p2.round_earning = 0 - Constants.vl - 2 * Constants.cost else: p1.round_earning = 0 - Constants.vl - 2 * Constants.cost p2.round_earning = Constants.vh - 2 * Constants.cost else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = 0 - 2 * Constants.cost p2.round_earning = Constants.k + Constants.vh - 2 * Constants.cost elif p1.period3_choice == 'Fight' and p2.period3_choice == 'Flee': if r5 > 0.5: # battle occurs# for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - 2 * Constants.cost p1.round_earning = 0 - Constants.vl - 2 * Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - 2 * Constants.cost p1.round_earning = Constants.vh - 2 * Constants.cost else: if r6 > 0.5: p1.round_earning = Constants.vh - 2 * Constants.cost p2.round_earning = 0 - Constants.vl - 2 * Constants.cost else: p1.round_earning = 0 - Constants.vl - 2 * Constants.cost p2.round_earning = Constants.vh - 2 * Constants.cost else: # escape occurs# for player in [p1, p2]: player.round_result = 'an escape' p1.round_earning = Constants.k + Constants.vh - 2 * Constants.cost p2.round_earning = 0 - 2 * Constants.cost elif p1.period3_choice == 'Fight' and p2.period3_choice == 'Fight': for player in [p1, p2]: player.round_result = 'a battle' if p2.ability > p1.ability: p2.round_earning = Constants.vh - 2 * Constants.cost p1.round_earning = 0 - Constants.vl - 2 * Constants.cost elif p2.ability < p1.ability: p2.round_earning = 0 - Constants.vl - 2 * Constants.cost p1.round_earning = Constants.vh - 2 * Constants.cost else: if r6 > 0.5: p1.round_earning = Constants.vh - 2 * Constants.cost p2.round_earning = 0 - Constants.vl - 2 * Constants.cost else: p1.round_earning = 0 - Constants.vl - 2 * Constants.cost p2.round_earning = Constants.vh - 2 * Constants.cost for player in [p1, p2]: #player.round_total_earning = player.round_earning + Constants.endowment if player.period == 1: player.cost_waiting = 0 elif player.period == 2: player.cost_waiting = 40 #that is the payoff when waiting cost = 0, when waiting cost equals other values, that need to be changed elif player.period == 3: player.cost_waiting = 80 #player.part1_currency_payoff = player.payoff.to_real_world_currency(self.session) def set_end_time (group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) for p in [p1,p2]: p.period3_seconds = time.time() + 10.00 def close_round(group): set_end_time(group) set_payoff(group) # PAGES class P1_Welcome(Page): def is_displayed(self): return self.round_number == 1 after_all_players_arrive = creating_session class P2_Quiz(Page): def is_displayed(self): return self.round_number == 1 form_model = 'player' form_fields = ['quiz1', 'quiz2', 'quiz3', 'quiz4', 'quiz5', 'quiz6', 'quiz7', 'quiz8'] @staticmethod def error_message(player, values): solutions = dict( quiz1='A randomly determined participant', quiz2='Randomly determined', quiz3='an escape', quiz4='0 points', quiz5='100+100=200 points' , quiz6='a battle', quiz7='40 points', quiz8='100-40=60 points' ) error_messages = dict() for field_name in solutions: if values[field_name] != solutions[field_name]: error_messages[field_name] = 'Wrong answer' return error_messages class P3_Practiceround_instruction(Page): def is_displayed(self): return self.round_number == 1 class P4_Ready_to_start(Page): @staticmethod def vars_for_template(player: Player): return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1 ) class P5_Initial_response(Page): form_model = 'player' form_fields = ['initial_period', 'initial_action' ] @staticmethod def vars_for_template(player: Player): return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1 ) class P6_Choice1(Page): form_model = 'player' form_fields = ['period1_choice'] timer_text = 'Time left to complete this period:' timeout_seconds = 10 @staticmethod def vars_for_template(player: Player): return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1 ) class P7_Choice2(Page): @staticmethod def is_displayed(player): if player.period1_end: return False else: return True form_model = 'player' form_fields = ['period2_choice'] timer_text = 'Time left to complete this period:' timeout_seconds = 10 @staticmethod def vars_for_template(player: Player): return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1 ) class P8_Choice3(Page): # @staticmethod # def vars_for_template(player): # return {'remaining_timeout_seconds': player.period3_seconds - time.time()} @staticmethod def is_displayed (player): if player.period1_end: return False elif player.period2_end: return False else: return True form_model = 'player' form_fields =['period3_choice'] @staticmethod def vars_for_template(player: Player): return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1, remaining_timeout_seconds = player.period3_seconds - time.time() ) class P9_Results(Page): @staticmethod def vars_for_template(player: Player): #group = player.group #other_ability = group.get_others_in_group()[0].ability other = player.get_others_in_group()[0] return dict( round_shown=player.round_number - 1, total_shown=Constants.num_rounds - 1, other=other ) class P10_Practiceround_summary (Page): def is_displayed(self): return self.round_number == 1 class P11_Part1_end(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds #@staticmethod #def vars_for_template(player: Player): # session = player.session # player_in_round1 = player.in_round(session.vars['paying_round1']) # player_in_round2 = player.in_round(session.vars['paying_round2']) # player_in_round3 = player.in_round(session.vars['paying_round3']) # player_in_round4 = player.in_round(session.vars['paying_round4']) # return dict( # round1_payoff=player_in_round1.round_total_earning, # round2_payoff=player_in_round2.round_total_earning, # round3_payoff=player_in_round3.round_total_earning, # round4_payoff=player_in_round4.round_total_earning, # part1_payoff = player_in_round1.round_total_earning + player_in_round2.round_total_earning + # player_in_round3.round_total_earning + player_in_round4.round_total_earning, # paying_round1=session.vars['paying_round1'] - 1, # paying_round2=session.vars['paying_round2'] - 1, # paying_round3=session.vars['paying_round3'] - 1, # paying_round4=session.vars['paying_round4'] - 1, # ) class P12_Lossaversion(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds form_model = 'player' form_fields = ['raw_responses'] @staticmethod def vars_for_template(player: Player): return dict(trials=Trial.filter(player=player)) @staticmethod def before_next_page(player: Player, timeout_happened): import json import random trials = Trial.filter(player=player) # you could adjust this code to handle timeout_happened. responses = json.loads(player.raw_responses) for trial in trials: trial.chose_lottery = responses[str(trial.id)] trial = random.choice(trials) trial.randomly_chosen = True player.chose_lottery = trial.chose_lottery if player.chose_lottery: # player.won_lottery = trial.probability > random.random() if trial.probability > 100 * (random.random()): player.won_lottery = True else: player.won_lottery = False if player.won_lottery: risk_payoff = trial.lottery_win else: risk_payoff = trial.lottery_lose else: risk_payoff = trial.sure_payoff player.payoff = risk_payoff class P13_Lossaversion_result(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds @staticmethod def vars_for_template(player: Player): trials = Trial.filter(player=player, randomly_chosen=True) return dict(trials=trials) class P14_Demographic(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds form_model = 'player' form_fields = ['age', 'gender', 'citizenship', 'major', 'year', 'risk'] class P15_Comment(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds form_model = 'player' form_fields = ['decision', 'comments'] class P16_End_game(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds @staticmethod def vars_for_template(player: Player): session = player.session player_in_round1 = player.in_round(session.vars['paying_round1']) player_in_round2 = player.in_round(session.vars['paying_round2']) player_in_round3 = player.in_round(session.vars['paying_round3']) player_in_round4 = player.in_round(session.vars['paying_round4']) return dict( round1_payoff=player_in_round1.round_earning, round2_payoff=player_in_round2.round_earning, round3_payoff=player_in_round3.round_earning, round4_payoff=player_in_round4.round_earning, part1_payoff= player_in_round1.round_earning + player_in_round2.round_earning + player_in_round3.round_earning + player_in_round4.round_earning + 500, part2_payoff= player.payoff, part1_payoff_currency=(500+player_in_round1.round_earning + player_in_round2.round_earning + player_in_round3.round_earning + player_in_round4.round_earning)*0.02, total_payoff_currency=7 + ((500+player_in_round1.round_earning + player_in_round2.round_earning + player_in_round3.round_earning + player_in_round4.round_earning)*0.02 + player.payoff), paying_round1=session.vars['paying_round1'] - 1, paying_round2=session.vars['paying_round2'] - 1, paying_round3=session.vars['paying_round3'] - 1, paying_round4=session.vars['paying_round4'] - 1, player_chose_lottery=player.chose_lottery, player_won_lottery=player.won_lottery ) #Wait page class Waitforperiod1(WaitPage): after_all_players_arrive = set_payoff class Waitforperiod2(WaitPage): after_all_players_arrive = close_round #@staticmethod #def set_end_time (group): # #set_payoff # for p in group.get_players(): # p.period3_seconds = time.time() + 10.00 class Waitforperiod3(WaitPage): after_all_players_arrive = set_payoff #class ResultsWaitPage(WaitPage): # pass page_sequence = [ P1_Welcome, P2_Quiz, P3_Practiceround_instruction, P4_Ready_to_start, P5_Initial_response, P6_Choice1, Waitforperiod1, P7_Choice2, Waitforperiod2, P8_Choice3, Waitforperiod3, P9_Results, P10_Practiceround_summary, P11_Part1_end, P12_Lossaversion, P13_Lossaversion_result, P14_Demographic, P15_Comment, P16_End_game ]