from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Barney Garda' doc = """ Blackjack - Decision Making from Intermediate Feedback """ class Constants(BaseConstants): name_in_url = 'blackjack' players_per_group = None num_practice_rounds = 5 # Non-oTree built-in member num_rounds = num_practice_rounds + 20 class Subsession(BaseSubsession): def draw_card(self): rank = random.randint(1, 10) # 1: Ace, others are numbers from 2 to 10 suit = random.randint(1, 4) # 1: club, 2: diamond, 3: heart, 4:spade return rank, suit def creating_session(self): first_draw = self.draw_card() second_draw = self.draw_card() self.first_draw_rank = first_draw[0] self.first_draw_suit = first_draw[1] self.second_draw_rank = second_draw[0] self.second_draw_suit = second_draw[1] first_draw_rank = models.IntegerField() first_draw_suit = models.IntegerField() second_draw_rank = models.IntegerField() second_draw_suit = models.IntegerField() class Group(BaseGroup): pass class Player(BasePlayer): first_draw_accepted = models.BooleanField() # first_draw_timed_out = models.BooleanField() highest_hand_first_draw = models.IntegerField(initial=0) highest_hand_second_draw = models.IntegerField(initial=0) def highest_hands(self): # for round in self.in_previous_rounds(): for round in self.in_rounds(1, self.round_number): if round.first_draw_accepted: if round.subsession.first_draw_rank + round.subsession.second_draw_rank \ > self.highest_hand_first_draw + self.highest_hand_second_draw: self.highest_hand_first_draw = round.subsession.first_draw_rank self.highest_hand_second_draw = round.subsession.second_draw_rank # print(self.highest_hand_first_draw, self.highest_hand_second_draw) def calculate_payoff(self): if(self.highest_hand_first_draw + self.highest_hand_second_draw) <= 15: self.payoff = self.session.config['participation_fee'] elif (self.highest_hand_first_draw + self.highest_hand_second_draw) == 16: self.payoff = self.session.config['participation_fee'] + self.session.config['participation_fee'] * 0.0625 elif (self.highest_hand_first_draw + self.highest_hand_second_draw) == 17: self.payoff = self.session.config['participation_fee'] + self.session.config['participation_fee'] * 0.125 elif (self.highest_hand_first_draw + self.highest_hand_second_draw) == 18: self.payoff = self.session.config['participation_fee'] + self.session.config['participation_fee'] * 0.25 elif (self.highest_hand_first_draw + self.highest_hand_second_draw) == 19: self.payoff = self.session.config['participation_fee'] + self.session.config['participation_fee'] * 0.5 elif (self.highest_hand_first_draw + self.highest_hand_second_draw) == 20: self.payoff = self.session.config['participation_fee'] + self.session.config['participation_fee'] * 1