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_v3' players_per_group = None num_rounds = 20 initial_amount = 100 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.IntegerField() w0 = models.IntegerField() w1 = models.IntegerField() min_g1 = models.IntegerField() max_g1 = models.IntegerField() C2 = models.IntegerField() w2 = models.IntegerField() min_g2 = models.IntegerField() max_g2 = models.IntegerField() final = models.IntegerField() t_stage1 = models.FloatField() t_stage2 = models.FloatField() t_final = models.FloatField() winning_round = models.IntegerField() id_number_v1 = 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.8*self.w0, 1.2*self.w0)) self.min_g1 = round(random.uniform(0.8*self.C, 0.99*self.C)) self.max_g1 = round(random.uniform(1.01*self.C, 1.25*self.C)) ### define first lottery winnings self.w1 = round(random.uniform(self.min_g1, self.max_g1)) if self.round_number == self.session.vars['good_round_low']: #self.w0 = 3333 self.w0 = Constants.initial_amount self.C = 80 self.min_g1 = 75 self.max_g1 = 95 self.w1 = 92 if self.round_number == self.session.vars['bad_round_low']: #self.w0 = 1111 self.w0 = Constants.initial_amount self.C = 93 self.min_g1 = 88 self.max_g1 = 109 self.w1 = 92 if self.round_number == self.session.vars['good_round_high']: #self.w0 = 9999 self.w0 = Constants.initial_amount self.C = 103 self.min_g1 = 97 self.max_g1 = 122 self.w1 = 118 if self.round_number == self.session.vars['bad_round_high']: #self.w0 = 6666 self.w0 = Constants.initial_amount self.C = 118 self.min_g1 = 112 self.max_g1 = 137 self.w1 = 116 def stage_2(self): ### define second round #self.C2 = round(random.uniform(0.95*self.w1, 1.05*self.w1), 1) self.min_g2 = round(random.uniform(0.75*self.w1, 0.99*self.w1)) self.max_g2 = round(random.uniform(1.01*self.w1, 1.3*self.w1)) self.w2 = round(random.uniform(self.min_g2, self.max_g2)) 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.C def payoff(self): self.winning_round = self.session.vars['paying_round']