from otree.api import * import random from otree.models import player, subsession doc = """ This is a standard 2-player trust game where the amount sent by player 1 gets tripled. The trust game was first proposed by Berg, Dickhaut, and McCabe (1995) . """ class Constants(BaseConstants): name_in_url = 'regret' players_per_group = None num_rounds = 12 instructions_template = 'regret/instructions.html' money = 1000 class Subsession(BaseSubsession): price = models.IntegerField() class Group(BaseGroup): pass class Player(BasePlayer): treatment = models.IntegerField() yours = models.IntegerField() other = models.IntegerField() is_regretted = models.BooleanField() points = models.IntegerField() gain = models.IntegerField() ask_price = models.IntegerField(min=1000, max=4000) payment = models.IntegerField(initial=0) utility = models.IntegerField(min=-10, max=10, initial=0) que1 = models.IntegerField( choices=[[0, '男性'], [1, '女性'], [2, 'その他']], widget=widgets.RadioSelect ) que2 = models.IntegerField(blank=True) que3 = models.IntegerField(choices=[ [1, '好まない'], [2, 'あまり好まない'], [3, 'どちらでもない'], [4, 'やや好む'], [5, '好む'] ]) que4 = models.LongStringField(blank=True) que5 = models.LongStringField(blank=True) # FANCTION def creating_session(s: Subsession): _diffs = [200, 500, 900, -200, -500, -900] def set_price(f, l): diffs = random.sample(_diffs, k=6) rounds = s.in_rounds(f, l) for d, r in zip(diffs, rounds): r.price = d set_price(1, 6) set_price(7, 12) for p in s.get_players(): p.treatment = random.choice([0, 1, 1, 1]) def set_card(p: Player): cards = [i for i in random.sample(range(1, 5, 1), k=2)] return cards def set_regret(p: Player, yours, other): if yours < other: p.is_regretted = True else: p.is_regretted = False def set_points(p: Player, yours): p.points = Constants.money * yours return p.points def set_product(p: Player, yours): thousand1 = ['お茶2L × 6', '柔軟剤', 'シャンプー詰め替え', 'うなぎの蒲焼'] thousand2 = ['マスク50枚', '米5kg', 'BBQ焼肉セット', 'ワイン一本'] thousand3 = ['美容室でカット代を支払う', '1か月のスマートフォン利用料金を支払う', '家電量販店で有線イヤホンを購入する', '家電量販店でモバイルバッテリーを購入する'] thousand4 = ['ホテル予約サイトで一泊分の宿泊代金を支払う', '家電量販店で扇風機を購入する', '1か月分の電気代を支払う'] if yours == 1: product = random.choice(thousand1) elif yours == 2: product = random.choice(thousand2) elif yours == 3: product = random.choice(thousand3) else: product = random.choice(thousand4) return product def set_gain(p: Player, ask_price, diff, points): p.payment = ask_price + diff p.gain = points - p.payment return p.payment def history(p: Player, num): if p.round_number == 12: index = [i for i in range(1, num+1)] all_ask_price = [p.ask_price for p in p.in_all_rounds()] all_payment = [p.payment for p in p.in_all_rounds()] all_utility = [p.utility for p in p.in_all_rounds()] all_gain = [p.gain for p in p.in_all_rounds()] all_cards = [p.yours for p in p.in_all_rounds()] all_sum = sum(all_gain) history = {'index': index, '予想価格': all_ask_price, '実際の価格': all_payment, '効用': all_utility, 'カード': all_cards, '利得': all_gain, '合計': all_sum} else: index = [i for i in range(1, num)] all_ask_price = [p.ask_price for p in p.in_previous_rounds()] all_payment = [p.payment for p in p.in_previous_rounds()] all_utility = [p.utility for p in p.in_previous_rounds()] history = {'index': index, '予想価格': all_ask_price, '実際の価格': all_payment, '効用': all_utility} return history # PAGES class Introduction(Page): def is_displayed(p: Player): return p.round_number == 1 class Select(Page): form_model = 'player' form_fields = ['yours', 'other'] @staticmethod def js_vars(p: Player): return dict( cards = set_card(p), treatment = p.in_round(1).treatment ) def vars_for_template(p: Player): return dict( treatment = p.in_round(1).treatment, ) class Transaction(Page): form_model = 'player' form_fields = ['ask_price'] def vars_for_template(p: Player): set_regret(p, p.yours, p.other) return dict( treatment = p.in_round(1).treatment, yours = p.yours, other = p.other, points = set_points(p, p.yours), product = set_product(p, p.yours) ) class Evaluation(Page): form_model = 'player' form_fields = ['utility'] def vars_for_template(p: Player): diff = p.subsession.price diff_minus = 0 if diff < 0: diff_minus = diff * -1 return dict( ask_price = p.ask_price, payment = set_gain(p, p.ask_price, diff, p.points), diff = diff, diff_minus = diff_minus, history = history(p, p.round_number) ) class Results(Page): def is_displayed(p: Player): return p.round_number == 12 def vars_for_template(p: Player): return dict( history = history(p, p.round_number) ) class Question(Page): form_model = 'player' form_fields = [ 'que1', 'que2', 'que3', 'que4', 'que5' ] def is_displayed(p: Player): return p.round_number == 12 def vars_for_template(p: Player): return dict( treatment = p.in_round(1).treatment, ) page_sequence = [Introduction, Select, Transaction, Evaluation, Results, Question]