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 = 80 form_model = 'group' form_fields = ['vh', 'vl_guess_1', 'ph_guess_1', 'pl_guess_1'] def before_next_page(self): if self.timeout_happened: self.group.vh = random.choice([19, 23.5, 24.5, 25, 37]) def is_displayed(self): return self.player.id_in_group % 2 == 0 class WaitForP1(WaitPage): pass class Qual_Player2(Page): timeout_seconds = 80 form_model = 'group' form_fields = ['vl', 'ph_guess_2', 'pl_guess_2'] def before_next_page(self): if self.timeout_happened: self.group.vl = random.choice([3.5, 4, 4.5, 5, 5.5]) def is_displayed(self): return self.player.id_in_group % 2 != 0 class WaitforP2(WaitPage): pass class Price(Page): timeout_seconds = 80 form_model = 'player' form_fields = ['ph', 'pl'] def before_next_page(self): if self.timeout_happened: self.player.ph = random.choice([598, 913, 1037, 1155, 2589]) self.player.pl = random.choice([54, 80, 101, 128, 129]) 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") print("#### Choices ####") print(type(self.group.vh)) print(type(self.group.vl)) print(type(self.player.ph)) print(type(self.player.pl)) print(type(Constants.kh)) # These are the demand formulas Dh = (100 - (play_high.ph-play_low.pl)/(Constants.kh*self.group.vh - Constants.kl*self.group.vl))/(100 - 0) Dl = ((play_high.ph-play_low.pl)/(Constants.kh*self.group.vh - Constants.kl*self.group.vl) - (play_low.pl)/(Constants.kl*self.group.vl))/(100-0) if Dh > 1: # Overwrite Dh Dh = 1 elif Dh < 0: # Overwrite Dh Dh = 0 else: Dh = Dh if Dl > 1: # Overwrite Dh Dl = 1 elif Dl < 0: # Overwrite Dh Dl = 0 else: Dl = Dl print("#### Calculation display ####") print(Dh) print(Dl) payoff_h = (play_high.ph * Dh - (self.group.vh ** Constants.alpha) / Constants.alpha) payoff_l = Constants.r * (play_low.pl * Dl - (self.group.vl ** Constants.alpha) / Constants.alpha) # if play_high.ph * Dh - (self.group.vh ** Constants.alpha) / Constants.alpha > 0.0: # payoff_h = (play_high.ph * Dh - (self.group.vh ** Constants.alpha) / Constants.alpha) # else: # payoff_h = 0 # if play_low.pl * Dl - (self.group.vl ** Constants.alpha) / Constants.alpha > 0: # payoff_l = Constants.r * (play_low.pl * Dl - (self.group.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), price_player1 = play_high.ph, price_player2=play_low.pl, ) page_sequence = [ Qual_Player1, WaitForP1, Qual_Player2, WaitforP2, Price, ResultsWaitPage, Results, ]