from otree.api import * doc = """ Your app description """ class C(BaseConstants): NAME_IN_URL = 'redis_spectator' PLAYERS_PER_GROUP = None NUM_ROUNDS = 1 CHARITY = "the British Red Cross" RANGE_MIN = 0 RANGE_MAX = 100 RANGE_MID = (RANGE_MIN + RANGE_MAX) // 2 CUM_EU_FACTOR = 10 CUM_EU_FACTOR_SPLIT = CUM_EU_FACTOR // 2 COMBINED_TASK_INFO = False def task_skill(label): return models.StringField( widget=widgets.RadioSelect(), label=label, ) class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): worker_allocation = models.IntegerField() charity_allocation = models.IntegerField() actual_worker_allocation = models.IntegerField() actual_charity_allocation = models.IntegerField() performance_info = models.BooleanField() redis = models.BooleanField(initial=0, blank=True) task1_skill = task_skill("Which skill do you think is required to excel in Task 1? Choose only one from the list that you think best captures the skill required:") task2_skill = task_skill("Which skill do you think is required to excel in Task 2? Choose only one from the list that you think best captures the skill required:") task1_skill_innate = models.IntegerField(label="To what extent do you believe innate talent for this skill is necessary to excel in Task 1, or to what extent do you believe this skill can be learned?") task2_skill_innate = models.IntegerField(label="To what extent do you believe innate talent for this skill is necessary to excel in Task 2, or to what extent do you believe this skill can be learned?") worker_id = models.StringField() sub_id = models.StringField() score1 = models.IntegerField() score2 = models.IntegerField() worker_keep = models.BooleanField() matrix_first = models.BooleanField() width = models.IntegerField() height = models.IntegerField() counted_char = models.StringField() word_length = models.IntegerField() time_per_task = models.IntegerField() # FUNCTION def creating_session(subsession: Subsession): import random import csv f = open (__name__ + '/worker_rerun_2.csv', encoding='utf-8-sig') # please change this file whenever necessary rows = list(csv.DictReader(f)) # random.shuffle(rows) # shuffles the workers information across spectator links players = subsession.get_players() for i in range(len(players)): row = rows[i] player = players[i] player.worker_id = str(row['worker_id']) player.sub_id = str(row['sub_id']) player.score1 = int(row['score1']) player.score2 = int(row['score2']) player.worker_keep = int(row['worker_keep']) player.matrix_first = int(row['matrix_first']) player.width = int(row['width']) player.height = int(row['height']) player.counted_char = str(row['counted_char']) player.word_length = int(row['word_length']) player.time_per_task = int(row['time_per_task']) player.performance_info = random.choice([True, False]) def task1_skill_choices(player: Player): import random choices = ['Mathematical skill', 'Linguistic ability', 'Problem solving', 'Creative flair'] random.shuffle(choices) return choices def task2_skill_choices(player: Player): import random choices = ['Mathematical skill', 'Linguistic ability', 'Problem solving', 'Creative flair'] random.shuffle(choices) return choices # PAGES class SpectatorInstructions(Page): @staticmethod def vars_for_template(player: Player): return dict( charity=C.CHARITY, cum_eu_factor=C.CUM_EU_FACTOR, cum_eu_factor_split=C.CUM_EU_FACTOR_SPLIT ) class TaskCombinedInformationPage(Page): form_model = 'player' form_fields = ['task1_skill', 'task2_skill', 'task1_skill_innate', 'task2_skill_innate'] @staticmethod def vars_for_template(player: Player): return dict( matrix_first=player.matrix_first, time_per_task=player.time_per_task, WIDTH= player.width, HEIGHT=player.height, COUNTED_CHAR=player.counted_char, WORD_LENGTH=player.word_length, ) class TaskOneInformationPage(Page): form_model = 'player' form_fields = ['task1_skill', 'task1_skill_innate'] @staticmethod def vars_for_template(player: Player): if not player.matrix_first: return dict( matrix_first=player.matrix_first, time_per_task=player.time_per_task, WORD_LENGTH=player.word_length, ) else: return dict( matrix_first=player.matrix_first, time_per_task=player.time_per_task, WIDTH=player.width, HEIGHT=player.height, COUNTED_CHAR=player.counted_char, ) class TaskTwoInformationPage(Page): form_model = 'player' form_fields = ['task2_skill', 'task2_skill_innate'] @staticmethod def vars_for_template(player: Player): if player.matrix_first: return dict( matrix_first=player.matrix_first, time_per_task=player.time_per_task, WORD_LENGTH=player.word_length, ) else: return dict( matrix_first=player.matrix_first, time_per_task=player.time_per_task, WIDTH=player.width, HEIGHT=player.height, COUNTED_CHAR=player.counted_char, ) class RedisPage(Page): form_model = 'player' form_fields = ['worker_allocation', 'redis'] @staticmethod def vars_for_template(player: Player): if player.worker_keep: return dict( performance_info=player.performance_info, task1_score=player.score1, task2_score=player.score2, charity=C.CHARITY, cum_eu_factor=C.CUM_EU_FACTOR, cum_eu_factor_split=C.CUM_EU_FACTOR_SPLIT, matrix_first=player.matrix_first, range_min = C.RANGE_MIN, range_max = C.RANGE_MAX, range_mid = C.RANGE_MID, worker_initial_allocation = C.RANGE_MAX, charity_initial_allocation = C.RANGE_MIN, worker_keep = player.worker_keep, ) else: return dict( performance_info=player.performance_info, task1_score=player.score1, task2_score=player.score2, charity=C.CHARITY, cum_eu_factor=C.CUM_EU_FACTOR, cum_eu_factor_split=C.CUM_EU_FACTOR_SPLIT, matrix_first=player.matrix_first, range_min = C.RANGE_MIN, range_max = C.RANGE_MAX, range_mid = C.RANGE_MID, worker_initial_allocation = C.RANGE_MID, charity_initial_allocation = C.RANGE_MID, worker_keep = player.worker_keep, ) @staticmethod def before_next_page(player: Player, timeout_happened): player.charity_allocation = C.RANGE_MAX - player.worker_allocation if player.redis: player.actual_worker_allocation = player.worker_allocation player.actual_charity_allocation = C.RANGE_MAX - player.actual_worker_allocation else: if player.worker_keep: player.actual_worker_allocation = C.RANGE_MAX player.actual_charity_allocation = C.RANGE_MIN else: player.actual_worker_allocation = C.RANGE_MID player.actual_charity_allocation = C.RANGE_MID if C.COMBINED_TASK_INFO: page_sequence = [ SpectatorInstructions, TaskCombinedInformationPage, RedisPage ] else: page_sequence = [ SpectatorInstructions, TaskOneInformationPage, TaskTwoInformationPage, RedisPage ]