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.

217 lines
8.4 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "cbase.h"
  3. #include "tf_rating_data.h"
  4. #include "gcsdk/enumutils.h"
  5. #ifdef CLIENT_DLL
  6. #include "tf_matchmaking_shared.h"
  7. #endif
  8. using namespace GCSDK;
  9. #if defined( GC )
  10. #include "tf_matchmaking_rating.h"
  11. IMPLEMENT_CLASS_MEMPOOL( CTFRatingData, 1000, UTLMEMORYPOOL_GROW_SLOW );
  12. // Make sure you visit all these if you add new fields to rating data
  13. #define ASSERT_LAST_FIELD( foo ) COMPILE_TIME_ASSERT( CSchRatingData::k_iFieldMax == CSchRatingData::k_iField_##foo + 1 )
  14. #else // defined( GC )
  15. #define ASSERT_LAST_FIELD( foo ) // No Sch on client. As long as it fails somewhere.
  16. #endif // defined( GC )
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Get player's rating data by steamID. Returns NULL if it doesn't exist
  21. //-----------------------------------------------------------------------------
  22. CTFRatingData *CTFRatingData::YieldingGetPlayerRatingDataBySteamID( const CSteamID &steamID, EMMRating eRatingType )
  23. {
  24. #ifdef GC_DLL
  25. GCSDK::CGCSharedObjectCache *pSOCache = GGCEcon()->YieldingFindOrLoadSOCache( steamID );
  26. #else
  27. GCSDK::CGCClientSharedObjectCache *pSOCache = GCClientSystem()->GetSOCache( steamID );
  28. #endif
  29. if ( pSOCache )
  30. {
  31. auto *pTypeCache = pSOCache->FindTypeCache( CTFRatingData::k_nTypeID );
  32. if ( pTypeCache )
  33. {
  34. for ( uint32 i = 0; i < pTypeCache->GetCount(); ++i )
  35. {
  36. CTFRatingData *pRatingData = (CTFRatingData*)pTypeCache->GetObject( i );
  37. if ( eRatingType == (EMMRating)pRatingData->Obj().rating_type() )
  38. {
  39. return pRatingData;
  40. }
  41. }
  42. }
  43. }
  44. return nullptr;
  45. }
  46. //-----------------------------------------------------------------------------
  47. #ifdef GC
  48. CTFRatingData::CTFRatingData( uint32 unAccountID, EMMRating eRatingType )
  49. : CTFRatingData( unAccountID, eRatingType, ITFMMRatingBackend::GetRatingBackend( eRatingType ).GetDefaultRatingData() )
  50. {}
  51. #endif
  52. //-----------------------------------------------------------------------------
  53. CTFRatingData::CTFRatingData()
  54. {}
  55. //-----------------------------------------------------------------------------
  56. CTFRatingData::CTFRatingData( uint32 unAccountID, EMMRating eRatingType, const MMRatingData_t &ratingData )
  57. {
  58. MutObj().set_account_id( unAccountID );
  59. MutObj().set_rating_type( eRatingType );
  60. MutObj().set_rating_primary( ratingData.unRatingPrimary );
  61. MutObj().set_rating_secondary( ratingData.unRatingSecondary );
  62. MutObj().set_rating_tertiary( ratingData.unRatingTertiary );
  63. // MMRatingData_t fields
  64. ASSERT_LAST_FIELD( unRatingTertiary );
  65. }
  66. #ifdef GC
  67. //-----------------------------------------------------------------------------
  68. bool CTFRatingData::BIsKeyLess( const CSharedObject & soRHS ) const
  69. {
  70. Assert( GetTypeID() == soRHS.GetTypeID() );
  71. const CSOTFRatingData &obj = Obj();
  72. const CSOTFRatingData &rhs = ( static_cast< const CTFRatingData & >( soRHS ) ).Obj();
  73. if ( obj.account_id() < rhs.account_id() ) return true;
  74. if ( obj.account_id() > rhs.account_id() ) return false;
  75. return obj.rating_type() < rhs.rating_type();
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Purpose: This is a database backed object, but, all mutations to these
  79. // should go through BYieldingUpdateRatingAndAddToTransaction and its
  80. // CTFSharedObjectCache user.
  81. //
  82. // Not implementing to prevent mis-use until there's a path that legitimately
  83. // needs them to work.
  84. //-----------------------------------------------------------------------------
  85. bool CTFRatingData::BYieldingAddInsertToTransaction( GCSDK::CSQLAccess & sqlAccess )
  86. { Assert( false ); return false; }
  87. bool CTFRatingData::BYieldingAddWriteToTransaction( GCSDK::CSQLAccess & sqlAccess, const CUtlVector< int > &fields )
  88. { Assert( false ); return false; }
  89. bool CTFRatingData::BYieldingAddRemoveToTransaction( GCSDK::CSQLAccess & sqlAccess )
  90. { Assert( false ); return false; }
  91. //-----------------------------------------------------------------------------
  92. void CTFRatingData::WriteToRecord( CSchRatingData *pRatingData ) const
  93. {
  94. pRatingData->m_unAccountID = Obj().account_id();
  95. pRatingData->m_nRatingType = Obj().rating_type();
  96. pRatingData->m_unRatingPrimary = Obj().rating_primary();
  97. pRatingData->m_unRatingSecondary = Obj().rating_secondary();
  98. pRatingData->m_unRatingTertiary = Obj().rating_tertiary();
  99. ASSERT_LAST_FIELD( unRatingTertiary );
  100. }
  101. //-----------------------------------------------------------------------------
  102. void CTFRatingData::ReadFromRecord( const CSchRatingData &ratingData )
  103. {
  104. MutObj().set_account_id( ratingData.m_unAccountID );
  105. MutObj().set_rating_type( ratingData.m_nRatingType );
  106. MutObj().set_rating_primary( ratingData.m_unRatingPrimary );
  107. MutObj().set_rating_secondary( ratingData.m_unRatingSecondary );
  108. MutObj().set_rating_tertiary( ratingData.m_unRatingTertiary );
  109. ASSERT_LAST_FIELD( unRatingTertiary );
  110. }
  111. //-----------------------------------------------------------------------------
  112. // Purpose: Hook for CTFSubscriberMessageFilter to allow us to stop certain
  113. // rating types from going to certain subscribers.
  114. //-----------------------------------------------------------------------------
  115. uint32_t CTFRatingData::FilterSendFlags( uint32_t unTypeFlags ) const
  116. {
  117. auto &backend = ITFMMRatingBackend::GetRatingBackend( (EMMRating)Obj().rating_type() );
  118. ITFMMRatingBackend::ERatingFlags eBackendFlags = backend.GetRatingFlags();
  119. bool bNetworked = ( eBackendFlags & ITFMMRatingBackend::eRatingFlag_Networked );
  120. bool bOwnerOnly = ( eBackendFlags & ITFMMRatingBackend::eRatingFlag_OwnerOnly );
  121. uint32_t unRemoveFlags = 0u;
  122. if ( !bNetworked || bOwnerOnly )
  123. {
  124. unRemoveFlags |= k_ESOFlag_SendToOtherGameservers;
  125. unRemoveFlags |= k_ESOFlag_SendToOtherUsers;
  126. }
  127. if ( !bNetworked )
  128. {
  129. unRemoveFlags |= k_ESOFlag_SendToOwner;
  130. }
  131. return unTypeFlags & ~unRemoveFlags;
  132. }
  133. //-----------------------------------------------------------------------------
  134. // Purpose: Updates the rating fields from an MMRatingData_t, add a write or
  135. // insert to the given transaction including an audit entry.
  136. //-----------------------------------------------------------------------------
  137. bool CTFRatingData::BYieldingUpdateRatingAndAddToTransaction( CSQLAccess &transaction,
  138. const MMRatingData_t &newData,
  139. EMMRatingSource eAdjustmentSource,
  140. uint64_t unSourceEventID,
  141. RTime32 rtTimestamp )
  142. {
  143. // MMRatingData_t fields
  144. ASSERT_LAST_FIELD( unRatingTertiary );
  145. CSchRatingHistory schHistory;
  146. schHistory.m_unAccountID = Obj().account_id();
  147. schHistory.m_nRatingType = Obj().rating_type();
  148. schHistory.m_nRatingSource = eAdjustmentSource;
  149. schHistory.m_unEventID = unSourceEventID;
  150. schHistory.m_rtime32Timestamp = rtTimestamp;
  151. schHistory.m_unOldRatingPrimary = Obj().rating_primary();
  152. schHistory.m_unOldRatingSecondary = Obj().rating_secondary();
  153. schHistory.m_unOldRatingTertiary = Obj().rating_tertiary();
  154. schHistory.m_unNewRatingPrimary = newData.unRatingPrimary;
  155. schHistory.m_unNewRatingSecondary = newData.unRatingSecondary;
  156. schHistory.m_unNewRatingTertiary = newData.unRatingTertiary;
  157. // Update ourself
  158. // MMRatingData_t fields
  159. ASSERT_LAST_FIELD( unRatingTertiary );
  160. MutObj().set_rating_primary( newData.unRatingPrimary );
  161. MutObj().set_rating_secondary( newData.unRatingSecondary );
  162. MutObj().set_rating_tertiary( newData.unRatingTertiary );
  163. // Create record
  164. CSchRatingData schRatingData;
  165. WriteToRecord( &schRatingData );
  166. // Add to transaction
  167. return transaction.BYieldingInsertOrUpdateOnPK( &schRatingData ) &&
  168. transaction.BYieldingInsertRecord( &schHistory );
  169. }
  170. #endif // GC
  171. //-----------------------------------------------------------------------------
  172. // Purpose: Reads out the rating fields for rating systems to use
  173. //-----------------------------------------------------------------------------
  174. MMRatingData_t CTFRatingData::GetRatingData()
  175. {
  176. MMRatingData_t rvoData;
  177. rvoData.unRatingPrimary = Obj().rating_primary();
  178. rvoData.unRatingSecondary = Obj().rating_secondary();
  179. rvoData.unRatingTertiary = Obj().rating_tertiary();
  180. // If you expanded MMRatingData_t
  181. ASSERT_LAST_FIELD( unRatingTertiary );
  182. return rvoData;
  183. }