from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage, ) import random import json from otree.models import participant doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'MPL_example' players_per_group = None num_rounds = 1 risky_low = 250 risky_high = 750 safer_low = 450 safer_high = 550 num_rows = 2 to_USD = 40 class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): response = models.StringField() random_draw = models.IntegerField() lottery_random = models.IntegerField() # Functions # def creating_session(subsession): # print('in creating session') # for p in subsession.get_players(): # if subsession.round_number == 1: # p.participant.vars['payoff_mpl'] = 0 #setting payoff to zero in every new round 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 # PAGES class Intro(Page): pass class Instructions(Page): pass class Example(Page): pass class MyPage(Page): form_model = 'player' form_fields = ['response'] def vars_for_template(player): return dict( num_rows=2, L1=" 1 = E$" + str(Constants.safer_high) + " (10%), or 2-10 = E$" + str(Constants.safer_low) + " (90%)", R1=" 1 = E$" + str(Constants.risky_high) + " (10%), or 2-10 = E$" + str(Constants.risky_low) + " (90%)", L2=" 1-2 = E$" + str(Constants.safer_high) + " (20%), or 3-10 = E$" + str(Constants.safer_low) + " (80%)", R2=" 1-2 = E$" + str(Constants.risky_high) + " (20%), or 3-10 = E$" + str(Constants.risky_low) + " (80%)", ) def before_next_page(player, timeout_happened): player.random_draw = random.choice(range(1, 2)) # random draw selects which decision line counts in MPL player.lottery_random = random.choice(range(1, 2)) # lottery_random chooses a number for the lottery set_payoffs(player) class ResultsWaitPage(WaitPage): pass class Lottery(Page): form_model = 'player' def vars_for_template(player): return dict( num_rows=2, L1=" 1 = E$" + str(Constants.safer_high) + " (10%), or 2-10 = E$" + str(Constants.safer_low) + " (90%)", R1=" 1 = E$" + str(Constants.risky_high) + " (10%), or 2-10 = E$" + str(Constants.risky_low) + " (90%)", L2=" 1-2 = E$" + str(Constants.safer_high) + " (20%), or 3-10 = E$" + str(Constants.safer_low) + " (90%)", R2=" 1-2 = E$" + str(Constants.risky_high) + " (20%), or 3-10 = E$" + str(Constants.risky_low) + " (90%)", ) def js_vars(player): return dict ( response=json.loads(player.response), ) class Results(Page): form_model = 'player' def vars_for_template(player): return dict( num_rows=2, L1=" 1 = E$" + str(Constants.safer_high) + " (10%), or 2-10 = E$" + str(Constants.safer_low) + " (90%)", R1=" 1 = E$" + str(Constants.risky_high) + " (10%), or 2-10 = E$" + str(Constants.risky_low) + " (90%)", L2=" 1-2 = E$" + str(Constants.safer_high) + " (20%), or 3-10 = E$" + str(Constants.safer_low) + " (90%)", R2=" 1-2 = E$" + str(Constants.risky_high) + " (20%), or 3-10 = E$" + str(Constants.risky_low) + " (90%)", ) def js_vars(player): return dict( response=json.loads(player.response), ) page_sequence = [Instructions, Example, MyPage, Lottery, Results, ResultsWaitPage]