var debug = require('debug')('wol:leaderboard');

/* interpret packet and structure match object for easier use */
module.exports = function parse(game, match) {
    /* fail if we're somehow missing players */
    if (!match.players || match.players.length < 1) return;

    /* normalize nicks and remove useless entries */
    var spectators = [];
    match.players.forEach(function(player, index) {
        /* typically Computer */
        if (!player.nam) {
            player.nam = 'Computer'; // stylized (CaMeLcAsE)
            player.name = 'computer'; // computational (lowercase)
            return;
        }

        /* lowercase username for further reference */
        player.name = player.nam.toLowerCase();

        /* remove spectators */
        if (player.spc && player.spc > 0) {
            spectators.push(index);
            return;
        }
    });

    spectators.forEach(function(spectator) {
        match.players.splice(spectator, 1);
    });

    /* if we have ra stats, normalize packet then carry on  */
    if (game == 'ra' || game == 'am') {
        match = require(global.cwd + '/lib/games/lib/ra').normalize(match);
    }

    /* if we have td stats, normalize packet then carry on  */
    if (game == 'td') {
        match = require(global.cwd + '/lib/games/lib/td').normalize(match);
    }

    /* determine win/loss outcome for each player */
    match.players.forEach(function(player) {
        player.won = 0;
        player.loss = 0;
        player.discon = 0;

        /* shortcut: if player lost connection */
        if (player.lcn && player.lcn > 0) {
            player.discon = 1;
        }

        /* evaluate completions */
        if (player.cmp) {
            switch (player.cmp) {
                case 2:
                    player.loss = 1;
                    player.discon = 1;
                    break;

                case 256:
                    player.won = 1;
                    break;

                case 512: /* resigned */
                case 528: /* defeated */
                    player.loss = 1;
                    break;
            }
        }

        /* remove unused info */
        delete player.ipa; /* could be tunnel or p2p (unreliable) */
        delete player.adr; /* same reason but for ra1 */
        delete player.ser; /* serials are not used on CnCNet */
    });

    return match;
};

@@ -325,24 +325,21 @@
     var 
-spectato
+playe
 rs = %5B%5D;
@@ -387,24 +387,140 @@
 r, index) %7B%0A
+        /* do nothing with spectators */%0A        if (player.spc && player.spc %3E 0) %7B%0A            return;%0A        %7D%0A%0A
         /* t
@@ -632,95 +632,8 @@
 sE)%0A
-            player.name = 'computer'; // computational (lowercase)%0A            return;%0A
     
@@ -758,101 +758,54 @@
  /* 
-remove spectators */%0A        if (player.spc && player.spc %3E 0) %7B%0A            spectato
+keep sanitized player */%0A        playe
 rs.push(
 inde
@@ -804,150 +804,88 @@
 ush(
-index
+player
 );%0A    
-        return;%0A        %7D%0A    %7D);%0A%0A    spectators.forEach(function(spectator) %7B%0A        match.players.splice(spectator, 1);%0A    %7D)
+%7D);%0A%0A    /* reassign players to keep */%0A    match.players = players
 ;%0A%0A 

preserve non-spectators