from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random import time class Constants(BaseConstants): name_in_url = 'gamble_practice_v1' players_per_group = None num_rounds = 3 initial_amount = 6.5 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: paying_round_pract = random.randint(1, 3) self.session.vars['paying_round_pract'] = paying_round_pract class Group(BaseGroup): pass class Player(BasePlayer): option_choice_1 = models.BooleanField(initial = False) option_choice_2 = models.BooleanField(initial = False) ### first stage variables w0 = models.FloatField() C0 = models.FloatField() C = models.FloatField() k1_min = models.FloatField() k1_max = models.FloatField() min_g1 = models.FloatField() max_g1 = models.FloatField() w1 = models.FloatField() ### second stage outcome1 = models.FloatField() C2 = models.FloatField() w2 = models.FloatField() k2_min = models.FloatField() k2_max = models.FloatField() min_g2 = models.FloatField() max_g2 = models.FloatField() final = models.FloatField() ### capture time t_stage1 = models.FloatField() t_stage2 = models.FloatField() t_final = models.FloatField() winning_round = models.IntegerField() id_number_p1 = models.IntegerField(label = "Your SONA ID", min=50000, max=199999) def stage_1(self): self.t_stage1 = time.time() ### define first round self.w0 = Constants.initial_amount self.C0 = 0 while self.C0 == 0: self.C0 = round(random.uniform(-0.6, 0.6), 1) self.C = round(self.w0 + self.C0, 1) self.k1_min = round(random.uniform(0.3, 0.8), 1) self.k1_max = round(random.uniform(0.3, 0.9), 1) self.min_g1 = round(self.C - self.k1_min, 1) while self.min_g1 == self.w0: self.k1_min = round(random.uniform(0.3, 0.8), 1) self.min_g1 = round(self.C - self.k1_min, 1) self.max_g1 = round(self.C + self.k1_max, 1) while self.max_g1 == self.w0: self.k1_max = round(random.uniform(0.3, 0.9), 1) self.max_g1 = round(self.C + self.k1_max, 1) ### define first lottery winnings self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) while self.w1 == self.w0: self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) if self.round_number == 1: self.C0 = -0.2 self.k1_min = 0.1 self.k1_max = 1.1 self.w0 = Constants.initial_amount #self.w0 = 1000 self.C = round(self.w0 + self.C0, 1) self.min_g1 = round(self.C - self.k1_min, 1) self.max_g1 = round(self.C + self.k1_max, 1) self.w1 = round(self.C + 0.6, 1) if self.round_number == 2: self.C0 = 0.4 self.k1_min = 1.1 self.k1_max = 0.1 self.w0 = Constants.initial_amount #self.w0 = 2000 self.C = round(self.w0 + self.C0, 1) self.min_g1 = round(self.C - self.k1_min, 1) self.max_g1 = round(self.C + self.k1_max, 1) self.w1 = round(self.C - 0.2, 1) def stage_2(self): self.t_stage2 = time.time() ### define second round self.k2_min = round(random.uniform(0.4, 1.1), 1) self.k2_max = round(random.uniform(0.4, 1.2), 1) if (self.option_choice_1): self.outcome1 = self.w1 self.min_g2 = round(self.outcome1 - self.k2_min, 1) while self.min_g2 == self.w0: self.k2_min = round(random.uniform(0.4, 1.1), 1) self.min_g2 = round(self.outcome1 - self.k2_min, 1) self.max_g2 = round(self.outcome1 + self.k2_max, 1) while self.max_g2 == self.w0: self.k2_max = round(random.uniform(0.4, 1.2), 1) self.max_g2 = round(self.outcome1 + self.k2_max, 1) self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) else: self.outcome1 = self.C self.min_g2 = round(self.outcome1 - self.k2_min, 1) while self.min_g2 == self.w0: self.k2_min = round(random.uniform(0.4, 1.1), 1) self.min_g2 = round(self.outcome1 - self.k2_min, 1) self.max_g2 = round(self.outcome1 + self.k2_max, 1) while self.max_g2 == self.w0: self.k2_max = round(random.uniform(0.4, 1.2), 1) self.max_g2 = round(self.outcome1 + self.k2_max, 1) self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) def final_result(self): self.t_final = time.time() if (self.option_choice_1): if (self.option_choice_2): self.final = self.w2 else: self.final = self.w1 else: if (self.option_choice_2): self.final = self.w2 else: self.final = self.C def payoff(self): self.winning_round = self.session.vars['paying_round']