View Single Post
  #1  
Old 09-09-2012, 11:13 PM
xXziroXx xXziroXx is offline
Malorian
xXziroXx's Avatar
Join Date: May 2004
Posts: 5,289
xXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant futurexXziroXx has a brilliant future
Method of sorting players into teams

Didn't really want to put this in the code gallery since it's nothing fancy what so ever, but I figured it may serve some use to someone eventually.

PHP Code:
function onCreated()
{
  
// -- START EDIT
  
temp.objectList    allplayers// List of players to process
  
temp.numberOfTeams 2// Number of teams
  // -- END EDIT
  
  
temp.playerList = new[0];
  
// Generates a new list of objects we can work with
  
for (temp.objecttemp.objectList) {
    
// Let's avoid adding those silly RemoteControls
    
if (temp.object.level.name != "")
      continue;
    
    
temp.playerList.add(temp.object);
  }
  
  
// Amount of players
  
temp.amountOfPlayers temp.playerList.size();
  
// Array creation based on number of teams
  
temp.teams           = new[temp.numberOfTeams];
  
temp.team            0;
  
  
// Do we have enough players to make even teams?
  
if (temp.amountOfPlayers%temp.numberOfTeams)
    
temp.oddManOut true;
  
  
// Let's add players to the teams!
  
while (!temp.teamsGenerated) {
    
// Grab a random player from the list...
    
temp.object      randomString(temp.playerList);
    
temp.objectIndex temp.playerList.index(temp.object);
    
    
// Abort if an object wasn't found anymore
    
if (temp.objectIndex == -1)
      return echo(
"[" this.name "]: Index of object returned -1!");
    
    
// Add the player to the team array
    
temp.teams[temp.team].add(temp.object);
    
// Remove the player from the player list
    
temp.playerList.delete(temp.objectIndex);
    
    echo(
"[" this.name "]: Added '" temp.object "' to team '" temp.team "'!");
    
    
// Based on how many players are left, we may stop now.
    
temp.remainingPlayers temp.playerList.size();
    if (
temp.remainingPlayers == 0)
      break;
    else if (
temp.remainingPlayers == temp.numberOfTeams && temp.oddManOut)
      break;
    
    
// If we do have more players to work with, change the team for the next one.
    
temp.team ++;
    
temp.team %= temp.numberOfTeams;
  }
  
  echo(
"[" this.name "]: All" SPC temp.numberOfTeams SPC "teams sorted evenly!" SPC temp.remainingPlayers SPC "player(s) were left out" SPC (temp.remainingPlayers ":(" ":)"));

All you really need to do is provide a list of players and determine the amount of teams, and it'll do the rest of the work for you.
__________________
Follow my work on social media post-Graal:Updated august 2025.

Last edited by xXziroXx; 09-09-2012 at 11:49 PM..
Reply With Quote