from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) import random author = 'Celine Zhang' doc = """ This is a one-period public goods game with 32 players and 4 per group. survey of stage 2.1 """ class Constants(BaseConstants): name_in_url = 'stage2a' players_per_group = None num_rounds = 1 instructions_template = 'stage2a/Instructions.html' remain = c(20) take_away_points = c(60) answer_if_do_nothing = c(0) answer_increment = c(3) answer_choices = currency_range(0, take_away_points, answer_increment) answer_choices_count = len(answer_choices) keep_give_amounts = []; for answer in answer_choices: keep_give_amounts.append((answer, remain - answer)) check1=['10','90','100','110'] check2=['-35','0','35','70'] check3=['0','5','6.67','20'] class Subsession(BaseSubsession): def creating_session(self): self.group_randomly() def player_type(self,player): total_answers = [(p.ans1 + p.ans2 + p.ans3) for p in self.get_players() if p.ans1 != None and p.ans2 != None and p.ans3 != 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): contribute = models.CurrencyField() class Player(BasePlayer): s2q1attempt = models.IntegerField(initial=0) s2q2attempt = models.IntegerField(initial=0) ans1 = models.CurrencyField(choices=Constants.answer_choices) ans2 = models.CurrencyField(choices=Constants.answer_choices) ans3 = models.CurrencyField(choices=Constants.answer_choices) def member_id(self): # player 1 is the leader, so member 1 is actually player 2 # return (self.id_in_group) return (self.participant.id_in_session) password = models.CharField(initial='') check1 = models.CharField(choices=Constants.check1) check2 = models.CharField(choices=Constants.check2) check3 = models.CharField(choices=Constants.check3) 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' password_after1 = models.CharField(initial='') punishcheck = models.CharField(choices=['Group Members will contribute less', 'Group Members contributions will not change', 'Group Members will contribute more' ], widget=widgets.RadioSelect) punishcheck2 = models.CharField(choices=['Removes many points from him/her', 'Do nothing', 'Removes a few points from him/her' ], widget=widgets.RadioSelect) Domcheck = models.CharField(choices=['D', 'M', 'N', 'T', 'G', 'U', ], widget=widgets.RadioSelect)