from otree.api import * c = cu doc = '' class Constants(BaseConstants): name_in_url = 'start' players_per_group = 2 num_rounds = 2 expert_role = 'A' consumer_role = 'B' valuation = 10 cost1 = 2 cost2 = 6 outside = 1.6 outside_test = 20 # def creating_session(subsession): # if player.round_number==1: # new_structure = [[1,2], [3,4], [5,6], [7,8]] # subsession.set_group_matrix(new_structure) # else: # new_structure2 = [[1,4], [3,6], [5,8], [7,2]] # subsession.set_group_matrix(new_structure2) class Subsession(BaseSubsession): pass ## ER working code for random matching # def creating_session(subsession): # if subsession.round_number ==1 : # session = subsession.session # subsession.group_randomly(fixed_id_in_group=True) # else: # session = subsession # subsession.group_randomly(fixed_id_in_group=True) ## ER working code for manual definition of matching each round (e.g. matching groups of eight) def creating_session(subsession): if subsession.round_number==1: new_structure = [[1,2], [3,4], [5,6], [7,8]] subsession.set_group_matrix(new_structure) else: new_structure2 = [[1,4], [3,6], [5,8], [7,2]] subsession.set_group_matrix(new_structure2) # def make_matrix(n_players, offset): # p1s = list(range(1, n_players // 2)) # p2s = list(range(n_players // 2, n_players + 1)) # return list(zip(p1s, p2s[offset:] + p2s[:offset])) # print(matrix) # subsession.set_group_matrix(matrix) def set_payoffs(group): if group.interaction==1 and group.price== 1 : group.pricechoice = group.price1 elif group.interaction==1 and group.price== 2: group.pricechoice = group.price2 if group.interaction==1 and group.action == 1 : cost=Constants.cost1 elif group.interaction==1 and group.action == 2 : cost= Constants.cost2 if group.interaction==1 and group.action >= group.problem : group.sufficient = 1 elif group.interaction==1 and group.action < group.problem: group.sufficient= 0 p1=group.get_player_by_role("A") p2=group.get_player_by_role("B") if group.interaction==1 and group.sufficient == 0: p1.payoff = group.pricechoice - cost + group.gift p2.payoff = - group.pricechoice -group.gift elif group.interaction==1 and group.sufficient == 1 : p1.payoff = group.pricechoice - cost + group.gift p2.payoff = Constants.valuation - group.pricechoice -group.gift else: p1.payoff = Constants.outside p2.payoff = Constants.outside def set_problem(group): import random group.rand = random.choice([1,2]) if group.rand == 1: group.problem = 1 else: group.problem = 2 class Group(BaseGroup): price1 = models.IntegerField(max=11, min=1) price2 = models.IntegerField(max=11, min=1) interaction = models.IntegerField(choices=[[0, 'No'], [1, 'Yes']], widget=widgets.RadioSelect) gift = models.IntegerField(choices=[[0, 'No'], [1, 'Yes']], widget=widgets.RadioSelect) price = models.IntegerField(choices=[[1, 'Price 1'], [2, 'Price 2']]) action = models.IntegerField(choices=[[1, 'Action 1'], [2, 'Action 2']]) pricechoice = models.IntegerField() problem = models.IntegerField() rand = models.IntegerField() sufficient = models.IntegerField() class Player(BasePlayer): pass class PriceA(Page): form_model = 'group' form_fields = ['price1', 'price2'] # timeout_seconds = 5 @staticmethod def is_displayed(player): return player.role == Constants.expert_role @staticmethod def error_message(player, values): if values['price1'] > values['price2']: return 'Price 1 must be lower or equal to price 2' # @staticmethod #changed below for bots # def get_timeout_seconds(player): # participant = player.participant # if participant.is_dropout: # return 1 # instant timeout, 1 second # else: # return 5*60 # @staticmethod # def before_next_page(player, timeout_happened): # participant = player.participant # if timeout_happened: # price1 = 4 # price2 = 8 # participant.is_dropout = True class Wait1consumer(WaitPage): after_all_players_arrive = 'set_problem' @staticmethod def is_displayed(player): return player.role == Constants.consumer_role class Interaction(Page): form_model = 'group' form_fields = ['interaction'] @staticmethod def is_displayed(player): return player.role == Constants.consumer_role class Gift(Page): form_model = 'group' form_fields = ['gift'] @staticmethod def is_displayed(player): group = player.group return player.role == Constants.consumer_role and group.interaction==1 class Wait1expert(WaitPage): @staticmethod def is_displayed(player): return player.role == Constants.expert_role class DecisionA(Page): form_model = 'group' form_fields = ['action', 'price'] @staticmethod def is_displayed(player): group = player.group return player.role == Constants.expert_role and group.interaction==1 class MyWaitPage(WaitPage): after_all_players_arrive = 'set_payoffs' class Results(Page): form_model = 'player' page_sequence = [PriceA, Wait1consumer, Interaction, Gift, Wait1expert, DecisionA, MyWaitPage, Results]