from otree.api import Currency as c, currency_range from . import models from ._builtin import Page, WaitPage from .models import Constants import numpy as np class TreatmentPage(Page): def is_displayed(self): return self.subsession.round_number==1 or self.subsession.round_number==Constants.num_rounds/2+1 def vars_for_template(self): if self.subsession.treatment==0: treatment='Winning Bids Are Revealed!' else: treatment='Winning Bids Are Not Revealed!' return{ 'treatment':treatment, } class BasePage(Page): def i(self): return 1 def is_displayed(self): return self.i()<=Constants.n_periods form_model = models.Player def get_form_fields(self): return ['bid'+str(self.i())] def vars_for_template(self): if self.i()==1: self.participant.vars['bids']=[] self.participant.vars['profits']=[] self.participant.vars['results']=[] self.participant.vars['opponent_bids']=[] self.participant.vars['winning_bids']=[] if self.subsession.treatment==0: treatment='Winning Bids Are Revealed!' winning_bids=self.participant.vars['winning_bids'] else: treatment='Winning Bids Are Not Revealed!' winning_bids=[] return{ 'treatment': treatment, 'i': self.i(), 'i_prev': [*range(1,self.i())], 'cost':self.player.cost, 'previous_bids': self.participant.vars['bids'], 'previous_profits' : self.participant.vars['profits'], 'previous_results' : self.participant.vars['results'], 'previous_opponent_bids' : self.participant.vars['opponent_bids'], 'previous_winning_bids':winning_bids } def before_next_page(self): bid=getattr(self.player, 'bid'+str(self.i())) self.participant.vars['current_i']=self.i() class PeriodWaitPage(WaitPage): def is_displayed(self): return self.participant.vars['current_i']<=Constants.n_periods wait_for_all_groups = False def after_all_players_arrive(self): self.group.call_auction() class RoundWaitPage(WaitPage): wait_for_all_groups = True def after_all_players_arrive(self): pass ################################## class Decision1(BasePage): def i(self): return 1 ################################## class Decision2(BasePage): def i(self): return 2 ################################## class Decision3(BasePage): def i(self): return 3 ################################## class Decision4(BasePage): def i(self): return 4 ################################## class Decision5(BasePage): def i(self): return 5 ################################## class Decision6(BasePage): def i(self): return 6 ################################## class Decision7(BasePage): def i(self): return 7 ################################## class Decision8(BasePage): def i(self): return 8 ################################## class Decision9(BasePage): def i(self): return 9 ################################## class Decision10(BasePage): def i(self): return 10 class Results(Page): def vars_for_template(self): if self.subsession.treatment==0: treatment='Winning Bids Are Revealed!' winning_bids=self.participant.vars['winning_bids'] else: treatment='Winning Bids Are Not Revealed!' winning_bids=[] return{ 'treatment': treatment, 'i': self.participant.vars['current_i'], 'i_prev': [*range(1,Constants.n_periods+1)], 'cost':self.player.cost, 'previous_bids': self.participant.vars['bids'], 'previous_profits' : self.participant.vars['profits'], 'previous_results' : self.participant.vars['results'], 'previous_opponent_bids' : self.participant.vars['opponent_bids'], 'previous_winning_bids':winning_bids } def before_next_page(self): self.player.all_bids=",".join(map(str,self.participant.vars['bids'])) self.player.all_profits = ",".join(map(str,self.participant.vars['profits'])) self.player.all_results = ",".join(map(str,self.participant.vars['results'])) self.player.all_opponent_bids = ",".join(map(str,self.participant.vars['opponent_bids'])) self.player.scaling=self.player.cost/sum([p.cost for p in self.group.get_players()]) class FinalPayment(Page): def is_displayed(self): return self.subsession.round_number==Constants.num_rounds def vars_for_template(self): rpt=Constants.rounds_per_treat rand = [np.random.choice(range(rpt*i+1, rpt*(i+1)+1)) for i in range(Constants.n_treats)] profits = np.array([np.sum(list(map(float,self.player.in_round(r).all_profits.split(",")))) for r in rand]) roles = np.array([self.player.in_round(r).role() for r in rand]) scalings= np.array([self.player.in_round(r).scaling for r in rand]) profits = np.round(profits*scalings) # profits = np.ceil(profits.dot(Constants.multipliers)) payments = np.round(Constants.F+np.clip(Constants.V/10*(profits-5), 0, Constants.V)) avg_pay = round(np.average(payments)) return{ 'rand': rand, 'profits': list(profits), 'payments': list(payments), 'avg_pay':avg_pay, } page_sequence = [ TreatmentPage, Decision1,PeriodWaitPage, Decision2,PeriodWaitPage, Decision3,PeriodWaitPage, Decision4,PeriodWaitPage, Decision5,PeriodWaitPage, Decision6,PeriodWaitPage, Decision7,PeriodWaitPage, Decision8,PeriodWaitPage, Decision9,PeriodWaitPage, Decision10,PeriodWaitPage, Results, FinalPayment ]