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 = 'mturk' players_per_group = None num_rounds = 1 instructions_template = 'stage4a/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)) check1 = ['0','50','100','200'] check2 = ['lower','higher'] check3 = ['lower','higher'] check4 = ['high','low','random'] to_continue = ['000M','000D','000N'] take_away_points = c(3) answer_if_do_nothing = c(0) answer_increment = c(1) answer_choices = currency_range(1, take_away_points, answer_increment) class Subsession(BaseSubsession): def creating_session(self): if self.round_number == 1: players = self.get_players() def player_type(self,player): total_answers = [p.number_id for p in self.get_players() if p.number_id != None ] # print(total_answers) sort_order = sorted(range(len(total_answers)), key=lambda k: total_answers[k]) print(sort_order) min_list = [] max_list = [] if len(sort_order) >= 32:#32 # rank = sort_order[player.member_id()-1] + 1 rank = sort_order.index(player.member_id() - 1) + 1 if rank<5: #5 player.participant.vars['type'] = 'C (participants who removed the fewest points)' return player.participant.vars['type'] elif rank>len(sort_order)-4: #4 player.participant.vars['type'] = 'A (participants who removed the most points)' return player.participant.vars['type'] else: player.participant.vars['type'] = 'member' return player.participant.vars['type'] 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 = 0 p.participant.vars['fouronebid'] = Constants.bid_fund - p.bid_leader else: leader = self.get_player_by_role('leader') leader.payoff = 0 p.participant.vars['fouronebid'] = 0 class Player(BasePlayer): number_id = models.IntegerField() 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)': return 'Non-dominant' elif self.participant.vars['type'] == 'A (participants who removed the most points)': return 'Dominant' else: return 'Member' def membername(self): if self.participant.vars['member_type'] == '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)' password_to_continue = models.CharField(initial='') Anger15 = models.IntegerField() Annoyed15 = models.IntegerField() Envy15 = models.IntegerField() MTL1 = models.IntegerField() RPS1 = models.IntegerField() JS1 = models.IntegerField() text = models.LongStringField()