from otree.api import * import random import collections import math doc = """ Coin & Dice """ class C(BaseConstants): NAME_IN_URL = 'coinanddice' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 class Subsession(BaseSubsession): total_headsr = models.IntegerField(initial=1) total_1r = models.IntegerField(initial=1) total_2r = models.IntegerField(initial=1) total_3r = models.IntegerField(initial=1) total_4r = models.IntegerField(initial=1) total_5r = models.IntegerField(initial=1) total_6r = models.IntegerField(initial=1) total_headsp = models.FloatField(initial = math.log(0.5)) total_tailsp = models.FloatField(initial = math.log(0.5)) totalrate_1p = models.FloatField(initial = math.log(1/6)) totalrate_2p = models.FloatField(initial = math.log(1/6)) totalrate_3p = models.FloatField(initial = math.log(1/6)) totalrate_4p = models.FloatField(initial = math.log(1/6)) totalrate_5p = models.FloatField(initial = math.log(1/6)) totalrate_6p = models.FloatField(initial = math.log(1/6)) num_people = models.IntegerField(initial=1) class Group(BaseGroup): pass class Player(BasePlayer): treatment = models.IntegerField() coin_1 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_2 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_3 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_4 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) coin_5 = models.IntegerField( choices =[[1,'表'],[0,'裏']] ) headsrate_p = models.IntegerField(min = 1, max = 100) dice1 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice2 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice3 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice4 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice5 = models.IntegerField( choices = [1,2,3,4,5,6] ) dice1rate_p = models.IntegerField(min = 1, max = 100) dice2rate_p = models.IntegerField(min = 1, max = 100) dice3rate_p = models.IntegerField(min = 1, max = 100) dice4rate_p = models.IntegerField(min = 1, max = 100) dice5rate_p = models.IntegerField(min = 1, max = 100) dice6rate_p = models.IntegerField(min = 1, max = 100) #Function def creating_session(subsession): import itertools num_treat = itertools.cycle([1, 2, 3]) for player in subsession.get_players(): player.treatment = next(num_treat) # PAGES class Instruction(Page): pass class Instruction2(Page): pass ### class coin_report_1(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5'] @staticmethod def is_displayed(player): return player.treatment == 1 class coin_report_2(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5'] @staticmethod def is_displayed(player): return player.treatment == 2 class coin_report_3(Page): form_model = 'player' form_fields = ['coin_1','coin_2','coin_3','coin_4','coin_5'] @staticmethod def is_displayed(player): return player.treatment == 3 def vars_for_template(player): total_headsr = player.subsession.total_headsr total_headsp = player.subsession.total_headsp total_tailsp = player.subsession.total_tailsp sub_num_people = player.subsession.num_people return dict( total_headsr = total_headsr, total_headsp = total_headsp, total_tailsp = total_tailsp, sub_num_people = sub_num_people, ) ### class coin_prediction(Page): form_model = 'player' form_fields = ['headsrate_p'] ### class dice_report_1(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5'] @staticmethod def is_displayed(player): return player.treatment == 1 class dice_report_2(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5'] @staticmethod def is_displayed(player): return player.treatment == 2 class dice_report_3(Page): form_model = 'player' form_fields = ['dice1','dice2','dice3','dice4','dice5'] @staticmethod def is_displayed(player): return player.treatment == 3 def vars_for_template(player): total_1r = player.subsession.total_1r total_2r = player.subsession.total_2r total_3r = player.subsession.total_3r total_4r = player.subsession.total_4r total_5r = player.subsession.total_5r total_6r = player.subsession.total_6r totalrate_1p = player.subsession.totalrate_1p totalrate_2p = player.subsession.totalrate_2p totalrate_3p = player.subsession.totalrate_3p totalrate_4p = player.subsession.totalrate_4p totalrate_5p = player.subsession.totalrate_5p totalrate_6p = player.subsession.totalrate_6p sub_num_people = player.subsession.num_people return dict( total_1r = total_1r, total_2r = total_2r, total_3r = total_3r, total_4r = total_4r, total_5r = total_5r, total_6r = total_6r, totalrate_1p = totalrate_1p, totalrate_2p = totalrate_2p, totalrate_3p = totalrate_3p, totalrate_4p = totalrate_4p, totalrate_5p = totalrate_5p, totalrate_6p = totalrate_6p, sub_num_people = sub_num_people, ) ### class dice_prediction(Page): form_model = 'player' form_fields = ['dice1rate_p','dice2rate_p','dice3rate_p','dice4rate_p','dice5rate_p','dice6rate_p'] @staticmethod def error_message(players,values): print('value is', values) if values['dice1rate_p']+values['dice2rate_p']+values['dice3rate_p']+values['dice4rate_p']+values['dice5rate_p']+values['dice6rate_p'] != 100: return '合計で100%になるよう回答してください。' class Results(Page): def vars_for_template(player): id_in_group = player.id_in_group treatment = player.treatment return dict( id_in_group = id_in_group, treatment = treatment ) def before_next_page(player: Player,timeout_happened): print("finish") #人数を追加 player.subsession.num_people += 1 #報告を追加 player.subsession.total_headsr += (player.coin_1 + player.coin_2 + player.coin_3 + player.coin_4 + player.coin_5) ids = [player.dice1,player.dice2,player.dice3,player.dice4,player.dice5] c = collections.Counter(ids) player.subsession.total_1r += c[1] player.subsession.total_2r += c[2] player.subsession.total_3r += c[3] player.subsession.total_4r += c[4] player.subsession.total_5r += c[5] player.subsession.total_6r += c[6] #予測を追加 player.subsession.total_headsp += math.log(player.headsrate_p/100) player.subsession.total_tailsp += math.log(1-(player.headsrate_p/100)) player.subsession.totalrate_1p += math.log(player.dice1rate_p/100) player.subsession.totalrate_2p += math.log(player.dice2rate_p/100) player.subsession.totalrate_3p += math.log(player.dice3rate_p/100) player.subsession.totalrate_4p += math.log(player.dice4rate_p/100) player.subsession.totalrate_5p += math.log(player.dice5rate_p/100) player.subsession.totalrate_6p += math.log(player.dice6rate_p/100) page_sequence = [ Instruction, Instruction2, coin_report_1, coin_report_2, coin_report_3, coin_prediction, dice_report_1, dice_report_2, dice_report_3, dice_prediction, Results]