from otree.api import * c = cu doc = 'Implementation of the m-mpl method' class C(BaseConstants): NAME_IN_URL = 'mmpl' PLAYERS_PER_GROUP = None NUM_ROUNDS = 30 NUM_PRODUCTS = 30 ENDOWMENT = 0 MAX = 10 PRODUCTS = ('Bare', 'Cheetos', 'CheezitOriginal', 'ChipsAhoy', 'Coke', 'CokeZero', 'Doritos', 'Flipz', 'Goldfish', 'Haribo', 'HersheysMilkChocolate', 'IceBreakersPeppermint', 'KindCaramelAlmond', 'KindDarkChocolate', 'Lays', 'LotusBiscoff', 'MilanoDarkChocolate', 'MilanoMilkChocolate', 'MNM', 'Oreo', 'PockyChocolate', 'PockyStrawberry', 'PopTartsFrostedCookies', 'PopTartsFrostedSmores', 'Pringles', 'PringlesOnion', 'Skittles', 'Snickers', 'Sprite', 'TwixBar') PRODUCTS_DESCRIPTIONS = ('Bare Fugi & Reds Apple Chips (3.4 oz)', 'Cheetos Crunchy (8.5 oz)', 'Cheezit Original (12.4 oz)', 'Chips Ahoy Real Chocolate Chip Cookies Original (13 oz)', 'Coke (12 fl oz)', 'Coke Zero Sugar (12 fl oz)', 'Doritos Nacho Cheese (9.25 oz)', 'Flipz Milk Chocolate Covered Pretzels (7.5 oz)', 'Goldfish Cheddar (6.6 oz)', 'Haribo Gummi Candy (8 oz)', "Hershey's Milk Chocolate (1.55 oz)", 'Ice Breaker Ice Cubes Peppermint Flavored (3.24 oz, 40 pieces)', 'KIND Caramel Almond & Sea Salt (1.4 oz)', 'KIND Dark Chocolate Nuts & Sea Salt (1.4 oz)', "Lay's Classic (8 oz)", 'Lotus Biscoff Cookies (8.8 oz)', 'Milano Cookies Double Dark Chocolate (7.5 oz)', 'Milano Cookies Milk Chocolate (6 oz)', "M&M's Milk Chocolate (3.14 oz)", 'OREO Chocolate Sandwich Cookies (14.3 oz)', 'Pocky Chocolate Cream (2.47 oz)', 'Pocky Strawberry Cream (2.47 oz)', 'Pop-Tarts Frosted Cookies and Crème (13.5 oz, 8 Pop-Tarts)', "Pop-Tarts Frosted S'mores (13.5 oz, 8 Pop-Tarts)", 'Pringles Original (5.2 oz)', 'Pringles Sour Cream & Onion (5.5 oz)', 'Skittles Original (2.17 oz)', 'Snickers (1.86 oz)', 'Sprite (12 fl oz)', 'Twix (1.79 oz)') EXAMPLE = 'Cheeseburger' MAX_QUESTION = 1000 class Subsession(BaseSubsession): pass def creating_session(subsession: Subsession): session = subsession.session round_number = subsession.round_number players = subsession.get_players() if round_number == 1: import random for p in players: participant = p.participant participant.vars['mpl_products_order'] = random.sample(C.PRODUCTS, k=len(C.PRODUCTS)) participant.mmpl_round_to_pay = random.randint(1, C.NUM_PRODUCTS) participant.mmpl_question_to_pay = random.randint(1, C.MAX_QUESTION) if participant.mpl_treatment == "mmpl": participant.mpl_round_to_pay = participant.mmpl_round_to_pay participant.mpl_question_to_pay = participant.mmpl_question_to_pay for p in players: p.product = p.participant.vars['mpl_products_order'][round_number - 1] class Group(BaseGroup): pass class Player(BasePlayer): decision_question = models.IntegerField() product = models.StringField(choices=C.PRODUCTS) decision = models.FloatField(max=C.MAX, min=0.01) quiz_1a = models.StringField(choices=[['True', 'True'], ['False', 'False']], widget=widgets.RadioSelect) quiz_1b = models.StringField(choices=[['A', 'Option A'], ['B', 'Option B']], widget=widgets.RadioSelect) quiz_1c = models.StringField(choices=[['True', 'True'], ['False', 'False']], widget=widgets.RadioSelect) quiz_2a = models.StringField(choices=[['True', 'True'], ['False', 'False']], widget=widgets.RadioSelect) quiz_2b = models.StringField(choices=[['A', 'Option A'], ['B', 'Option B']], widget=widgets.RadioSelect) quiz_2c = models.StringField(choices=[['True', 'True'], ['False', 'False']], widget=widgets.RadioSelect) def set_payoff(player: Player): participant = player.participant player_to_pay = player.in_round(participant.mmpl_round_to_pay) participant.mmpl_decision = player_to_pay.decision participant.mmpl_decision_question = int(player_to_pay.decision * 100) participant.mmpl_product = player_to_pay.product if participant.mmpl_question_to_pay < participant.mmpl_decision_question: participant.mmpl_money = 0 else: participant.mmpl_money = round(0.01 * participant.mmpl_question_to_pay, 2) if participant.mpl_treatment == "mmpl": participant.mpl_decision = participant.mmpl_decision participant.mpl_decision_question = participant.mmpl_decision_question participant.mpl_money = participant.mmpl_money participant.mpl_product = participant.mmpl_product class InstructionA(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 class QuizA(Page): form_model = 'player' form_fields = ['quiz_1a', 'quiz_1b', 'quiz_1c', 'quiz_2a', 'quiz_2b', 'quiz_2c'] @staticmethod def is_displayed(player: Player): return player.round_number == 1 class AnswerA(Page): form_model = 'player' @staticmethod def is_displayed(player: Player): return player.round_number == 1 class DecisionA(Page): form_model = 'player' form_fields = ['decision'] @staticmethod def vars_for_template(player: Player): return dict( product_description=C.PRODUCTS_DESCRIPTIONS[C.PRODUCTS.index(player.product)], product_img=f'mpl/{player.product}.jpg' ) @staticmethod def before_next_page(player: Player, timeout_happened): if player.round_number == C.NUM_ROUNDS: set_payoff(player) player.decision = round(player.decision, 2) page_sequence = [InstructionA, QuizA, AnswerA, DecisionA]