from otree.api import * import settings import numpy as np import json import random doc = """ This is the app for result. It comes at the end. """ author = "Mouli Modak" class C(BaseConstants): NAME_IN_URL = 'results' PLAYERS_PER_GROUP = None NUM_PARAMS = settings.SESSION_CONFIG_DEFAULTS['num_parameters'] NUM_TYPES = settings.SESSION_CONFIG_DEFAULTS['num_types'] NUM_ROUNDS = 1 seed = random.randint(0, 2 ** 32 - 1) SURVEY_PAY = settings.SESSION_CONFIG_DEFAULTS['survey_payment'] class Subsession(BaseSubsession): pass class Group(BaseGroup): pass class Player(BasePlayer): # Questionnaire ageType = models.IntegerField( min=18, verbose_name='Age:' ) genderSelect = models.StringField( choices=['Male', 'Female', 'Prefer Not to Say' ], verbose_name='Gender:' ) majorType = models.StringField( verbose_name='Major:', ) hsSelect = models.StringField( choices=['In US', 'Outside of US', 'Prefer Not to Say' ], verbose_name='Went to High School:' ) complete = models.IntegerField(initial=0) mytype = models.StringField(initial="") myPayoff = models.FloatField(initial=0) yourChoice = models.BooleanField() otherid = models.IntegerField() exp_round_final = models.IntegerField() task_number_final = models.IntegerField() param_number_final = models.IntegerField() app_final = models.StringField(intial="") quantity_final = models.StringField() typeAPayoff_final = models.FloatField() typeBPayoff_final = models.FloatField() typeCPayoff_final = models.FloatField() # Subsession method def creating_session(subsession: Subsession): player_list = subsession.get_players() for p in player_list: p.mytype = p.participant.vars["mytype"] # CUSTOM FUNCTIONS def set_payoffs(subsession): players_list = subsession.get_players() # Choosing a random task totalDecisions_list = [] for p in players_list: # print(p.participant.vars["myGroupID"]) totalDecisions_list.append(len(p.participant.vars["myGroupID"])) totalDecisions = min(totalDecisions_list) # print("List of lengths of decision", totalDecisions_list) # print("Minimum Length = ", totalDecisions) task_in_session = random.randint(0, totalDecisions-1) # print("Chosen Task = ", task_in_session) totalNumGroups = int(len(players_list)/(2*C.NUM_TYPES)) # Categorizing players in groups of task task_in_session players_group_dict = {key: [] for key in range(1, totalNumGroups+1)} for p in players_list: # print(p.participant.vars["myGroupID"][task_in_session]) group_id = p.participant.vars["myGroupID"][task_in_session] players_group_dict[group_id].append(p) # For each player choose a random player for payment for ig in range(1, totalNumGroups+1): other_ID_chosen_player = 0 exp_round_final = 0 task_number_final = 0 param_number_final = 0 quantity_final = str(0) typeAPayoff_final = 0 typeBPayoff_final = 0 typeCPayoff_final = 0 app_final = 0 # Generating random player ID player_in_session = random.randint(1, 2*C.NUM_TYPES) # Setting variables for players for pg in players_group_dict[ig]: # If player is chosen player_id_in_group_in_task = pg.participant.vars["myIDinGroup"][task_in_session] if player_id_in_group_in_task == player_in_session: pg.yourChoice = True exp_round_final = pg.participant.vars["roundHistory"][task_in_session] task_number_final = pg.participant.vars["taskHistory"][task_in_session] param_number_final = pg.participant.vars["paramHistory"][task_in_session] quantity_final = str(pg.participant.vars["quantityHistory"][task_in_session]) typeAPayoff_final = pg.participant.vars["typeAPayoffHistory"][task_in_session] typeBPayoff_final = pg.participant.vars["typeBPayoffHistory"][task_in_session] typeCPayoff_final = pg.participant.vars["typeCPayoffHistory"][task_in_session] app_final = pg.participant.vars["appHistory"][task_in_session] if app_final != "DwoC": pg.otherid = int(pg.participant.vars["otherID"][task_in_session]) other_ID_chosen_player = pg.otherid else: pg.yourChoice = False # Setting other's type and changing yourChoice if in CwC for pg in players_group_dict[ig]: pg.exp_round_final = exp_round_final pg.task_number_final = task_number_final pg.param_number_final = param_number_final pg.quantity_final = str(quantity_final) pg.typeAPayoff_final = typeAPayoff_final pg.typeBPayoff_final = typeBPayoff_final pg.typeCPayoff_final = typeCPayoff_final pg.app_final = app_final if pg.mytype == "A": pg.myPayoff = typeAPayoff_final elif pg.mytype == "B": pg.myPayoff = typeBPayoff_final player_id_in_group_in_task = pg.participant.vars["myIDinGroup"][task_in_session] if pg.app_final != "DwoC": # If pg in other of chosen player in group if player_id_in_group_in_task == other_ID_chosen_player: # If app in CwC if pg.app_final == "CwC": pg.yourChoice = True # PAGES class P1_Questionnaire(Page): form_model = 'player' form_fields = ['ageType', 'genderSelect', 'majorType', 'hsSelect', 'complete'] # @ staticmethod # def before_next_page(player, timeout_happened): # if player.complete == 1: # player.payoff = 5 class WP1_Results(WaitPage): template_name = "results/WP1_Results.html" wait_for_all_groups = True after_all_players_arrive = set_payoffs @staticmethod def is_displayed(player: Player): return player.round_number == 1 class P2_Results(Page): @staticmethod def vars_for_template(player): """ A list of variables accessible in the template in GraphPage.html Parameters: player (Player): Player accessing the page Returns: dict: A dictionary of variables containing the chat channel the player will be accessing """ return dict( mytype=player.mytype, exp_round = player.exp_round_final, task_number = player.task_number_final, app_for_decision = player.app_final, ) @staticmethod def js_vars(player): """ A list of variables accessible in the javascript in Results.html Parameters: player (Player): Player accessing the page Returns: dict: A dictionary of variables containing equations, colors, player type, and other player type """ if player.complete == 1: player.payoff = player.myPayoff + C.SURVEY_PAY else: player.payoff = player.myPayoff if(player.session.config['line_input'] != "None"): equations = json.loads(player.session.config['line_input']) else: equations = player.session.config['line_dict'] return dict( equations=equations, colors = player.session.config['graph_colors'], yourChoice=player.yourChoice, mytype=player.mytype, param_number = player.param_number_final, quantity = player.quantity_final, myPayoff = player.myPayoff, app_for_decision = player.app_final, ) def is_displayed(self): """ Displays the Results page after the final round Args: self (Player): The player accessing the page Returns: bool: true if it's the final round, false otherwise """ if self.round_number == C.NUM_ROUNDS: return True else: return False pass page_sequence = [ P1_Questionnaire, WP1_Results, P2_Results ]