var Mode = require('./Mode');

function Teams() {
    Mode.apply(this, Array.prototype.slice.call(arguments));

    this.ID = 0;
    this.name = "Teams";
    this.decayMod = 1.5;
    this.packetLB = 50;
    this.haveTeams = true;
    this.colorFuzziness = 32;

    // Special
    this.teamAmount = 3; // Amount of teams. Having more than 3 teams will cause the leaderboard to work incorrectly (client issue).
    this.colors = [
        {'r':255 - this.colorFuzziness, 'g':  0 + this.colorFuzziness, 'b':  0 + this.colorFuzziness},
        {'r':  0 + this.colorFuzziness, 'g':255 - this.colorFuzziness, 'b':  0 + this.colorFuzziness},
        {'r':  0 + this.colorFuzziness, 'g':  0 + this.colorFuzziness, 'b':255 - this.colorFuzziness},
    ]; // Make sure you add extra colors here if you wish to increase the team amount [Default colors are: Red, Green, Blue]
    this.nodes = []; // Teams
}

module.exports = Teams;
Teams.prototype = new Mode();

//Gamemode Specific Functions

Teams.prototype.fuzzColorComponent = function(component, amount) {
    component += Math.floor(Math.random() * (amount * 2 + 1)) - amount;
    return component < 0 ? 0 : component > 255 ? 255 : component;
};

Teams.prototype.fuzzColor = function(color, amount) {
    return {
        r: this.fuzzColorComponent(color.r, amount),
        b: this.fuzzColorComponent(color.b, amount),
        g: this.fuzzColorComponent(color.g, amount)
    };
};

Teams.prototype.getTeamColor = function(team) {
    return this.fuzzColor(this.colors[team], this.colorFuzziness);
};

// Override

Teams.prototype.onServerInit = function(gameServer) {
    // Set up teams
    for (var i = 0; i < this.teamAmount; i++) {
        this.nodes[i] = [];
    }
};

Teams.prototype.onPlayerInit = function(player) {
    // Get random team
    player.team = Math.floor(Math.random() * this.teamAmount);
    player.color = this.getTeamColor(player.team);
};

Teams.prototype.onCellAdd = function(cell) {
    // Add to team list
    this.nodes[cell.owner.getTeam()].push(cell);
};

Teams.prototype.onCellRemove = function(cell) {
    // Remove from team list
    var index = this.nodes[cell.owner.getTeam()].indexOf(cell);
    if (index != -1) {
        this.nodes[cell.owner.getTeam()].splice(index, 1);
    }
};

Teams.prototype.updateLB = function(gameServer) {
    var total = 0;
    var teamMass = [];
    // Get mass
    for (var i = 0; i < this.teamAmount; i++) {
        // Set starting mass
        teamMass[i] = 0;

        // Loop through cells
        for (var j = 0; j < this.nodes[i].length;j++) {
            var cell = this.nodes[i][j];

            if (!cell) {
                continue;
            }

            teamMass[i] += cell.mass;
            total += cell.mass;
        }
    }
    // Calc percentage
    for (var i = 0; i < this.teamAmount; i++) {
        // No players
        if (total <= 0) {
            continue;
        }

        gameServer.leaderboard[i] = teamMass[i]/total;
    }
};


@@ -439,301 +439,100 @@
 'r':
-255 - this.colorFuzziness, 'g':  0 + this.colorFuzziness, 'b':  0 + this.colorFuzziness%7D,%0A        %7B'r':  0 + this.colorFuzziness, 'g':255 - this.colorFuzziness, 'b':  0 + this.colorFuzziness%7D,%0A        %7B'r':  0 + this.colorFuzziness, 'g':  0 + this.colorFuzziness, 'b':255 - this.colorFuzziness
+ 223, 'g': 0, 'b': 0%7D,%0A        %7B'r': 0, 'g': 223, 'b': 0%7D,%0A        %7B'r': 0, 'g': 0, 'b': 223
 %7D,%0A 
@@ -829,24 +829,16 @@
 omponent
-, amount
 ) %7B%0A    
@@ -859,112 +859,55 @@
 ath.
-floor(Math.random() * (amount * 2 + 1)) - amount;%0A    return component %3C 0 ? 0 : component %3E 255 ? 255 :
+random() * this.colorFuzziness %3E%3E 0;%0A    return
  com
@@ -930,28 +930,31 @@
 s.prototype.
-fuzz
+getTeam
 Color = func
@@ -962,24 +962,50 @@
 ion(
-color, amount) %7B
+team) %7B%0A    var color = this.colors%5Bteam%5D;
 %0A   
@@ -1056,24 +1056,16 @@
 (color.r
-, amount
 ),%0A     
@@ -1101,24 +1101,16 @@
 (color.b
-, amount
 ),%0A     
@@ -1150,16 +1150,8 @@
 or.g
-, amount
 )%0A  
@@ -1155,24 +1155,37 @@
 %0A    %7D;%0A%7D;%0A%0A
+// Override%0A%0A
 Teams.protot
@@ -1192,122 +1192,197 @@
 ype.
-getTeamColor = function(team) %7B%0A    return this.fuzzColor(this.colors%5Bteam%5D, this.colorFuzziness);%0A%7D;%0A%0A// Override
+onPlayerSpawn = function(gameServer,player) %7B%0A    // Random color based on team%0A    player.color = this.getTeamColor(player.team);%0A    // Spawn player%0A    gameServer.spawnPlayer(player);%0A%7D;
 %0A%0ATe
@@ -1679,59 +1679,8 @@
 t);%0A
-    player.color = this.getTeamColor(player.team);%0A
 %7D;%0A%0A

Simplify code