import os from otree.api import * # for randomly assigned conditions import random doc = """ 个体是如何分配一定数量的金钱。 """ class Constants(BaseConstants): name_in_url = 'dictator' players_per_group = 2 num_rounds = 1 # instruction template instructions_template = 'dictator/instructions.html' # Initial amount allocated to the dictator endowment = cu(100) #问题 # to measure postquestions surveyName = "事后问题" surveyInstruction = "请您认真回答以下问题:" current_working_path = os.getcwd() txtFileName = 'Questions.txt' appFolderName = 'dictator' txtPathName = "%s/%s/%s"%(current_working_path, appFolderName, txtFileName) with open(txtPathName, 'r', encoding='utf-8') as file_object: text = [] for line in file_object.readlines(): text.append(line.strip()) answer = [[1, '非常不满意'], [2, '比较不满意'], [3, '一般满意'], [4, '比较满意'], [5, '非常满意']] class Subsession(BaseSubsession): pass class Group(BaseGroup): kept = models.CurrencyField( doc="""Amount dictator decided to keep for himself""", min=0, max=Constants.endowment, ) condition = models.StringField() class Player(BasePlayer): Questionsq1 = models.IntegerField(label=Constants.text[0], choices=Constants.answer, widget=widgets.RadioSelect) Questionsq2 = models.IntegerField(label=Constants.text[1], choices=Constants.answer, widget=widgets.RadioSelect) Questionsq3 = models.IntegerField(label=Constants.text[2], choices=Constants.answer, widget=widgets.RadioSelect) # FUNCTIONS def creating_session(subsession: Subsession): for group in subsession.get_groups(): import random group.condition = random.choice(['experimental', 'control']) # print("@@@@ set group.condition to ", group.condition) def set_payoffs(group: Group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) p1.payoff = group.kept p2.payoff = Constants.endowment - group.kept # setting waiting page def print_par_id(group): print("@@@@ wait for chat") # PAGES class Welcome(Page): pass class InformedConsent(Page): # form_model = 'player' # form_fields = ['agreement'] pass class MyWaitPage1(WaitPage): group_by_arrival_time = True title_text = "等待界面" body_text = "请稍等,正在等待另一位参与者的加入。" class Introduction(Page): pass class Role(Page): pass # @staticmethod # def is_displayed(player: Player): # return player.id_in_group == 1 class ResultsWaitPage1(WaitPage): after_all_players_arrive = print_par_id class ChatWait(Page): # waiting about 5 seconds timeout_seconds = 5 class Chat(Page): # timeout_seconds = 60 # print('@@@@ time out seconds 60s') # 用is_display 函数,选择只在实验组呈现聊天界面 @staticmethod def is_displayed(player: Player): return player.group.condition == "experimental" # pass class Offer(Page): form_model = 'group' form_fields = ['kept'] @staticmethod def is_displayed(player: Player): return player.id_in_group == 1 class ResultsWaitPage(WaitPage): after_all_players_arrive = set_payoffs title_text = "等待界面" body_text = "请稍等,其它参与者正在决定中。" class Results(Page): @staticmethod def vars_for_template(player: Player): group = player.group return dict(offer=Constants.endowment - group.kept) class Questions(Page): form_model = 'player' form_fields = ['Questionsq1', 'Questionsq2', 'Questionsq3'] # page sequence page_sequence = [ Introduction, Role, ResultsWaitPage1, ChatWait, Chat, Offer, ResultsWaitPage, Results, # Chat2, Questions ]