from otree.api import * import random class Constants(BaseConstants): name_in_url = 'choice' players_per_group = 2 num_rounds = 1 participation_fee = cu(10) # parameters for contract good_state_probability = .2 value_N = cu(200) value_A = cu(180) value_B = cu(120) cost_N = cu(30) cost_A = cu(55) cost_B = cu(40) total_quantity = 5 # 抽取无重复的三轮支付 # round_chosen = random.sample(range(1, num_rounds + 1), 3) class Subsession(BaseSubsession): is_good_state = models.BooleanField() def creating_session(subsession: Subsession): # 每一轮角色保持不变 subsession.group_randomly(fixed_id_in_group=True) r = random.random() subsession.is_good_state = (r <= Constants.good_state_probability) class Group(BaseGroup): price_good = models.CurrencyField( choices=[35, 55, 75, 95, 115, 135], widget=widgets.RadioSelect, label="景气环境下你的出价是" ) price_bad = models.CurrencyField( choices=[60, 70, 80, 90, 100, 110], widget=widgets.RadioSelect, label="不景气环境下你的出价是" ) quantity_A_bad = models.IntegerField( choices=[0, 1, 2, 3, 4, 5], widget=widgets.RadioSelect, label="不景气环境下你提供的A产品数量是" ) quantity_B_bad = Constants.total_quantity - quantity_A_bad class Player(BasePlayer): total_payoff = models.CurrencyField() # function def set_payoffs(group): p1 = group.get_player_by_id(1) p2 = group.get_player_by_id(2) #if group.subsession.is_good_state: #p1.payoff = (Constants.value_N - group.price_good) * Constants.total_quantity #p2.payoff = (group.price_good - Constants.cost_N) * Constants.total_quantity #else: p1.payoff = (Constants.value_A - group.price_bad) * group.quantity_A_bad + ( Constants.value_B - group.price_bad) * group.quantity_B_bad p2.payoff = (group.price_bad - Constants.cost_A) * group.quantity_A_bad + ( group.price_bad - Constants.value_B) * group.quantity_B_bad # PAGES class Welcome(Page): pass class RoleAssign(Page): pass class GoodStatePrice(Page): form_model = 'group' form_fields = ['price_good'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitP1_GoodState(WaitPage): pass class StateReveal(Page): pass class BadStatePrice(Page): form_model = 'group' form_fields = ['price_bad'] @staticmethod def is_displayed(player): return player.id_in_group == 1 class WaitP1_BadState(WaitPage): pass class BadStateQuantity(Page): form_model = 'group' form_fields = ['quantity_A_bad'] @staticmethod def is_displayed(player): return player.id_in_group == 2 class WaitP2_BadState(WaitPage): pass class WaitRoundResults(WaitPage): after_all_players_arrive = 'set_payoffs' class RoundResults(Page): pass class Results(Page): @staticmethod def is_displayed(player: Player): return player.round_number == Constants.num_rounds @staticmethod def vars_for_template(player: Player): player.total_payoff = Constants.participation_fee for k in Constants.round_chosen: player.total_payoff = player.total_payoff + player.in_round(k).payoff page_sequence = [ Welcome, RoleAssign, GoodStatePrice, WaitP1_GoodState, StateReveal, BadStatePrice, WaitP1_BadState, BadStateQuantity, WaitP2_BadState, RoundResults, Results, ]