/*Source for routines to sort/and set MVP media through
  powerups struct
  Also includes LOGGING of STATS for offensive,defensive acts,
  for stats-tracking software.
  Scott "Banshee" Labrecque
 
*/
#include "g_local.h"
#include "bb_mvp.h"

/*
================
Scans all players, gets scores, sorts totals, 
and award the MVP Medals
 
================
*/
void scanPlayersMVP (void) {
    int				i, numSorted,mvpDMVPHolder,mvpDMVPScore,mvpOMVPHolder,mvpOMVPScore;
    gclient_t		*cl;

    mvpDMVPHolder = 0;
    mvpDMVPScore = 0;
    mvpOMVPHolder = 0;
    mvpOMVPScore = 0;

    numSorted = level.numConnectedClients;

    if ( g_gametype.integer == GT_TEAM || g_gametype.integer == GT_CTF) {

        //Offense Loop(doing both in the same loop caused some sorting issues for me
        for (i=0 ; i < numSorted ; i++) {

            cl = &level.clients[level.sortedClients[i]];

            /*This is where we need to write the clientNum to a
              data type to record WHO has the highest MVP score */
            //if client is in spectator at time of death, they dont get a medal!
            //we dont tally their mvp points then, ONLY their normal stats
            if (cl->ps.pm_type == PM_SPECTATOR)
                continue;
            //Do writing to log only in offense loops, , no need for dupes
            G_LogPrintf( " MVP Logging = client: %i %s defense: %i  offense: %i  frags: %i \n",
                         level.sortedClients[i],
                         cl->pers.netname,
                         cl->pers.mvpDefense,
                         cl->pers.mvpOffense,
                         cl->ps.stats[STAT_FRAGS] );


            if ((cl->pers.mvpOffense + cl->ps.stats[STAT_FRAGS]) > mvpOMVPScore) {
                mvpOMVPScore = (cl->pers.mvpOffense + cl->ps.stats[STAT_FRAGS]);
                mvpOMVPHolder = i;
            }
        }

        numSorted = level.numConnectedClients;
        //Defense Loop - Remember to check to see if current MVP awardee is an offense winner
        for (i=0 ; i < numSorted ; i++) {

            cl = &level.clients[level.sortedClients[i]];
            //if client is in spectator at time of death, they dont get a medal!
            if (cl->ps.pm_type == PM_SPECTATOR)
                continue;

            /*scan defense stats in clientnum order. if current client
              num = clientnum of offense mvp, move on */

            if ((cl->pers.mvpDefense + cl->ps.stats[STAT_FRAGS]) > mvpDMVPScore) {
                mvpDMVPScore = (cl->pers.mvpDefense + cl->ps.stats[STAT_FRAGS]);
                mvpDMVPHolder = i;
            }
        }

        //secondary mvp loop if you win BOTH medals
        numSorted = level.numConnectedClients;
        if (mvpOMVPHolder == mvpDMVPHolder) {
            if (mvpOMVPScore > mvpDMVPScore) {
                mvpDMVPScore = 0;
                mvpDMVPHolder = 0;
                for (i=0 ; i < numSorted ; i++) {
                    cl = &level.clients[level.sortedClients[i]];
                    //if client is in spectator at time of death, they dont get a medal!
                    if (cl->ps.pm_type == PM_SPECTATOR)
                        continue;
                    /*scan defense stats in clientnum order. if current client
                          num = clientnum of offense mvp, move on */

                    if ((cl->pers.mvpDefense + cl->ps.stats[STAT_FRAGS]) > mvpDMVPScore) {
                        if (mvpOMVPHolder == i) {
                            //do nothing
                        }
                        else {
                            mvpDMVPScore = (cl->pers.mvpDefense + cl->ps.stats[STAT_FRAGS]);
                            mvpDMVPHolder = i;
                        }
                    }
                }
            } else {
                mvpOMVPHolder = 0;
                mvpOMVPScore = 0;
                for (i=0 ; i < numSorted ; i++) {
                    cl = &level.clients[level.sortedClients[i]];
                    //if client is in spectator at time of death, they dont get a medal!
                    if (cl->ps.pm_type == PM_SPECTATOR)
                        continue;
                    /*scan defense stats in clientnum order. if current client
                    num = clientnum of offense mvp, move on */

                    if ((cl->pers.mvpOffense + cl->ps.stats[STAT_FRAGS]) > mvpOMVPScore) {
                        if (mvpDMVPHolder == i) {
                            //do nothing
                        }
                        else {
                            mvpOMVPScore = (cl->pers.mvpOffense + cl->ps.stats[STAT_FRAGS]);
                            mvpOMVPHolder = i;
                        }
                    }
                }
            }
        }


        //End Secondary Loop
        /*
        cl = &level.clients[level.sortedClients[mvpOMVPHolder]];	
        G_LogPrintf( " MVP AWARDS = offense: %i %s score:%i\n", 
        	level.sortedClients[mvpOMVPHolder],
        	cl->pers.netname, 
        	mvpOMVPScore);
        cl->ps.powerups[PW_OMVP] |= INT_MAX;    


        cl = &level.clients[level.sortedClients[mvpDMVPHolder]];
        G_LogPrintf( " MVP AWARDS = defense: %i %s score:%i\n", 
        	level.sortedClients[mvpDMVPHolder],
        	cl->pers.netname, 
        	mvpDMVPScore);
        cl->ps.powerups[PW_DMVP] |= INT_MAX;
        */
    }
}
/*
================
Scans all players, stats, and writes out in a logfile format
================
*/
void scanPlayersStats (void) {
    int				i, numSorted;
    gclient_t		*cl;

    numSorted = level.numConnectedClients;

    if ( g_gametype.integer == GT_TEAM || g_gametype.integer == GT_CTF) {

        //Offense Loop(doing both in the same loop caused some sorting issues for me
        G_LogPrintf( "Stats Score Tracking - Start:\n");
        for (i=0 ; i < numSorted ; i++) {

            cl = &level.clients[level.sortedClients[i]];

            //G_LogPrintf( "SST %i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%d,client: %i,%s\n",
            G_LogPrintf( "SST %i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%i,%d,client: %i,%s\n",
                         cl->pers.statBaseDefense,
                         cl->pers.statCap,
                         //cl->ps.persistant[PERS_CAP_COUNT],
                         cl->pers.statCarrierDefense,
                         cl->pers.statCarrierSave,
                         cl->pers.statDefenderKill,
                         //cl->pers.statEnemyCarrierKill,
                         //cl->ps.persistant[PERS_FC_KILLS],
                         cl->pers.statFlagHolding,
                         cl->pers.statFlagReturnAssist,
                         cl->ps.stats[STAT_FLAGRETURN],
                         cl->ps.stats[STAT_FRAGS],
                         //cl->ps.persistant[PERS_ACCURACY_SHOTS],
                         //cl->ps.persistant[PERS_ACCURACY_HITS],
                         cl->ps.persistant[PERS_IMPRESSIVE_COUNT],
                         cl->ps.persistant[PERS_EXCELLENT_COUNT],
                         cl->ps.persistant[PERS_GAUNTLET_FRAG_COUNT],
                         //cl->ps.persistant[PERS_HEADSHOT_COUNT],
                         cl->ps.persistant[PERS_HITS],
                         //			cl->pers.statRailShots,
                         //			cl->pers.statRailHits,
                         cl->ps.persistant[PERS_SCORE],
                         cl->ps.persistant[PERS_KILLED],
                         ((cl->pers.cmd.serverTime - cl->pers.enterTime) / 60000),
                         level.sortedClients[i],
                         cl->pers.netname
                       );
        }
        G_LogPrintf( "-Stats Score Tracking - End:\n" );
    }
}
