from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random doc = """ this is demo of public goods stage 2 second part """ class Constants(BaseConstants): name_in_url = 's5atest' players_per_group = None num_rounds = 1 instructions_template = 's5atest/Instructions.html' initial_endowment = c(90) tax = c(10) bid_fund = c(100) take_away_endo = c(40) pool_multiplier = c(2) take_away_multiplier = c(3) answer_increment = c(1) leader_decision = currency_range(0, take_away_endo, answer_increment) leader_decision_count = len(leader_decision) keep_give_amounts = [] for answer in leader_decision: keep_give_amounts.append((answer, take_away_endo - answer)) password = ['The total contribution of my group will determine the victory status.','The victory status between groups is randomly determined.'] check1 = ['Total group contribution', 'Randomly determined','Total earnings of individual members'] check2 = ['+5%', '+10%', '+15%', '+20%','+25%'] class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: players = self.get_players() def member_type(self,player): bid_point = [p.bid_leader for p in self.get_players() if p.bid_leader!= None] print(bid_point) member_idlist = [p.member_id()for p in self.get_players() if p.bid_leader!= None] print(member_idlist) sort_order = sorted(range(len(bid_point)), key=lambda k: bid_point[k]) print(sort_order) min_list = [] max_list = [] # return player.member_id() if len(sort_order) >= 6 and player.member_id() in member_idlist: rank = sort_order.index(member_idlist.index(player.member_id())) + 1 if rank < 4: player.participant.vars['member_type51'] = 'Category C (participants who removed the fewest points)' return player.participant.vars['member_type51'] else: player.participant.vars['member_type51'] = 'Category A (participants who removed the most points)' return player.participant.vars['member_type51'] else: player.participant.vars['member_type51'] = 'Leader' return 'Leader' class Group(BaseGroup): def set_payoff(self): for p in self.get_players(): if 'member' in p.role(): p.bid_cost = p.bid_leader p.payoff = Constants.bid_fund - p.bid_leader p.participant.vars['fiveonebid'] = Constants.bid_fund - p.bid_leader else: leader = self.get_player_by_role('leader') leader.payoff = 0 p.participant.vars['fiveonebid'] = 0 class Player(BasePlayer): bid_cost = models.CurrencyField() def member_id(self): # player 1 is the leader, so member 1 is actually player 2 return (self.participant.id_in_session) def role(self): if self.participant.vars['type'] == 'C (participants who removed the fewest points)' or self.participant.vars['type'] == 'A (participants who removed the most points)': return 'leader' else: print("1") return 'member' def membername51(self): if self.participant.vars['member_type51'] == 'Category C (participants who removed the fewest points)': return 'Category C (participants who removed the fewest points)' else: return 'Category A (participants who removed the most points)' bid_leader = models.CurrencyField( min=0, max=Constants.bid_fund, doc="""How much you want to pay to bid a leader? from 0 to 100""", ) password = models.CharField(choices=Constants.password) password_two = models.CharField(initial='') check1 = models.CharField(choices=['higher Total Group Contribution', 'lower Total Group Contribution', ], widget=widgets.RadioSelect) check2 = models.CharField(choices=['It will remain the same', 'It will increase by 10%', 'It will decrease by 10%', 'It will increase by 20%', 'It will decrease by 20%'], widget=widgets.RadioSelect) password_after4 = models.CharField(initial='')