Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Method of sorting players into teams (https://forums.graalonline.com/forums/showthread.php?t=134267095)

xXziroXx 09-09-2012 11:13 PM

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.

adam 09-10-2012 12:23 AM

I was an essential part of testing this script!
Quote:

Weapon/GUI-script Public/xXziroXx/TeamRandomizer added/updated by xXziroXx
[Public/xXziroXx/TeamRandomizer]: Added 'xXziroXx' to team '0'!
[Public/xXziroXx/TeamRandomizer]: Added 'Graal771034' to team '1'!
[Public/xXziroXx/TeamRandomizer]: Added 'MMM_P2P' to team '0'!
[Public/xXziroXx/TeamRandomizer]: Added 'adam' to team '1'!
[Public/xXziroXx/TeamRandomizer]: All 2 teams sorted evenly! 0 player(s) were left out :)


All times are GMT +2. The time now is 12:57 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.