PageRenderTime 88ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/sourcemod/scripting/mvm_stats.sp

https://bitbucket.org/kimoto/sushi
Unknown | 176 lines | 155 code | 21 blank | 0 comment | 0 complexity | 12af98466dae34900908d148a21f5ce3 MD5 | raw file
  1. #pragma semicolon 1
  2. #include <sourcemod>
  3. #include <dbi>
  4. #include <tf2>
  5. #define PLUGIN_VERSION "1.0.0"
  6. public Plugin:myinfo =
  7. {
  8. name = "MvM Stats",
  9. author = "kimoto",
  10. description = "MvM Stats",
  11. version = PLUGIN_VERSION,
  12. url = "http://kymt.me/"
  13. };
  14. new Handle:db = INVALID_HANDLE;
  15. new bool:alreadySent = false;
  16. new countRobotKilled = 0;
  17. new countPickupedCurrency = 0;
  18. PrintLastError()
  19. {
  20. decl String:error[256];
  21. SQL_GetError(db, error, sizeof(error));
  22. PrintToServer("Failed to query (error: %s)", error);
  23. }
  24. InitDB()
  25. {
  26. alreadySent = false;
  27. countRobotKilled = 0;
  28. countPickupedCurrency = 0;
  29. LogMessage("Try to connect mysql server");
  30. new String:error[256];
  31. if((db = SQL_DefConnect(error, sizeof(error))) == INVALID_HANDLE) {
  32. PrintToServer("Could not connect (enable silent mode): %s", error);
  33. return;
  34. }
  35. LogMessage("connected: %s", error);
  36. }
  37. IsAliveDB()
  38. {
  39. return (db != INVALID_HANDLE);
  40. }
  41. CountLoaded()
  42. {
  43. if(!IsAliveDB()) return;
  44. decl String:map[256];
  45. GetCurrentMap(map, sizeof(map));
  46. SQL_QuoteString(db, map, map, sizeof(map));
  47. decl String:query[256];
  48. Format(query, sizeof(query), "insert into game_stats (map_code, loaded) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE loaded = loaded + 1;", map);
  49. if(!SQL_FastQuery(db, query)){
  50. PrintLastError();
  51. }
  52. }
  53. CountPlayed()
  54. {
  55. if(!IsAliveDB()) return;
  56. decl String:map[256];
  57. GetCurrentMap(map, sizeof(map));
  58. SQL_QuoteString(db, map, map, sizeof(map));
  59. decl String:query[256];
  60. Format(query, sizeof(query), "insert into game_stats (map_code, played) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE played = played + 1;", map);
  61. if(!SQL_FastQuery(db, query)){
  62. PrintLastError();
  63. }
  64. }
  65. CountWin()
  66. {
  67. if(!IsAliveDB()) return;
  68. decl String:map[256];
  69. GetCurrentMap(map, sizeof(map));
  70. SQL_QuoteString(db, map, map, sizeof(map));
  71. decl String:query[256];
  72. Format(query, sizeof(query), "insert into game_stats (map_code, win) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE win = win + 1;", map);
  73. if(!SQL_FastQuery(db, query)){
  74. PrintLastError();
  75. }
  76. }
  77. SendRobotKilled()
  78. {
  79. if(!IsAliveDB()) return;
  80. decl String:query[256];
  81. Format(query, sizeof(query), "UPDATE mvm_stats SET total_robot_killed = total_robot_killed + %d, total_amount = total_amount + %d;", countRobotKilled, countPickupedCurrency);
  82. if(SQL_FastQuery(db, query)){
  83. countRobotKilled = 0; // reset to zero if SQL query succeed
  84. countPickupedCurrency = 0; // reset to zero if SQL query succeed
  85. }else{
  86. PrintLastError();
  87. }
  88. }
  89. CloseDB()
  90. {
  91. if(IsAliveDB())
  92. CloseHandle(db);
  93. }
  94. public OnPluginStart()
  95. {
  96. InitDB();
  97. RegServerCmd("mvm_stats_killed", Command_MvMStatsKilled);
  98. HookEvent("mvm_wave_complete", Event_WaveComplete, EventHookMode_PostNoCopy);
  99. HookEvent("mvm_mission_complete", Event_MissionComplete, EventHookMode_PostNoCopy);
  100. HookEvent("mvm_pickup_currency", Event_MvMPickupCurrency, EventHookMode_Post);
  101. HookEvent("teamplay_round_active", Event_RoundActive, EventHookMode_PostNoCopy);
  102. HookEvent("player_death", Event_PlayerDeath, EventHookMode_Post);
  103. }
  104. public Action:Command_MvMStatsKilled(args)
  105. {
  106. LogMessage("Robot was killed: %d", countRobotKilled);
  107. LogMessage("Pickuped Amount: %d", countPickupedCurrency);
  108. }
  109. public Action:Event_RoundActive(Handle:event, const String:name[], bool:dontBroadcast)
  110. {
  111. if(alreadySent == false){
  112. CountPlayed();
  113. alreadySent = true;
  114. }
  115. }
  116. public Action:Event_WaveComplete(Handle:event, const String:name[], bool:dontBroadcast)
  117. {
  118. LogMessage("Wave Complete Event");
  119. LogMessage("Sending robot killed information");
  120. SendRobotKilled();
  121. }
  122. public Action:Event_MissionComplete(Handle:event, const String:name[], bool:dontBroadcast)
  123. {
  124. LogMessage("event mission complete!!");
  125. CountWin();
  126. }
  127. public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast)
  128. {
  129. new client = GetClientOfUserId(GetEventInt(event, "userid"));
  130. if(GetClientTeam(client) == _:TFTeam_Blue){
  131. // count killed
  132. countRobotKilled++;
  133. }
  134. }
  135. public Action:Event_MvMPickupCurrency(Handle:event, const String:name[], bool:dontBroadcast)
  136. {
  137. new currency = GetEventInt(event, "currency");
  138. //LogMessage("pickuped: %d", currency);
  139. countPickupedCurrency += currency;
  140. }
  141. public OnPluginEnd()
  142. {
  143. CloseDB();
  144. }
  145. public OnMapStart()
  146. {
  147. alreadySent = false;
  148. countRobotKilled = 0;
  149. countPickupedCurrency = 0;
  150. CountLoaded();
  151. }
  152. public OnMapEnd()
  153. {
  154. SendRobotKilled();
  155. }