//========= Copyright Valve Corporation, All rights reserved. ============// // // Purpose: // // $NoKeywords: $ //=============================================================================// #include "cbase.h" #include "dod_playerstats.h" #include "hud_macros.h" const char *g_pszShortTeamNames[2] = { "US", "Ger" }; const char *g_pszClassNames[NUM_DOD_PLAYERCLASSES] = { "Rifleman", "Assault", "Support", "Sniper", "MG", "Rocket" }; const char *g_pszStatNames[DODSTAT_MAX] = { "iPlayTime", "iRoundsWon", "iRoundsLost", "iKills", "iDeaths", "iCaptures", "iBlocks", "iBombsPlanted", "iBombsDefused", "iDominations", "iRevenges", "iShotsHit", "iShotsFired", "iHeadshots" }; int iValidWeaponStatBitMask = ( (1<GetName(); if ( FStrEq( pEventName, "user_data_downloaded" ) ) { // Store our stat data Assert( steamapicontext->SteamUserStats() ); if ( !steamapicontext->SteamUserStats() ) return; CGameID gameID( engine->GetAppID() ); // reset everything Q_memset( &m_PlayerStats, 0, sizeof(m_PlayerStats) ); Q_memset( &m_WeaponStats, 0, sizeof(m_WeaponStats) ); // read playerclass stats for ( int iTeam=0;iTeam<2;iTeam++ ) { for ( int iClass=0;iClassSteamUserStats()->GetStat( szStatName, &iData ) ) { // use Steam's value m_PlayerStats[iTeam][iClass].m_iStat[iStat] = iData; } } } } } // read weapon stats for ( int iWeapon=WEAPON_NONE+1;iWeaponSteamUserStats()->GetStat( szStatName, &iData ) ) { // use Steam's value m_WeaponStats[iWeapon].m_iStat[iStat] = iData; } } } } IGameEvent * event = gameeventmanager->CreateEvent( "player_stats_updated" ); if ( event ) { event->SetBool( "forceupload", false ); gameeventmanager->FireEventClientSide( event ); } } } void CDODPlayerStats::MsgFunc_DODPlayerStatsUpdate( bf_read &msg ) { dod_stat_accumulator_t playerStats; Q_memset( &playerStats, 0, sizeof(playerStats) ); // get the fixed-size information int iClass = msg.ReadByte(); int iTeam = msg.ReadByte(); int iPlayerStatBits = msg.ReadLong(); Assert( iClass >= 0 && iClass <= 5 ); if ( iClass < 0 || iClass > 5 ) return; // the bitfield indicates which stats are contained in the message. Set the stats appropriately. int iStat = DODSTAT_FIRST; while ( iPlayerStatBits > 0 ) { if ( iPlayerStatBits & 1 ) { playerStats.m_iStat[iStat] = msg.ReadLong(); } iPlayerStatBits >>= 1; iStat++; } int iNumWeapons = msg.ReadByte(); int i; CUtlVector vecWeaponStats; for ( i=0;i 0 ) { if ( iWeaponStatMask & 1 ) { vecWeaponStats[i].stats.m_iStat[iStat] = msg.ReadLong(); } iWeaponStatMask >>= 1; iStat++; } } // sanity check: the message should contain exactly the # of bytes we expect based on the bit field Assert( !msg.IsOverflowed() ); Assert( 0 == msg.GetNumBytesLeft() ); // if byte count isn't correct, bail out and don't use this data, rather than risk polluting player stats with garbage if ( msg.IsOverflowed() || ( 0 != msg.GetNumBytesLeft() ) ) return; UpdateStats( iClass, iTeam, &playerStats, &vecWeaponStats ); } //----------------------------------------------------------------------------- // Purpose: Uploads stats for current Steam user to Steam //----------------------------------------------------------------------------- void CDODPlayerStats::UploadStats() { // only upload if Steam is running if ( !steamapicontext->SteamUserStats() ) return; CGameID gameID( engine->GetAppID() ); char szStatName[256]; // write playerclass stats for ( int iTeam=0;iTeam<2;iTeam++ ) { for ( int iClass=0;iClassSteamUserStats()->SetStat( szStatName, m_PlayerStats[iTeam][iClass].m_iStat[iStat] ); } } } } } // write weapon stats for ( int iWeapon=WEAPON_NONE+1;iWeaponSteamUserStats()->SetStat( szStatName, m_WeaponStats[iWeapon].m_iStat[iStat] ); } } } } SetNextForceUploadTime(); } void CDODPlayerStats::UpdateStats( int iPlayerClass, int iTeam, dod_stat_accumulator_t *playerStats, CUtlVector *vecWeaponStats ) { Assert( iPlayerClass >= 0 && iPlayerClass < NUM_DOD_PLAYERCLASSES ); if ( iPlayerClass < 0 || iPlayerClass >= NUM_DOD_PLAYERCLASSES ) return; // translate the team index into [0,1] int iTeamIndex = ( iTeam == TEAM_ALLIES ) ? 0 : 1; // upload this class' data // add this stat update to our stored stats for ( int iStat=0;iStatm_iStat[iStat] > 0 ) { m_PlayerStats[iTeamIndex][iPlayerClass].m_iStat[iStat] += playerStats->m_iStat[iStat]; m_PlayerStats[iTeamIndex][iPlayerClass].m_bDirty[iStat] = true; } } } int iWeaponStatCount = vecWeaponStats->Count(); for ( int i=0;iElement(i).iWeaponID; for ( int iStat=0;iStatElement(i).stats.m_iStat[iStat]; if ( iValue > 0 ) { m_WeaponStats[iWeaponID].m_iStat[iStat] += iValue; m_WeaponStats[iWeaponID].m_bDirty[iStat] = true; } } } } // if we haven't uploaded stats in a long time, upload them if ( ( gpGlobals->curtime >= m_flTimeNextForceUpload ) ) { UploadStats(); } } //----------------------------------------------------------------------------- // Purpose: sets the next time to force a stats upload at //----------------------------------------------------------------------------- void CDODPlayerStats::SetNextForceUploadTime() { // pick a time a while from now (an hour +/- 15 mins) to upload stats if we haven't gotten a map change by then m_flTimeNextForceUpload = gpGlobals->curtime + ( 60 * RandomInt( 45, 75 ) ); } CDODPlayerStats g_DODPlayerStats;