Skip to content

Commit

Permalink
Tweaks to course top tables to reduce bad align issues caused by non-…
Browse files Browse the repository at this point in the history
…standard characters
  • Loading branch information
zer0k-z committed Feb 12, 2025
1 parent 2c9dd2a commit 63923d7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 42 deletions.
1 change: 1 addition & 0 deletions src/cs2kz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ PLUGIN_EXPOSE(KZPlugin, g_KZPlugin);

bool KZPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
setlocale(LC_ALL, "en_US.utf8");
PLUGIN_SAVEVARS();

if (!utils::Initialize(ismm, error, maxlen))
Expand Down
2 changes: 1 addition & 1 deletion src/kz/timer/queries/base_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct BaseRequest
CUtlString courseName;
bool requestingFirstCourse = false;

u64 limit = 20;
u64 limit = 10;
u64 offset = 0;

u64 targetSteamID64 {};
Expand Down
96 changes: 65 additions & 31 deletions src/kz/timer/queries/course_top.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ struct CourseTopRequest : public BaseRequest
u64 steamid64;
u64 points {}; // 0 for local database

CUtlString GetName()
{
return name;
}

CUtlString GetRunID()
{
CUtlString fmt;
Expand Down Expand Up @@ -268,79 +273,108 @@ struct CourseTopRequest : public BaseRequest
void ReplyGlobal()
{
KZPlayer *player = g_pKZPlayerManager->ToPlayer(userID);
CUtlString rank;

// Overall table
CUtlString headers[Q_ARRAYSIZE(columnKeysGlobal)];
for (u32 i = 0; i < Q_ARRAYSIZE(columnKeysGlobal); i++)
{
headers[i] = player->languageService->PrepareMessage(columnKeysGlobal[i]).c_str();
}
utils::Table<Q_ARRAYSIZE(columnKeysGlobal)> table(
player->languageService->PrepareMessage(COURSE_TOP_TABLE_KEY_GLOBAL, mapName.Get(), courseName.Get(), modeName.Get()).c_str(), headers);
FOR_EACH_VEC(wrData.overallData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = wrData.overallData[i];
table.SetRow(i, rank, stats.GetName(), stats.GetTime(), stats.GetTeleportCount(), stats.GetSteamID64(), stats.GetPoints(),
stats.GetRunID());
}
player->PrintConsole(false, false, table.GetSeparator("="));
player->PrintConsole(false, false, table.GetTitle());
player->PrintConsole(false, false, table.GetHeader());

for (u32 i = 0; i < table.GetNumEntries(); i++)
{
player->PrintConsole(false, false, table.GetLine(i));
}
player->PrintConsole(false, false, table.GetSeparator("="));

// Pro table
CUtlString headersPro[Q_ARRAYSIZE(columnKeysGlobalPro)];
for (u32 i = 0; i < Q_ARRAYSIZE(columnKeysGlobalPro); i++)
{
headersPro[i] = player->languageService->PrepareMessage(columnKeysGlobalPro[i]).c_str();
}
utils::DualTable<Q_ARRAYSIZE(columnKeysGlobal), Q_ARRAYSIZE(columnKeysGlobalPro)> dualTable(
player->languageService->PrepareMessage(COURSE_TOP_TABLE_KEY_GLOBAL, mapName.Get(), courseName.Get(), modeName.Get()).c_str(), headers,
utils::Table<Q_ARRAYSIZE(columnKeysGlobalPro)> tablePro(
player->languageService->PrepareMessage(COURSE_TOP_PRO_TABLE_KEY_GLOBAL, mapName.Get(), courseName.Get(), modeName.Get()).c_str(),
headersPro);
CUtlString rank;
FOR_EACH_VEC(wrData.overallData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = wrData.overallData[i];
dualTable.left.SetRow(i, rank, stats.name, stats.GetTime(), stats.GetTeleportCount(), stats.GetSteamID64(), stats.GetPoints(),
stats.GetRunID());
}
FOR_EACH_VEC(wrData.proData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = wrData.proData[i];
dualTable.right.SetRow(i, rank, stats.name, stats.GetTime(), stats.GetSteamID64(), stats.GetPoints(), stats.GetRunID());
tablePro.SetRow(i, rank, stats.GetName(), stats.GetTime(), stats.GetSteamID64(), stats.GetPoints(), stats.GetRunID());
}
player->PrintConsole(false, false, dualTable.GetTitle());
player->PrintConsole(false, false, dualTable.GetHeader());
player->PrintConsole(false, false, dualTable.GetSeparator());
for (u32 i = 0; i < dualTable.GetNumEntries(); i++)
player->PrintConsole(false, false, tablePro.GetSeparator("="));
player->PrintConsole(false, false, tablePro.GetTitle());
player->PrintConsole(false, false, tablePro.GetHeader());
for (u32 i = 0; i < tablePro.GetNumEntries(); i++)
{
player->PrintConsole(false, false, dualTable.GetLine(i));
player->PrintConsole(false, false, tablePro.GetLine(i));
}
player->PrintConsole(false, false, tablePro.GetSeparator("="));
}

void ReplyLocal()
{
KZPlayer *player = g_pKZPlayerManager->ToPlayer(userID);
CUtlString rank;

// Overall table
CUtlString headers[Q_ARRAYSIZE(columnKeysLocal)];
for (u32 i = 0; i < Q_ARRAYSIZE(columnKeysLocal); i++)
{
headers[i] = player->languageService->PrepareMessage(columnKeysLocal[i]).c_str();
}
utils::Table<Q_ARRAYSIZE(columnKeysLocal)> table(
player->languageService->PrepareMessage(COURSE_TOP_TABLE_KEY, mapName.Get(), courseName.Get(), modeName.Get()).c_str(), headers);
FOR_EACH_VEC(srData.overallData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = srData.overallData[i];
table.SetRow(i, rank, stats.GetName(), stats.GetTime(), stats.GetTeleportCount(), stats.GetSteamID64(), stats.GetRunID());
}
player->PrintConsole(false, false, table.GetSeparator("="));
player->PrintConsole(false, false, table.GetTitle());
player->PrintConsole(false, false, table.GetHeader());

for (u32 i = 0; i < table.GetNumEntries(); i++)
{
player->PrintConsole(false, false, table.GetLine(i));
}
player->PrintConsole(false, false, table.GetSeparator("="));
// Pro table
CUtlString headersPro[Q_ARRAYSIZE(columnKeysLocalPro)];
for (u32 i = 0; i < Q_ARRAYSIZE(columnKeysLocalPro); i++)
{
headersPro[i] = player->languageService->PrepareMessage(columnKeysLocalPro[i]).c_str();
}
utils::DualTable<Q_ARRAYSIZE(columnKeysLocal), Q_ARRAYSIZE(columnKeysLocalPro)> dualTable(
player->languageService->PrepareMessage(COURSE_TOP_TABLE_KEY, mapName.Get(), courseName.Get(), modeName.Get()).c_str(), headers,
utils::Table<Q_ARRAYSIZE(columnKeysLocalPro)> tablePro(
player->languageService->PrepareMessage(COURSE_TOP_PRO_TABLE_KEY, mapName.Get(), courseName.Get(), modeName.Get()).c_str(), headersPro);
CUtlString rank;
FOR_EACH_VEC(srData.overallData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = srData.overallData[i];
dualTable.left.SetRow(i, rank, stats.name, stats.GetTime(), stats.GetTeleportCount(), stats.GetSteamID64(), stats.GetRunID());
}
FOR_EACH_VEC(srData.proData, i)
{
rank.Format("%llu", this->offset + i + 1);
RunStats stats = srData.proData[i];
dualTable.right.SetRow(i, rank, stats.name, stats.GetTime(), stats.GetSteamID64(), stats.GetRunID());
tablePro.SetRow(i, rank, stats.GetName(), stats.GetTime(), stats.GetSteamID64(), stats.GetRunID());
}
player->PrintConsole(false, false, dualTable.GetTitle());
player->PrintConsole(false, false, dualTable.GetHeader());
player->PrintConsole(false, false, dualTable.GetSeparator());
for (u32 i = 0; i < dualTable.GetNumEntries(); i++)
player->PrintConsole(false, false, tablePro.GetSeparator("="));
player->PrintConsole(false, false, tablePro.GetTitle());
player->PrintConsole(false, false, tablePro.GetHeader());
for (u32 i = 0; i < tablePro.GetNumEntries(); i++)
{
player->PrintConsole(false, false, dualTable.GetLine(i));
player->PrintConsole(false, false, tablePro.GetLine(i));
}
player->PrintConsole(false, false, tablePro.GetSeparator("="));
}
};

Expand Down
27 changes: 17 additions & 10 deletions src/utils/tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace utils
for (u32 i = 0; i < columnCount; i++)
{
this->headers[i] = headers[i];
this->headers[i].Append(""); // Add a space to reset the "font" to monospace
columnLengths[i] = this->headers[i].Length();
}
}
Expand All @@ -28,6 +29,7 @@ namespace utils
for (u32 i = 0; i < columnCount; i++)
{
this->headers[i] = headers[i];
this->headers[i].Append(""); // Add a space to reset the "font" to monospace
columnLengths[i] = this->headers[i].Length();
}
}
Expand Down Expand Up @@ -55,7 +57,8 @@ namespace utils
entries.SetCountNonDestructively(row + 1);
}
entries[row].data[column] = value;
columnLengths[column] = MAX(columnLengths[column], V_strlen(value));
entries[row].data[column].Append(""); // Add a space to reset the "font" to monospace
columnLengths[column] = MAX(columnLengths[column], V_strlen(entries[row].data[i].Get()));
return true;
}

Expand All @@ -75,6 +78,7 @@ namespace utils
entries[row] = {args...};
for (u32 i = 0; i < columnCount; i++)
{
entries[row].data[i].Append(""); // Add a space to reset the "font" to monospace
columnLengths[i] = MAX(columnLengths[i], (u32)V_strlen(entries[row].data[i].Get()));
}
}
Expand All @@ -101,7 +105,8 @@ namespace utils
for (u32 i = 0; i < columnCount; i++)
{
CUtlString value;
value.Format("%-*s%s", columnLengths[i], headers[i].Get(), i == columnCount - 1 ? "" : " ");
value.Format("%-*s%s", columnLengths[i] + GetPaddingForWideString(headers[i].Get()), headers[i].Get(),
i == columnCount - 1 ? "" : " ");
for (i32 i = 0; i < value.Length() - 1; i++)
{
if (value[i] == '%' && value[i + 1] == '%')
Expand All @@ -125,7 +130,8 @@ namespace utils
for (u32 i = 0; i < columnCount; i++)
{
CUtlString value;
value.Format("%-*s%s", columnLengths[i], entries[row].data[i].Get(), i == columnCount - 1 ? "" : " ");
value.Format("%-*s%s", columnLengths[i] + GetPaddingForWideString(entries[row].data[i].Get()), entries[row].data[i].Get(),
i == columnCount - 1 ? "" : " ");
for (i32 i = 0; i < value.Length() - 1; i++)
{
if (value[i] == '%' && value[i + 1] == '%')
Expand All @@ -139,12 +145,12 @@ namespace utils
return result;
}

CUtlString GetSeparator()
CUtlString GetSeparator(const char *characters = "_")
{
CUtlString result;
for (u32 i = 0; i < GetTableWidth(); i++)
{
result += "_";
result += characters;
}
return result;
}
Expand Down Expand Up @@ -176,14 +182,14 @@ namespace utils
CUtlString GetTitle()
{
CUtlString result;
result.Format("%-*s | %-*s", left.GetTableWidth(), left.GetTitle().Get(), right.GetTableWidth(), right.GetTitle().Get());
result.Format("%-*s\t|\t%-*s", left.GetTableWidth(), left.GetTitle().Get(), right.GetTableWidth(), right.GetTitle().Get());
return result;
}

CUtlString GetHeader()
{
CUtlString result;
result.Format("%-*s | %-*s", left.GetTableWidth(), left.GetHeader().Get(), right.GetTableWidth(), right.GetHeader().Get());
result.Format("%-*s\t|\t%-*s", left.GetTableWidth(), left.GetHeader().Get(), right.GetTableWidth(), right.GetHeader().Get());
return result;
}

Expand All @@ -194,14 +200,15 @@ namespace utils
return "";
}
CUtlString result;
result.Format("%-*s | %-*s", left.GetTableWidth(), left.GetLine(row).Get(), right.GetTableWidth(), right.GetLine(row).Get());
result.Format("%-*s\t|\t%-*s", left.GetTableWidth(), left.GetLine(row).Get(), right.GetTableWidth(), right.GetLine(row).Get());
return result;
}

CUtlString GetSeparator()
CUtlString GetSeparator(const char *characters = "_")
{
CUtlString result;
result.Format("%-*s | %-*s", left.GetTableWidth(), left.GetSeparator().Get(), right.GetTableWidth(), right.GetSeparator().Get());
result.Format("%-*s \t|\t %-*s", left.GetTableWidth(), left.GetSeparator(characters).Get(), right.GetTableWidth(),
right.GetSeparator(characters).Get());
return result;
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ namespace utils

bool ParseSteamID2(std::string_view steamID, u64 &out);

inline u32 GetPaddingForWideString(const char *string)
{
return MAX(0, strlen(string) - mbstowcs(NULL, string, 0));
}
} // namespace utils

0 comments on commit 63923d7

Please sign in to comment.