// IW6 GSC SOURCE
// Generated by https://github.com/xensik/gsc-tool

init()
{
    if ( !isdefined( game["gamestarted"] ) )
    {
        setmatchdatadef( "mp/matchdata.def" );
        setmatchdata( "map", level.script );

        if ( level.hardcoremode )
        {
            var_0 = level.gametype + " hc";
            setmatchdata( "gametype", var_0 );
        }
        else
            setmatchdata( "gametype", level.gametype );

        setmatchdata( "buildVersion", getbuildversion() );
        setmatchdata( "buildNumber", getbuildnumber() );
        setmatchdata( "dateTime", getsystemtime() );
        setmatchdataid();
    }

    level.maxlives = 285;
    level.maxnamelength = 26;
    level.maxevents = 150;
    level.maxkillstreaks = 64;
    level.maxlogclients = 30;
    level.maxnumchallengesperplayer = 10;
    level.maxnumawardsperplayer = 10;

    if ( !maps\mp\_utility::is_aliens() )
    {
        level thread gameendlistener();
        level thread endofgamesummarylogger();
    }
}

getmatchdatetime()
{
    return getmatchdata( "dateTime" );
}

logkillstreakevent( var_0, var_1 )
{
    if ( !canlogclient( self ) || !canlogkillstreak() )
        return;

    var_2 = getmatchdata( "killstreakCount" );
    setmatchdata( "killstreakCount", var_2 + 1 );
    setmatchdata( "killstreaks", var_2, "eventType", var_0 );
    setmatchdata( "killstreaks", var_2, "player", self.clientid );
    setmatchdata( "killstreaks", var_2, "eventTime", gettime() );
    setmatchdata( "killstreaks", var_2, "eventPos", 0, int( var_1[0] ) );
    setmatchdata( "killstreaks", var_2, "eventPos", 1, int( var_1[1] ) );
    setmatchdata( "killstreaks", var_2, "eventPos", 2, int( var_1[2] ) );
}

loggameevent( var_0, var_1 )
{
    if ( !canlogclient( self ) || !canlogevent() )
        return;

    var_2 = getmatchdata( "eventCount" );
    setmatchdata( "eventCount", var_2 + 1 );
    setmatchdata( "events", var_2, "eventType", var_0 );
    setmatchdata( "events", var_2, "player", self.clientid );
    setmatchdata( "events", var_2, "eventTime", gettime() );
    setmatchdata( "events", var_2, "eventPos", 0, int( var_1[0] ) );
    setmatchdata( "events", var_2, "eventPos", 1, int( var_1[1] ) );
    setmatchdata( "events", var_2, "eventPos", 2, int( var_1[2] ) );
}

logkillevent( var_0, var_1 )
{
    if ( !canloglife( var_0 ) )
        return;

    setmatchdata( "lives", var_0, "modifiers", var_1, 1 );
}

logmultikill( var_0, var_1 )
{
    if ( !canloglife( var_0 ) )
        return;

    setmatchdata( "lives", var_0, "multikill", var_1 );
}

logplayerlife()
{
    if ( !canlogclient( self ) )
        var_0 = level.maxlives;

    if ( self.curclass == "gamemode" )
        var_0 = self logmatchdatalife( self.clientid, self.spawnpos, self.spawntime, self.wasti );
    else if ( issubstr( self.curclass, "custom" ) )
    {
        var_1 = maps\mp\_utility::getclassindex( self.curclass );
        var_2 = maps\mp\gametypes\_class::cac_getweapon( var_1, 0 );
        var_3 = maps\mp\gametypes\_class::cac_getweaponattachment( var_1, 0 );
        var_4 = maps\mp\gametypes\_class::cac_getweaponattachmenttwo( var_1, 0 );
        var_5 = maps\mp\gametypes\_class::cac_getweapon( var_1, 1 );
        var_6 = maps\mp\gametypes\_class::cac_getweaponattachment( var_1, 1 );
        var_7 = maps\mp\gametypes\_class::cac_getweaponattachmenttwo( var_1, 1 );
        var_8 = "none";
        var_9 = maps\mp\gametypes\_class::cac_getperk( var_1, 0 );
        var_0 = self logmatchdatalife( self.clientid, self.spawnpos, self.spawntime, self.wasti, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 );
        logplayerabilityperks( var_0 );
    }
    else
    {
        var_1 = maps\mp\_utility::getclassindex( self.curclass );
        var_2 = maps\mp\gametypes\_class::table_getweapon( level.classtablename, var_1, 0 );
        var_3 = maps\mp\gametypes\_class::table_getweaponattachment( level.classtablename, var_1, 0, 0 );
        var_4 = maps\mp\gametypes\_class::table_getweaponattachment( level.classtablename, var_1, 0, 1 );
        var_5 = maps\mp\gametypes\_class::table_getweapon( level.classtablename, var_1, 1 );
        var_6 = maps\mp\gametypes\_class::table_getweaponattachment( level.classtablename, var_1, 1, 0 );
        var_7 = maps\mp\gametypes\_class::table_getweaponattachment( level.classtablename, var_1, 1, 1 );
        var_8 = maps\mp\gametypes\_class::table_getoffhand( level.classtablename, var_1 );
        var_9 = maps\mp\gametypes\_class::table_getequipment( level.classtablename, var_1, 0 );
        var_0 = self logmatchdatalife( self.clientid, self.spawnpos, self.spawntime, self.wasti, var_2, var_3, var_4, var_5, var_6, var_7, var_8, var_9 );
        logplayerabilityperks( var_0 );
    }

    return var_0;
}

logplayerabilityperks( var_0 )
{
    if ( getdvarint( "scr_trackPlayerAbilities", 0 ) != 0 )
    {
        if ( isdefined( self.abilityflags ) && self.abilityflags.size == 2 )
        {
            setmatchdata( "lives", var_0, "abilityFlags", 0, self.abilityflags[0] );
            setmatchdata( "lives", var_0, "abilityFlags", 1, self.abilityflags[1] );
        }
    }
}

logplayerxp( var_0, var_1 )
{
    if ( !canlogclient( self ) )
        return;

    setmatchdata( "players", self.clientid, var_1, var_0 );
}

logplayerdeath( var_0, var_1, var_2, var_3, var_4, var_5, var_6 )
{
    if ( !canlogclient( self ) )
        return;

    if ( var_0 >= level.maxlives )
        return;

    if ( isplayer( var_1 ) && canlogclient( var_1 ) )
        self logmatchdatadeath( var_0, self.clientid, var_1, var_1.clientid, var_4, var_3, maps\mp\_utility::iskillstreakweapon( var_4 ), var_1 maps\mp\_utility::isjuggernaut() );
    else
        self logmatchdatadeath( var_0, self.clientid, undefined, undefined, var_4, var_3, maps\mp\_utility::iskillstreakweapon( var_4 ), 0 );
}

logplayerdata()
{
    if ( !canlogclient( self ) )
        return;

    setmatchdata( "players", self.clientid, "score", maps\mp\_utility::getpersstat( "score" ) );

    if ( maps\mp\_utility::getpersstat( "assists" ) > 255 )
        setmatchdata( "players", self.clientid, "assists", 255 );
    else
        setmatchdata( "players", self.clientid, "assists", maps\mp\_utility::getpersstat( "assists" ) );

    if ( maps\mp\_utility::getpersstat( "longestStreak" ) > 255 )
        setmatchdata( "players", self.clientid, "longestStreak", 255 );
    else
        setmatchdata( "players", self.clientid, "longestStreak", maps\mp\_utility::getpersstat( "longestStreak" ) );

    if ( maps\mp\_utility::getpersstat( "validationInfractions" ) > 255 )
        setmatchdata( "players", self.clientid, "validationInfractions", 255 );
    else
        setmatchdata( "players", self.clientid, "validationInfractions", maps\mp\_utility::getpersstat( "validationInfractions" ) );
}

endofgamesummarylogger()
{
    level waittill( "game_ended" );

    foreach ( var_1 in level.players )
    {
        wait 0.05;

        if ( !isdefined( var_1 ) )
            continue;

        if ( isdefined( var_1.detectedexploit ) && var_1.detectedexploit && var_1 maps\mp\_utility::rankingenabled() )
            var_1 setrankedplayerdata( "restXPGoal", var_1.detectedexploit );

        if ( isdefined( var_1.weaponsused ) )
        {
            var_1 doublebubblesort();
            var_2 = 0;

            if ( var_1.weaponsused.size > 3 )
            {
                for ( var_3 = var_1.weaponsused.size - 1; var_3 > var_1.weaponsused.size - 3; var_3-- )
                {
                    var_1 setcommonplayerdata( "round", "weaponsUsed", var_2, var_1.weaponsused[var_3] );
                    var_1 setcommonplayerdata( "round", "weaponXpEarned", var_2, var_1.weaponxpearned[var_3] );
                    var_2++;
                }
            }
            else
            {
                for ( var_3 = var_1.weaponsused.size - 1; var_3 >= 0; var_3-- )
                {
                    var_1 setcommonplayerdata( "round", "weaponsUsed", var_2, var_1.weaponsused[var_3] );
                    var_1 setcommonplayerdata( "round", "weaponXpEarned", var_2, var_1.weaponxpearned[var_3] );
                    var_2++;
                }
            }
        }
        else
        {
            var_1 setcommonplayerdata( "round", "weaponsUsed", 0, "none" );
            var_1 setcommonplayerdata( "round", "weaponsUsed", 1, "none" );
            var_1 setcommonplayerdata( "round", "weaponsUsed", 2, "none" );
            var_1 setcommonplayerdata( "round", "weaponXpEarned", 0, 0 );
            var_1 setcommonplayerdata( "round", "weaponXpEarned", 1, 0 );
            var_1 setcommonplayerdata( "round", "weaponXpEarned", 2, 0 );
        }

        if ( isdefined( var_1.operationscompleted ) )
            var_1 setcommonplayerdata( "round", "operationNumCompleted", var_1.operationscompleted.size );
        else
            var_1 setcommonplayerdata( "round", "operationNumCompleted", 0 );

        for ( var_3 = 0; var_3 < 5; var_3++ )
        {
            if ( isdefined( var_1.operationscompleted ) && isdefined( var_1.operationscompleted[var_3] ) && var_1.operationscompleted[var_3] != "ch_prestige" && !issubstr( var_1.operationscompleted[var_3], "_daily" ) && !issubstr( var_1.operationscompleted[var_3], "_weekly" ) )
            {
                var_1 setcommonplayerdata( "round", "operationsCompleted", var_3, var_1.operationscompleted[var_3] );
                continue;
            }

            var_1 setcommonplayerdata( "round", "operationsCompleted", var_3, "" );
        }

        if ( isdefined( var_1.challengescompleted ) )
            var_1 setcommonplayerdata( "round", "challengeNumCompleted", var_1.challengescompleted.size );
        else
            var_1 setcommonplayerdata( "round", "challengeNumCompleted", 0 );

        for ( var_3 = 0; var_3 < 20; var_3++ )
        {
            if ( isdefined( var_1.challengescompleted ) && isdefined( var_1.challengescompleted[var_3] ) && var_1.challengescompleted[var_3] != "ch_prestige" && !issubstr( var_1.challengescompleted[var_3], "_daily" ) && !issubstr( var_1.challengescompleted[var_3], "_weekly" ) )
            {
                var_1 setcommonplayerdata( "round", "challengesCompleted", var_3, var_1.challengescompleted[var_3] );
                continue;
            }

            var_1 setcommonplayerdata( "round", "challengesCompleted", var_3, "" );
        }

        var_1 setcommonplayerdata( "round", "gameMode", level.gametype );
        var_1 setcommonplayerdata( "round", "map", tolower( getdvar( "mapname" ) ) );

        if ( issquadsmode() )
        {
            var_1 setcommonplayerdata( "round", "squadMode", 1 );
            continue;
        }

        var_1 setcommonplayerdata( "round", "squadMode", 0 );
    }
}

doublebubblesort()
{
    var_0 = self.weaponxpearned;
    var_1 = self.weaponxpearned.size;

    for ( var_2 = var_1 - 1; var_2 > 0; var_2-- )
    {
        for ( var_3 = 1; var_3 <= var_2; var_3++ )
        {
            if ( var_0[var_3 - 1] < var_0[var_3] )
            {
                var_4 = self.weaponsused[var_3];
                self.weaponsused[var_3] = self.weaponsused[var_3 - 1];
                self.weaponsused[var_3 - 1] = var_4;
                var_5 = self.weaponxpearned[var_3];
                self.weaponxpearned[var_3] = self.weaponxpearned[var_3 - 1];
                self.weaponxpearned[var_3 - 1] = var_5;
                var_0 = self.weaponxpearned;
            }
        }
    }
}

gameendlistener()
{
    level waittill( "game_ended" );

    foreach ( var_1 in level.players )
    {
        var_1 logplayerdata();

        if ( !isalive( var_1 ) )
            continue;

        var_1 logplayerlife();
    }
}

canlogclient( var_0 )
{
    if ( isagent( var_0 ) )
        return 0;

    return var_0.clientid < level.maxlogclients;
}

canlogevent()
{
    return getmatchdata( "eventCount" ) < level.maxevents;
}

canlogkillstreak()
{
    return getmatchdata( "killstreakCount" ) < level.maxkillstreaks;
}

canloglife( var_0 )
{
    return getmatchdata( "lifeCount" ) < level.maxlives;
}

logweaponstat( var_0, var_1, var_2 )
{
    if ( !canlogclient( self ) )
        return;

    if ( var_0 == "iw6_pdwauto" )
        var_0 = "iw6_pdw";
    else if ( var_0 == "iw6_knifeonlyfast" )
        var_0 = "iw6_knifeonly";

    if ( maps\mp\_utility::iskillstreakweapon( var_0 ) )
        return;

    storeweaponandattachmentstats( "weaponStats", var_0, var_1, var_2 );
}

logattachmentstat( var_0, var_1, var_2 )
{
    if ( !canlogclient( self ) )
        return;

    storeweaponandattachmentstats( "attachmentsStats", var_0, var_1, var_2 );
}

storeweaponandattachmentstats( var_0, var_1, var_2, var_3 )
{
    var_4 = getmatchdata( "players", self.clientid, var_0, var_1, var_2 );
    var_5 = var_4 + var_3;

    if ( var_2 == "kills" || var_2 == "deaths" || var_2 == "headShots" )
    {
        if ( var_5 > 255 )
            var_5 = 255;
    }
    else if ( var_5 > 65535 )
        var_5 = 65535;

    setmatchdata( "players", self.clientid, var_0, var_1, var_2, var_5 );
}

buildbaseweaponlist()
{
    var_0 = [];
    var_1 = 149;

    for ( var_2 = 0; var_2 <= var_1; var_2++ )
    {
        var_3 = tablelookup( "mp/statstable.csv", 0, var_2, 4 );

        if ( var_3 == "" || var_3 == "uav" || var_3 == "iw6_knifeonlyfast" || var_3 == "laser_designator" || var_3 == "iw6_pdwauto" )
            continue;

        if ( !issubstr( tablelookup( "mp/statsTable.csv", 0, var_2, 2 ), "weapon_" ) )
            continue;

        if ( tablelookup( "mp/statsTable.csv", 0, var_2, 2 ) == "weapon_other" )
            continue;

        var_0[var_0.size] = var_3;
    }

    return var_0;
}

logchallenge( var_0, var_1 )
{
    if ( !canlogclient( self ) )
        return;

    if ( issubstr( var_0, "_daily" ) || issubstr( var_0, "_weekly" ) )
        return;

    var_2 = getmatchdata( "players", self.clientid, "challengeCount" );

    if ( var_2 < level.maxnumchallengesperplayer )
    {
        setmatchdata( "players", self.clientid, "challenge", var_2, var_0 );
        setmatchdata( "players", self.clientid, "tier", var_2, var_1 );
        setmatchdata( "players", self.clientid, "challengeCount", var_2 + 1 );
    }
}

logaward( var_0 )
{
    if ( !canlogclient( self ) )
        return;

    var_1 = getmatchdata( "players", self.clientid, "awardCount" );

    if ( var_1 < level.maxnumawardsperplayer )
    {
        setmatchdata( "players", self.clientid, "awards", var_1, var_0 );
        setmatchdata( "players", self.clientid, "awardCount", var_1 + 1 );
    }
}

logkillsconfirmed()
{
    if ( !canlogclient( self ) )
        return;

    setmatchdata( "players", self.clientid, "killsConfirmed", self.pers["confirmed"] );
}

logkillsdenied()
{
    if ( !canlogclient( self ) )
        return;

    setmatchdata( "players", self.clientid, "killsDenied", self.pers["denied"] );
}

loginitialstats()
{
    if ( getdvarint( "mdsd" ) > 0 )
    {
        setmatchdata( "players", self.clientid, "startXp", self getrankedplayerdata( "experience" ) );
        setmatchdata( "players", self.clientid, "startKills", self getrankedplayerdata( "kills" ) );
        setmatchdata( "players", self.clientid, "startDeaths", self getrankedplayerdata( "deaths" ) );
        setmatchdata( "players", self.clientid, "startWins", self getrankedplayerdata( "wins" ) );
        setmatchdata( "players", self.clientid, "startLosses", self getrankedplayerdata( "losses" ) );
        setmatchdata( "players", self.clientid, "startHits", self getrankedplayerdata( "hits" ) );
        setmatchdata( "players", self.clientid, "startMisses", self getrankedplayerdata( "misses" ) );
        setmatchdata( "players", self.clientid, "startGamesPlayed", self getrankedplayerdata( "gamesPlayed" ) );
        setmatchdata( "players", self.clientid, "startTimePlayedTotal", self getrankedplayerdata( "timePlayedTotal" ) );
        setmatchdata( "players", self.clientid, "startScore", self getrankedplayerdata( "score" ) );
        setmatchdata( "players", self.clientid, "startUnlockPoints", self getrankedplayerdata( "unlockPoints" ) );
        setmatchdata( "players", self.clientid, "startPrestige", self getrankedplayerdata( "prestige" ) );

        for ( var_0 = 0; var_0 < 10; var_0++ )
            setmatchdata( "players", self.clientid, "startCharacterXP", var_0, self getrankedplayerdata( "characterXP", var_0 ) );
    }
}

logfinalstats()
{
    if ( getdvarint( "mdsd" ) > 0 )
    {
        setmatchdata( "players", self.clientid, "endXp", self getrankedplayerdata( "experience" ) );
        setmatchdata( "players", self.clientid, "endKills", self getrankedplayerdata( "kills" ) );
        setmatchdata( "players", self.clientid, "endDeaths", self getrankedplayerdata( "deaths" ) );
        setmatchdata( "players", self.clientid, "endWins", self getrankedplayerdata( "wins" ) );
        setmatchdata( "players", self.clientid, "endLosses", self getrankedplayerdata( "losses" ) );
        setmatchdata( "players", self.clientid, "endHits", self getrankedplayerdata( "hits" ) );
        setmatchdata( "players", self.clientid, "endMisses", self getrankedplayerdata( "misses" ) );
        setmatchdata( "players", self.clientid, "endGamesPlayed", self getrankedplayerdata( "gamesPlayed" ) );
        setmatchdata( "players", self.clientid, "endTimePlayedTotal", self getrankedplayerdata( "timePlayedTotal" ) );
        setmatchdata( "players", self.clientid, "endScore", self getrankedplayerdata( "score" ) );
        setmatchdata( "players", self.clientid, "endUnlockPoints", self getrankedplayerdata( "unlockPoints" ) );
        setmatchdata( "players", self.clientid, "endPrestige", self getrankedplayerdata( "prestige" ) );

        for ( var_0 = 0; var_0 < 10; var_0++ )
            setmatchdata( "players", self.clientid, "endCharacterXP", var_0, self getrankedplayerdata( "characterXP", var_0 ) );
    }
}
