from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, ) import random author = 'Endowment calculation' doc = """ Your app description """ class Constants(BaseConstants): name_in_url = 'my_endowment' players_per_group = 4 num_rounds = 1 participant_list = [] endowment_list_for_UF = [50,100,150,200] class Subsession(BaseSubsession): def creat_group(self): endowment_structure = [[1,5,9,13],[2,6,10,14],[3,7,11,15],[4,8,12,16]] if self.round_number == 1: self.set_group_matrix(endowment_structure) print(self.get_group_matrix()) class Group(BaseGroup): def set_participant_list(self): self.session.vars['participant_list'] = Constants.participant_list.copy() for p in self.get_players(): p.session.vars['participant_list'].append(p.participant.vars) print('participant_list is', p.session.vars['participant_list']) def endowment_in_stage2(self): for p in self.get_players(): p.set_endowment() p.participant.vars['endowment_in_stage2'] = p.endowment_stage2 print('participant.vars are like this:', p.participant.vars) class Player(BasePlayer): treatment = models.StringField() endowment_stage2 = models.IntegerField() participant_number = models.IntegerField() def score_num1(self): return sorted(self.session.vars['participant_list'], key=lambda x:x['score_in_stage1'])[-1] def score_num2(self): return sorted(self.session.vars['participant_list'], key=lambda x:x['score_in_stage1'])[-2] def score_num3(self): return sorted(self.session.vars['participant_list'], key=lambda x:x['score_in_stage1'])[-3] def set_endowment_UF(self): if self.participant.vars['treatment'] == 'UF': self.endowment_stage2 = self.session.vars['endowment_for_UF'].pop(random.randint(0,(len(self.session.vars['endowment_for_UF'])-1))) self.participant.vars['endowment_in_stage2'] = self.endowment_stage2 print('endowment list for UF is:', self.session.vars['endowment_for_UF']) print('participant.vars is:', self.participant.vars) else: pass def set_endowment(self): if self.participant.vars['treatment'] == 'UF': pass else: if self.participant.vars['participant_id'] == self.score_num1()['participant_id']: self.endowment_stage2 = 200 else: if self.participant.vars['participant_id'] == self.score_num2()['participant_id']: self.endowment_stage2 = 150 else: if self.participant.vars['participant_id'] == self.score_num3()['participant_id']: self.endowment_stage2 = 100 else: self.endowment_stage2 = 50