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 = random.choice([19, 23.5, 24.5, 25, 37]) player.vl_guess_1 = random.choice([3.5, 4, 4.5, 5, 5.5]) player.ph_guess_1 = random.choice([598, 913, 1037, 1155, 2589]) player.pl_guess_1 = random.choice([54, 80, 101, 128, 129]) 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 = 120 form_model = 'player' form_fields = ['vl', 'ph_guess_2', 'pl_guess_2'] def before_next_page(self): if self.timeout_happened: self.player.vl = random.choice([3.5, 4, 4.5, 5, 5.5]) self.player.ph_guess_2 = random.choice([598, 913, 1037, 1155, 2589]) self.player.pl_guess_2 = random.choice([54, 80, 101, 128, 129]) # 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 = 120 form_model = 'player' form_fields = ['ph_guess_3', 'pl'] def before_next_page(self): if self.timeout_happened: self.player.ph_guess_3 = random.choice([598, 913, 1037, 1155, 2589]) self.player.pl = random.choice([54, 80, 101, 128, 129]) class ResultsWaitPage(WaitPage): pass # # def after_all_players_arrive(self): # pass class Results(Page): def vars_for_template(self): play = self.player # These are the demand formulas Dh = (100 - (play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl * play.vl))/(100 - 0) Dl = ((play.ph_guess_1-play.pl)/(Constants.kh*play.vh - Constants.kl * play.vl) - (play.pl)/(Constants.kl * play.vl))/(100-0) if Dh > 1: # Overwrite Dh Dh=1 elif Dh<0: # Overwrite Dh Dh=0 else: Dh=Dh if Dl>1: # Overwrite Dl Dl=1 elif Dl<0: # Overwrite Dl Dl=0 else: Dl=Dl print("#### Calculation display ####") print(Dh) print(Dl) print(play.ph_guess_1) print((play.vh ** Constants.alpha) / Constants.alpha) if play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha > 0.0: payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) else: payoff_h = (play.ph_guess_1 * Dh - (play.vh ** Constants.alpha) / Constants.alpha) if play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha > 0: payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) else: payoff_l = Constants.r * (play.pl * Dl - (play.vl ** Constants.alpha) / Constants.alpha) print(payoff_h) print(payoff_l) return dict( payoff_high = round(payoff_h,2), payoff_low = round(payoff_l,2), ) page_sequence = [ Qual_Player1, WaitBeforeStart, Qual_Player2, Price, # ResultsWaitPage, Results, ]