Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added knife kills, utility damage and enemies/friendlies-flashed tracking + use in-game flash-assist logic #612

Merged
merged 1 commit into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 36 additions & 32 deletions misc/import_stats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,42 @@ CREATE TABLE `get5_stats_maps`

CREATE TABLE `get5_stats_players`
(
`matchid` int(10) unsigned NOT NULL,
`mapnumber` tinyint(3) unsigned NOT NULL,
`steamid64` bigint(21) unsigned NOT NULL,
`team` varchar(16) NOT NULL DEFAULT '',
`rounds_played` smallint(5) unsigned NOT NULL,
`name` varchar(64) NOT NULL,
`kills` smallint(5) unsigned NOT NULL,
`deaths` smallint(5) unsigned NOT NULL,
`assists` smallint(5) unsigned NOT NULL,
`flashbang_assists` smallint(5) unsigned NOT NULL,
`teamkills` smallint(5) unsigned NOT NULL,
`headshot_kills` smallint(5) unsigned NOT NULL,
`damage` int(10) unsigned NOT NULL,
`bomb_plants` smallint(5) unsigned NOT NULL,
`bomb_defuses` smallint(5) unsigned NOT NULL,
`v1` smallint(5) unsigned NOT NULL,
`v2` smallint(5) unsigned NOT NULL,
`v3` smallint(5) unsigned NOT NULL,
`v4` smallint(5) unsigned NOT NULL,
`v5` smallint(5) unsigned NOT NULL,
`2k` smallint(5) unsigned NOT NULL,
`3k` smallint(5) unsigned NOT NULL,
`4k` smallint(5) unsigned NOT NULL,
`5k` smallint(5) unsigned NOT NULL,
`firstkill_t` smallint(5) unsigned NOT NULL,
`firstkill_ct` smallint(5) unsigned NOT NULL,
`firstdeath_t` smallint(5) unsigned NOT NULL,
`firstdeath_ct` smallint(5) unsigned NOT NULL,
`tradekill` smallint(5) unsigned NOT NULL,
`kast` smallint(5) unsigned NOT NULL,
`contribution_score` smallint(5) unsigned NOT NULL,
`mvp` smallint(5) unsigned NOT NULL,
`matchid` int(10) unsigned NOT NULL,
`mapnumber` tinyint(3) unsigned NOT NULL,
`steamid64` bigint(21) unsigned NOT NULL,
`team` varchar(16) NOT NULL DEFAULT '',
`rounds_played` smallint(5) unsigned NOT NULL,
`name` varchar(64) NOT NULL,
`kills` smallint(5) unsigned NOT NULL,
`deaths` smallint(5) unsigned NOT NULL,
`assists` smallint(5) unsigned NOT NULL,
`flashbang_assists` smallint(5) unsigned NOT NULL,
`teamkills` smallint(5) unsigned NOT NULL,
`knife_kills` smallint(5) unsigned NOT NULL,
`headshot_kills` smallint(5) unsigned NOT NULL,
`damage` int(10) unsigned NOT NULL,
`utility_damage` smallint(5) unsigned NOT NULL,
`enemies_flashed` smallint(5) unsigned NOT NULL,
`friendlies_flashed` smallint(5) unsigned NOT NULL,
`bomb_plants` smallint(5) unsigned NOT NULL,
`bomb_defuses` smallint(5) unsigned NOT NULL,
`v1` smallint(5) unsigned NOT NULL,
`v2` smallint(5) unsigned NOT NULL,
`v3` smallint(5) unsigned NOT NULL,
`v4` smallint(5) unsigned NOT NULL,
`v5` smallint(5) unsigned NOT NULL,
`2k` smallint(5) unsigned NOT NULL,
`3k` smallint(5) unsigned NOT NULL,
`4k` smallint(5) unsigned NOT NULL,
`5k` smallint(5) unsigned NOT NULL,
`firstkill_t` smallint(5) unsigned NOT NULL,
`firstkill_ct` smallint(5) unsigned NOT NULL,
`firstdeath_t` smallint(5) unsigned NOT NULL,
`firstdeath_ct` smallint(5) unsigned NOT NULL,
`tradekill` smallint(5) unsigned NOT NULL,
`kast` smallint(5) unsigned NOT NULL,
`contribution_score` smallint(5) unsigned NOT NULL,
`mvp` smallint(5) unsigned NOT NULL,
PRIMARY KEY (`matchid`, `mapnumber`, `steamid64`),
KEY `steamid64` (`steamid64`),
CONSTRAINT `get5_stats_players_matchid` FOREIGN KEY (`matchid`) REFERENCES `get5_stats_matches` (`matchid`)
Expand Down
2 changes: 0 additions & 2 deletions scripting/get5.sp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ bool g_SetTeamClutching[4];
int g_RoundKills[MAXPLAYERS + 1]; // kills per round each client has gotten
int g_RoundClutchingEnemyCount[MAXPLAYERS +
1]; // number of enemies left alive when last alive on your team
int g_LastFlashBangThrower = -1; // last client to have a flashbang detonate
int g_RoundFlashedBy[MAXPLAYERS + 1];
bool g_TeamFirstKillDone[MATCHTEAM_COUNT];
bool g_TeamFirstDeathDone[MATCHTEAM_COUNT];
int g_PlayerKilledBy[MAXPLAYERS + 1];
Expand Down
8 changes: 4 additions & 4 deletions scripting/get5/eventlogger.sp
Original file line number Diff line number Diff line change
Expand Up @@ -134,18 +134,18 @@ public void EventLogger_GoingLive() {
}

public void EventLogger_PlayerDeath(int killer, int victim, bool headshot, int assister,
int flash_assister, const char[] weapon) {
bool flash_assist, const char[] weapon) {
EventLogger_StartEvent();
AddMapData(params);
AddPlayer(params, "attacker", killer);
AddPlayer(params, "victim", victim);
params.SetInt("headshot", headshot);
params.SetString("weapon", weapon);

if (assister > 0)
if (assister > 0) {
AddPlayer(params, "assister", assister);
if (flash_assister > 0)
AddPlayer(params, "flash_assister", flash_assister);
params.SetBool("flash_assist", flash_assist);
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is a breaking change, but I don't know how much semantic versioning you normally apply to the event-logger.

EventLogger_EndEvent("player_death");
}
Expand Down
107 changes: 63 additions & 44 deletions scripting/get5/stats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public void Stats_PluginStart() {
HookEvent("bomb_planted", Stats_BombPlantedEvent);
HookEvent("bomb_defused", Stats_BombDefusedEvent);
HookEvent("bomb_exploded", Stats_BombExplodedEvent);
HookEvent("flashbang_detonate", Stats_FlashbangDetonateEvent, EventHookMode_Pre);
HookEvent("player_blind", Stats_PlayerBlindEvent);
HookEvent("round_mvp", Stats_RoundMVPEvent);
}
Expand Down Expand Up @@ -44,7 +43,6 @@ public void Stats_ResetRoundValues() {
public void Stats_ResetClientRoundValues(int client) {
g_RoundKills[client] = 0;
g_RoundClutchingEnemyCount[client] = 0;
g_RoundFlashedBy[client] = 0;
g_PlayerKilledBy[client] = -1;
g_PlayerKilledByTime[client] = 0.0;
g_PlayerRoundKillOrAssistOrTradedDeath[client] = false;
Expand Down Expand Up @@ -193,6 +191,7 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr
int victim = GetClientOfUserId(event.GetInt("userid"));
int assister = GetClientOfUserId(event.GetInt("assister"));
bool headshot = event.GetBool("headshot");
bool assistedFlash = event.GetBool("assistedflash");

char weapon[32];
event.GetString("weapon", weapon, sizeof(weapon));
Expand Down Expand Up @@ -232,21 +231,44 @@ public Action Stats_PlayerDeathEvent(Event event, const char[] name, bool dontBr
IncrementPlayerStat(attacker, STAT_KILLS);
g_PlayerRoundKillOrAssistOrTradedDeath[attacker] = true;

if (headshot)
if (headshot) {
IncrementPlayerStat(attacker, STAT_HEADSHOT_KILLS);
}

if (IsValidClient(assister)) {
IncrementPlayerStat(assister, STAT_ASSISTS);
g_PlayerRoundKillOrAssistOrTradedDeath[assister] = true;
// We need the weapon ID to reliably translate to a knife. The regular "bayonet" - as the only knife - is not
// prefixed with "knife" for whatever reason, so searching weapon name strings is unsafe.
CSWeaponID weaponId = CS_AliasToWeaponID(weapon);

// Other than these constants, all knives can be found after CSWeapon_MAX_WEAPONS_NO_KNIFES.
// See https://sourcemod.dev/#/cstrike/enumeration.CSWeaponID
if (weaponId == CSWeapon_KNIFE
|| weaponId == CSWeapon_KNIFE_GG
|| weaponId == CSWeapon_KNIFE_T
|| weaponId == CSWeapon_KNIFE_GHOST
|| weaponId > CSWeapon_MAX_WEAPONS_NO_KNIFES) {
IncrementPlayerStat(attacker, STAT_KNIFE_KILLS);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change to this method instead of StrContains("knife"). As mentioned in the comments here, there's one knife that does not contain "knife"; the regular bayonet. This method should also be future-proof.


int flasher = g_RoundFlashedBy[victim];
if (IsValidClient(flasher) && flasher != attacker)
IncrementPlayerStat(flasher, STAT_FLASHBANG_ASSISTS);
else
flasher = 0;
// Assists should only count towards opposite team
if (HelpfulAttack(assister, victim)) {

// You cannot flash-assist and regular-assist for the same kill.
if (assistedFlash) {
IncrementPlayerStat(assister, STAT_FLASHBANG_ASSISTS);
} else {
IncrementPlayerStat(assister, STAT_ASSISTS);
g_PlayerRoundKillOrAssistOrTradedDeath[assister] = true;
}

} else {

EventLogger_PlayerDeath(attacker, victim, headshot, assister, flasher, weapon);
// Don't count friendly-fire assist at all.
assister = 0;
assistedFlash = false;

}

EventLogger_PlayerDeath(attacker, victim, headshot, assister, assistedFlash, weapon);

} else {
if (attacker == victim)
Expand Down Expand Up @@ -297,10 +319,8 @@ public Action Stats_DamageDealtEvent(Event event, const char[] name, bool dontBr

int attacker = GetClientOfUserId(event.GetInt("attacker"));
int victim = GetClientOfUserId(event.GetInt("userid"));
bool validAttacker = IsValidClient(attacker);
bool validVictim = IsValidClient(victim);

if (validAttacker && validVictim) {
if (HelpfulAttack(attacker, victim)) {
int preDamageHealth = GetClientHealth(victim);
int damage = event.GetInt("dmg_health");
int postDamageHealth = event.GetInt("health");
Expand All @@ -314,7 +334,19 @@ public Action Stats_DamageDealtEvent(Event event, const char[] name, bool dontBr

g_DamageDone[attacker][victim] += damage;
g_DamageDoneHits[attacker][victim]++;

AddToPlayerStat(attacker, STAT_DAMAGE, damage);

// Damage can be dealt by throwing grenades "at" people, physically, but the regular score board does not
// count this as utility damage, so neither do we. Hence no 'smokegrenade' or 'flashbang' here.

char weapon[32];
event.GetString("weapon", weapon, sizeof(weapon));

if (StrEqual(weapon, "hegrenade") || StrEqual(weapon, "inferno")) {
AddToPlayerStat(attacker, STAT_UTILITY_DAMAGE, damage);
}

}

return Plugin_Continue;
Expand Down Expand Up @@ -364,36 +396,35 @@ public Action Stats_BombExplodedEvent(Event event, const char[] name, bool dontB
return Plugin_Continue;
}

public Action Stats_FlashbangDetonateEvent(Event event, const char[] name, bool dontBroadcast) {
public Action Stats_PlayerBlindEvent(Event event, const char[] name, bool dontBroadcast) {
if (g_GameState != Get5State_Live) {
return Plugin_Continue;
}

int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
float duration = event.GetFloat("blind_duration");

if (IsValidClient(client)) {
g_LastFlashBangThrower = client;
if (duration < 2.5) {
// 2.5 is an arbitrary value that closely matches the "enemies flashed" column of the in-game scoreboard.
return Plugin_Continue;
}

return Plugin_Continue;
}
int victim = GetClientOfUserId(event.GetInt("userid"));
int attacker = GetClientOfUserId(event.GetInt("attacker"));

public Action Timer_ResetFlashStatus(Handle timer, int serial) {
int client = GetClientFromSerial(serial);
if (IsValidClient(client)) {
g_RoundFlashedBy[client] = -1;
if (attacker == victim || !IsValidClient(attacker) || !IsValidClient(victim)) {
return Plugin_Continue;
}
}

public Action Stats_PlayerBlindEvent(Event event, const char[] name, bool dontBroadcast) {
if (g_GameState != Get5State_Live) {
int victimTeam = GetClientTeam(victim);
if (victimTeam == CS_TEAM_SPECTATOR || victimTeam == CS_TEAM_NONE) {
return Plugin_Continue;
}

int userid = event.GetInt("userid");
int client = GetClientOfUserId(userid);
RequestFrame(GetFlashInfo, GetClientSerial(client));
if (GetClientTeam(attacker) != victimTeam) {
IncrementPlayerStat(attacker, STAT_ENEMIES_FLASHED);
} else {
IncrementPlayerStat(attacker, STAT_FRIENDLIES_FLASHED);
}

return Plugin_Continue;
}
Expand All @@ -413,18 +444,6 @@ public Action Stats_RoundMVPEvent(Event event, const char[] name, bool dontBroad
return Plugin_Continue;
}

public void GetFlashInfo(int serial) {
int client = GetClientFromSerial(serial);
if (IsValidClient(client)) {
float flashDuration =
GetEntDataFloat(client, FindSendPropInfo("CCSPlayer", "m_flFlashDuration"));
if (flashDuration >= 2.5) {
g_RoundFlashedBy[client] = g_LastFlashBangThrower;
}
CreateTimer(flashDuration, Timer_ResetFlashStatus, serial);
}
}

static int GetPlayerStat(int client, const char[] field) {
GoToPlayer(client);
int value = g_StatsKv.GetNum(field);
Expand Down
20 changes: 15 additions & 5 deletions scripting/get5_mysqlstats.sp
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,11 @@ public void AddPlayerStats(KeyValues kv, MatchTeam team) {
int assists = kv.GetNum(STAT_ASSISTS);
int teamkills = kv.GetNum(STAT_TEAMKILLS);
int damage = kv.GetNum(STAT_DAMAGE);
int utility_damage = kv.GetNum(STAT_UTILITY_DAMAGE);
int enemies_flashed = kv.GetNum(STAT_ENEMIES_FLASHED);
int friendlies_flashed = kv.GetNum(STAT_FRIENDLIES_FLASHED);
int headshot_kills = kv.GetNum(STAT_HEADSHOT_KILLS);
int knife_kills = kv.GetNum(STAT_KNIFE_KILLS);
int roundsplayed = kv.GetNum(STAT_ROUNDSPLAYED);
int plants = kv.GetNum(STAT_BOMBPLANTS);
int defuses = kv.GetNum(STAT_BOMBDEFUSES);
Expand All @@ -263,14 +267,15 @@ public void AddPlayerStats(KeyValues kv, MatchTeam team) {
char teamString[16];
GetTeamString(team, teamString, sizeof(teamString));

// TODO: this should really get split up somehow. Once it hits 32-arguments
// (aka just a few more) it will cause runtime errors and the Format will fail.
// Note that Format() has a 127 argument limit. See SP_MAX_CALL_ARGUMENTS in sourcepawn.
// At this time we're at around 33, so this should not be a problem in the foreseeable future.
// clang-format off
Format(queryBuffer, sizeof(queryBuffer),
"INSERT INTO `get5_stats_players` \
(`matchid`, `mapnumber`, `steamid64`, `team`, \
`rounds_played`, `name`, `kills`, `deaths`, `flashbang_assists`, \
`assists`, `teamkills`, `headshot_kills`, `damage`, \
`assists`, `teamkills`, `knife_kills`, `headshot_kills`, \
`damage`, `utility_damage`, `enemies_flashed`, `friendlies_flashed`, \
`bomb_plants`, `bomb_defuses`, \
`v1`, `v2`, `v3`, `v4`, `v5`, \
`2k`, `3k`, `4k`, `5k`, \
Expand All @@ -280,7 +285,7 @@ public void AddPlayerStats(KeyValues kv, MatchTeam team) {
(%d, %d, '%s', '%s', \
%d, '%s', %d, %d, %d, \
%d, %d, %d, %d, \
%d, %d, \
%d, %d, %d, %d, %d, %d, \
%d, %d, %d, %d, %d, \
%d, %d, %d, %d, \
%d, %d, %d, %d, \
Expand All @@ -292,8 +297,12 @@ public void AddPlayerStats(KeyValues kv, MatchTeam team) {
`flashbang_assists` = VALUES(`flashbang_assists`), \
`assists` = VALUES(`assists`), \
`teamkills` = VALUES(`teamkills`), \
`knife_kills` = VALUES(`knife_kills`), \
`headshot_kills` = VALUES(`headshot_kills`), \
`damage` = VALUES(`damage`), \
`utility_damage` = VALUES(`utility_damage`), \
`enemies_flashed` = VALUES(`enemies_flashed`), \
`friendlies_flashed` = VALUES(`friendlies_flashed`), \
`bomb_plants` = VALUES(`bomb_plants`), \
`bomb_defuses` = VALUES(`bomb_defuses`), \
`v1` = VALUES(`v1`), \
Expand All @@ -315,7 +324,8 @@ public void AddPlayerStats(KeyValues kv, MatchTeam team) {
`mvp` = VALUES(`mvp`)",
g_MatchID, mapNumber, authSz, teamString,
roundsplayed, nameSz, kills, deaths, flashbang_assists,
assists, teamkills, headshot_kills, damage,
assists, teamkills, knife_kills, headshot_kills, damage, utility_damage,
enemies_flashed, friendlies_flashed,
plants, defuses,
v1, v2, v3, v4, v5,
k2, k3, k4, k5,
Expand Down
4 changes: 4 additions & 0 deletions scripting/include/get5.inc
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ native int Get5_IncreasePlayerStat(int client, const char[] statName, int amount
#define STAT_TEAMKILLS "teamkills"
#define STAT_SUICIDES "suicides"
#define STAT_DAMAGE "damage"
#define STAT_UTILITY_DAMAGE "util_damage"
#define STAT_ENEMIES_FLASHED "enemies_flashed"
#define STAT_FRIENDLIES_FLASHED "friendlies_flashed"
#define STAT_KNIFE_KILLS "knife_kills"
#define STAT_HEADSHOT_KILLS "headshot_kills"
#define STAT_ROUNDSPLAYED "roundsplayed"
#define STAT_BOMBDEFUSES "bomb_defuses"
Expand Down