from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BasePlayer, BaseGroup, Currency as c, currency_range, Page, WaitPage, ) import random import json from otree.models import participant doc = """ MPL """ class Constants(BaseConstants): name_in_url = 'MPL' players_per_group = None num_rounds = 1 risky_low = 200 risky_high = 800 safer_low = 400 safer_high = 600 num_rows = 10 conversion_rate = 25 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): response = models.StringField() random_draw = models.IntegerField() lottery_random = models.IntegerField() Earnings_mpl = models.FloatField() # Functions def set_payoffs(player): response_list = json.loads(player.response) if player.random_draw == 1: if response_list[0] == 0: # Safer option if player.lottery_random == 1: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[0] == 1: # Risky option if player.lottery_random == 1: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 2: if response_list[1] == 0: # Safer option if player.lottery_random <= 2: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[1] == 1: # Risky option if player.lottery_random <=2: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 3: if response_list[2] == 0: # Safer option if player.lottery_random <= 3: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[2] == 1: # Risky option if player.lottery_random <= 3: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 4: if response_list[3] == 0: # Safer option if player.lottery_random <= 4: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[3] == 1: # Risky option if player.lottery_random <= 4: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 5: if response_list[4] == 0: # Safer option if player.lottery_random <= 5: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[4] == 1: # Risky option if player.lottery_random <= 5: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 6: if response_list[5] == 0: # Safer option if player.lottery_random <= 6: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[5] == 1: # Risky option if player.lottery_random <= 6: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 7: if response_list[6] == 0: # Safer option if player.lottery_random <= 7: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[6] == 1: # Risky option if player.lottery_random <= 7: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 8: if response_list[7] == 0: # Safer option if player.lottery_random <= 8: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[7] == 1: # Risky option if player.lottery_random <= 8: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low if player.random_draw == 9: if response_list[8] == 0: # Safer option if player.lottery_random <= 9: player.payoff = Constants.safer_high else: player.payoff = Constants.safer_low if response_list[8] == 1: # Risky option if player.lottery_random <= 9: player.payoff = Constants.risky_high else: player.payoff = Constants.risky_low # player.Earnings_mpl = player.payoff / Constants.conversion_rate # PAGES class AIntro(Page): pass # class Instructions(Page): # pass class BSelectLotery(Page): form_model = 'player' form_fields = ['response'] def vars_for_template(player): return dict( num_rows=9, L1=" 1 = " + str(Constants.safer_high) + ", or 2-10 = " + str(Constants.safer_low) , R1=" 1 = " + str(Constants.risky_high) + ", or 2-10 = " + str(Constants.risky_low) , L2=" 1-2 = " + str(Constants.safer_high) + ", or 3-10 = " + str(Constants.safer_low) , R2=" 1-2 = " + str(Constants.risky_high) + ", or 3-10 = " + str(Constants.risky_low) , L3=" 1-3 = " + str(Constants.safer_high) + ", or 4-10 = " + str(Constants.safer_low) , R3=" 1-3 = " + str(Constants.risky_high) + ", or 4-10 = " + str(Constants.risky_low) , L4=" 1-4 = " + str(Constants.safer_high) + ", or 5-10 = " + str(Constants.safer_low) , R4=" 1-4 = " + str(Constants.risky_high) + ", or 5-10 = " + str(Constants.risky_low) , L5=" 1-5 = " + str(Constants.safer_high) + ", or 6-10 = " + str(Constants.safer_low) , R5=" 1-5 = " + str(Constants.risky_high) + ", or 6-10 = " + str(Constants.risky_low) , L6=" 1-6 = " + str(Constants.safer_high) + ", or 7-10 = " + str(Constants.safer_low) , R6=" 1-6 = " + str(Constants.risky_high) + ", or 7-10 = " + str(Constants.risky_low) , L7=" 1-7 = " + str(Constants.safer_high) + ", or 8-10 = " + str(Constants.safer_low) , R7=" 1-7 = " + str(Constants.risky_high) + ", or 8-10 = " + str(Constants.risky_low) , L8=" 1-8 = " + str(Constants.safer_high) + ", or 9-10 = " + str(Constants.safer_low) , R8=" 1-8 = " + str(Constants.risky_high) + ", or 9-10 = " + str(Constants.risky_low) , L9=" 1-9 = " + str(Constants.safer_high) + ", or 10 = " + str(Constants.safer_low) , R9=" 1-9 = " + str(Constants.risky_high) + ", or 10 = " + str(Constants.risky_low) , ) def before_next_page(player, timeout_happened): player.random_draw = random.choice(range(1, 9)) # random draw selects which decision line counts in MPL player.lottery_random = random.choice(range(1, 9)) # lottery_random chooses a number for the lottery set_payoffs(player) class ResultsWaitPage(WaitPage): pass class CRandomLottery(Page): form_model = 'player' def vars_for_template(player): return dict( num_rows=9, L1=" 1 = " + str(Constants.safer_high) + ", or 2-10 = " + str(Constants.safer_low) , R1=" 1 = " + str(Constants.risky_high) + ", or 2-10 = " + str(Constants.risky_low) , L2=" 1-2 = " + str(Constants.safer_high) + ", or 3-10 = " + str(Constants.safer_low) , R2=" 1-2 = " + str(Constants.risky_high) + ", or 3-10 = " + str(Constants.risky_low) , L3=" 1-3 = " + str(Constants.safer_high) + ", or 4-10 = " + str(Constants.safer_low) , R3=" 1-3 = " + str(Constants.risky_high) + ", or 4-10 = " + str(Constants.risky_low) , L4=" 1-4 = " + str(Constants.safer_high) + ", or 5-10 = " + str(Constants.safer_low) , R4=" 1-4 = " + str(Constants.risky_high) + ", or 5-10 = " + str(Constants.risky_low) , L5=" 1-5 = " + str(Constants.safer_high) + ", or 6-10 = " + str(Constants.safer_low) , R5=" 1-5 = " + str(Constants.risky_high) + ", or 6-10 = " + str(Constants.risky_low) , L6=" 1-6 = " + str(Constants.safer_high) + ", or 7-10 = " + str(Constants.safer_low) , R6=" 1-6 = " + str(Constants.risky_high) + ", or 7-10 = " + str(Constants.risky_low) , L7=" 1-7 = " + str(Constants.safer_high) + ", or 8-10 = " + str(Constants.safer_low) , R7=" 1-7 = " + str(Constants.risky_high) + ", or 8-10 = " + str(Constants.risky_low) , L8=" 1-8 = " + str(Constants.safer_high) + ", or 9-10 = " + str(Constants.safer_low) , R8=" 1-8 = " + str(Constants.risky_high) + ", or 9-10 = " + str(Constants.risky_low) , L9=" 1-9 = " + str(Constants.safer_high) + ", or 10 = " + str(Constants.safer_low) , R9=" 1-9 = " + str(Constants.risky_high) + ", or 10 = " + str(Constants.risky_low) , ) def js_vars(player): return dict ( response=json.loads(player.response), ) class DResults(Page): form_model = 'player' def vars_for_template(player): return dict( num_rows=9, L1=" 1 = " + str(Constants.safer_high) + ", or 2-10 = " + str(Constants.safer_low) , R1=" 1 = " + str(Constants.risky_high) + ", or 2-10 = " + str(Constants.risky_low) , L2=" 1-2 = " + str(Constants.safer_high) + ", or 3-10 = " + str(Constants.safer_low) , R2=" 1-2 = " + str(Constants.risky_high) + ", or 3-10 = " + str(Constants.risky_low) , L3=" 1-3 = " + str(Constants.safer_high) + ", or 4-10 = " + str(Constants.safer_low) , R3=" 1-3 = " + str(Constants.risky_high) + ", or 4-10 = " + str(Constants.risky_low) , L4=" 1-4 = " + str(Constants.safer_high) + ", or 5-10 = " + str(Constants.safer_low) , R4=" 1-4 = " + str(Constants.risky_high) + ", or 5-10 = " + str(Constants.risky_low) , L5=" 1-5 = " + str(Constants.safer_high) + ", or 6-10 = " + str(Constants.safer_low) , R5=" 1-5 = " + str(Constants.risky_high) + ", or 6-10 = " + str(Constants.risky_low) , L6=" 1-6 = " + str(Constants.safer_high) + ", or 7-10 = " + str(Constants.safer_low) , R6=" 1-6 = " + str(Constants.risky_high) + ", or 7-10 = " + str(Constants.risky_low) , L7=" 1-7 = " + str(Constants.safer_high) + ", or 8-10 = " + str(Constants.safer_low) , R7=" 1-7 = " + str(Constants.risky_high) + ", or 8-10 = " + str(Constants.risky_low) , L8=" 1-8 = " + str(Constants.safer_high) + ", or 9-10 = " + str(Constants.safer_low) , R8=" 1-8 = " + str(Constants.risky_high) + ", or 9-10 = " + str(Constants.risky_low) , L9=" 1-9 = " + str(Constants.safer_high) + ", or 10 = " + str(Constants.safer_low) , R9=" 1-9 = " + str(Constants.risky_high) + ", or 10 = " + str(Constants.risky_low) , ) def js_vars(player): return dict( response=json.loads(player.response), ) def before_next_page(player, timeout_happened): participant = player.participant participant.Points_mpl = player.payoff participant.Earnings_mpl = player.payoff / Constants.conversion_rate page_sequence = [AIntro, BSelectLotery, CRandomLottery, DResults] #removed ResultsWaitPage from page seq