Skip to content

Commit

Permalink
add Shavit_GuessPointsForTime so we can recalc styles only on WR
Browse files Browse the repository at this point in the history
  • Loading branch information
rtldg committed Oct 5, 2021
1 parent 3388c7b commit 22a87ce
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 6 deletions.
16 changes: 16 additions & 0 deletions addons/sourcemod/scripting/include/shavit.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2699,6 +2699,21 @@ native void Shavit_SetPlayerPreFrames(int client, int PreFrame);
*/
native void Shavit_GetPlainChatrank(int client, char[] buf, int buflen, bool includename=false);

/*
* Calculates how many points a time will give.
* Used to minimize number of SQL queries.
* Requires shavit-rankings.
*
* @param track The track the time is from.
* @param style The style the time is from.
* @param tier The map tier. -1 to use the current map's tier.
* @param time The time you want to calculate the points for.
* @param wr WR.
*
* @return The number of points the time would give.
*/
native float Shavit_GuessPointsForTime(int track, int style, int tier, float time, float wr);

/*
* Used to delete a WR. Used to ensure Shavit_OnWRDeleted is ran.
*
Expand Down Expand Up @@ -2876,5 +2891,6 @@ public void __pl_shavit_SetNTVOptional()
MarkNativeAsOptional("Shavit_GetReplayCachePostFrames");
MarkNativeAsOptional("Shavit_GetLoopingBotByName");
MarkNativeAsOptional("Shavit_SetReplayCacheName");
MarkNativeAsOptional("Shavit_GuessPointsForTime");
}
#endif
56 changes: 54 additions & 2 deletions addons/sourcemod/scripting/shavit-rankings.sp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public APLRes AskPluginLoad2(Handle myself, bool late, char[] error, int err_max
CreateNative("Shavit_GetWRCount", Native_GetWRCount);
CreateNative("Shavit_GetWRHolders", Native_GetWRHolders);
CreateNative("Shavit_GetWRHolderRank", Native_GetWRHolderRank);
CreateNative("Shavit_GuessPointsForTime", Native_GuessPointsForTime);

RegPluginLibrary("shavit-rankings");

Expand Down Expand Up @@ -291,7 +292,7 @@ public void Shavit_OnDatabaseLoaded()
hTrans.AddQuery(sQuery);

FormatEx(sQuery, sizeof(sQuery),
"CREATE FUNCTION GetRecordPoints(rstyle INT, rtrack INT, rtime FLOAT, rmap VARCHAR(255), pointspertier FLOAT, stylemultiplier FLOAT, pwr FLOAT, xtier INT) " ...
"CREATE FUNCTION GetRecordPoints(rtrack INT, rtime FLOAT, rmap VARCHAR(255), pointspertier FLOAT, stylemultiplier FLOAT, pwr FLOAT, xtier INT) " ...
"RETURNS FLOAT " ...
"READS SQL DATA " ...
"BEGIN " ...
Expand Down Expand Up @@ -743,7 +744,7 @@ void FormatRecalculate(bool bUseCurrentMap, int track, int style, char[] sQuery,
" WHERE style = %d %s %s%s%c %s %s"...
" GROUP BY map, track, style "...
") as WR "...
"SET PT.points = GetRecordPoints(PT.style, PT.track, PT.time, %s, %.1f, %.3f, WR.time, %d) "...
"SET PT.points = GetRecordPoints(PT.track, PT.time, %s, %.1f, %.3f, WR.time, %d) "...
"WHERE PT.style = WR.style and PT.track = WR.track and PT.map = WR.map;",
gS_MySQLPrefix, gS_MySQLPrefix, style,
(bUseCurrentMap || track != -1) ? "AND" : "",
Expand Down Expand Up @@ -832,6 +833,11 @@ void RecalculateAll()

public void Shavit_OnFinish_Post(int client, int style, float time, int jumps, int strafes, float sync, int rank, int overwrite, int track)
{
if (rank != 1)
{
return;
}

#if defined DEBUG
PrintToServer("Recalculating points. (%s, %d, %d)", map, track, style);
#endif
Expand Down Expand Up @@ -1301,6 +1307,52 @@ public int Native_Rankings_DeleteMap(Handle handler, int numParams)
gH_SQL.Query(SQL_DeleteMap_Callback, sQuery, StrEqual(gS_Map, sMap, false), DBPrio_High);
}

public int Native_GuessPointsForTime(Handle plugin, int numParams)
{
int rtrack = GetNativeCell(1);
int rstyle = GetNativeCell(2);
int tier = GetNativeCell(3);
float rtime = view_as<float>(GetNativeCell(4));
float pwr = view_as<float>(GetNativeCell(5));

float ppoints = Sourcepawn_GetRecordPoints(
rtrack,
rtime,
gCV_PointsPerTier.FloatValue,
Shavit_GetStyleSettingFloat(rstyle, "rankingmultiplier"),
pwr,
tier == -1 ? gI_Tier : tier
);

return view_as<int>(ppoints);
}

float Sourcepawn_GetRecordPoints(int rtrack, float rtime, float pointspertier, float stylemultiplier, float pwr, int ptier)
{
float ppoints = 0.0;

if (rtrack > 0)
{
ptier = 1;
}

ppoints = ((pointspertier * ptier) * 1.5) + (pwr / 15.0);

if (rtime > pwr)
{
ppoints = ppoints * (pwr / rtime);
}

ppoints = ppoints * stylemultiplier;

if (rtrack > 0)
{
ppoints = ppoints * 0.25;
}

return ppoints;
}

public void SQL_DeleteMap_Callback(Database db, DBResultSet results, const char[] error, any data)
{
if(results == null)
Expand Down
10 changes: 6 additions & 4 deletions addons/sourcemod/scripting/shavit-wr.sp
Original file line number Diff line number Diff line change
Expand Up @@ -2486,6 +2486,8 @@ public void Shavit_OnFinish(int client, int style, float time, int jumps, int st

if(iOverwrite > 0)
{
float fPoints = gB_Rankings ? Shavit_GuessPointsForTime(track, style, -1, time, gF_WRTime[style][track]) : 0.0;

char sQuery[1024];

if(iOverwrite == 1) // insert
Expand All @@ -2494,17 +2496,17 @@ public void Shavit_OnFinish(int client, int style, float time, int jumps, int st
gS_ChatStrings.sVariable, sTrack, gS_ChatStrings.sText, "FirstCompletion", LANG_SERVER, gS_ChatStrings.sVariable2, client, gS_ChatStrings.sText, gS_ChatStrings.sStyle, gS_StyleStrings[style].sStyleName, gS_ChatStrings.sText, gS_ChatStrings.sVariable2, sTime, gS_ChatStrings.sText, gS_ChatStrings.sVariable, iRank, gS_ChatStrings.sText, jumps, strafes, sSync, gS_ChatStrings.sText);

FormatEx(sQuery, sizeof(sQuery),
"INSERT INTO %splayertimes (auth, map, time, jumps, date, style, strafes, sync, points, track, perfs, exact_time_int) VALUES (%d, '%s', %f, %d, %d, %d, %d, %.2f, 0.0, %d, %.2f, %d);",
gS_MySQLPrefix, iSteamID, gS_Map, time, jumps, timestamp, style, strafes, sync, track, perfs, view_as<int>(time));
"INSERT INTO %splayertimes (auth, map, time, jumps, date, style, strafes, sync, points, track, perfs, exact_time_int) VALUES (%d, '%s', %f, %d, %d, %d, %d, %.2f, %f, %d, %.2f, %d);",
gS_MySQLPrefix, iSteamID, gS_Map, time, jumps, timestamp, style, strafes, sync, fPoints, track, perfs, view_as<int>(time));
}
else // update
{
FormatEx(sMessage, 255, "%s[%s]%s %T",
gS_ChatStrings.sVariable, sTrack, gS_ChatStrings.sText, "NotFirstCompletion", LANG_SERVER, gS_ChatStrings.sVariable2, client, gS_ChatStrings.sText, gS_ChatStrings.sStyle, gS_StyleStrings[style].sStyleName, gS_ChatStrings.sText, gS_ChatStrings.sVariable2, sTime, gS_ChatStrings.sText, gS_ChatStrings.sVariable, iRank, gS_ChatStrings.sText, jumps, strafes, sSync, gS_ChatStrings.sText, gS_ChatStrings.sWarning, sDifference);

FormatEx(sQuery, sizeof(sQuery),
"UPDATE %splayertimes SET time = %f, jumps = %d, date = %d, strafes = %d, sync = %.02f, points = 0.0, perfs = %.2f, exact_time_int = %d, completions = completions + 1 WHERE map = '%s' AND auth = %d AND style = %d AND track = %d;",
gS_MySQLPrefix, time, jumps, timestamp, strafes, sync, perfs, view_as<int>(time), gS_Map, iSteamID, style, track);
"UPDATE %splayertimes SET time = %f, jumps = %d, date = %d, strafes = %d, sync = %.02f, points = %f, perfs = %.2f, exact_time_int = %d, completions = completions + 1 WHERE map = '%s' AND auth = %d AND style = %d AND track = %d;",
gS_MySQLPrefix, time, jumps, timestamp, strafes, sync, fPoints, perfs, view_as<int>(time), gS_Map, iSteamID, style, track);
}

gH_SQL.Query(SQL_OnFinish_Callback, sQuery, GetClientSerial(client), DBPrio_High);
Expand Down

0 comments on commit 22a87ce

Please sign in to comment.