from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) from mpl.config import * import random from random import randrange # ******************************************************************************************************************** # # *** CLASS SUBSESSION # ******************************************************************************************************************** # class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: n = Constants.num_choices for p in self.get_players(): # create list of lottery indices # ---------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n)] indices.append(n) if Constants.certain_choice else None # create list of lottery indices # ---------------------------------------------------------------------------------------------------- choices = [500, 505, 520, 550, 600, 750, 1000] # create list of probabilities # ---------------------------------------------------------------------------------------------------- if Constants.percentage: probabilities = [ "{0:.2f}".format(k / n * 100) + "%" for k in indices ] else: probabilities = [ str(k) + "/" + str(n) for k in indices ] # create list corresponding to form_field variables including all choices # ---------------------------------------------------------------------------------------------------- form_fields1 = ['choice1_' + str(k) for k in indices] form_fields2 = ['choice2_' + str(k) for k in indices] # create list of choices # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_choices_time1'] = list( zip(indices, form_fields1, choices) ) p.participant.vars['mpl_choices_time2'] = list( zip(indices, form_fields2, choices) ) # randomly determine index/choice of binary decision to pay # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_index_to_pay'] = random.choice(indices) p.participant.vars['mpl_choice_to_pay'] = 'choice_' + str(p.participant.vars['mpl_index_to_pay']) # randomize order of lotteries if # ---------------------------------------------------------------------------------------------------- if Constants.random_order: random.shuffle(p.participant.vars['mpl_choices_time1']) random.shuffle(p.participant.vars['mpl_choices_time2']) # initiate list for choices made # ---------------------------------------------------------------------------------------------------- p.participant.vars['mpl_choices_made'] = [None for j in range(1, n + 1)] class Group(BaseGroup): pass # ******************************************************************************************************************** # # *** CLASS PLAYER # ******************************************************************************************************************** # class Player(BasePlayer): # add model fields to class player # :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: for j in range(1, Constants.num_choices + 1): locals()['choice1_' + str(j)] = models.StringField() locals()['choice2_' + str(j)] = models.StringField() del j random_draw = models.IntegerField() choice_to_pay = models.StringField() option_to_pay = models.StringField()