Skip to content
This repository has been archived by the owner on Oct 22, 2024. It is now read-only.

Commit

Permalink
fix: AutoPurge query
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Apr 12, 2024
1 parent 1f6558c commit 3550842
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 64 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
-- 2023.04.12 - V4.1.2

- fix: AutoPurge query

-- 2023.04.11 - V4.1.1

- fix: Upgrade MySQL to primary key instead of UNIQUE
- fix: Player couldnt load
- fix: Rank command not prints
- fix: Player data process

-- 2023.04.11 - V4.1.0

- feat: Add Reset option for all module
Expand Down
10 changes: 5 additions & 5 deletions K4-System/src/Module/Rank/RankCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ public void OnCommandRank(CCSPlayerController? player, CommandInfo info)
Server.NextFrame(() =>
{
info.ReplyToCommand($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.title", k4player.PlayerName]}");
info.ReplyToCommand(plugin.Localizer["k4.ranks.rank.line1", playerData.Points, playerData.Rank.Color, playerData.Rank.Name, rankDictionary.Count - higherRanksCount, rankDictionary.Count]);
player!.PrintToChat($" {plugin.Localizer["k4.general.prefix"]} {plugin.Localizer["k4.ranks.rank.title", k4player.PlayerName]}");
player.PrintToChat(plugin.Localizer["k4.ranks.rank.line1", playerData.Points, playerData.Rank.Color, playerData.Rank.Name, rankDictionary.Count - higherRanksCount, rankDictionary.Count]);
KeyValuePair<string, Rank> nextRankEntry = rankDictionary
.Where(kv => kv.Value.Point > playerData.Rank.Point)
Expand All @@ -137,11 +137,11 @@ public void OnCommandRank(CCSPlayerController? player, CommandInfo info)
{
Rank nextRank = nextRankEntry.Value;
info.ReplyToCommand(plugin.Localizer["k4.ranks.rank.line2", nextRank.Color, nextRank.Name]);
info.ReplyToCommand(plugin.Localizer["k4.ranks.rank.line3", nextRank.Point - playerData.Points]);
player.PrintToChat(plugin.Localizer["k4.ranks.rank.line2", nextRank.Color, nextRank.Name]);
player.PrintToChat(plugin.Localizer["k4.ranks.rank.line3", nextRank.Point - playerData.Points]);
}
info.ReplyToCommand(plugin.Localizer["k4.ranks.rank.line4", playerPlace, totalPlayers]);
player.PrintToChat(plugin.Localizer["k4.ranks.rank.line4", playerPlace, totalPlayers]);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions K4-System/src/Module/Rank/RankFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public void ModifyPlayerPoints(K4Player k4player, int amount, string reason, str

if (result != null)
{
int playerPlace = result.playerPlace + 1;
int totalPlayers = result.totalPlayers;
int playerPlace = (int)result.playerPlace + 1;
int totalPlayers = (int)result.totalPlayers;
return (playerPlace, totalPlayers);
}
}
Expand Down
3 changes: 3 additions & 0 deletions K4-System/src/Module/Utils/UtilsCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public void OnCommandAdmins(CCSPlayerController? player, CommandInfo info)
continue;

if (PlayerHasPermission(k4player, entry.Permission))
{
adminList.Add($"{plugin.ApplyPrefixColors(entry.ListColor ?? "default")}{k4player.PlayerName}");
break;
}
}
}

Expand Down
15 changes: 6 additions & 9 deletions K4-System/src/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,19 @@ public override void Unload(bool hotReload)
public async Task<bool> CreateMultipleTablesAsync()
{
string timesModuleTable = @$"CREATE TABLE IF NOT EXISTS `{this.Config.DatabaseSettings.TablePrefix}k4times` (
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' UNIQUE NOT NULL,
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' PRIMARY KEY NOT NULL,
`name` VARCHAR(255) COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`lastseen` DATETIME NOT NULL,
`all` INT NOT NULL DEFAULT 0,
`ct` INT NOT NULL DEFAULT 0,
`t` INT NOT NULL DEFAULT 0,
`spec` INT NOT NULL DEFAULT 0,
`dead` INT NOT NULL DEFAULT 0,
`alive` INT NOT NULL DEFAULT 0,
UNIQUE (`steam_id`)
`alive` INT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

string statsModuleTable = $@"CREATE TABLE IF NOT EXISTS `{this.Config.DatabaseSettings.TablePrefix}k4stats` (
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' UNIQUE NOT NULL,
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' PRIMARY KEY NOT NULL,
`name` VARCHAR(255) COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`lastseen` DATETIME NOT NULL,
`kills` INT NOT NULL DEFAULT 0,
Expand Down Expand Up @@ -161,17 +160,15 @@ public async Task<bool> CreateMultipleTablesAsync()
`flashed_kill` INT NOT NULL DEFAULT 0,
`dominated_kill` INT NOT NULL DEFAULT 0,
`revenge_kill` INT NOT NULL DEFAULT 0,
`assist_flash` INT NOT NULL DEFAULT 0,
UNIQUE (`steam_id`)
`assist_flash` INT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

string ranksModuleTable = $@"CREATE TABLE IF NOT EXISTS `{this.Config.DatabaseSettings.TablePrefix}k4ranks` (
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' UNIQUE NOT NULL,
`steam_id` VARCHAR(32) COLLATE 'utf8mb4_unicode_ci' PRIMARY KEY NOT NULL,
`name` VARCHAR(255) COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`lastseen` DATETIME NOT NULL,
`rank` VARCHAR(255) COLLATE 'utf8mb4_unicode_ci' NOT NULL,
`points` INT NOT NULL DEFAULT 0,
UNIQUE (`steam_id`)
`points` INT NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

string lvlranksModuleTable = @$"CREATE TABLE IF NOT EXISTS `{Config.DatabaseSettings.LvLRanksTableName}` (
Expand Down
145 changes: 98 additions & 47 deletions K4-System/src/Plugin/PluginDatabase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

using System.Data;
using System.Text;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using Dapper;
Expand Down Expand Up @@ -44,9 +45,9 @@ public async Task PurgeTableRowsAsync()
parameters.Add("@days", Config.GeneralSettings.TablePurgeDays);

string query = $@"
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4times` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` != '0000-00-00';
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4stats` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` != '0000-00-00';
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` != '0000-00-00';
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4times` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` > STR_TO_DATE('1970-01-01', '%Y-%m-%d');
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4stats` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` > STR_TO_DATE('1970-01-01', '%Y-%m-%d');
DELETE FROM `{Config.DatabaseSettings.TablePrefix}k4ranks` WHERE `lastseen` < NOW() - INTERVAL @days DAY AND `lastseen` > STR_TO_DATE('1970-01-01', '%Y-%m-%d');
";

if (Config.GeneralSettings.LevelRanksCompatibility)
Expand Down Expand Up @@ -260,11 +261,11 @@ ON DUPLICATE KEY UPDATE
`name` = @escapedName,
`lastseen` = CURRENT_TIMESTAMP;
INSERT INTO `{Config.DatabaseSettings.TablePrefix}k4stats` (`name`, `steam_id`, `lastseen`, `kills`, `firstblood`, `deaths`, `assists`, `shoots`, `hits_taken`, `hits_given`, `headshots`, `grenades`, `mvp`, `round_win`, `round_lose`, `game_win`, `game_lose`, `rounds_overall`, `rounds_ct`, `rounds_t`, `bomb_planted`, `bomb_defused`, `hostage_rescued`, `hostage_killed`)
INSERT INTO `{Config.DatabaseSettings.TablePrefix}k4stats` (`name`, `steam_id`, `lastseen`)
VALUES (
@escapedName,
@steamid,
CURRENT_TIMESTAMP,
CURRENT_TIMESTAMP
)
ON DUPLICATE KEY UPDATE
`name` = @escapedName,
Expand All @@ -281,7 +282,7 @@ ON DUPLICATE KEY UPDATE
`lastseen` = CURRENT_TIMESTAMP;
SELECT
r.`points`,
r.`points`,
s.`kills`,
s.`firstblood`,
s.`deaths`,
Expand All @@ -290,6 +291,16 @@ ON DUPLICATE KEY UPDATE
s.`hits_taken`,
s.`hits_given`,
s.`headshots`,
s.`chest_hits`,
s.`stomach_hits`,
s.`left_arm_hits`,
s.`right_arm_hits`,
s.`left_leg_hits`,
s.`right_leg_hits`,
s.`neck_hits`,
s.`unused_hits`,
s.`gear_hits`,
s.`special_hits`,
s.`grenades`,
s.`mvp`,
s.`round_win`,
Expand All @@ -303,6 +314,13 @@ ON DUPLICATE KEY UPDATE
s.`bomb_defused`,
s.`hostage_rescued`,
s.`hostage_killed`,
s.`noscope_kill`,
s.`penetrated_kill`,
s.`thrusmoke_kill`,
s.`flashed_kill`,
s.`dominated_kill`,
s.`revenge_kill`,
s.`assist_flash`,
t.`all`,
t.`ct`,
t.`t`,
Expand All @@ -320,30 +338,30 @@ LEFT JOIN
";


MySqlParameter[] parameters =
[
new MySqlParameter("@escapedName", k4player.PlayerName),
new MySqlParameter("@steamid", k4player.SteamID),
new MySqlParameter("@noneRankName", ModuleRank.GetNoneRank()?.Name ?? "none"),
new MySqlParameter("@startPoints", Config.RankSettings.StartPoints)
];

try
{
using (var connection = CreateConnection(Config))
{
await connection.OpenAsync();

var parameters = new DynamicParameters();
parameters.Add("@escapedName", k4player.PlayerName);
parameters.Add("@steamid", k4player.SteamID);
parameters.Add("@noneRankName", ModuleRank.GetNoneRank()?.Name ?? "none");
parameters.Add("@startPoints", Config.RankSettings.StartPoints);

// Execute the query with the parameters
var rows = await connection.QueryAsync(combinedQuery, parameters);

foreach (var row in rows)
{
LoadPlayerRowToCache(k4player, row);
LoadPlayerRowToCache(k4player, row, false);
}
}
}
catch (Exception ex)
{
Logger.LogError($"A problem occurred while loading single player cache: {ex.Message}");
Logger.LogError("A problem occurred while loading single player cache: {ErrorMessage}", ex.Message);
}
}

Expand All @@ -354,29 +372,58 @@ private void LoadAllPlayersCache()
if (players.Count == 0)
return;

foreach (var player in players)
{
K4Player k4player = new K4Player(this, player);
K4Players.Add(k4player);
}

string combinedQuery = $@"SELECT
r.`steam_id`,
r.`points`,
s.`kills`,
s.`shoots`,
s.`firstblood`,
s.`deaths`,
s.`hits_given`,
s.`hits_taken`,
s.`headshots`,
s.`grenades`,
s.`mvp`,
s.`round_win`,
s.`round_lose`,
s.`game_win`,
s.`game_lose`,
s.`assists`,
t.`all`,
t.`ct`,
t.`t`,
t.`spec`,
t.`alive`,
t.`dead`
r.`points`,
s.`kills`,
s.`firstblood`,
s.`deaths`,
s.`assists`,
s.`shoots`,
s.`hits_taken`,
s.`hits_given`,
s.`headshots`,
s.`chest_hits`,
s.`stomach_hits`,
s.`left_arm_hits`,
s.`right_arm_hits`,
s.`left_leg_hits`,
s.`right_leg_hits`,
s.`neck_hits`,
s.`unused_hits`,
s.`gear_hits`,
s.`special_hits`,
s.`grenades`,
s.`mvp`,
s.`round_win`,
s.`round_lose`,
s.`game_win`,
s.`game_lose`,
s.`rounds_overall`,
s.`rounds_ct`,
s.`rounds_t`,
s.`bomb_planted`,
s.`bomb_defused`,
s.`hostage_rescued`,
s.`hostage_killed`,
s.`noscope_kill`,
s.`penetrated_kill`,
s.`thrusmoke_kill`,
s.`flashed_kill`,
s.`dominated_kill`,
s.`revenge_kill`,
s.`assist_flash`,
t.`all`,
t.`ct`,
t.`t`,
t.`spec`,
t.`alive`,
t.`dead`
FROM
`{Config.DatabaseSettings.TablePrefix}k4ranks` AS r
LEFT JOIN
Expand Down Expand Up @@ -406,12 +453,15 @@ public async Task LoadAllPlayersCacheAsync(string combinedQuery)

foreach (var k4player in K4Players)
{
if (!k4player.IsValid || !k4player.IsPlayer)
continue;

await connection.OpenAsync();
var rows = await connection.QueryAsync(combinedQuery);

foreach (var row in rows)
{
LoadPlayerRowToCache(k4player, row);
LoadPlayerRowToCache(k4player, row, true);
}
}
}
Expand All @@ -422,26 +472,26 @@ public async Task LoadAllPlayersCacheAsync(string combinedQuery)
}
}

public void LoadPlayerRowToCache(K4Player k4player, DataRow row)
public void LoadPlayerRowToCache(K4Player k4player, dynamic row, bool all)
{
/** ? Load Rank to Cache */
RankData? rankData = null;

if (Config.GeneralSettings.ModuleRanks)
{
int points = Convert.ToInt32(row["points"]);

rankData = new RankData
{
Points = points,
Rank = ModuleRank.GetPlayerRank(points),
Points = row.points,
Rank = ModuleRank.GetPlayerRank(row.points),
PlayedRound = false,
RoundPoints = 0,
HideAdminTag = false,
MuteMessages = false
};
}

var rowDictionary = (IDictionary<string, object>)row;

/** ? Load Stat to Cache */
StatData? statData = null;

Expand All @@ -453,7 +503,7 @@ public void LoadPlayerRowToCache(K4Player k4player, DataRow row)

foreach (string statField in statFieldNames)
{
NewStatFields[statField] = Convert.ToInt32(row[statField]);
NewStatFields[statField] = (int)rowDictionary[statField];
}

statData = new StatData
Expand All @@ -473,7 +523,7 @@ public void LoadPlayerRowToCache(K4Player k4player, DataRow row)

foreach (string timeField in timeFieldNames)
{
TimeFields[timeField] = Convert.ToInt32(row[timeField]);
TimeFields[timeField] = (int)rowDictionary[timeField];
}

DateTime now = DateTime.UtcNow;
Expand All @@ -494,6 +544,7 @@ public void LoadPlayerRowToCache(K4Player k4player, DataRow row)
k4player.statData = statData;
k4player.timeData = timeData;

K4Players.Add(k4player);
if (!all)
K4Players.Add(k4player);
}
}
2 changes: 1 addition & 1 deletion K4-System/src/Plugin/PluginManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin

public override string ModuleAuthor => "K4ryuu";

public override string ModuleVersion => "4.1.0 " +
public override string ModuleVersion => "4.1.2 " +
#if RELEASE
"(release)";
#else
Expand Down

0 comments on commit 3550842

Please sign in to comment.