from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, cu, ) from urllib import parse import ast from otree.app_template.models import Player import random import itertools author = 'Monika Leszczynska' doc = """ Checking 'd' and 'b' letters """ class Constants(BaseConstants): name_in_url = 'matrices_second' players_per_group = None task_timer = 120 num_rounds = 10 d_total = 20 b_total = 60 total_letters = 80 fixed_payoff = cu(1.00) bonus_payoff = cu(0.15) class Subsession(BaseSubsession): def creating_session(self): n = Constants.total_letters for p in self.get_players(): # create a list of letter choices # ---------------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n + 1)] # create a list corresponding to form_field variables including all choices # ---------------------------------------------------------------------------------------------------------- form_fields = ['choice_' + str(k) for k in indices] # create a list of letters # ---------------------------------------------------------------------------------------------------------- d_list = ['d'] * Constants.d_total b_list = ['b'] * Constants.b_total letters_list = d_list + b_list p.participant.vars['letters_choices'] = list( zip( indices, form_fields, letters_list ) ) p.participant.vars['letters_choices_made'] = [None for j in range(1, n + 1)] p.participant.vars['endgame'] = 0 class Group(BaseGroup): pass class Player(BasePlayer): # create choice fields for the letters task # ---------------------------------------------------------------------------------------------------------- for j in range(1, Constants.total_letters + 1): locals()['choice_' + str(j)] = models.StringField(blank=True) del j # create fields for consent # ---------------------------------------------------------------------------------------------------------- consent = models.BooleanField(blank=True, initial=False) submit_noConsent = models.BooleanField(blank=True, initial=False) # create fields for demographics questionnaire # ---------------------------------------------------------------------------------------------------------- gender = models.StringField() gender_others = models.StringField(blank=True) age = models.IntegerField(min=18, max=100) # create field for players task timer and field for prolific PID # ---------------------------------------------------------------------------------------------------------- task_timer = models.IntegerField(initial=Constants.task_timer) # create fields for checking correctness of the task # ---------------------------------------------------------------------------------------------------------- d_crossed = models.IntegerField(initial=0) b_crossed = models.IntegerField(initial=0) total_correct = models.IntegerField(initial=0) correct = models.IntegerField(initial=0) is_correct = models.BooleanField(initial=False) def count_effort(self): self.d_crossed = self.participant.vars['letters_choices_made'].count('d') self.b_crossed = self.participant.vars['letters_choices_made'].count('b') def check_correctness(self): if self.participant.vars['letters_choices_made'].count('d') == Constants.d_total \ and self.participant.vars['letters_choices_made'].count('b') == 0: self.correct = 1 self.is_correct = True else: self.correct = 0 self.is_correct = False def update_endgame(self): self.participant.vars['endgame'] = 1 print(self.participant.vars['endgame']) def consent_error_message(self, value): print('value is', value) if value is None: return 'Please check the box if you agree!'