from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import time #import requests class Introduction(Page): def is_displayed(self): return self.round_number==1 form_model = 'player' #form_fields = ['codice_lab'] #form_model = 'player' #form_fields = ['stock_a_buy', 'stock_b_buy', 'stock_c_buy'] #def stock_a_buy_error_message(self, value): # if value!='Buy': # return 'You must buy one unit of EACH stock before beginning the game!' #def stock_b_buy_error_message(self, value): # if value!='Buy': # return 'You must buy one unit of EACH stock before beginning the game!' #def stock_c_buy_error_message(self, value): # if value!='Buy': # return 'You must buy one unit of EACH stock before beginning the game!' #def before_next_page(self): # self.player.stock_val() def before_next_page(self): self.player.buy_in_r1() self.player.stock_val() self.participant.vars['expiry'] = time.time() + Constants.eight_hours*(Constants.num_rounds+2) # this is 5 seconds over all periods + 2 periods of change in treatment pass class Alert(Page): def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) def is_displayed(self): return self.round_number==Constants.start_second_phase def before_next_page(self): self.player.back_to_start() pass class CurrentStatusR1(Page): #timeout_seconds = Constants.one_minute def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) def is_displayed(self): return self.round_number==1 def vars_for_template(self): return{ 'cash': self.player.cash, 'q_a':self.player.stock_a, 'q_b':self.player.stock_b, 'q_c':self.player.stock_c, 'value_a': self.player.stock_a * self.player.price_a, 'value_b': self.player.stock_b * self.player.price_b, 'value_c': self.player.stock_c * self.player.price_c, 'price_ba': self.player.price_bought_a, 'price_bb': self.player.price_bought_b, 'price_bc': self.player.price_bought_c, 'total': self.player.stock_a * self.player.price_a+self.player.stock_b * self.player.price_b+ self.player.stock_c * self.player.price_c+self.player.cash } # def before_next_page(self): # # user has 5 minutes to complete as many pages as possible # self.participant.vars['expiry'] = time.time() + Constants.two_hours pass class CurrentStatusMidR(Page): def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) # def get_timeout_seconds(self): # return self.participant.vars['expiry'] - time.time() + Constants.two_hours # def is_displayed(self): # return self.round_number in Constants.mid_r def vars_for_template(self): return { 'cash': self.player.cash, 'q_a': self.player.stock_a, 'q_b': self.player.stock_b, 'q_c': self.player.stock_c, 'value_a': self.player.stock_a * self.player.price_a, 'value_b': self.player.stock_b * self.player.price_b, 'value_c': self.player.stock_c * self.player.price_c, 'price_ba': self.player.price_bought_a, 'price_bb': self.player.price_bought_b, 'price_bc': self.player.price_bought_c, 'total': self.player.stock_a * self.player.price_a + self.player.stock_b * self.player.price_b + self.player.stock_c * self.player.price_c + self.player.cash } # def before_next_page(self): # # user has 5 minutes to complete as many pages as possible # self.participant.vars['expiry'] = time.time() + Constants.two_hours pass class CurrentStatusLR(Page): # timeout_seconds = Constants.one_minute def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) # def get_timeout_seconds(self): # return self.participant.vars['expiry'] - time.time() + Constants.fourteen_hours # def is_displayed(self): # return self.get_timeout_seconds()>1 and self.round_number in Constants.last_r def vars_for_template(self): return { 'cash': self.player.cash, 'q_a': self.player.stock_a, 'q_b': self.player.stock_b, 'q_c': self.player.stock_c, 'value_a': self.player.stock_a * self.player.price_a, 'value_b': self.player.stock_b * self.player.price_b, 'value_c': self.player.stock_c * self.player.price_c, 'price_ba': self.player.price_bought_a, 'price_bb': self.player.price_bought_b, 'price_bc': self.player.price_bought_c, 'total': self.player.stock_a * self.player.price_a + self.player.stock_b * self.player.price_b + self.player.stock_c * self.player.price_c + self.player.cash } # def before_next_page(self): # # user has 5 minutes to complete as many pages as possible # self.participant.vars['expiry'] = time.time() + Constants.two_hours pass class Action(Page): #do they have to know the price of the stock they are buying or selling? def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) def is_displayed(self): return self.round_number>1 and self.round_number not in Constants.blocked_r and (self.round_number not in Constants.blocked_last_r or self.player.in_round(Constants.start_third_phase).restrict_last_part!='Con algoritmo') and self.round_number!=Constants.start_third_phase and self.round_number!=Constants.start_second_phase form_model = 'player' def get_form_fields(self): if self.player.in_round(self.round_number-1).stock_a==0 and self.player.in_round(self.round_number-1).stock_b==0 and self.player.in_round(self.round_number-1).stock_c==0: return ['stock_a_buy','stock_b_buy','stock_c_buy'] elif self.player.in_round(self.round_number-1).stock_a==0 and self.player.in_round(self.round_number-1).stock_b==0 and self.player.in_round(self.round_number-1).stock_c==1: return ['stock_a_buy','stock_b_buy','stock_c_sell'] elif self.player.in_round(self.round_number-1).stock_a==0 and self.player.in_round(self.round_number-1).stock_b==1 and self.player.in_round(self.round_number-1).stock_c==0: return ['stock_a_buy','stock_b_sell','stock_c_buy'] elif self.player.in_round(self.round_number-1).stock_a==0 and self.player.in_round(self.round_number-1).stock_b==1 and self.player.in_round(self.round_number-1).stock_c==1: return ['stock_a_buy','stock_b_sell','stock_c_sell'] elif self.player.in_round(self.round_number-1).stock_a==1 and self.player.in_round(self.round_number-1).stock_b==0 and self.player.in_round(self.round_number-1).stock_c==0: return ['stock_a_sell','stock_b_buy','stock_c_buy'] elif self.player.in_round(self.round_number-1).stock_a==1 and self.player.in_round(self.round_number-1).stock_b==0 and self.player.in_round(self.round_number-1).stock_c==1: return ['stock_a_sell','stock_b_buy','stock_c_sell'] elif self.player.in_round(self.round_number-1).stock_a==1 and self.player.in_round(self.round_number-1).stock_b==1 and self.player.in_round(self.round_number-1).stock_c==0: return ['stock_a_sell','stock_b_sell','stock_c_buy'] else: return ['stock_a_sell','stock_b_sell','stock_c_sell'] def vars_for_template(self): return { 'new': self.round_number in [Constants.start_second_phase, Constants.start_third_phase], 'price_ba': self.player.in_round(self.round_number - 1).price_bought_a, 'price_bb': self.player.in_round(self.round_number - 1).price_bought_b, 'price_bc': self.player.in_round(self.round_number - 1).price_bought_c, 'price_ba_n': self.player.price_bought_a, 'price_bb_n': self.player.price_bought_b, 'price_bc_n': self.player.price_bought_c, # 'a_up': self.player.stock_update == 'a', # 'b_up': self.player.stock_update == 'b', # 'c_up': self.player.stock_update == 'c', 'price_change_a': self.player.price_a - self.player.in_round(self.round_number - 1).price_a, 'price_change_b': self.player.price_b - self.player.in_round(self.round_number - 1).price_b, 'price_change_c': self.player.price_c - self.player.in_round(self.round_number - 1).price_c, 'price_change_a_n': self.player.price_a - 100, 'price_change_b_n': self.player.price_b - 100, 'price_change_c_n': self.player.price_c - 100, 'round_n': self.round_number - 1, 'cash': self.player.in_round(self.round_number - 1).cash, 'cash_n': self.player.cash, 'q_a': self.player.in_round(self.round_number - 1).stock_a, 'q_b': self.player.in_round(self.round_number - 1).stock_b, 'q_c': self.player.in_round(self.round_number - 1).stock_c, 'q_a_n': self.player.stock_a, 'q_b_n': self.player.stock_b, 'q_c_n': self.player.stock_c, 'value_a': self.player.in_round(self.round_number - 1).stock_a * self.player.price_a, 'value_b': self.player.in_round(self.round_number - 1).stock_b * self.player.price_b, 'value_c': self.player.in_round(self.round_number - 1).stock_c * self.player.price_c, 'value_a_n': self.player.stock_a * self.player.price_a, 'value_b_n': self.player.stock_b * self.player.price_b, 'value_c_n': self.player.stock_c * self.player.price_c, 'total': self.player.in_round(self.round_number - 1).stock_a * self.player.price_a + self.player.in_round( self.round_number - 1).stock_b * self.player.price_b + self.player.in_round( self.round_number - 1).stock_c * self.player.price_c + self.player.in_round(self.round_number - 1).cash, 'total_n': self.player.stock_a * self.player.price_a + self.player.stock_b * self.player.price_b + self.player.stock_c * self.player.price_c + self.player.cash } def before_next_page(self): self.player.stock_val() self.player.set_payoff() # if self.timeout_happened: # id = self.participant.code # participant code # title = "Attenzione!" # change this # description = "Il tempo per scambiare lo stock รจ scaduto! Apri AppLab per giocare al prossimo round" # change this # url = "https://us-central1-app-lab-research.cloudfunctions.net/sendNotificationDueToDelayedActivity" # # payload = "experimentProviderUid=" + str(id) + "&title=" + str(title) + "&description=" + str(description) # "" # headers = { # 'content-type': "application/x-www-form-urlencoded", # 'auth': "136326cf0a73ae10744b1a97de3c2be042134c4e", # secret key # } # # response = requests.request("POST", url, data=payload, headers=headers) # # print(response.text) pass class ActionBlocked(Page): #do they have to know the price of the stock they are buying or selling? def get_timeout_seconds(self): return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) def is_displayed(self): return self.round_number in Constants.blocked_r or (self.round_number in Constants.blocked_last_r and self.player.in_round(Constants.start_third_phase).restrict_last_part=='Con algoritmo') def vars_for_template(self): return { 'new': self.round_number in [Constants.start_second_phase, Constants.start_third_phase], 'price_ba': self.player.in_round(self.round_number - 1).price_bought_a, 'price_bb': self.player.in_round(self.round_number - 1).price_bought_b, 'price_bc': self.player.in_round(self.round_number - 1).price_bought_c, 'price_ba_n': self.player.price_bought_a, 'price_bb_n': self.player.price_bought_b, 'price_bc_n': self.player.price_bought_c, # 'a_up': self.player.stock_update == 'a', # 'b_up': self.player.stock_update == 'b', # 'c_up': self.player.stock_update == 'c', 'price_change_a': self.player.price_a - self.player.in_round(self.round_number - 1).price_a, 'price_change_b': self.player.price_b - self.player.in_round(self.round_number - 1).price_b, 'price_change_c': self.player.price_c - self.player.in_round(self.round_number - 1).price_c, 'price_change_a_n': self.player.price_a - 100, 'price_change_b_n': self.player.price_b - 100, 'price_change_c_n': self.player.price_c - 100, 'round_n': self.round_number - 1, 'cash': self.player.in_round(self.round_number - 1).cash, 'cash_n': self.player.cash, 'q_a': self.player.in_round(self.round_number - 1).stock_a, 'q_b': self.player.in_round(self.round_number - 1).stock_b, 'q_c': self.player.in_round(self.round_number - 1).stock_c, 'q_a_n': self.player.stock_a, 'q_b_n': self.player.stock_b, 'q_c_n': self.player.stock_c, 'value_a': self.player.in_round(self.round_number - 1).stock_a * self.player.price_a, 'value_b': self.player.in_round(self.round_number - 1).stock_b * self.player.price_b, 'value_c': self.player.in_round(self.round_number - 1).stock_c * self.player.price_c, 'value_a_n': self.player.stock_a * self.player.price_a, 'value_b_n': self.player.stock_b * self.player.price_b, 'value_c_n': self.player.stock_c * self.player.price_c, 'total': self.player.in_round(self.round_number - 1).stock_a * self.player.price_a + self.player.in_round( self.round_number - 1).stock_b * self.player.price_b + self.player.in_round( self.round_number - 1).stock_c * self.player.price_c + self.player.in_round(self.round_number - 1).cash, 'total_n': self.player.stock_a * self.player.price_a + self.player.stock_b * self.player.price_b + self.player.stock_c * self.player.price_c + self.player.cash, } def before_next_page(self): self.player.robo_decision() self.player.stock_val() self.player.set_payoff() pass class EndBlock(Page): # def get_timeout_seconds(self): # return self.participant.vars['expiry'] - time.time() - Constants.eight_hours*(Constants.num_rounds-self.round_number+1*(self.round_number<=Constants.start_second_phase-1)+1*(self.round_number<=Constants.start_third_phase-1)) def is_displayed(self): return self.round_number==Constants.start_third_phase form_model = 'player' form_fields = ['restrict_last_part'] def before_next_page(self): self.player.back_to_start() pass #class CalculatingPayoff(Page): # def before_next_page(self): # self.player.set_payoff() # pass class Results(Page): #timeout_seconds = Constants.two_minutes def is_displayed(self): return self.round_number==Constants.num_rounds def vars_for_template(self): return{ 'cash': self.player.cash, 'q_a':self.player.stock_a, 'q_b':self.player.stock_b, 'q_c':self.player.stock_c, 'value_a': self.player.stock_a * self.player.price_a, 'value_b': self.player.stock_b * self.player.price_b, 'value_c': self.player.stock_c * self.player.price_c, 'total': self.player.stock_a * self.player.price_a+self.player.stock_b * self.player.price_b+ self.player.stock_c * self.player.price_c+self.player.cash, 'FH': self.player.payoff_part==1, 'SH': self.player.payoff_part == 2, 'price_a_sh': self.player.participant.vars['price_a_sh'], 'price_b_sh': self.player.participant.vars['price_b_sh'], 'price_c_sh': self.player.participant.vars['price_c_sh'], 'cash_sh': self.player.participant.vars['cash_sh'], 'q_a_sh': self.player.participant.vars['q_a_sh'], 'q_b_sh': self.player.participant.vars['q_b_sh'], 'q_c_sh': self.player.participant.vars['q_c_sh'], 'value_a_sh': self.player.participant.vars['value_a_sh'], 'value_b_sh': self.player.participant.vars['value_b_sh'], 'value_c_sh': self.player.participant.vars['value_c_sh'], 'price_a_fh': self.player.participant.vars['price_a_fh'], 'price_b_fh': self.player.participant.vars['price_b_fh'], 'price_c_fh': self.player.participant.vars['price_c_fh'], 'cash_fh': self.player.participant.vars['cash_fh'], 'q_a_fh': self.player.participant.vars['q_a_fh'], 'q_b_fh': self.player.participant.vars['q_b_fh'], 'q_c_fh': self.player.participant.vars['q_c_fh'], 'value_a_fh': self.player.participant.vars['value_a_fh'], 'value_b_fh': self.player.participant.vars['value_b_fh'], 'value_c_fh': self.player.participant.vars['value_c_fh'] } pass page_sequence = [ Introduction, Alert, EndBlock, ActionBlocked, Action, CurrentStatusR1, CurrentStatusMidR, #CurrentStatusLR, #Update, #CalculatingPayoff, Results ]