from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from .models import Constants import random class Qual_Player1(Page): timeout_seconds = 600 form_model = 'player' form_fields = ['vh', 'vl_guess_1', 'ph_guess_1', 'pl_guess_1'] def before_next_page(self): if self.timeout_happened: self.player.vh = round(random.uniform(Constants.min_quality_high,Constants.max_quality_high),2) def is_displayed(self): return self.player.id_in_group % 2 == 0 class Qual_Player2(Page): timeout_seconds = 600 form_model = 'player' form_fields = ['vl','vh_guess_1', '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) def is_displayed(self): return self.player.id_in_group % 2 != 0 class WaitForPlayer(WaitPage): pass class Price(Page): timeout_seconds = 600 form_model = 'player' form_fields = ['ph', 'pl'] def vars_for_template(self): play_high = self.group.get_player_by_role("Leader") play_low = self.group.get_player_by_role("Follower") return dict( qual_player1 = play_high.vh, qual_player2 = play_low.vl, ) def before_next_page(self): if self.timeout_happened: self.player.ph = 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): self.group.set_payoffs() class Results(Page): def vars_for_template(self): play_high = self.group.get_player_by_role("Leader") play_low = self.group.get_player_by_role("Follower") # These are the demand formulas Dh = (Constants.theta_upper - (play_high.ph-play_low.pl)/(Constants.kh*play_high.vh - Constants.kl*play_low.vl))/(Constants.theta_upper - Constants.theta_lower) Dl = ((play_high.ph-play_low.pl)/(Constants.kh*play_high.vh - Constants.kl*play_low.vl) - (play_low.pl)/(Constants.kl*play_low.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 Dh Dl = Dl elif Dl < 0: # Overwrite Dh Dl = 0 else: Dl = Dl print("#### Calculation display ####") print(Dh) print(Dl) if ((abs(play_high.vl_guess_1 - play_low.vl) < Constants.close_q_low ) and (abs(play_high.pl - play_low.pl) < Constants.close_p_low)): payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) + Constants.pay + Constants.pay - (Constants.c*play_high.vh*Dh) guess_qual1 = 'guessed' guess_price1 = 'guessed' elif ((abs(play_high.vl_guess_1 - play_low.vl) < Constants.close_q_low) and (abs(play_high.pl - play_low.pl) > Constants.close_p_low)): payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) + Constants.pay - (Constants.c*play_high.vh*Dh) guess_qual1 = 'guessed' guess_price1 = 'did not guess' elif ((abs(play_high.vl_guess_1 - play_low.vl) > Constants.close_q_low) and (abs(play_high.pl - play_low.pl) < Constants.close_p_low)): payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) + Constants.pay - (Constants.c*play_high.vh*Dh) guess_qual1 = 'did not guess' guess_price1 = 'guessed' else: payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) - (Constants.c*play_high.vh*Dh) guess_qual1 = 'did not guess' guess_price1 = 'did not guess' if ((abs(play_low.vh_guess_1 - play_high.vh) < Constants.close_q_high) and (abs(play_low.ph - play_high.ph) < Constants.close_p_high)): payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) + Constants.pay + Constants.pay - (Constants.c*play_low.vl*Dl) guess_qual2 = 'guessed' guess_price2 = 'guessed' elif ((abs(play_low.vh_guess_1 - play_high.vh) < Constants.close_q_high) and (abs(play_low.ph - play_high.ph) > Constants.close_p_high)): payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) + Constants.pay - (Constants.c*play_low.vl*Dl) guess_qual2 = 'guessed' guess_price2 = 'did not guess' elif ((abs(play_low.vh_guess_1 - play_high.vh) > Constants.close_q_high) and (abs(play_low.ph - play_high.ph) < Constants.close_p_high)): payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) + Constants.pay - (Constants.c*play_low.vl*Dl) guess_qual2 = 'did not guess' guess_price2 = 'guessed' else: payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) - (Constants.c*play_low.vl*Dl) guess_qual2 = 'did not guess' guess_price2 = 'did not guess' # if play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha > 0.0: # payoff_h = (play_high.ph * Dh - (play_high.vh ** Constants.alpha) / Constants.alpha) # else: # payoff_h = 0 # if play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha > 0: # payoff_l = Constants.r * (play_low.pl * Dl - (play_low.vl ** Constants.alpha) / Constants.alpha) # else: # payoff_l = 0 print(payoff_h) print(payoff_l) return dict( payoff_high = round(payoff_h,2), payoff_low = round(payoff_l,2), qual_player1 = play_high.vh, qual_player2 = play_low.vl, price_player1 = play_high.ph, price_player2=play_low.pl, guessed_quality1 = guess_qual1, guessed_quality2=guess_qual2, guessed_price1 = guess_price1, guessed_price2=guess_price2, ) page_sequence = [ Qual_Player1, Qual_Player2, WaitForPlayer, Price, ResultsWaitPage, Results, ]