Skip to content

Commit 65a1d2d

Browse files
committed
Date data types fetched from the database have been converted to strings. Let mysql do the formatting instead.
1 parent 3c3f85f commit 65a1d2d

File tree

1 file changed

+132
-144
lines changed

1 file changed

+132
-144
lines changed

scripting/l4d2_simpleplayerstats.sp

Lines changed: 132 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -660,9 +660,9 @@ public void TQ_SyncStatModifiers(Database db, DBResultSet results, const char[]
660660
modifier = pack.ReadFloat();
661661

662662
if (results.AffectedRows > 0) {
663-
Debug("Synchronized cached entry to DB (%s = %.2f)", name, modifier);
663+
Info("Synchronized cached entry to DB (%s = %.2f)", name, modifier);
664664
} else {
665-
Debug("Nothing was synced (%s = %.2f)", name, modifier);
665+
Info("Nothing was synced (%s = %.2f)", name, modifier);
666666
}
667667
}
668668

@@ -1211,13 +1211,13 @@ public void TQ_ShowPlayerRankPanel(Database db, DBResultSet results, const char[
12111211
ExtractPlayerStats(results, map);
12121212

12131213
char steamId[128];
1214-
int createDate;
1215-
int lastJoinDate;
1214+
char createDate[255];
1215+
char lastJoinDate[255];
12161216

12171217
//Retrieve general info
12181218
map.GetString(STATS_STEAM_ID, steamId, sizeof(steamId));
1219-
map.GetValue(STATS_LAST_JOIN_DATE, lastJoinDate);
1220-
map.GetValue(STATS_CREATE_DATE, createDate);
1219+
map.GetString(STATS_LAST_JOIN_DATE, lastJoinDate, sizeof(lastJoinDate));
1220+
map.GetString(STATS_CREATE_DATE, createDate, sizeof(createDate));
12211221

12221222
char msg[255];
12231223
Panel panel = new Panel();
@@ -1412,7 +1412,7 @@ public void TQ_ShowExtraStatsPanel(Database db, DBResultSet results, const char[
14121412

14131413
PanelDrawStatLineBreak(panel);
14141414

1415-
panel.DrawItem("Back", ITEMDRAW_DEFAULT);
1415+
PanelDrawStatItem(panel, "Back");
14161416

14171417
panel.Send(client, ShowExtraStatsMenuHandler, g_iStatsMenuTimeout.IntValue);
14181418

@@ -1423,6 +1423,25 @@ public void TQ_ShowExtraStatsPanel(Database db, DBResultSet results, const char[
14231423
}
14241424
}
14251425

1426+
/**
1427+
* Retrieve the modifier/multiplier value for the requested stat key
1428+
*/
1429+
public float GetStatModifier(const char[] statKey) {
1430+
if (g_mStatModifiers == null) {
1431+
Error("GetStatMultiplier :: The modifier map has not yet been initialized. Using default.");
1432+
return DEFAULT_POINT_MODIFIER;
1433+
}
1434+
float modifier = DEFAULT_POINT_MODIFIER;
1435+
if (!g_mStatModifiers.GetValue(statKey, modifier)) {
1436+
Error("GetStatMultiplier :: Could not retrieve stat modifier for '%s'. Using default.", statKey);
1437+
}
1438+
Debug("Using modifier for '%s' = %.2f", statKey, modifier);
1439+
return modifier;
1440+
}
1441+
1442+
/**
1443+
* Utility function to draw a stat item to a panel. Points are automatically computed by this method using the cached modifiers.
1444+
*/
14261445
public void PanelDrawStat(Panel & panel, const char[] label, const char[] statKey, StringMap & map) {
14271446
int amount = 0;
14281447
char msg[64];
@@ -1437,14 +1456,7 @@ public void PanelDrawStat(Panel & panel, const char[] label, const char[] statKe
14371456

14381457
int displayType = g_iStatsDisplayType.IntValue;
14391458

1440-
//apply points modifier
1441-
float modifier = DEFAULT_POINT_MODIFIER;
1442-
1443-
//retrieve modifier from global map (if available)
1444-
if (!g_mStatModifiers.GetValue(statKey, modifier))
1445-
Debug("No modifier found for stat '%s'. Default modifier will be used (%.2f).", statKey, DEFAULT_POINT_MODIFIER);
1446-
1447-
float points = amount * modifier;
1459+
float points = amount * GetStatModifier(statKey);
14481460

14491461
//display both points and amount
14501462
if (displayType == STATS_DISPLAY_TYPE_BOTH) {
@@ -1552,68 +1564,16 @@ public void ExtractPlayerStats(DBResultSet & results, StringMap & map) {
15521564
return;
15531565
}
15541566

1555-
int idxSteamId = -1;
1556-
int idxLastKnownAlias = -1;
1557-
int idxLastJoinDate = -1;
1558-
int idxSurvivorsKilled = -1;
1559-
int idxSurvivorsIncapped = -1;
1560-
int idxInfectedKilled = -1;
1561-
int idxInfectedHeadshot = -1;
1562-
int idxTotalPoints = -1;
1563-
int idxPlayerRank = -1;
1564-
int idxCreateDate = -1;
1565-
1566-
//Retrieve field indices
1567-
results.FieldNameToNum(STATS_STEAM_ID, idxSteamId);
1568-
results.FieldNameToNum(STATS_LAST_KNOWN_ALIAS, idxLastKnownAlias);
1569-
results.FieldNameToNum(STATS_LAST_JOIN_DATE, idxLastJoinDate);
1570-
results.FieldNameToNum(STATS_SURVIVOR_KILLED, idxSurvivorsKilled);
1571-
results.FieldNameToNum(STATS_SURVIVOR_INCAPPED, idxSurvivorsIncapped);
1572-
results.FieldNameToNum(STATS_INFECTED_KILLED, idxInfectedKilled);
1573-
results.FieldNameToNum(STATS_INFECTED_HEADSHOT, idxInfectedHeadshot);
1574-
results.FieldNameToNum(STATS_TOTAL_POINTS, idxTotalPoints);
1575-
results.FieldNameToNum(STATS_RANK, idxPlayerRank);
1576-
results.FieldNameToNum(STATS_CREATE_DATE, idxCreateDate);
1577-
1578-
//Fetch values
1579-
char steamId[128];
1580-
char lastKnownAlias[255];
1581-
int lastJoinDate = 0;
1582-
float totalPoints = 0.0;
1583-
int rankNum = -1;
1584-
1585-
//Basic Stats
1586-
int survivorsKilled = 0;
1587-
int survivorsIncapped = 0;
1588-
int infectedKilled = 0;
1589-
int infectedHeadshot = 0;
1590-
int createDate = 0;
1591-
1592-
//Fetch general info
1593-
results.FetchString(idxSteamId, steamId, sizeof(steamId));
1594-
results.FetchString(idxLastKnownAlias, lastKnownAlias, sizeof(lastKnownAlias));
1595-
lastJoinDate = results.FetchInt(idxLastJoinDate);
1596-
createDate = results.FetchInt(idxCreateDate);
1597-
totalPoints = results.FetchFloat(idxTotalPoints);
1598-
rankNum = results.FetchInt(idxPlayerRank);
1599-
1600-
//Fetch basic stats
1601-
survivorsKilled = results.FetchInt(idxSurvivorsKilled);
1602-
survivorsIncapped = results.FetchInt(idxSurvivorsIncapped);
1603-
infectedKilled = results.FetchInt(idxInfectedKilled);
1604-
infectedHeadshot = results.FetchInt(idxInfectedHeadshot);
1605-
createDate = results.FetchInt(idxCreateDate);
1606-
1607-
map.SetString(STATS_STEAM_ID, steamId, true);
1608-
map.SetString(STATS_LAST_KNOWN_ALIAS, lastKnownAlias, true);
1609-
map.SetValue(STATS_LAST_JOIN_DATE, lastJoinDate, true);
1610-
map.SetValue(STATS_TOTAL_POINTS, totalPoints, true);
1611-
map.SetValue(STATS_RANK, rankNum, true);
1612-
map.SetValue(STATS_SURVIVOR_KILLED, survivorsKilled, true);
1613-
map.SetValue(STATS_SURVIVOR_INCAPPED, survivorsIncapped, true);
1614-
map.SetValue(STATS_INFECTED_KILLED, infectedKilled, true);
1615-
map.SetValue(STATS_INFECTED_HEADSHOT, infectedHeadshot, true);
1616-
map.SetValue(STATS_CREATE_DATE, createDate, true);
1567+
FetchStrFieldToMap(results, STATS_STEAM_ID, map);
1568+
FetchStrFieldToMap(results, STATS_LAST_KNOWN_ALIAS, map);
1569+
FetchStrFieldToMap(results, STATS_LAST_JOIN_DATE, map);
1570+
FetchFloatFieldToMap(results, STATS_TOTAL_POINTS, map);
1571+
FetchIntFieldToMap(results, STATS_RANK, map);
1572+
FetchIntFieldToMap(results, STATS_SURVIVOR_KILLED, map);
1573+
FetchIntFieldToMap(results, STATS_SURVIVOR_INCAPPED, map);
1574+
FetchIntFieldToMap(results, STATS_INFECTED_KILLED, map);
1575+
FetchIntFieldToMap(results, STATS_INFECTED_HEADSHOT, map);
1576+
FetchStrFieldToMap(results, STATS_CREATE_DATE, map);
16171577
}
16181578

16191579
/**
@@ -1625,57 +1585,72 @@ public void ExtractPlayerStatsExtra(DBResultSet & results, StringMap & map) {
16251585
return;
16261586
}
16271587

1628-
int idxSkeetHunterSniper = -1;
1629-
int idxSkeetHunterShotgun = -1;
1630-
int idxSkeetHunterMelee = -1;
1631-
int idxSkeetTankRock = -1;
1632-
int idxWitchCrownStandard = -1;
1633-
int idxWitchCrownDraw = -1;
1634-
int idxBoomerPop = -1;
1635-
int idxChargerLevel = -1;
1636-
int idxSmokerTongueCut = -1;
1637-
int idxHunterDeadStop = -1;
1638-
int idxBoomerQuad = -1;
1639-
int idxHunterTwentyFive = -1;
1640-
int idxDeathCharge = -1;
1641-
int idxTankRockHits = -1;
1642-
1643-
bool success = true;
1644-
1645-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_SKEET_HUNTER_SNIPER, idxSkeetHunterSniper);
1646-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_SKEET_HUNTER_SHOTGUN, idxSkeetHunterShotgun);
1647-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_SKEET_HUNTER_MELEE, idxSkeetHunterMelee);
1648-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_SKEET_TANK_ROCK, idxSkeetTankRock);
1649-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_WITCH_CROWN_STD, idxWitchCrownStandard);
1650-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_WITCH_CROWN_DRAW, idxWitchCrownDraw);
1651-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_BOOMER_POP, idxBoomerPop);
1652-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_CHARGER_LEVEL, idxChargerLevel);
1653-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_SMOKER_TONGUE_CUT, idxSmokerTongueCut);
1654-
success &= results.FieldNameToNum(STATS_EXTRA_SURV_HUNTER_DEADSTOP, idxHunterDeadStop);
1655-
success &= results.FieldNameToNum(STATS_EXTRA_SI_BOOMER_QUAD, idxBoomerQuad);
1656-
success &= results.FieldNameToNum(STATS_EXTRA_SI_HUNTER_25, idxHunterTwentyFive);
1657-
success &= results.FieldNameToNum(STATS_EXTRA_SI_DEATHCHARGE, idxDeathCharge);
1658-
success &= results.FieldNameToNum(STATS_EXTRA_SI_TANK_ROCK_HITS, idxTankRockHits);
1659-
1660-
if (!success) {
1661-
Error("There was a problem retrieving one of the field names from the result set");
1662-
return;
1663-
}
1664-
1665-
map.SetValue(STATS_EXTRA_SURV_SKEET_HUNTER_SNIPER, results.FetchInt(idxSkeetHunterSniper));
1666-
map.SetValue(STATS_EXTRA_SURV_SKEET_HUNTER_SHOTGUN, results.FetchInt(idxSkeetHunterShotgun));
1667-
map.SetValue(STATS_EXTRA_SURV_SKEET_HUNTER_MELEE, results.FetchInt(idxSkeetHunterMelee));
1668-
map.SetValue(STATS_EXTRA_SURV_SKEET_TANK_ROCK, results.FetchInt(idxSkeetTankRock));
1669-
map.SetValue(STATS_EXTRA_SURV_WITCH_CROWN_STD, results.FetchInt(idxWitchCrownStandard));
1670-
map.SetValue(STATS_EXTRA_SURV_WITCH_CROWN_DRAW, results.FetchInt(idxWitchCrownDraw));
1671-
map.SetValue(STATS_EXTRA_SURV_BOOMER_POP, results.FetchInt(idxBoomerPop));
1672-
map.SetValue(STATS_EXTRA_SURV_CHARGER_LEVEL, results.FetchInt(idxChargerLevel));
1673-
map.SetValue(STATS_EXTRA_SURV_SMOKER_TONGUE_CUT, results.FetchInt(idxSmokerTongueCut));
1674-
map.SetValue(STATS_EXTRA_SURV_HUNTER_DEADSTOP, results.FetchInt(idxHunterDeadStop));
1675-
map.SetValue(STATS_EXTRA_SI_BOOMER_QUAD, results.FetchInt(idxBoomerQuad));
1676-
map.SetValue(STATS_EXTRA_SI_HUNTER_25, results.FetchInt(idxHunterTwentyFive));
1677-
map.SetValue(STATS_EXTRA_SI_DEATHCHARGE, results.FetchInt(idxDeathCharge));
1678-
map.SetValue(STATS_EXTRA_SI_TANK_ROCK_HITS, results.FetchInt(idxTankRockHits));
1588+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_SKEET_HUNTER_SNIPER, map);
1589+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_SKEET_HUNTER_SHOTGUN, map);
1590+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_SKEET_HUNTER_MELEE, map);
1591+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_SKEET_TANK_ROCK, map);
1592+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_WITCH_CROWN_STD, map);
1593+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_WITCH_CROWN_DRAW, map);
1594+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_BOOMER_POP, map);
1595+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_CHARGER_LEVEL, map);
1596+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_SMOKER_TONGUE_CUT, map);
1597+
FetchIntFieldToMap(results, STATS_EXTRA_SURV_HUNTER_DEADSTOP, map);
1598+
FetchIntFieldToMap(results, STATS_EXTRA_SI_BOOMER_QUAD, map);
1599+
FetchIntFieldToMap(results, STATS_EXTRA_SI_HUNTER_25, map);
1600+
FetchIntFieldToMap(results, STATS_EXTRA_SI_DEATHCHARGE, map);
1601+
FetchIntFieldToMap(results, STATS_EXTRA_SI_TANK_ROCK_HITS, map);
1602+
}
1603+
1604+
/**
1605+
* Convenience function to fetch a string field from a resultset and store it's value into a StringMap instance
1606+
*/
1607+
public bool FetchStrFieldToMap(DBResultSet & results, const char[] field, StringMap & map) {
1608+
if (results == null || map == null || StringBlank(field)) {
1609+
return false;
1610+
}
1611+
1612+
int fieldId = -1;
1613+
if (results.FieldNameToNum(field, fieldId) && fieldId >= 0) {
1614+
char value[255];
1615+
results.FetchString(fieldId, value, sizeof(value));
1616+
map.SetString(field, value, true);
1617+
return true;
1618+
}
1619+
return false;
1620+
}
1621+
1622+
/**
1623+
* Convenience function to fetch a float field from a resultset and store it's value into a StringMap instance
1624+
*/
1625+
public bool FetchFloatFieldToMap(DBResultSet & results, const char[] field, StringMap & map) {
1626+
if (results == null || map == null || StringBlank(field)) {
1627+
return false;
1628+
}
1629+
1630+
int fieldId = -1;
1631+
if (results.FieldNameToNum(field, fieldId) && fieldId >= 0) {
1632+
float value = results.FetchFloat(fieldId);
1633+
map.SetValue(field, value, true);
1634+
return true;
1635+
}
1636+
return false;
1637+
}
1638+
1639+
/**
1640+
* Convenience function to fetch an integer field from a resultset and store it's value into a StringMap instance
1641+
*/
1642+
public bool FetchIntFieldToMap(DBResultSet & results, const char[] field, StringMap & map) {
1643+
if (results == null || map == null || StringBlank(field)) {
1644+
return false;
1645+
}
1646+
1647+
int fieldId = -1;
1648+
if (results.FieldNameToNum(field, fieldId) && fieldId >= 0) {
1649+
int value = results.FetchInt(fieldId);
1650+
map.SetValue(field, value, true);
1651+
return true;
1652+
}
1653+
return false;
16791654
}
16801655

16811656
/**
@@ -1984,8 +1959,8 @@ public void TQ_PlayerConnectAnnounce(Database db, DBResultSet results, const cha
19841959

19851960
char steamId[128];
19861961
char lastKnownAlias[255];
1987-
int createDate;
1988-
int lastJoinDate;
1962+
char createDate[255];
1963+
char lastJoinDate[255];
19891964
float totalPoints;
19901965
int rankNum;
19911966
int survivorsKilled;
@@ -1995,19 +1970,20 @@ public void TQ_PlayerConnectAnnounce(Database db, DBResultSet results, const cha
19951970

19961971
map.GetString(STATS_STEAM_ID, steamId, sizeof(steamId));
19971972
map.GetString(STATS_LAST_KNOWN_ALIAS, lastKnownAlias, sizeof(lastKnownAlias));
1998-
map.GetValue(STATS_LAST_JOIN_DATE, lastJoinDate);
1973+
map.GetString(STATS_LAST_JOIN_DATE, lastJoinDate, sizeof(lastJoinDate));
1974+
map.GetString(STATS_CREATE_DATE, createDate, sizeof(createDate));
19991975
map.GetValue(STATS_TOTAL_POINTS, totalPoints);
20001976
map.GetValue(STATS_RANK, rankNum);
20011977
map.GetValue(STATS_SURVIVOR_KILLED, survivorsKilled);
20021978
map.GetValue(STATS_SURVIVOR_INCAPPED, survivorsIncapped);
20031979
map.GetValue(STATS_INFECTED_KILLED, infectedKilled);
20041980
map.GetValue(STATS_INFECTED_HEADSHOT, infectedHeadshot);
2005-
map.GetValue(STATS_CREATE_DATE, createDate);
20061981

2007-
char tmpMsg[253];
1982+
char tmpMsg[255];
20081983

20091984
//parse stats
20101985
ParseKeywordsWithMap(g_ConfigAnnounceFormat, tmpMsg, sizeof(tmpMsg), map);
1986+
20111987
Debug("PARSE RESULT = %s", tmpMsg);
20121988

20131989
Client_PrintToChatAll(true, tmpMsg);
@@ -2094,11 +2070,10 @@ public void ParseKeywordsWithMap(const char[] text, char[] buffer, int size, Str
20942070
found = true;
20952071
}
20962072
else if ((pos = StrContains(g_ConfigAnnounceFormat, searchKeyDate, false)) > -1) {
2097-
int valueInt;
2098-
map.GetValue(keyName, valueInt);
2073+
map.GetString(keyName, valueStr, sizeof(valueStr));
20992074
FormatEx(sKey, searchKeySize, searchKeyDate);
2100-
FormatTime(valueStr, sizeof(valueStr), NULL_STRING, valueInt);
2101-
Debug("(%i: %s) Key '%s' FOUND at position %i (value = %s (%i), type = date)", i, keyName, sKey, pos, valueStr, valueInt);
2075+
//FormatTime(valueStr, sizeof(valueStr), NULL_STRING, valueInt);
2076+
Debug("(%i: %s) Key '%s' FOUND at position %i (value = %s, type = date)", i, keyName, sKey, pos, valueStr);
21022077
found = true;
21032078
}
21042079
else {
@@ -2275,7 +2250,7 @@ public Action Event_PlayerDeath(Event event, const char[] name, bool dontBroadca
22752250
/**
22762251
* Utility function for updating the stat field of the player
22772252
*/
2278-
public void UpdateStat(int client, const char[] column, int amount) {
2253+
void UpdateStat(int client, const char[] column, int amount = 1, int victim = -1) {
22792254
if (!AllowCollectStats()) {
22802255
return;
22812256
}
@@ -2325,29 +2300,41 @@ public void UpdateStat(int client, const char[] column, int amount) {
23252300
pack.WriteString(column);
23262301
pack.WriteCell(client);
23272302
pack.WriteCell(amount);
2303+
pack.WriteCell(victim);
23282304

23292305
g_hDatabase.Query(TQ_UpdateStat, query, pack);
23302306
}
23312307

2332-
public void TQ_UpdateStat(Database db, DBResultSet results, const char[] error, any data) {
2308+
public void TQ_UpdateStat(Database db, DBResultSet results, const char[] error, DataPack pack) {
23332309
if (results == null) {
23342310
Error("TQ_UpdateStat :: Query failed (Reason: %s)", error);
23352311
return;
23362312
}
23372313

2338-
DataPack pack = data;
23392314
char column[128];
23402315

23412316
pack.Reset();
23422317
pack.ReadString(column, sizeof(column));
23432318
int clientId = pack.ReadCell();
2344-
int points = pack.ReadCell();
2319+
int count = pack.ReadCell();
2320+
int victimId = pack.ReadCell();
2321+
2322+
float modifier = GetStatModifier(column);
2323+
float points = count * modifier;
23452324

23462325
if (results.AffectedRows > 0) {
2347-
Debug("Stat '%s' updated for %N (Points: %i)", column, clientId, points);
2326+
if (IS_VALID_CLIENT(victimId)) {
2327+
Debug("Stat '%s' updated for %N (Count: %i, Multiplier: %.2f, Points: %.2f, Victim: %N)", column, clientId, count, modifier, points, victimId);
2328+
} else {
2329+
Debug("Stat '%s' updated for %N (Count: %i, Multiplier: %.2f, Points: %.2f, Victim: N/A)", column, clientId, count, modifier, points);
2330+
}
23482331
}
23492332
else {
2350-
Debug("Stat '%s' not updated for %N (Points: %i)", column, clientId, points);
2333+
if (IS_VALID_CLIENT(victimId)) {
2334+
Debug("Stat '%s' not updated for %N (Count: %i, Multiplier: %.2f, Points: %.2f, Victim: %N)", column, clientId, count, modifier, points, victimId);
2335+
} else {
2336+
Debug("Stat '%s' not updated for %N (Count: %i, Multiplier: %.2f, Points: %.2f, Victim: N/A)", column, clientId, count, modifier, points);
2337+
}
23512338
}
23522339

23532340
delete pack;
@@ -2701,7 +2688,8 @@ public void OnBunnyHopStreak(int survivor, int streak, float maxVelocity) {
27012688
Debug("Stat 'OnBunnyHopStreak' is skipped. Extra stat recording is disabled");
27022689
return;
27032690
}
2704-
Debug("%N had a BHOP stream of %i (Speed: %.2f)", survivor, streak, maxVelocity);
2691+
if (streak >= 3)
2692+
Debug("%N had a BHOP streak of %i (Speed: %.2f)", survivor, streak, maxVelocity);
27052693
}
27062694

27072695
/************* END: SKILL DETECTION *********************/

0 commit comments

Comments
 (0)