from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Qual_Player1(Page): def before_next_page(self): player = self.player player.vh = round(random.uniform(Constants.min_quality_high,Constants.max_quality_high),2) player.vl_guess_1 = round(random.uniform(Constants.min_quality_low,Constants.max_quality_low),2) player.ph_guess_1 = round(random.uniform(Constants.min_price_high, Constants.max_price_high),2) player.pl_guess_1 = round(random.uniform(Constants.min_price_low, Constants.max_price_low),2) print(player.vh) print(player.vl_guess_1) print(player.ph_guess_1) print(player.pl_guess_1) class WaitBeforeStart(WaitPage): pass class Qual_Player2(Page): timeout_seconds = 600 form_model = 'player' form_fields = ['vl', 'vh_guess_2', 'ph_guess_2', 'pl_guess_2'] def before_next_page(self): if self.timeout_happened: self.player.vl = round(random.uniform(Constants.min_quality_low, Constants.max_quality_low),2) self.player.ph_guess_2 = round(random.uniform(Constants.min_price_high, Constants.max_price_high),2) self.player.pl_guess_2 = round(random.uniform(Constants.min_price_low, Constants.max_price_low),2) # class WaitForP1(WaitPage): # pass # # class Qual_Player2(Page): # # #timeout_seconds = 360 # # form_model = 'group' # form_fields = ['vl', 'ph_guess_2', 'pl_guess_2'] # # # def is_displayed(self): # return self.player.id_in_group == 2 # # # class WaitforP2(WaitPage): # pass class Price(Page): timeout_seconds = 600 form_model = 'player' form_fields = ['ph_guess_3', 'pl'] def before_next_page(self): if self.timeout_happened: self.player.ph_guess_3 = round(random.uniform(Constants.min_price_high,Constants.max_price_high),2) self.player.pl = round(random.uniform(Constants.min_price_low,Constants.max_price_low),2) class ResultsWaitPage(WaitPage): def after_all_players_arrive(self): pass class Results(Page): def vars_for_template(self): play = self.player # These are the demand formulas # These are the demand formulas Dh = (Constants.theta_upper - (play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl*play.vl))/(Constants.theta_upper - Constants.theta_lower) Dl = ((play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl*play.vl) - (play.pl)/(Constants.kl*play.vl))/(Constants.theta_upper - Constants.theta_lower) if Dh > 1: # Overwrite Dh Dh=Dh elif Dh<0: # Overwrite Dh Dh=0 else: Dh=Dh if Dl>1: # Overwrite Dl Dl=Dl elif Dl<0: # Overwrite Dl Dl=0 else: Dl=Dl print("#### Calculation display ####") print(Dh) print((Constants.theta_upper - (play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl*play.vl))/(Constants.theta_upper - Constants.theta_lower)) print(Dl) print(((play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl*play.vl) - (play.pl)/(Constants.kl*play.vl))/(Constants.theta_upper - Constants.theta_lower)) print(play.vh) print(play.vl) print(play.ph_guess_1) print(play.pl) #print((play.vh ** Constants.alpha) / Constants.alpha) if ((abs(play.vl_guess_1 - play.vl) < Constants.close_q_low) and (abs(play.pl_guess_2 - play.pl) < Constants.close_p_low)): payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) + Constants.pay + Constants.pay - Constants.c*play.vh*Dh guess_qual1 = 'guessed' guess_price1 = 'guessed' elif ((abs(play.vl_guess_1 - play.vl) < Constants.close_q_low) and (abs(play.pl_guess_2 - play.pl) > Constants.close_p_low)): payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) + Constants.pay - Constants.c*play.vh*Dh guess_qual1 = 'guessed' guess_price1 = 'did not guess' elif ((abs(play.vl_guess_1 - play.vl) > Constants.close_q_low) and (abs(play.pl_guess_2 - play.pl) < Constants.close_p_low)): payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) + Constants.pay - Constants.c*play.vh*Dh guess_qual1 = 'did not guess' guess_price1 = 'guessed' else: payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) - Constants.c*play.vh*Dh guess_qual1 = 'did not guess' guess_price1 = 'did not guess' if ((abs(play.vh_guess_2 - play.vh) < Constants.close_q_high) and (abs(play.ph_guess_1 - play.ph_guess_3) < Constants.close_q_high)): payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) + Constants.pay + Constants.pay - Constants.c*play.vl*Dl guess_qual2 = 'guessed' guess_price2 = 'guessed' elif ((abs(play.vh_guess_2 - play.vh) < Constants.close_q_high) and (abs(play.ph_guess_1 - play.ph_guess_3) > Constants.close_q_high)): payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) + Constants.pay - Constants.c*play.vl*Dl guess_qual2 = 'guessed' guess_price2 = 'did not guess' elif ((abs(play.vh_guess_2 - play.vh) > Constants.close_q_high) and (abs(play.ph_guess_1 - play.ph_guess_3) < Constants.close_q_high)): payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) + Constants.pay - Constants.c*play.vl*Dl guess_qual2 = 'did not guess' guess_price2 = 'guessed' else: payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) - Constants.c*play.vl*Dl guess_qual2 = 'did not guess' guess_price2 = 'did not guess' print(payoff_h) print(payoff_l) return dict( payoff_high = round(payoff_h,2), payoff_low = round(payoff_l,2), qual_player1 = play.vh, qual_player2 = play.vl, price_player1 = play.ph_guess_1, price_player2=play.pl, guessed_quality1 = guess_qual1, guessed_quality2=guess_qual2, guessed_price1 = guess_price1, guessed_price2=guess_price2, ) page_sequence = [ Qual_Player1, WaitBeforeStart, Qual_Player2, Price, ResultsWaitPage, Results, ]