#include <sourcemod>
int TotalDamage[MAXPLAYERS+1];
Database db;
public void OnPluginStart()
{
Database.Connect(SQLCB, "topdmground");
HookEvent("player_hurt", eventHurt);
HookEvent("round_start", eventStart);
HookEvent("client_disconnect", eventUpdate);
HookEvent("player_death", eventUpdate);
HookEvent("player_spawn", eventSpawn);
}
public void SQLCB(Database dbs, const char[] error, any data)
{
if(error[0] || dbs == INVALID_HANDLE)
{
SetFailState("Fail: %s", error)
return;
}
db = dbs;
char sQuery[256];
Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `topdmground` (\
`index` INTEGER(10) NOT NULL PRIMARY KEY, \
`damage` INTEGER(12) NOT NULL");
SQL_Query(db, sQuery);
}
public void OnMapStart()
{
CreateTimer(1.0, TimerRepeat, _, TIMER_REPEAT|TIMER_FLAG_NO_MAPCHANGE);
}
public void eventStart(Event hEvent, const char[] sEvent, bool bdb)
{
char sQuery[32];
Format(sQuery, sizeof(sQuery), "TRUNCATE TABLE `topdmground`");
SQL_Query(db, sQuery);
}
public void eventHurt(Event hEvent, const char[] sEvent, bool bdb)
{
int iClient = GetClientOfUserId(hEvent.GetInt("attacker"));
if(iClient != 0) TotalDamage[iClient] += hEvent.GetInt("dmg_health") + hEvent.GetInt("dmg_armor");
}
public void eventSpawn(Event hEvent, const char[] sEvent, bool bdb)
{
char sQuery[256];
int i = GetClientOfUserId(hEvent.GetInt("userid"));
if(i != 0)
{
if(IsClientInGame(i) && !IsFakeClient(i) && IsPlayerAlive(i))
{
Format(sQuery, sizeof(sQuery), "INSERT INTO `topdmground` (`index`, `damage`) VALUES ('%i', '%i')", i, TotalDamage[i]);
SQL_Query(db, sQuery);
}
}
}
public void eventUpdate(Event hEvent, const char[] sEvent, bool bdb)
{
char sQuery[256];
int i = GetClientOfUserId(hEvent.GetInt("userid"));
if(i != 0)
{
if(IsClientInGame(i) && !IsFakeClient(i) && IsPlayerAlive(i))
{
Format(sQuery, sizeof(sQuery), "UPDATE `topdmground` SET `damage`='%i' WHERE `index`='%i'", TotalDamage[i], i);
SQL_Query(db, sQuery);
}
}
}
public Action TimerRepeat(Handle hTimer)
{
char sQuery[256], cat[2][16];
int totalDMG, index, place, dmg, needdmg;
SetHudTextParams(0.2, 0.80, 1.0, 0, 255, 0, 255, 2, 0.0 , 0.0, 0.0);
for(int i = 1; i <= MaxClients; i++)
{
if(IsClientInGame(i))
{
totalDMG += TotalDamage[i];
Format(sQuery, sizeof(sQuery), "SELECT * FROM `topdmground` ORDER BY `damage` DESC LIMIT %i", i);
DBResultSet result = SQL_Query(db, sQuery);
if(result.HasResults && result != INVALID_HANDLE)
{
for(int j = 1; j <= i; j++)
{
result.FetchRow()
}
index = result.FetchInt(0);
place = i;
dmg = result.FetchInt(1);
totalDMG -= dmg;
if(i == 1) needdmg = 0;
if(i > 1)
{
result.Rewind();
for(int j = 1; j < i; j++)
{
result.FetchRow();
}
needdmg = result.FetchInt(1) - dmg;
}
else return Plugin_Stop;
}
delete result;
if(needdmg > 0)
{
IntToString(needdmg, cat[0], sizeof(cat));
StrCat("+", 16, cat[0]);
}
if(totalDMG > 0)
{
IntToString(totalDMG, cat[1], sizeof(cat));
StrCat("+", 16, cat[1]);
}
ShowHudText(index, -1, "#%i (D: %i | N: %s | F: %s)", place, dmg, cat[0], cat[1]);
}
}
return Plugin_Continue;
}