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_v2m' players_per_group = None num_rounds = 25 initial_amount = 6.5 paying_round2 = random.randint(1, num_rounds) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: paying_round = Constants.paying_round2 self.session.vars['paying_round2'] = paying_round special_rounds = [int(round(0.2*Constants.num_rounds)), int(round(0.4*Constants.num_rounds)), int(round(0.6*Constants.num_rounds)), int(round(0.8*Constants.num_rounds)) ] random.shuffle(special_rounds) good_round_low = special_rounds[0] bad_round_low = special_rounds[1] self.session.vars['good_round_low'] = good_round_low self.session.vars['bad_round_low'] = bad_round_low good_round_high = special_rounds[2] bad_round_high = special_rounds[3] self.session.vars['good_round_high'] = good_round_high self.session.vars['bad_round_high'] = bad_round_high 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() K1 = models.FloatField() ### second stage outcome1 = models.FloatField() outcome_disp = models.FloatField() C2 = models.FloatField() w2 = models.FloatField() k2_min = models.FloatField() k2_max = models.FloatField() min_g2 = models.FloatField() max_g2 = models.FloatField() K2 = models.FloatField() final = models.FloatField() ### capture time t_stage1 = models.FloatField() t_stage2 = models.FloatField() t_final = models.FloatField() winning_round_2 = models.IntegerField() def stage_1(self): self.t_stage1 = float(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_min = self.k1_min * (-1) self.k1_max = round(random.uniform(0.3, 1), 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.k1_min = self.k1_min * (-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, 1), 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 or self.w1 == self.C: self.w1 = round(random.uniform(self.min_g1, self.max_g1), 1) self.K1 = round(self.w1 - self.C, 1) if self.round_number == self.session.vars['good_round_low']: self.C0 = -1.2 self.k1_min = -0.3 self.k1_max = 0.9 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.7, 1) self.K1 = round(self.w1 - self.C, 1) if self.round_number == self.session.vars['bad_round_low']: self.C0 = -0.4 self.k1_min = -0.3 self.k1_max = 0.9 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.1, 1) self.K1 = round(self.w1 - self.C, 1) if self.round_number == self.session.vars['good_round_high']: self.C0 = 0.2 self.k1_min = -0.4 self.k1_max = 1.1 self.w0 = Constants.initial_amount #self.w0 = 3000 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.8, 1) self.K1 = round(self.w1 - self.C, 1) if self.round_number == self.session.vars['bad_round_high']: self.C0 = 1.1 self.k1_min = -0.4 self.k1_max = 1.1 self.w0 = Constants.initial_amount #self.w0 = 4000 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.1, 1) self.K1 = round(self.w1 - self.C, 1) def stage_2(self): self.t_stage2 = float(time.time()) ### define second round self.k2_min = round(random.uniform(0.4, 1.1), 1) self.k2_min = self.k2_min * (-1) self.k2_max = round(random.uniform(0.4, 1.2), 1) if (self.option_choice_1): self.outcome1 = self.w1 self.outcome_disp = self.K1 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.k2_min = self.k2_min * (-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) while self.w2 == self.outcome1: self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) self.K2 = round(self.w2 - self.outcome1, 1) else: self.outcome1 = self.C self.outcome_disp = 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.k2_min = self.k2_min * (-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) while self.w2 == self.outcome1: self.w2 = round(random.uniform(self.min_g2, self.max_g2), 1) self.K2 = round(self.w2 - self.outcome1, 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 if self.round_number == self.session.vars['paying_round2']: self.participant.vars['payoff2'] = self.final def payoff(self): self.winning_round_2 = self.session.vars['paying_round2']