Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
10 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. //
  4. //=============================================================================//
  5. #include "cbase.h"
  6. #include "lifetime_stats_page.h"
  7. #include <vgui_controls/SectionedListPanel.h>
  8. #include "cs_client_gamestats.h"
  9. using namespace vgui;
  10. // memdbgon must be the last include file in a .cpp file!!!
  11. #include "tier0/memdbgon.h"
  12. //-----------------------------------------------------------------------------
  13. // Purpose: creates child panels, passes down name to pick up any settings from res files.
  14. //-----------------------------------------------------------------------------
  15. CLifetimeStatsPage::CLifetimeStatsPage(vgui::Panel *parent, const char *name) : BaseClass(parent, "CSLifetimeStatsDialog")
  16. {
  17. m_allStatsGroupPanel = AddGroup( L"all", "Stats_Button_All", L"All" );
  18. m_detailedWeaponStatsGroupPanel = AddGroup( L"weapon", "Stats_Button_Weapon", L"Weapon Stats" );
  19. m_specialSkillsStatsGroupPanel = AddGroup( L"skills", "Stats_Button_Skills", L"Special Skills" );
  20. m_mapAndMiscellanyStatsGroupPanel = AddGroup( L"map", "Stats_Button_Misc", L"Miscellaneous" );
  21. m_mapVictoryStatsGroupPanel = AddGroup( L"map", "Stats_Button_Victories", L"Map Victories" );
  22. m_missionAndObjectiveStatsGroupPanel = AddGroup( L"mission", "Stats_Button_Mission", L"Mission && Objectives" );
  23. m_allStatsGroupPanel->SetGroupActive(true);
  24. }
  25. //-----------------------------------------------------------------------------
  26. // Purpose: Loads settings from statsdialog.res in hl2/resource/ui/
  27. //-----------------------------------------------------------------------------
  28. void CLifetimeStatsPage::ApplySchemeSettings( vgui::IScheme *pScheme )
  29. {
  30. BaseClass::ApplySchemeSettings( pScheme );
  31. LoadControlSettings("resource/ui/CSLifetimeStatsDialog.res");
  32. m_statsList->AddColumnToSection( 0, "name", "", SectionedListPanel::COLUMN_CENTER, 280);
  33. m_statsList->AddColumnToSection( 0, "playerValue", "", SectionedListPanel::COLUMN_CENTER, 250);
  34. }
  35. void CLifetimeStatsPage::RepopulateStats()
  36. {
  37. m_statsList->RemoveAll();
  38. const StatsCollection_t& personalLifetimeStats = g_CSClientGameStats.GetLifetimeStats();
  39. if (m_missionAndObjectiveStatsGroupPanel->IsGroupActive() || m_allStatsGroupPanel->IsGroupActive())
  40. {
  41. AddSimpleStat(CSSTAT_ROUNDS_WON, personalLifetimeStats);
  42. AddSimpleStat(CSSTAT_KILLS, personalLifetimeStats);
  43. AddSimpleStat(CSSTAT_DEATHS, personalLifetimeStats);
  44. AddKillToDeathStat(personalLifetimeStats);
  45. AddSimpleStat(CSSTAT_DAMAGE, personalLifetimeStats);
  46. AddSimpleStat(CSSTAT_NUM_BOMBS_PLANTED, personalLifetimeStats);
  47. AddSimpleStat(CSSTAT_NUM_BOMBS_DEFUSED, personalLifetimeStats);
  48. AddSimpleStat(CSSTAT_NUM_HOSTAGES_RESCUED, personalLifetimeStats);
  49. AddSimpleStat(CSSTAT_PISTOLROUNDS_WON, personalLifetimeStats);
  50. }
  51. if (m_specialSkillsStatsGroupPanel->IsGroupActive() || m_allStatsGroupPanel->IsGroupActive())
  52. {
  53. AddSimpleStat(CSSTAT_MVPS, personalLifetimeStats);
  54. AddFavoriteWeaponStat(personalLifetimeStats);
  55. AddSimpleStat(CSSTAT_SHOTS_FIRED, personalLifetimeStats);
  56. AddSimpleStat(CSSTAT_SHOTS_HIT, personalLifetimeStats);
  57. AddAccuracyStat(personalLifetimeStats);
  58. AddSimpleStat(CSSTAT_DOMINATIONS, personalLifetimeStats);
  59. AddSimpleStat(CSSTAT_DOMINATION_OVERKILLS, personalLifetimeStats);
  60. AddSimpleStat(CSSTAT_REVENGES, personalLifetimeStats);
  61. AddSimpleStat(CSSTAT_KILLS_HEADSHOT, personalLifetimeStats);
  62. AddSimpleStat(CSSTAT_KILLS_AGAINST_ZOOMED_SNIPER, personalLifetimeStats);
  63. AddSimpleStat(CSSTAT_KILLS_ENEMY_BLINDED, personalLifetimeStats);
  64. AddSimpleStat(CSSTAT_KILLS_ENEMY_WEAPON, personalLifetimeStats);
  65. AddSimpleStat(CSSTAT_KILLS_KNIFE_FIGHT, personalLifetimeStats);
  66. }
  67. if (m_mapAndMiscellanyStatsGroupPanel->IsGroupActive() || m_allStatsGroupPanel->IsGroupActive())
  68. {
  69. AddSimpleStat(CSSTAT_MONEY_EARNED, personalLifetimeStats);
  70. AddSimpleStat(CSSTAT_DECAL_SPRAYS, personalLifetimeStats);
  71. AddSimpleStat(CSSTAT_NIGHTVISION_DAMAGE, personalLifetimeStats);
  72. AddSimpleStat(CSSTAT_NUM_BROKEN_WINDOWS, personalLifetimeStats);
  73. AddSimpleStat(CSSTAT_WEAPONS_DONATED, personalLifetimeStats);
  74. }
  75. if (m_mapVictoryStatsGroupPanel->IsGroupActive() || m_allStatsGroupPanel->IsGroupActive())
  76. {
  77. AddSimpleStat(CSSTAT_MAP_WINS_CS_ASSAULT, personalLifetimeStats);
  78. AddSimpleStat(CSSTAT_MAP_WINS_CS_COMPOUND, personalLifetimeStats);
  79. AddSimpleStat(CSSTAT_MAP_WINS_CS_HAVANA, personalLifetimeStats);
  80. AddSimpleStat(CSSTAT_MAP_WINS_CS_ITALY, personalLifetimeStats);
  81. AddSimpleStat(CSSTAT_MAP_WINS_CS_MILITIA, personalLifetimeStats);
  82. AddSimpleStat(CSSTAT_MAP_WINS_CS_OFFICE, personalLifetimeStats);
  83. AddSimpleStat(CSSTAT_MAP_WINS_DE_AZTEC, personalLifetimeStats);
  84. AddSimpleStat(CSSTAT_MAP_WINS_DE_CBBLE, personalLifetimeStats);
  85. AddSimpleStat(CSSTAT_MAP_WINS_DE_CHATEAU, personalLifetimeStats);
  86. AddSimpleStat(CSSTAT_MAP_WINS_DE_DUST2, personalLifetimeStats);
  87. AddSimpleStat(CSSTAT_MAP_WINS_DE_DUST, personalLifetimeStats);
  88. AddSimpleStat(CSSTAT_MAP_WINS_DE_INFERNO, personalLifetimeStats);
  89. AddSimpleStat(CSSTAT_MAP_WINS_DE_NUKE, personalLifetimeStats);
  90. AddSimpleStat(CSSTAT_MAP_WINS_DE_PIRANESI, personalLifetimeStats);
  91. AddSimpleStat(CSSTAT_MAP_WINS_DE_PORT, personalLifetimeStats);
  92. AddSimpleStat(CSSTAT_MAP_WINS_DE_PRODIGY, personalLifetimeStats);
  93. AddSimpleStat(CSSTAT_MAP_WINS_DE_TIDES, personalLifetimeStats);
  94. AddSimpleStat(CSSTAT_MAP_WINS_DE_TRAIN, personalLifetimeStats);
  95. }
  96. if (m_detailedWeaponStatsGroupPanel->IsGroupActive() || m_allStatsGroupPanel->IsGroupActive())
  97. {
  98. CSStatType_t hitsStat = CSSTAT_HITS_DEAGLE;
  99. CSStatType_t shotsStat = CSSTAT_SHOTS_DEAGLE;
  100. for (CSStatType_t killStat = CSSTAT_KILLS_DEAGLE ; killStat <= CSSTAT_KILLS_HEGRENADE ; killStat = (CSStatType_t)(killStat + 1))
  101. {
  102. if (shotsStat <= CSSTAT_SHOTS_M249)
  103. {
  104. AddSimpleStat(shotsStat, personalLifetimeStats);
  105. }
  106. if (hitsStat <= CSSTAT_HITS_M249)
  107. {
  108. AddSimpleStat(hitsStat, personalLifetimeStats);
  109. }
  110. AddSimpleStat(killStat, personalLifetimeStats);
  111. hitsStat = (CSStatType_t)(hitsStat + 1);
  112. shotsStat = (CSStatType_t)(shotsStat + 1);
  113. }
  114. }
  115. int itemCount = m_statsList->GetItemCount();
  116. for (int i = 0; i < itemCount ; ++i)
  117. {
  118. m_statsList->SetItemBgColor(i, Color(0,0,0,0));
  119. }
  120. }
  121. int CLifetimeStatsPage::AddSimpleStat( int desiredStat, const StatsCollection_t& personalLifetimeStats)
  122. {
  123. PlayerStatData_t stat = g_CSClientGameStats.GetStatById(desiredStat);
  124. KeyValues *pKeyValues = new KeyValues( "data" );
  125. pKeyValues->SetWString( "name", stat.pStatDisplayName );
  126. pKeyValues->SetFloat( "playerValue", 0 );
  127. char buf[64];
  128. Q_snprintf( buf, sizeof( buf ), "%d", personalLifetimeStats[stat.iStatId] );
  129. pKeyValues->SetString( "playerValue", (personalLifetimeStats[stat.iStatId])?buf:"" );
  130. int newItem = m_statsList->AddItem(0, pKeyValues);
  131. pKeyValues->deleteThis();
  132. m_statsList->SetItemFont(newItem , m_listItemFont);
  133. m_statsList->SetItemFgColor(newItem, Color(197,197,197,255));
  134. return newItem;
  135. }
  136. int CLifetimeStatsPage::AddFavoriteWeaponStat(const StatsCollection_t& personalLifetimeStats)
  137. {
  138. PlayerStatData_t statPlayerBestWeapon;
  139. statPlayerBestWeapon.iStatId = CSSTAT_UNDEFINED;
  140. int previousBestKills = 0;
  141. for (CSStatType_t statId = CSSTAT_KILLS_DEAGLE ; statId <= CSSTAT_KILLS_M249; statId = (CSStatType_t)(statId + 1))
  142. {
  143. PlayerStatData_t stat = g_CSClientGameStats.GetStatById( statId );
  144. //Compare this to previous Weapons
  145. int playerKillsWithWeapon = personalLifetimeStats[statId];
  146. {
  147. if (playerKillsWithWeapon > previousBestKills)
  148. {
  149. statPlayerBestWeapon = stat;
  150. previousBestKills = playerKillsWithWeapon;
  151. }
  152. }
  153. }
  154. KeyValues *pKeyValues = new KeyValues( "data" );
  155. pKeyValues->SetWString( "name", LocalizeTagOrUseDefault( "Stats_FavoriteWeapon", L"Favorite Weapon" ) );
  156. pKeyValues->SetWString( "playerValue", TranslateWeaponKillIDToAlias(statPlayerBestWeapon.iStatId) );
  157. int newItem = m_statsList->AddItem(0, pKeyValues);
  158. pKeyValues->deleteThis();
  159. m_statsList->SetItemFont(newItem , m_listItemFont);
  160. m_statsList->SetItemFgColor(newItem, Color(197,197,197,255));
  161. return newItem;
  162. }
  163. int CLifetimeStatsPage::AddKillToDeathStat(const StatsCollection_t& personalLifetimeStats)
  164. {
  165. PlayerStatData_t statKills = g_CSClientGameStats.GetStatById(CSSTAT_KILLS);
  166. PlayerStatData_t statDeaths = g_CSClientGameStats.GetStatById(CSSTAT_DEATHS);
  167. KeyValues *pKeyValues = new KeyValues( "data" );
  168. pKeyValues->SetWString( "name", LocalizeTagOrUseDefault( "Stats_KillToDeathRatio", L"Kill to Death Ratio" ) );
  169. pKeyValues->SetFloat( "playerValue", 0 );
  170. float playerKills = personalLifetimeStats[statKills.iStatId];
  171. float playerDeaths = personalLifetimeStats[statDeaths.iStatId];
  172. float playerKillToDeathRatio = 1.0f;
  173. if (playerDeaths > 0)
  174. {
  175. playerKillToDeathRatio = playerKills / playerDeaths;
  176. }
  177. char buf[64];
  178. Q_snprintf( buf, sizeof( buf ), "%.2f", playerKillToDeathRatio );
  179. pKeyValues->SetString( "playerValue", (playerKillToDeathRatio)?buf:"" );
  180. int newItem = m_statsList->AddItem(0, pKeyValues);
  181. pKeyValues->deleteThis();
  182. m_statsList->SetItemFont(newItem , m_listItemFont);
  183. m_statsList->SetItemFgColor(newItem, Color(197,197,197,255));
  184. return newItem;
  185. }
  186. int CLifetimeStatsPage::AddAccuracyStat(const StatsCollection_t& personalLifetimeStats)
  187. {
  188. PlayerStatData_t statHits = g_CSClientGameStats.GetStatById(CSSTAT_SHOTS_HIT);
  189. PlayerStatData_t statShots = g_CSClientGameStats.GetStatById(CSSTAT_SHOTS_FIRED);
  190. KeyValues *pKeyValues = new KeyValues( "data" );
  191. pKeyValues->SetWString( "name", LocalizeTagOrUseDefault( "Stats_Accuracy", L"Accuracy" ) );
  192. pKeyValues->SetFloat( "playerValue", 0 );
  193. float playerHits = personalLifetimeStats[statHits.iStatId];
  194. float playerShots = personalLifetimeStats[statShots.iStatId];
  195. float playerAccuracy = 0.0;
  196. if (playerShots > 0)
  197. {
  198. playerAccuracy = 100.0f * playerHits / playerShots;
  199. }
  200. char buf[64];
  201. Q_snprintf( buf, sizeof( buf ), "%.1f%%", playerAccuracy );
  202. pKeyValues->SetString( "playerValue", (playerAccuracy)?buf:"" );
  203. int newItem = m_statsList->AddItem(0, pKeyValues);
  204. pKeyValues->deleteThis();
  205. m_statsList->SetItemFont(newItem , m_listItemFont);
  206. m_statsList->SetItemFgColor(newItem, Color(197,197,197,255));
  207. return newItem;
  208. }