from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import time import random class grouping_and_cost(Page): def is_displayed(self): return self.round_number == 1 def vars_for_template(self): return { 'cost': self.participant.vars['donation_cost'], 'group_id': self.participant.vars['group_id'], } class start_of_game_wait(WaitPage): def is_displayed(self): return self.round_number == 1 def after_all_players_arrive(self): print("set expiry time", time.asctime()) self.session.vars['expiry'] = time.time() + Constants.minutes_for_endofround * 60 self.session.vars['diced'] = -1 self.session.vars['set_final_payoff'] = 0 for p in self.group.get_players(): p.participant.vars['diced'] = -1 class start_of_round(Page): def is_displayed(self): self.player.setup() return self.participant.vars['group_id'] == (self.round_number % 3) \ and self.round_number <= self.session.vars['num_rounds'] class start_of_round_wait(WaitPage): template_name = 'designated/WaitPage_template.html' def is_displayed(self): #return self.participant.vars['group_id'] == (self.round_number % 3) \ return self.round_number <= self.session.vars['num_rounds'] def after_all_players_arrive(self): print("start_of_round_wait for round:", self.round_number) if self.round_number > 1: self.group.check_inheritance_for_players(self.round_number % 3) # for Designated treatment class donation_decision(Page): form_model = 'player' form_fields = ['designate_junior'] # for Designated treatment def is_displayed(self): return self.participant.vars['group_id'] == (self.round_number % 3) \ and self.round_number <= self.session.vars['num_rounds'] \ and self.session.vars['diced'] != 0 def vars_for_template(self): return { 'first_round': self.round_number == 1, 'inherit_voucher': self.player.inherit_voucher, 'cost': self.participant.vars['donation_cost'], } def before_next_page(self): if self.player.designate_junior == 0: self.player.is_donor = False else: self.player.is_donor = True def error_message(self, values): if values['designate_junior'] is None: return "Please select one of the three options before proceed." class ResultsWaitPage(WaitPage): template_name = 'designated/WaitPage_template.html' def is_displayed(self): #return self.participant.vars['group_id'] == (self.round_number % 3) \ return self.round_number <= self.session.vars['num_rounds'] \ and self.session.vars['diced'] != 0 def after_all_players_arrive(self): print("in ResultsWaitPage after_all_players_arrive") grpid = self.round_number % 3 self.group.draw_health_outcome(grpid) self.group.donation_allocation(grpid) self.group.set_round_payoff(grpid) if self.round_number > 1: self.group.set_senior_payoff(grpid) class SeniorsWaitPage(WaitPage): template_name = 'designated/WaitPage_template.html' def is_displayed(self): # both Juniors and Seniors pass this page. Ready to proceed after finishing set_senior_payoff in ResultsWaitPage return self.round_number != 1 \ and (self.participant.vars['group_id'] == ((self.round_number - 1) % 3) or self.participant.vars['group_id'] == (self.round_number % 3)) \ and self.round_number <= self.session.vars['num_rounds'] \ and self.session.vars['diced'] != 0 def after_all_players_arrive(self): print("in SeniorsWaitPage after_all_players_arrive") pass class results_display_forSeniors(Page): def is_displayed(self): return self.round_number != 1 and self.participant.vars['group_id'] == ((self.round_number - 1) % 3) \ and self.round_number <= self.session.vars['num_rounds'] \ and self.session.vars['diced'] != 0 def vars_for_template(self): my_player = self.player.in_round(self.round_number-1) return { 'junior_payoff': my_player.junior_payoff, 'own_payoff': my_player.own_payoff, 'effective_earning': my_player.payoff, } class results_display(Page): def is_displayed(self): return self.participant.vars['group_id'] == (self.round_number % 3) \ and self.round_number <= self.session.vars['num_rounds'] \ and self.session.vars['diced'] != 0 def vars_for_template(self): return { 'is_donor': self.player.is_donor, 'B_health': self.player.B_health, 'is_receiver': self.player.is_receiver, 'payoff': self.player.payoff, } #def get_timeout_seconds(self): # return self.group.get_timeout_seconds() class end_of_round_wait(WaitPage): template_name = 'designated/WaitPage_template.html' def is_displayed(self): return self.round_number <= self.session.vars['num_rounds'] #and self.session.vars['diced'] != 0 def after_all_players_arrive(self): #pass #""" if self.session.vars['expiry'] - time.time() < Constants.minutes_for_dice*60 \ and self.session.vars['diced'] == -1: self.session.vars['diced'] = 0 print("ready to dice, after round ", self.round_number) elif self.session.vars['diced'] == 2: self.session.vars['diced'] = 1 for p in self.group.get_players(): p.participant.vars['diced'] = self.session.vars['diced'] class dice_WaitPage(WaitPage): def is_displayed(self): return self.participant.vars['diced'] == 0 def after_all_players_arrive(self): players = self.group.get_players() p = players.pop() if p.participant.vars['diced'] == 0: dice = [1, 2, 3, 4, 5, 6] draw = random.sample(dice, 1) if draw[0] == 1: self.session.vars['num_rounds'] = self.round_number self.session.vars['diced'] = 1 else: self.session.vars['num_rounds'] = self.round_number + 1 self.session.vars['diced'] = 2 print("dice result: ", draw[0]) print("So, num_rounds = ", self.session.vars['num_rounds']) self.session.vars['draw'] = draw[0] #p.participant.vars['diced'] = self.session.vars['diced'] #for pp in players: # pp.participant.vars['diced'] = self.session.vars['diced'] class dice_final_round(Page): def is_displayed(self): return self.participant.vars['diced'] == 0 def vars_for_template(self): return { 'draw': self.session.vars['draw'], 'passed_minutes': Constants.minutes_for_endofround - Constants.minutes_for_dice } def before_next_page(self): self.participant.vars['diced'] = self.session.vars['diced'] #""" class final_results_wait(WaitPage): template_name = 'designated/WaitPage_template.html' def is_displayed(self): return self.session.vars['diced'] == 1 or (self.round_number == Constants.num_rounds and self.session.vars['set_final_payoff'] == 0) #return self.round_number == Constants.num_rounds def after_all_players_arrive(self): if self.session.vars['diced'] == 1 and self.session.vars['set_final_payoff'] == 0: self.group.set_final_payoff() self.session.vars['diced'] = 100 self.session.vars['set_final_payoff'] = 1 else: print("Tried to set_final_payoff twice in designated.") """ class final_results(Page): def is_displayed(self): return self.session.vars['diced'] == 1 #return self.round_number == Constants.num_rounds def vars_for_template(self): return { 'picked_r1': self.participant.vars['picked_r1'], 'picked_r2': self.participant.vars['picked_r2'], 'picked_p1': self.participant.vars['picked_p1'], 'picked_p2': self.participant.vars['picked_p2'], 'final_payoff': self.participant.vars['game2_s1_payoff'], } """ class after_timeout_page(Page): def is_displayed(self): return self.session.vars['diced'] == 100 def get_timeout_seconds(self): return -1 page_sequence = [ grouping_and_cost, start_of_game_wait, start_of_round, start_of_round_wait, donation_decision, ResultsWaitPage, SeniorsWaitPage, results_display, results_display_forSeniors, end_of_round_wait, dice_WaitPage, dice_final_round, final_results_wait, #final_results, after_timeout_page, ]