#1 by Bochen
Hi all, I would like to assign players to groups according to their roles, which are determined by their choices in a previous app. For example, based on their decisions in the first app, players have the role of either "high score" or "low score". In the second app, players need to be divided into groups of four people - one with the role of "high score", and the other three with "low score". Now the logic of assigning roles to players seems to divide players into groups by their ID and then assign roles to them within a group, so I am wondering whether it is possible that players first have roles and then are assigned to groups according to their roles. Do you have any thoughts on achieving this function? Thanks in advance! Best, Bochen
#2 by xindamate_xyz
# At the end of the first app, where you determine roles player.participant.vars['role'] = player.role # Assuming 'role' is a field on your Player model in the first app # In the first app's models.py class Player(BasePlayer): role = models.StringField() # Assuming the role is determined at the end of the first app def set_role(player): # Your logic here to determine the role player.role = 'high score' # or 'low score' player.save() # In the second app's models.py class Subsession(BaseSubsession): def creating_session(self): players = self.get_players() # Retrieve roles and sort players high_scores = [p for p in players if p.participant.vars['role'] == 'high score'] low_scores = [p for p in players if p.participant.vars['role'] == 'low score'] # Ensure there's a mechanism to handle if there aren't enough high score players or the division isn't perfect # For simplicity, this example assumes an ideal scenario groups = [] # Assuming there's enough players to form complete groups of 4 with 1 high score and 3 low scores each while high_scores and len(low_scores) >= 3: group = [high_scores.pop()] group.extend(low_scores[:3]) low_scores = low_scores[3:] groups.append(group) # Assign the groups self.set_group_matrix(groups) Best regards, Tina Zheng Visit our website https://otree.xindamate.xyz/en for more information