Counter Strike : Global Offensive Source Code
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.

293 lines
7.1 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #include "EventLog.h"
  10. #include "team.h"
  11. #include "keyvalues.h"
  12. #include "nav.h"
  13. #include "nav_area.h"
  14. // memdbgon must be the last include file in a .cpp file!!!
  15. #include "tier0/memdbgon.h"
  16. CEventLog::CEventLog()
  17. {
  18. }
  19. CEventLog::~CEventLog()
  20. {
  21. }
  22. void CEventLog::FormatPlayer( CBaseEntity *ent, char *str, int len ) const
  23. {
  24. if ( !str || len <= 0 )
  25. {
  26. return;
  27. }
  28. CBasePlayer *player = ToBasePlayer( ent );
  29. const char *playerName = "Unknown";
  30. int userID = 0;
  31. const char *networkIDString = "";
  32. const char *teamName = "";
  33. int areaID = 0;
  34. if ( player )
  35. {
  36. playerName = player->GetPlayerName();
  37. userID = player->GetUserID();
  38. networkIDString = player->GetNetworkIDString();
  39. CTeam *team = player->GetTeam();
  40. if ( team )
  41. {
  42. teamName = team->GetName();
  43. }
  44. }
  45. if ( ent && ent->MyCombatCharacterPointer() )
  46. {
  47. CNavArea *area = ent->MyCombatCharacterPointer()->GetLastKnownArea();
  48. if ( area )
  49. {
  50. areaID = area->GetID();
  51. }
  52. }
  53. V_snprintf( str, len, "\"%s<%i><%s><%s><Area %d>\"", playerName, userID, networkIDString, teamName, areaID );
  54. }
  55. const char *CEventLog::FormatPlayer( CBaseEntity *ent ) const
  56. {
  57. const int MaxEntries = 4;
  58. const int BufferLength = PLAYER_LOGINFO_SIZE;
  59. static char s_buffer[ MaxEntries ][ BufferLength ];
  60. static int s_index = 0;
  61. char *ret = s_buffer[ s_index++ ];
  62. if ( s_index >= MaxEntries )
  63. {
  64. s_index = 0;
  65. }
  66. FormatPlayer( ent, ret, BufferLength );
  67. return ret;
  68. }
  69. void CEventLog::FireGameEvent( IGameEvent *event )
  70. {
  71. if ( !g_bIsLogging )
  72. return;
  73. PrintEvent ( event );
  74. }
  75. bool CEventLog::PrintEvent( IGameEvent *event )
  76. {
  77. const char * name = event->GetName();
  78. if ( StringHasPrefixCaseSensitive( name, "server_" ) )
  79. {
  80. return true; // we don't care about server events (engine does)
  81. }
  82. else if ( StringHasPrefixCaseSensitive( name, "player_" ) )
  83. {
  84. return PrintPlayerEvent( event );
  85. }
  86. else if ( StringHasPrefixCaseSensitive( name, "team_" ) )
  87. {
  88. return PrintTeamEvent( event );
  89. }
  90. else if ( StringHasPrefixCaseSensitive( name, "game_" ) )
  91. {
  92. return PrintGameEvent( event );
  93. }
  94. else
  95. {
  96. return PrintOtherEvent( event ); // bomb_, round_, et al
  97. }
  98. }
  99. bool CEventLog::PrintGameEvent( IGameEvent *event )
  100. {
  101. // const char * name = event->GetName() + Q_strlen("game_"); // remove prefix
  102. return false;
  103. }
  104. bool CEventLog::PrintPlayerEvent( IGameEvent *event )
  105. {
  106. const char * eventName = event->GetName();
  107. const int userid = event->GetInt( "userid" );
  108. if ( StringHasPrefixCaseSensitive( eventName, "player_connect" ) ) // player connect is before the CBasePlayer pointer is setup
  109. {
  110. const char *name = event->GetString( "name" );
  111. const char *address = event->GetString( "address" );
  112. const char *networkid = event->GetString("networkid" );
  113. UTIL_LogPrintf( "\"%s<%i><%s><>\" connected, address \"%s\"\n", name, userid, networkid, address);
  114. return true;
  115. }
  116. else if ( StringHasPrefixCaseSensitive( eventName, "player_disconnect" ) )
  117. {
  118. const char *reason = event->GetString("reason" );
  119. const char *name = event->GetString("name" );
  120. const char *networkid = event->GetString("networkid" );
  121. CTeam *team = NULL;
  122. CBasePlayer *pPlayer = UTIL_PlayerByUserId( userid );
  123. if ( pPlayer )
  124. {
  125. team = pPlayer->GetTeam();
  126. }
  127. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" disconnected (reason \"%s\")\n", name, userid, networkid, team ? team->GetName() : "", reason );
  128. return true;
  129. }
  130. CBasePlayer *pPlayer = UTIL_PlayerByUserId( userid );
  131. if ( !pPlayer)
  132. {
  133. DevMsg( "CEventLog::PrintPlayerEvent: Failed to find player (userid: %i, event: %s)\n", userid, eventName );
  134. return false;
  135. }
  136. if ( StringHasPrefixCaseSensitive( eventName, "player_team" ) )
  137. {
  138. const bool bDisconnecting = event->GetBool( "disconnect" );
  139. if ( !bDisconnecting )
  140. {
  141. const int newTeam = event->GetInt( "team" );
  142. const int oldTeam = event->GetInt( "oldteam" );
  143. CTeam *team = GetGlobalTeam( newTeam );
  144. CTeam *oldteam = GetGlobalTeam( oldTeam );
  145. const char *playerName = pPlayer->GetPlayerName();
  146. int userID = pPlayer->GetUserID();
  147. if ( userID > 0 )
  148. {
  149. const char *networkID = pPlayer->GetNetworkIDString();
  150. const char *oldTeamName = (oldteam) ? oldteam->GetName() : "Unassigned";
  151. const char *teamName = (team) ? team->GetName() : "Unassigned";
  152. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" joined team \"%s\"\n",
  153. playerName,
  154. userID,
  155. networkID,
  156. oldTeamName,
  157. teamName );
  158. }
  159. }
  160. return true;
  161. }
  162. else if ( StringHasPrefixCaseSensitive( eventName, "player_death" ) )
  163. {
  164. const int attackerid = event->GetInt("attacker" );
  165. CBasePlayer *pAttacker = UTIL_PlayerByUserId( attackerid );
  166. CTeam *team = pPlayer->GetTeam();
  167. CTeam *attackerTeam = NULL;
  168. if ( pAttacker )
  169. {
  170. attackerTeam = pAttacker->GetTeam();
  171. }
  172. if ( pPlayer == pAttacker && pPlayer )
  173. {
  174. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" committed suicide with \"%s\"\n",
  175. pPlayer->GetPlayerName(),
  176. userid,
  177. pPlayer->GetNetworkIDString(),
  178. team ? team->GetName() : "",
  179. pAttacker->GetClassname()
  180. );
  181. }
  182. else if ( pAttacker )
  183. {
  184. CTeam *attackerTeam = pAttacker->GetTeam();
  185. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" killed \"%s<%i><%s><%s>\"\n",
  186. pAttacker->GetPlayerName(),
  187. attackerid,
  188. pAttacker->GetNetworkIDString(),
  189. attackerTeam ? attackerTeam->GetName() : "",
  190. pPlayer->GetPlayerName(),
  191. userid,
  192. pPlayer->GetNetworkIDString(),
  193. team ? team->GetName() : ""
  194. );
  195. }
  196. else
  197. {
  198. // killed by the world
  199. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" committed suicide with \"world\"\n",
  200. pPlayer->GetPlayerName(),
  201. userid,
  202. pPlayer->GetNetworkIDString(),
  203. team ? team->GetName() : ""
  204. );
  205. }
  206. return true;
  207. }
  208. else if ( StringHasPrefixCaseSensitive( eventName, "player_activate" ) )
  209. {
  210. UTIL_LogPrintf( "\"%s<%i><%s><>\" entered the game\n",
  211. pPlayer->GetPlayerName(),
  212. userid,
  213. pPlayer->GetNetworkIDString()
  214. );
  215. return true;
  216. }
  217. else if ( StringHasPrefixCaseSensitive( eventName, "player_changename" ) )
  218. {
  219. const char *newName = event->GetString( "newname" );
  220. const char *oldName = event->GetString( "oldname" );
  221. CTeam *team = pPlayer->GetTeam();
  222. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" changed name to \"%s\"\n",
  223. oldName,
  224. userid,
  225. pPlayer->GetNetworkIDString(),
  226. team ? team->GetName() : "",
  227. newName
  228. );
  229. return true;
  230. }
  231. // ignored events
  232. //player_hurt
  233. return false;
  234. }
  235. bool CEventLog::PrintTeamEvent( IGameEvent *event )
  236. {
  237. // const char * name = event->GetName() + Q_strlen("team_"); // remove prefix
  238. return false;
  239. }
  240. bool CEventLog::PrintOtherEvent( IGameEvent *event )
  241. {
  242. return false;
  243. }
  244. bool CEventLog::Init()
  245. {
  246. ListenForGameEvent( "player_changename" );
  247. ListenForGameEvent( "player_activate" );
  248. ListenForGameEvent( "player_death" );
  249. ListenForGameEvent( "player_team" );
  250. ListenForGameEvent( "player_disconnect" );
  251. ListenForGameEvent( "player_connect" );
  252. return true;
  253. }