[TF2]Rivalry Counter

Posted on October 19, 2013

This is an idea I got from the TF2 Killer’s info plugin. What TF2 Killer’s info does is show relevant info about your killer upon death, like his remaining HP.

I think that a wonderful complement to this would be to also display how many times you killed that player, and how many times he killed you.

I plan to use MySQL for storing the two values. With Sourcemod’s SQL integration and GetSteamAccountId it shouldn’t be too hard.

Okay here’s a first late-night code I’ll have to test. At least it compiles!

/* Plugin Template generated by Pawn Studio */

/*
* Licensed under the GPLv3
*/

#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <tf2>
#include <tf2_stocks>
#include <clients>

#define TAUNT_PLUGIN_VERSION "0.1"

new Handle:db = INVALID_HANDLE;

public Plugin:myinfo = 
{
    name = "Rivalry Counter",
    author = "Ohmnivore",
    description = "Upon death, displays how many times you've been killed by your killer and how many times you've killed him.",
    version = TAUNT_PLUGIN_VERSION,
    url = "ohmnivore.elementfx.com"
};

public OnPluginStart()
{
    CreateConVar("sm_rivalry_version", TAUNT_PLUGIN_VERSION, "Rivalry Counter version.", FCVAR_PLUGIN|FCVAR_NOTIFY);
    HookEvent("player_death", HandleDeath, EventHookMode_Post);
    //RegAdminCmd("sm_rivalry_reset", ResetRivalry, ADMFLAG_SLAY, "Resets all Rivalry Counter data in database.");
    AutoExecConfig(false);
    ConnectToDB();
    InitDB();
}

public ConnectToDB()
{
    new String:error[255];
    db = SQL_Connect("Rivalry", true, error, sizeof(error));

    if (db == INVALID_HANDLE)
    {
        PrintToServer("Could not connect: %s", error);
    } else {
        CloseHandle(db);
    }
}

public InitDB()
{
    if (!SQL_FastQuery(db, "CREATE TABLE IF NOT EXISTS 'rivalry' ('steamid1' VARCHAR(32) NOT NULL PRIMARY KEY, 'steamid2' VARCHAR(32) NOT NULL, 'kill1' INTEGER, 'kill2' INTEGER)"))
    {
        new String:error[255];
        SQL_GetError(db, error, sizeof(error));
        PrintToServer("Could not connect: %s", error);
    }
}

public Action:HandleDeath(Handle:event, const String:name[], bool:dontBroadcast)
{
    new victim = GetEventInt(event,"userid");
    new attacker = GetEventInt(event,"attacker");
    new victimid = GetSteamAccountID(victim, true);
    new attackerid = GetSteamAccountID(attacker, true);
    new String:attackername[60];
    GetClientName(attacker, attackername, sizeof(attackername));
    new String:querydemand[255];

    Format(querydemand, sizeof(querydemand), "SELECT kill1, kill2 FROM rivalry WHERE steamid1 = '%s' AND steamid2 = '%s'", attackerid, victimid);
    new Handle:query = SQL_Query(db, querydemand);
    if (query == INVALID_HANDLE)
    {
        Format(querydemand, sizeof(querydemand), "SELECT kill1, kill2 FROM rivalry WHERE steamid1 = '%s' AND steamid2 = '%s'", victimid, attackerid);
        query = SQL_Query(db, querydemand);
        if (query == INVALID_HANDLE)
        {
            PrintToChat(victim,"'%s' has killed you '%i' times. You've killed him '%i' times.",attackername,1,0);
            Format(querydemand, sizeof(querydemand), "INSERT INTO rivalry VALUES ('%s','%s',1,0)",attackerid,victimid);
            if (!SQL_FastQuery(db, querydemand))
            {
                new String:error[255];
                SQL_GetError(db, error, sizeof(error));
                PrintToServer("Failed to create new rivalry (error: %s)", error);
            }
        }
        else
        {
            SQL_FetchRow(query);
            new kills1 = SQL_FetchInt(query, 1);
            new kills2 = SQL_FetchInt(query, 0);
            PrintToChat(victim,"'%s' has killed you '%i' times. You've killed him '%i' times.",attackername,kills1+1,kills2);
            CloseHandle(query);
            Format(querydemand, sizeof(querydemand), "UPDATE rivalry SET kill2='%s' WHERE steamid1 = '%s' AND steamid2 = '%s'", kills1+1,victimid, attackerid);
            SQL_FastQuery(db, querydemand);
        }
    } 
    else 
    {
        SQL_FetchRow(query);
        new kills1 = SQL_FetchInt(query, 0);
        new kills2 = SQL_FetchInt(query, 1);
        PrintToChat(victim,"'%s' has killed you '%i' times. You've killed him '%i' times.",attackername,kills1+1,kills2);
        CloseHandle(query);
        Format(querydemand, sizeof(querydemand), "UPDATE rivalry SET kill1='%s' WHERE steamid1 = '%s' AND steamid2 = '%s'", kills1+1,attackerid, victimid);
        SQL_FastQuery(db, querydemand);
    }
}