from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import time import random class ACTION_voucher_instructions(Page): def is_displayed(self): return self.round_number == 1 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") 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 = 'benchmark2b/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("in start_of_round_wait after_all_players_arrive") pass class donation_decision(Page): form_model = 'player' form_fields = ['is_donor'] 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 { 'cost': self.participant.vars['donation_cost'], } class ResultsWaitPage(WaitPage): template_name = 'benchmark2b/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): 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 = 'benchmark2b/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 = 'benchmark2b/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): 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): title_text = 'Please wait' body_text = 'Calculating final earnings for Game II...' 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) 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['set_final_payoff'] = 1 else: print("Tried to set_final_payoff twice in benchmark2b.") 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): final_payoff = self.participant.vars['picked_p1'] + self.participant.vars['picked_p2'] \ + self.participant.vars['sec_picked_p1'] + self.participant.vars['sec_picked_p2'] 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'], 'sec_picked_r1': self.participant.vars['sec_picked_r1'], 'sec_picked_r2': self.participant.vars['sec_picked_r2'], 'sec_picked_p1': self.participant.vars['sec_picked_p1'], 'sec_picked_p2': self.participant.vars['sec_picked_p2'], 'final_payoff': final_payoff, } def before_next_page(self): self.session.vars['diced'] = 100 class after_timeout_page(Page): def is_displayed(self): return self.session.vars['diced'] == 100 def get_timeout_seconds(self): return -1 page_sequence = [ ACTION_voucher_instructions, 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, ]