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.

256 lines
6.4 KiB

  1. //========= Copyright 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. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. CEventLog::CEventLog()
  15. {
  16. }
  17. CEventLog::~CEventLog()
  18. {
  19. }
  20. void CEventLog::FireGameEvent( IGameEvent *event )
  21. {
  22. PrintEvent ( event );
  23. }
  24. bool CEventLog::PrintEvent( IGameEvent *event )
  25. {
  26. const char * name = event->GetName();
  27. if ( Q_strncmp(name, "server_", strlen("server_")) == 0 )
  28. {
  29. return true; // we don't care about server events (engine does)
  30. }
  31. else if ( Q_strncmp(name, "player_", strlen("player_")) == 0 )
  32. {
  33. return PrintPlayerEvent( event );
  34. }
  35. else if ( Q_strncmp(name, "team_", strlen("team_")) == 0 )
  36. {
  37. return PrintTeamEvent( event );
  38. }
  39. else if ( Q_strncmp(name, "game_", strlen("game_")) == 0 )
  40. {
  41. return PrintGameEvent( event );
  42. }
  43. else
  44. {
  45. return PrintOtherEvent( event ); // bomb_, round_, et al
  46. }
  47. }
  48. bool CEventLog::PrintGameEvent( IGameEvent *event )
  49. {
  50. // const char * name = event->GetName() + Q_strlen("game_"); // remove prefix
  51. return false;
  52. }
  53. bool CEventLog::PrintPlayerEvent( IGameEvent *event )
  54. {
  55. const char * eventName = event->GetName();
  56. const int userid = event->GetInt( "userid" );
  57. if ( !Q_strncmp( eventName, "player_connect", Q_strlen("player_connect") ) ) // player connect is before the CBasePlayer pointer is setup
  58. {
  59. const char *name = event->GetString( "name" );
  60. const char *address = event->GetString( "address" );
  61. const char *networkid = event->GetString("networkid" );
  62. UTIL_LogPrintf( "\"%s<%i><%s><>\" connected, address \"%s\"\n", name, userid, networkid, address);
  63. return true;
  64. }
  65. else if ( !Q_strncmp( eventName, "player_disconnect", Q_strlen("player_disconnect") ) )
  66. {
  67. const char *reason = event->GetString("reason" );
  68. const char *name = event->GetString("name" );
  69. const char *networkid = event->GetString("networkid" );
  70. CTeam *team = NULL;
  71. CBasePlayer *pPlayer = UTIL_PlayerByUserId( userid );
  72. if ( pPlayer )
  73. {
  74. team = pPlayer->GetTeam();
  75. }
  76. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" disconnected (reason \"%s\")\n", name, userid, networkid, team ? team->GetName() : "", reason );
  77. return true;
  78. }
  79. CBasePlayer *pPlayer = UTIL_PlayerByUserId( userid );
  80. if ( !pPlayer)
  81. {
  82. DevMsg( "CEventLog::PrintPlayerEvent: Failed to find player (userid: %i, event: %s)\n", userid, eventName );
  83. return false;
  84. }
  85. if ( !Q_strncmp( eventName, "player_team", Q_strlen("player_team") ) )
  86. {
  87. const bool bDisconnecting = event->GetBool( "disconnect" );
  88. if ( !bDisconnecting )
  89. {
  90. const int newTeam = event->GetInt( "team" );
  91. const int oldTeam = event->GetInt( "oldteam" );
  92. CTeam *team = GetGlobalTeam( newTeam );
  93. CTeam *oldteam = GetGlobalTeam( oldTeam );
  94. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" joined team \"%s\"\n",
  95. pPlayer->GetPlayerName(),
  96. pPlayer->GetUserID(),
  97. pPlayer->GetNetworkIDString(),
  98. oldteam->GetName(),
  99. team->GetName() );
  100. }
  101. return true;
  102. }
  103. else if ( !Q_strncmp( eventName, "player_death", Q_strlen("player_death") ) )
  104. {
  105. const int attackerid = event->GetInt("attacker" );
  106. #ifdef HL2MP
  107. const char *weapon = event->GetString( "weapon" );
  108. #endif
  109. CBasePlayer *pAttacker = UTIL_PlayerByUserId( attackerid );
  110. CTeam *team = pPlayer->GetTeam();
  111. CTeam *attackerTeam = NULL;
  112. if ( pAttacker )
  113. {
  114. attackerTeam = pAttacker->GetTeam();
  115. }
  116. if ( pPlayer == pAttacker && pPlayer )
  117. {
  118. #ifdef HL2MP
  119. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" committed suicide with \"%s\"\n",
  120. pPlayer->GetPlayerName(),
  121. userid,
  122. pPlayer->GetNetworkIDString(),
  123. team ? team->GetName() : "",
  124. weapon
  125. );
  126. #else
  127. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" committed suicide with \"%s\"\n",
  128. pPlayer->GetPlayerName(),
  129. userid,
  130. pPlayer->GetNetworkIDString(),
  131. team ? team->GetName() : "",
  132. pAttacker->GetClassname()
  133. );
  134. #endif
  135. }
  136. else if ( pAttacker )
  137. {
  138. CTeam *attackerTeam = pAttacker->GetTeam();
  139. #ifdef HL2MP
  140. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" killed \"%s<%i><%s><%s>\" with \"%s\"\n",
  141. pAttacker->GetPlayerName(),
  142. attackerid,
  143. pAttacker->GetNetworkIDString(),
  144. attackerTeam ? attackerTeam->GetName() : "",
  145. pPlayer->GetPlayerName(),
  146. userid,
  147. pPlayer->GetNetworkIDString(),
  148. team ? team->GetName() : "",
  149. weapon
  150. );
  151. #else
  152. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" killed \"%s<%i><%s><%s>\"\n",
  153. pAttacker->GetPlayerName(),
  154. attackerid,
  155. pAttacker->GetNetworkIDString(),
  156. attackerTeam ? attackerTeam->GetName() : "",
  157. pPlayer->GetPlayerName(),
  158. userid,
  159. pPlayer->GetNetworkIDString(),
  160. team ? team->GetName() : ""
  161. );
  162. #endif
  163. }
  164. else
  165. {
  166. // killed by the world
  167. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" committed suicide with \"world\"\n",
  168. pPlayer->GetPlayerName(),
  169. userid,
  170. pPlayer->GetNetworkIDString(),
  171. team ? team->GetName() : ""
  172. );
  173. }
  174. return true;
  175. }
  176. else if ( !Q_strncmp( eventName, "player_activate", Q_strlen("player_activate") ) )
  177. {
  178. UTIL_LogPrintf( "\"%s<%i><%s><>\" entered the game\n",
  179. pPlayer->GetPlayerName(),
  180. userid,
  181. pPlayer->GetNetworkIDString()
  182. );
  183. return true;
  184. }
  185. else if ( !Q_strncmp( eventName, "player_changename", Q_strlen("player_changename") ) )
  186. {
  187. const char *newName = event->GetString( "newname" );
  188. const char *oldName = event->GetString( "oldname" );
  189. CTeam *team = pPlayer->GetTeam();
  190. UTIL_LogPrintf( "\"%s<%i><%s><%s>\" changed name to \"%s\"\n",
  191. oldName,
  192. userid,
  193. pPlayer->GetNetworkIDString(),
  194. team ? team->GetName() : "",
  195. newName
  196. );
  197. return true;
  198. }
  199. // ignored events
  200. //player_hurt
  201. return false;
  202. }
  203. bool CEventLog::PrintTeamEvent( IGameEvent *event )
  204. {
  205. // const char * name = event->GetName() + Q_strlen("team_"); // remove prefix
  206. return false;
  207. }
  208. bool CEventLog::PrintOtherEvent( IGameEvent *event )
  209. {
  210. return false;
  211. }
  212. bool CEventLog::Init()
  213. {
  214. ListenForGameEvent( "player_changename" );
  215. ListenForGameEvent( "player_activate" );
  216. ListenForGameEvent( "player_death" );
  217. ListenForGameEvent( "player_team" );
  218. ListenForGameEvent( "player_disconnect" );
  219. ListenForGameEvent( "player_connect" );
  220. return true;
  221. }
  222. void CEventLog::Shutdown()
  223. {
  224. StopListeningForAllEvents();
  225. }