Sushi鯖で殺されたロボットの数を計測することにしました。
特に意味なし。どのくらいBOTが死んでいったのか知りたくなっただけっていう。
motdの上のほうで見れる
計測するためのSourceMod Pluginは後述。
重要なのはこのへんかな。
HookEvent("mvm_wave_complete", Event_WaveComplete, EventHookMode_PostNoCopy); HookEvent("mvm_mission_complete", Event_MissionComplete, EventHookMode_PostNoCopy); HookEvent("mvm_pickup_currency", Event_MvMPickupCurrency, EventHookMode_Post);
EventHookModeのPostNoCopyとPostの違いは、eventのデータを使えるか使えないかということ。単にイベントが発生したときになんかしたいだけならNoCopyで、イベントに含まれる情報を使いたいならPostにする必要がある。当然ながらNoCopyのほうが負荷/リソースが軽量で済む。(ってTutorialに書いてあったよ)
#pragma semicolon 1 #include <sourcemod> #include <dbi> #include <tf2> #define PLUGIN_VERSION "1.0.0" public Plugin:myinfo = { name = "MvM Stats", author = "kimoto", description = "MvM Stats", version = PLUGIN_VERSION, url = "http://kymt.me/" }; new Handle:db = INVALID_HANDLE; new bool:alreadySent = false; new countRobotKilled = 0; new countPickupedCurrency = 0; PrintLastError() { decl String:error[256]; SQL_GetError(db, error, sizeof(error)); PrintToServer("Failed to query (error: %s)", error); } InitDB() { alreadySent = false; countRobotKilled = 0; countPickupedCurrency = 0; LogMessage("Try to connect mysql server"); new String:error[256]; if((db = SQL_DefConnect(error, sizeof(error))) == INVALID_HANDLE) { PrintToServer("Could not connect (enable silent mode): %s", error); return; } LogMessage("connected: %s", error); } IsAliveDB() { return (db != INVALID_HANDLE); } CountLoaded() { if(!IsAliveDB()) return; decl String:map[256]; GetCurrentMap(map, sizeof(map)); SQL_QuoteString(db, map, map, sizeof(map)); decl String:query[256]; Format(query, sizeof(query), "insert into game_stats (map_code, loaded) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE loaded = loaded + 1;", map); if(!SQL_FastQuery(db, query)){ PrintLastError(); } } CountPlayed() { if(!IsAliveDB()) return; decl String:map[256]; GetCurrentMap(map, sizeof(map)); SQL_QuoteString(db, map, map, sizeof(map)); decl String:query[256]; Format(query, sizeof(query), "insert into game_stats (map_code, played) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE played = played + 1;", map); if(!SQL_FastQuery(db, query)){ PrintLastError(); } } CountWin() { if(!IsAliveDB()) return; decl String:map[256]; GetCurrentMap(map, sizeof(map)); SQL_QuoteString(db, map, map, sizeof(map)); decl String:query[256]; Format(query, sizeof(query), "insert into game_stats (map_code, win) VALUES (\"%s\", 1) ON DUPLICATE KEY UPDATE win = win + 1;", map); if(!SQL_FastQuery(db, query)){ PrintLastError(); } } SendRobotKilled() { if(!IsAliveDB()) return; decl String:query[256]; Format(query, sizeof(query), "UPDATE mvm_stats SET total_robot_killed = total_robot_killed + %d, total_amount = total_amount + %d;", countRobotKilled, countPickupedCurrency); if(SQL_FastQuery(db, query)){ countRobotKilled = 0; // reset to zero if SQL query succeed countPickupedCurrency = 0; // reset to zero if SQL query succeed }else{ PrintLastError(); } } CloseDB() { if(IsAliveDB()) CloseHandle(db); } public OnPluginStart() { InitDB(); RegServerCmd("mvm_stats_killed", Command_MvMStatsKilled); HookEvent("mvm_wave_complete", Event_WaveComplete, EventHookMode_PostNoCopy); HookEvent("mvm_mission_complete", Event_MissionComplete, EventHookMode_PostNoCopy); HookEvent("mvm_pickup_currency", Event_MvMPickupCurrency, EventHookMode_Post); HookEvent("teamplay_round_active", Event_RoundActive, EventHookMode_PostNoCopy); HookEvent("player_death", Event_PlayerDeath, EventHookMode_Post); } public Action:Command_MvMStatsKilled(args) { LogMessage("Robot was killed: %d", countRobotKilled); LogMessage("Pickuped Amount: %d", countPickupedCurrency); } public Action:Event_RoundActive(Handle:event, const String:name[], bool:dontBroadcast) { if(alreadySent == false){ CountPlayed(); alreadySent = true; } } public Action:Event_WaveComplete(Handle:event, const String:name[], bool:dontBroadcast) { LogMessage("Wave Complete Event"); LogMessage("Sending robot killed information"); SendRobotKilled(); } public Action:Event_MissionComplete(Handle:event, const String:name[], bool:dontBroadcast) { LogMessage("event mission complete!!"); CountWin(); } public Action:Event_PlayerDeath(Handle:event, const String:name[], bool:dontBroadcast) { new client = GetClientOfUserId(GetEventInt(event, "userid")); if(GetClientTeam(client) == _:TFTeam_Blue){ // count killed countRobotKilled++; } } public Action:Event_MvMPickupCurrency(Handle:event, const String:name[], bool:dontBroadcast) { new currency = GetEventInt(event, "currency"); //LogMessage("pickuped: %d", currency); countPickupedCurrency += currency; } public OnPluginEnd() { CloseDB(); } public OnMapStart() { alreadySent = false; countRobotKilled = 0; countPickupedCurrency = 0; CountLoaded(); } public OnMapEnd() { SendRobotKilled(); }