from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage ) import csv cu = c doc = '' class Constants(BaseConstants): name_in_url = 'real_effort' players_per_group = 3 payment_per_correct_answer = 1 score_per_correct_answer = 1 with open('real_effort/answer_key.csv') as questions_file: questions = list(csv.DictReader(questions_file)) # num_rounds = len(questions) num_rounds = 70 player1_role = 'Player 1' player2_role = 'Player 2' player3_role = 'Player 3' class Subsession(BaseSubsession): group_total_correct = models.IntegerField(initial=-1) shared_income = models.FloatField() def creating_session(self): if self.round_number == 1: self.session.vars['questions'] = Constants.questions.copy() for p in self.get_players(): question_data = p.current_question() p.round_number = self.round_number for g in self.get_groups(): if (g.id_in_subsession == 1): g.question_id = int(question_data['question_id_1']) g.solution = int(question_data['solution_1']) elif ( g.id_in_subsession == 2): g.question_id = int(question_data['question_id_2']) g.solution = int(question_data['solution_2']) else: g.question_id = int(question_data['question_id_3']) g.solution = int(question_data['solution_3']) def set_timer(self): import time for player in self.get_players(): player.participant.vars['expiry'] = time.time() + 35 * 60 def after_all_players_arrive(self): for group in self.get_groups(): group.team_check_correct() class Group(BaseGroup): question_id = models.IntegerField() solution = models.IntegerField() team_is_correct = models.IntegerField(initial=-1) team_total_correct = models.IntegerField(initial=-1) team_total_incorrect = models.IntegerField() def team_check_correct(group): players = group.get_players() team_answers = [p.is_correct for p in players if p.is_correct !=-1] if (sum(team_answers) == 3 and len(team_answers) == 3): group.team_is_correct = 1 elif (sum(team_answers) != 3 and len(team_answers) == 3): group.team_is_correct = 0 else: group.team_is_correct = -1 team_in_all_rounds = group.in_all_rounds() filterT = [g.team_is_correct for g in team_in_all_rounds if g.team_is_correct !=-1] group.team_total_correct = sum(filterT) group.team_total_incorrect = len(filterT) - sum(filterT) class Player(BasePlayer): task_answers = models.IntegerField(label="Answer (the number of 4s):") is_correct = models.IntegerField(initial=-1) timePlayed = models.FloatField(initial=0) games_played = models.IntegerField(blank=True) f_open = models.FloatField(blank=True) timeSpent = models.FloatField(initial=0) total_time_played = models.FloatField(blank=True) total_game_income = models.FloatField(blank=True) player_payoff = models.FloatField() other_teams_correct = models.IntegerField() def role(self): if self.id_in_group == 1: return 'Player 1' if self.id_in_group == 2: return 'Player 2' if self.id_in_group == 3: return 'Player 3' def current_question(self): return self.session.vars['questions'][self.round_number - 1] def check_correct(self): group = self.group if (self.task_answers == self.group.solution and self.task_answers != -1): self.is_correct = 1 elif (self.task_answers != self.group.solution and self.task_answers != -1): self.is_correct = 0 else: self.is_correct = -1