from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer ) import csv import random author = 'Eveline Vandewal' doc = """ Part 2 """ class Constants(BaseConstants): name_in_url = 'GMTA1_Part2' players_per_group = None with open('GMTA1_Part2/situations.csv') as situations_file: situations = list(csv.DictReader(situations_file)) num_rounds = len(situations) class Subsession(BaseSubsession): def calculate_payoffs(self): for p in self.get_players(): p.paying_period = random.randint(1, 80) p.paying_decision = random.randint(1, 2) p.participant.vars['paying_period'] = p.paying_period p.participant.vars['paying_decision'] = p.paying_decision p.paying_situation = p.participant.vars.get('situation_array')[p.paying_period - 1] if p.paying_situation <= 40: p.other_situation = p.paying_situation if 40 < p.paying_situation <= 60: p.other_situation = p.paying_situation + 20 if p.paying_situation > 60: p.other_situation = p.paying_situation - 20 others_used = p.participant.vars.get('others_used') p.other1 = others_used[1 - 1] p.other2 = others_used[2 - 1] p.choice_used = p.participant.vars.get('choice_data')[p.paying_situation - 1] p.belief_used = p.participant.vars.get('belief_data')[p.paying_situation - 1] p.problem_used = p.participant.vars.get('problem_data')[p.paying_situation - 1] if p.problem_used: p.profit_part2 = 0 else: if p.paying_decision == 1: for q in self.get_players(): if p.other1 == q.id_in_group: p.choice_other = q.participant.vars.get('choice_data')[p.other_situation - 1] if p.paying_situation <= 20 or 40 < p.paying_situation <= 60: if p.choice_used == p.choice_other: p.profit_part2 = 15 if p.choice_used != p.choice_other: p.profit_part2 = 0 if 20 < p.paying_situation <= 40 or 60 < p.paying_situation <= 80: if p.choice_used != p.choice_other: p.profit_part2 = 15 if p.choice_used == p.choice_other: p.profit_part2 = 0 if p.paying_decision == 2: for q in self.get_players(): if p.other2 == q.id_in_group: p.choice_other = q.participant.vars.get('choice_data')[p.other_situation - 1] if p.choice_other == 1: p.loss = (p.belief_used / 100) * (p.belief_used / 100) p.random_number = random.uniform(0, 1) if p.loss < p.random_number: p.profit_part2 = 15 if p.loss > p.random_number: p.profit_part2 = 0 if p.choice_other == 2: p.loss = (1 - (p.belief_used / 100)) * (1 - (p.belief_used / 100)) p.random_number = random.uniform(0, 1) if p.loss < p.random_number: p.profit_part2 = 15 if p.loss > p.random_number: p.profit_part2 = 0 p.participant.vars['profit_part2'] = p.profit_part2 print(p.participant.vars) print(p.paying_period) print(p.paying_decision) print(p.paying_situation) print(p.other_situation) print(others_used) print(p.other1) print(p.other2) print(p.choice_used) print(p.belief_used) print(p.problem_used) print(p.profit_part2) print(p.choice_other) print(p.loss) print(p.random_number) def vars_for_admin_report(self): profits_part2 = [p.profit_part2 for p in self.get_players()] return dict(profits_part2=profits_part2) class Group(BaseGroup): pass class Player(BasePlayer): situation_id = models.IntegerField() label_left = models.StringField() label_right = models.StringField() task_you = models.StringField() task_other = models.StringField() order = models.IntegerField() similarity = models.IntegerField() time_pressure = models.IntegerField() perceived_similarity = models.IntegerField() position = models.IntegerField() random_position = models.IntegerField() practice_submitted_decision = models.IntegerField() practice_submitted_choice = models.IntegerField( choices=[ [1, 'Left Label'], [2, 'Right Label'], ], widget=widgets.RadioSelect ) practice_submitted_belief = models.IntegerField() practice_time_choice = models.FloatField() practice_time_choice_rounded = models.IntegerField() practice_time_belief = models.FloatField() practice_time_belief_rounded = models.IntegerField() new_practice_time_choice = models.FloatField() new_practice_time_choice_rounded = models.IntegerField() new_practice_time_belief = models.FloatField() new_practice_time_belief_rounded = models.IntegerField() practice_problem = models.BooleanField() submitted_decision = models.IntegerField() submitted_choice = models.IntegerField( choices=[ [1, 'Left Label'], [2, 'Right Label'], ], widget=widgets.RadioSelect ) submitted_belief = models.IntegerField() time_choice = models.FloatField() time_choice_rounded = models.IntegerField() time_belief = models.FloatField() time_belief_rounded = models.IntegerField() new_time_choice = models.FloatField() new_time_choice_rounded = models.IntegerField() new_time_belief = models.FloatField() new_time_belief_rounded = models.IntegerField() problem = models.BooleanField() paying_period = models.IntegerField() paying_decision = models.IntegerField() paying_situation = models.IntegerField() other_situation = models.IntegerField() other1 = models.IntegerField() other2 = models.IntegerField() choice_used = models.IntegerField() belief_used = models.IntegerField() problem_used = models.BooleanField() choice_other = models.IntegerField() loss = models.FloatField() random_number = models.FloatField() profit_part2 = models.IntegerField() def current_situation(self): return Constants.situations[self.participant.vars['situation_array'][self.round_number - 1] - 1]