diff --git a/CHANGELOG b/CHANGELOG index 60a2662..6a9b5fc 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -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 diff --git a/K4-System/src/Module/Rank/RankCommands.cs b/K4-System/src/Module/Rank/RankCommands.cs index 9122727..de1ebf3 100644 --- a/K4-System/src/Module/Rank/RankCommands.cs +++ b/K4-System/src/Module/Rank/RankCommands.cs @@ -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 nextRankEntry = rankDictionary .Where(kv => kv.Value.Point > playerData.Rank.Point) @@ -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]); }); }); } diff --git a/K4-System/src/Module/Rank/RankFunctions.cs b/K4-System/src/Module/Rank/RankFunctions.cs index 0a7ab02..bf1d3a4 100644 --- a/K4-System/src/Module/Rank/RankFunctions.cs +++ b/K4-System/src/Module/Rank/RankFunctions.cs @@ -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); } } diff --git a/K4-System/src/Module/Utils/UtilsCommands.cs b/K4-System/src/Module/Utils/UtilsCommands.cs index 3195971..ce07c33 100644 --- a/K4-System/src/Module/Utils/UtilsCommands.cs +++ b/K4-System/src/Module/Utils/UtilsCommands.cs @@ -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; + } } } diff --git a/K4-System/src/Plugin/Plugin.cs b/K4-System/src/Plugin/Plugin.cs index ac5bfb7..427921d 100644 --- a/K4-System/src/Plugin/Plugin.cs +++ b/K4-System/src/Plugin/Plugin.cs @@ -108,7 +108,7 @@ public override void Unload(bool hotReload) public async Task 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, @@ -116,12 +116,11 @@ public async Task CreateMultipleTablesAsync() `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, @@ -161,17 +160,15 @@ public async Task 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}` ( diff --git a/K4-System/src/Plugin/PluginDatabase.cs b/K4-System/src/Plugin/PluginDatabase.cs index bda26fe..c4c6215 100644 --- a/K4-System/src/Plugin/PluginDatabase.cs +++ b/K4-System/src/Plugin/PluginDatabase.cs @@ -1,5 +1,6 @@ using System.Data; +using System.Text; using CounterStrikeSharp.API; using CounterStrikeSharp.API.Core; using Dapper; @@ -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) @@ -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, @@ -281,7 +282,7 @@ ON DUPLICATE KEY UPDATE `lastseen` = CURRENT_TIMESTAMP; SELECT - r.`points`, + r.`points`, s.`kills`, s.`firstblood`, s.`deaths`, @@ -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`, @@ -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`, @@ -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); } } @@ -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 @@ -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); } } } @@ -422,19 +472,17 @@ 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, @@ -442,6 +490,8 @@ public void LoadPlayerRowToCache(K4Player k4player, DataRow row) }; } + var rowDictionary = (IDictionary)row; + /** ? Load Stat to Cache */ StatData? statData = null; @@ -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 @@ -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; @@ -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); } } \ No newline at end of file diff --git a/K4-System/src/Plugin/PluginManifest.cs b/K4-System/src/Plugin/PluginManifest.cs index 1cce980..75b3525 100644 --- a/K4-System/src/Plugin/PluginManifest.cs +++ b/K4-System/src/Plugin/PluginManifest.cs @@ -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