from otree.api import * import random as rand from pprint import pprint import json from ast import literal_eval doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'payment' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): pass def creating_session(subsession): for p in subsession.get_players(): ranking_paid = rand.randint(0,1) p.ranking_paid = ranking_paid pprint('ranking paid:') pprint(p.ranking_paid) if p.ranking_paid == 1: p.task_paid = "ranking" if p.ranking_paid == 0: p.task_paid = "choices" # RANKING PAYMENT ranking = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] chosen_items = rand.sample(ranking, 2) p.item1 = chosen_items[0] p.item2 = chosen_items[1] pprint('item1 is: ') pprint(p.item1) pprint('item2 is: ') pprint(p.item2) # CHOICES/QUESTIONS PAYMENT p.question_paid = rand.randint(0, 57) pprint('question to pay is:') pprint(p.question_paid) p.lottery_nr = rand.randint(1,100) pprint('lottery number for questions is:') pprint(p.lottery_nr) #####TESTING #p.participant.vars['rankpay1'] = "obj1" #p.participant.vars['rankpay2'] = "obj2" #p.participant.vars['rankpay3'] = "obj3" #p.participant.vars['rankpay4'] = "obj4" #p.participant.vars['rankpay5'] = "obj5" #p.participant.vars['rankpay6'] = "obj6" #p.participant.vars['rankpay7'] = "obj7" #p.participant.vars['rankpay8'] = "obj8" #p.participant.vars['rankpay9'] = "obj9" #p.participant.vars['rankpay10'] = "obj10" #p.participant.vars['rankpay11'] = "obj11" #### I WON'T BE ABLE TO HAVE THIS IN CREATE SESSION BECAUSE THEY HAVEN'T MADE ANY CHOICES! # QUESTIONS PAYMENT ########### STUCK HERE #p.participant.vars['objects_ordered'] = json.dumps(allobjects) #p.best_objects = json.dumps(p.participant.vars['best_objects']) #p.best_objects = p.participant.vars['best_objects'] #p.best_objects = json.loads(p.participant.vars['best_objects']) #pprint('best objects is:') #pprint(p.best_objects) #pprint(player.participant.vars['best_objects']) #pprint(player.participant.vars['worst_objects']) #pprint(player.participant.vars['best_objects_p']) #pprint(player.participant.vars['worst_objects_p']) #pprint(player.participant.vars['riskychoices']) #p.b_obj = p.participant.vars['b_obj'].in_round(p.question_paid) ### I COULD USE THAT AS THE NTH QUESTION AND GET THE PARAMETERS ## OBJ RISKY, OBJ SAFE, PROB RISKY, PROB SAFE, OPTION CHOSEN ## BY IN_ROUND? class Group(BaseGroup): pass class Player(BasePlayer): ranking_paid = models.IntegerField() task_paid = models.StringField() item1 = models.IntegerField() item2 = models.IntegerField() item1name = models.StringField() question_paid = models.IntegerField() lottery_nr = models.IntegerField() choice_paid = models.IntegerField() b_obj_p_pay = models.IntegerField() w_obj_p_pay = models.IntegerField() birth_year = models.CharField(max_length=30, choices=['1920', '1921', '1922', '1923', '1924', '1925', '1926', '1927', '1928', '1929', '1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938', '1939', '1940', '1941', '1942', '1943', '1944', '1945', '1946', '1947', '1948', '1949', '1950', '1951', '1952', '1953', '1954', '1955', '1956', '1957', '1958', '1959', '1960', '1961', '1962', '1963', '1964', '1965', '1966', '1967', '1968', '1969', '1970', '1971', '1972', '1973', '1974', '1975', '1976', '1977', '1978', '1979', '1980', '1981', '1982', '1983', '1984', '1985', '1986', '1987', '1988', '1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998', '1999', '2000', '2001', '2002', '2003', '2004', '2005', '2006', 'I prefer not to answer'], label='What is your year of birth?' ) gender = models.StringField( choices=['Female', 'Male', 'I prefer not to answer', 'Other (describe in the box below if you wish)'], widget=widgets.RadioSelect, label='What is your gender?' ) genderOther = models.StringField( blank=True, label='' ) economics = models.IntegerField( choices=[ [1, 'Yes'], [0, 'No']], widget=widgets.RadioSelect, label='Have you ever taken a course or learnt about economics and/or game theory?' ) @property def bestobj_as_list(self): return json.loads(self.participant.vars['best_objects']) @property def worstobj_as_list(self): #return self.participant.vars['worst_objects'].split(",") return json.loads(self.participant.vars['worst_objects']) @property def bestobj_p_as_list(self): #return self.participant.vars['best_objects_p'].split(",") return json.loads(self.participant.vars['best_objects_p']) @property def worstobj_p_as_list(self): #return self.participant.vars['worst_objects_p'].split(",") return json.loads(self.participant.vars['worst_objects_p']) @property def choice_as_list(self): #return self.participant.vars['riskychoices'].split(",") return json.loads(self.participant.vars['riskychoices']) # FUNCTIONS def set_payoffs(player: Player): p = player if p.task_paid == "ranking": if p.item1 > p.item2: p.participant.vars['payment'] = p.participant.vars["rankpay" + str(p.item2)] if p.item1 < p.item2: p.participant.vars['payment'] = p.participant.vars["rankpay" + str(p.item1)] # PAGES class demogs(Page): form_model = 'player' form_fields = [ 'birth_year','gender', 'genderOther', 'economics' ] @staticmethod def before_next_page(self, timeout_happened): self.participant.vars['b_object_pay'] = self.bestobj_as_list[self.question_paid] self.participant.vars['w_object_pay'] = self.worstobj_as_list[self.question_paid] self.participant.vars['b_obj_p_pay'] = self.bestobj_p_as_list[self.question_paid] self.participant.vars['w_obj_p_pay'] = self.worstobj_p_as_list[self.question_paid] self.participant.vars['choice_paid'] = self.choice_as_list[self.question_paid] self.choice_paid = self.participant.vars['choice_paid'] self.b_obj_p_pay = self.participant.vars['b_obj_p_pay'] self.w_obj_p_pay = self.participant.vars['w_obj_p_pay'] #return set_payment() class pay_task(Page): @staticmethod def vars_for_template(self): return { 'task_paid': self.task_paid, 'best_object_pay': self.participant.vars['b_object_pay'], 'worst_object_pay': self.participant.vars['w_object_pay'], 'best_obj_p_pay': self.participant.vars['b_obj_p_pay'], 'worst_obj_p_pay': self.participant.vars['w_obj_p_pay'], 'choice_paid': self.participant.vars['choice_paid'] } #def before_next_page(self, timeout_happened): # p = self # p.star = str(p.participant.vars['star']) class pay_item(Page): @staticmethod def vars_for_template(self): return { 'task_paid': self.task_paid, 'question_paid': self.question_paid, 'item1': self.item1, 'item2': self.item2, 'item1name': self.participant.vars["rankpay" + str(self.item1)], 'item2name': self.participant.vars["rankpay" + str(self.item2)], 'lottery_nr': self.lottery_nr, 'bestobjpay_name': self.participant.vars["ranked" + str(self.participant.vars['b_object_pay'])], 'worstobjpay_name': self.participant.vars["ranked" + str(self.participant.vars['w_object_pay'])], 'b_obj_p_pay': self.b_obj_p_pay, 'w_obj_p_pay': self.w_obj_p_pay, } page_sequence = [demogs, pay_task, pay_item,]