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_v2' players_per_group = None num_rounds = 20 initial_amount = 6 class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: paying_round = random.randint(1, Constants.num_rounds) self.session.vars['paying_round'] = 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() C = models.FloatField() w0 = models.FloatField() w1 = models.FloatField() min_g1 = models.FloatField() max_g1 = models.FloatField() k1 = models.FloatField() C2 = models.FloatField() w2 = models.FloatField() min_g2 = models.FloatField() max_g2 = models.FloatField() k2 = models.FloatField() final = models.FloatField() t_stage1 = models.FloatField() t_stage2 = models.FloatField() t_final = models.FloatField() winning_round = models.IntegerField() id_number_v2 = models.IntegerField(label = "Your ID", min=1, max=20) def stage_1(self): t_stage1 = float(time.time()) ### define first round self.w0 = Constants.initial_amount self.C = round(random.uniform(-0.6, 0.6), 1) self.min_g1 = round(self.C - random.uniform(0.01, 0.6), 1) self.max_g1 = round(self.C + random.uniform(0.01, 1.2), 1) ### define first lottery winnings self.k1 = round(random.uniform(self.min_g1, self.max_g1), 1) if self.round_number == self.session.vars['good_round_low']: #self.w0 = 3333 self.w0 = Constants.initial_amount self.C = -1.2 self.min_g1 = -1.5 self.max_g1 = -0.3 self.k1 = -0.5 if self.round_number == self.session.vars['bad_round_low']: #self.w0 = 1111 self.w0 = Constants.initial_amount self.C = -0.4 self.min_g1 = -0.7 self.max_g1 = 0.5 self.k1 = -0.5 if self.round_number == self.session.vars['good_round_high']: #self.w0 = 9999 self.w0 = Constants.initial_amount self.C = 0.2 self.min_g1 = -0.2 self.max_g1 = 1.3 self.k1 = 1 if self.round_number == self.session.vars['bad_round_high']: #self.w0 = 6666 self.w0 = Constants.initial_amount self.C = 1.1 self.min_g1 = 0.7 self.max_g1 = 2.2 self.k1 = 1 self.w1 = self.w0 + self.k1 def stage_2(self): t_stage2 = float(time.time()) ### define second round self.min_g2 = round(self.k1 - random.uniform(0.01, 1.4), 1) self.max_g2 = round(self.k1 + random.uniform(0.01, 1.75), 1) self.k2 = round(random.uniform(self.min_g2, self.max_g2), 1) self.w2 = self.w0 + self.k2 def final_result(self): t_final = float(time.time()) if (self.option_choice_1): if (self.option_choice_2): self.final = self.w2 else: self.final = self.w1 else: self.final = self.w0 + self.C def payoff(self): self.winning_round = self.session.vars['paying_round']