from otree.api import ( Page, WaitPage, models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random doc = """ Measuring flexibility """ class Constants(BaseConstants): name_in_url = 'flex' players_per_group = None num_rounds = 3 cost1 = 20 cost2 = 30 penalty_q = 5 quantity_a = 40 price_a = 20 num_rows = 1 conversion_rate = 25 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # nature_abnormal = models.IntegerField() # a random draw here will determine the state choose_b = models.BooleanField() price_b = models.IntegerField() cost = models.IntegerField() # cost for display in descriptive table of state of nature costsn = models.IntegerField() # cost for payoff calculation after state of nature is known random_nature = models.IntegerField() quantity_b = models.IntegerField( label=' ', min=0, max=40) payoff_flex = models.IntegerField() Earnings_1 = models.FloatField() Earnings_2 = models.FloatField() Earnings_3 = models.FloatField() Points_1 = models.IntegerField() Points_2 = models.IntegerField() Points_3 = models.IntegerField() # FUNCTIONS def creating_session(subsession): print('in creating session') # Defining price B across rounds: for player in subsession.get_players(): if player.round_number == 1: player.price_b = 20 if player.round_number == 2: player.price_b = 19 if player.round_number == 3: player.price_b = 18 for player in subsession.get_players(): if player.round_number == 1: player.cost = 20 else: player.cost = 30 def set_payoffs(player): # Logic # A (40*20)-(5*cost) # B (quantity_b*price)-((quantity_b-35)*costsn) if not player.choose_b: player.payoff_flex = ((Constants.quantity_a*Constants.price_a)-(5*player.costsn)) else: player.payoff_flex = ((player.quantity_b*player.price_b)-((player.quantity_b-35)*player.costsn)) if player.round_number == 1: player.Points_1 = player.payoff_flex player.Earnings_1 = player.payoff_flex / Constants.conversion_rate if player.round_number == 2: player.Points_2 = player.payoff_flex player.Earnings_2 = player.payoff_flex / Constants.conversion_rate if player.round_number == 3: player.Points_3 = player.payoff_flex player.Earnings_3 = player.payoff_flex / Constants.conversion_rate # PAGES class Next(Page): form_model = 'player' # def is_displayed(player): # return player.round_number > 1 def before_next_page(player, timeout_happened): player.random_nature = random.choice(range(1, 10)) class AChoice(Page): form_model = 'player' form_fields = ['choose_b'] def vars_for_template(player): return dict( num_rows=1, L="Sell 40 units for 20 points each", R="Choose how many units to sell (0 to 40) for " + str(player.price_b) + " points each.
You may adjust the quantity after you know the future.", C="No costs until quantity 35, and a tax of " + str(player.cost) + " points for each additional unit" ) def before_next_page(player, timeout_happened): if player.random_nature <= 8: # normal state, no cost player.costsn = 0 else: # atypical state, cost vary player.costsn = player.cost class BStateOfTheWorld(Page): form_model = 'player' @staticmethod def before_next_page(player, timeout_happened): if player.choose_b == False: set_payoffs(player) class B1QuantityB(Page): form_model = 'player' form_fields = ['quantity_b'] def is_displayed(player): return player.choose_b == True def before_next_page(player, timeout_happened): if player.choose_b == True: set_payoffs(player) class RoundResults(Page): # pass form_model = 'player' @staticmethod def before_next_page(player, timeout_happened): # if player.round_number==3: participant = player.participant if player.round_number==1: participant.Points_1 = player.Points_1 participant.Earnings_1 = player.Earnings_1 if player.round_number == 2: participant.Points_2 = player.Points_2 participant.Earnings_2 = player.Earnings_2 if player.round_number == 3: participant.Points_3 = player.Points_3 participant.Earnings_3 = player.Earnings_3 class ResultsWaitPage(WaitPage): pass # class CResults(Page): # pass page_sequence = [Next, AChoice, BStateOfTheWorld, B1QuantityB, RoundResults]