From 47a86fb5a1265a7e9b92640eecc88ae8bbe28a5e Mon Sep 17 00:00:00 2001 From: K4ryuu <104531589+K4ryuu@users.noreply.github.com> Date: Wed, 15 May 2024 22:25:58 +0200 Subject: [PATCH] Patch v1.0.1 --- CHANGELOG.md | 5 ++ src-plugin/src/Models/LevelModel.cs | 18 ++++--- src-plugin/src/Models/PlayerModel.cs | 4 +- src-plugin/src/Plugin.cs | 5 ++ src-plugin/src/PluginDatabase.cs | 77 ++++++++++++++++++++++++++- src-plugin/src/PluginManifest.cs | 2 +- src-shared/K4-RPG-API.dll | Bin 4096 -> 4096 bytes 7 files changed, 101 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e69de29..03588df 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -0,0 +1,5 @@ +-- 2024.05.15 - v1.0.1 + +- fix: Load player properly on reload +- fix: Start the levelling from level 1 instead of 0 +- fix: Experience data shows 0 to all value on first level diff --git a/src-plugin/src/Models/LevelModel.cs b/src-plugin/src/Models/LevelModel.cs index 411e975..f204b61 100644 --- a/src-plugin/src/Models/LevelModel.cs +++ b/src-plugin/src/Models/LevelModel.cs @@ -12,10 +12,13 @@ public sealed partial class Plugin : BasePlugin { public int GetExperienceForLevel(int level) { + if (level <= 1) + return 0; + int totalExperience = 0; - for (int i = 1; i < level; i++) + for (int i = 2; i <= level; i++) { - totalExperience += (int)(Config.LevelSettings.BaseExperience * Math.Pow(i, Config.LevelSettings.ExperienceMultiplier)); + totalExperience += (int)(Config.LevelSettings.BaseExperience * Math.Pow(i - 1, Config.LevelSettings.ExperienceMultiplier)); } return totalExperience; @@ -23,11 +26,14 @@ public int GetExperienceForLevel(int level) public int GetLevelForExperience(long experience) { - int level = 1; - long totalExperience = 0; - while (totalExperience < experience) + if (experience < Config.LevelSettings.BaseExperience) + return 1; + + int level = 2; + long totalExperience = Config.LevelSettings.BaseExperience; + while (totalExperience <= experience) { - totalExperience += (long)(Config.LevelSettings.BaseExperience * Math.Pow(level, Config.LevelSettings.ExperienceMultiplier)); + totalExperience += (long)(Config.LevelSettings.BaseExperience * Math.Pow(level - 1, Config.LevelSettings.ExperienceMultiplier)); level++; } diff --git a/src-plugin/src/Models/PlayerModel.cs b/src-plugin/src/Models/PlayerModel.cs index 8774ef0..b1bc6a6 100644 --- a/src-plugin/src/Models/PlayerModel.cs +++ b/src-plugin/src/Models/PlayerModel.cs @@ -76,8 +76,8 @@ public async Task LoadPlayerDataAsync() string tablePrefix = Plugin.Config.DatabaseSettings.TablePrefix; string insertOrUpdateQuery = @$" - INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `LastSeen`) - VALUES (@SteamID, CURRENT_TIMESTAMP) + INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `Level`, `LastSeen`) + VALUES (@SteamID, 1, CURRENT_TIMESTAMP) ON DUPLICATE KEY UPDATE `LastSeen` = CURRENT_TIMESTAMP;"; diff --git a/src-plugin/src/Plugin.cs b/src-plugin/src/Plugin.cs index fd8891c..b7a44b6 100644 --- a/src-plugin/src/Plugin.cs +++ b/src-plugin/src/Plugin.cs @@ -44,6 +44,11 @@ public override void Load(bool hotReload) Initialize_Events(); Initialize_Commands(); Initialize_DynamicEvents(); + + if (hotReload) + { + Task.Run(LoadAllPlayersDataAsync); + } } public override void Unload(bool hotReload) diff --git a/src-plugin/src/PluginDatabase.cs b/src-plugin/src/PluginDatabase.cs index 6d078e6..8aee7b5 100644 --- a/src-plugin/src/PluginDatabase.cs +++ b/src-plugin/src/PluginDatabase.cs @@ -4,6 +4,7 @@ using Dapper; using Microsoft.Extensions.Logging; using K4RPG.Models; +using CounterStrikeSharp.API; namespace K4RPG; @@ -40,7 +41,7 @@ public async Task CreateTableAsync() CREATE TABLE IF NOT EXISTS `{tablePrefix}k4-rpg_playerskills` ( `SkillID` VARCHAR(255), `PlayerSteamID` BIGINT UNSIGNED, - `Level` INT, + `Level` INT DEFAULT 1, FOREIGN KEY (`PlayerSteamID`) REFERENCES `{tablePrefix}k4-rpg_players`(`SteamID`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;"; @@ -87,6 +88,80 @@ DELETE FROM `{tablePrefix}k4-rpg_players` } } + public void LoadAllPlayersDataAsync() + { + Utilities.GetPlayers().Where(p => p.IsValid && !p.IsBot && !p.IsHLTV && p.Connected == PlayerConnectedState.PlayerConnected).ToList().ForEach(p => + { + RPGPlayer newPlayer = new RPGPlayer(this, p); + RPGPlayers.Add(newPlayer); + }); + + if (RPGPlayers.Count == 0) + return; + + string tablePrefix = Config.DatabaseSettings.TablePrefix; + string query = @$" + INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `Level`, `LastSeen`) + SELECT @SteamID, 1, CURRENT_TIMESTAMP + FROM DUAL + WHERE NOT EXISTS ( + SELECT 1 + FROM `{tablePrefix}k4-rpg_players` + WHERE `SteamID` = @SteamID + ); + + SELECT + p.`SteamID`, + p.`Experience`, + p.`SkillPoints`, + s.`SkillID`, + s.`Level` + FROM `{tablePrefix}k4-rpg_players` p + LEFT JOIN `{tablePrefix}k4-rpg_playerskills` s ON p.`SteamID` = s.`PlayerSteamID` + WHERE p.`SteamID` IN ({string.Join(",", RPGPlayers.Select(p => p.SteamID))});"; + + Task.Run(async () => + { + using MySqlConnection connection = CreateConnection(Config); + await connection.OpenAsync(); + + try + { + var players = (await connection.QueryAsync(query, (player, skill) => + { + RPGPlayer? cPlayer = RPGPlayers.Find(p => p.SteamID == player.SteamID); + if (cPlayer != null) + { + cPlayer.Experience = player.Experience; + cPlayer.SkillPoints = player.SkillPoints; + cPlayer.KnownLevel = GetLevelForExperience(cPlayer.Experience); + if (skill != null) + { + RPGSkill? rpgSkill = RPGSkills.Find(s => s.ID == skill.SkillID); + if (rpgSkill != null) + { + cPlayer.Skills.Add(rpgSkill.ID, skill.Level); + } + else + Server.NextWorldUpdate(() => Logger.LogError($"Failed to load skill data for skill ID {skill.SkillID} due to missing skill.")); + } + } + if (cPlayer != null) + { + return cPlayer; + } + else + throw new Exception("Failed to find RPGPlayer with the specified SteamID."); + }, splitOn: "SkillID")).ToList(); + RPGPlayers = players.Where(p => p != null).ToList(); + } + catch (Exception ex) + { + Logger.LogError($"Error loading all players data: {ex.Message}"); + } + }); + } + public bool IsDatabaseConfigDefault(PluginConfig config) { DatabaseSettings _settings = config.DatabaseSettings; diff --git a/src-plugin/src/PluginManifest.cs b/src-plugin/src/PluginManifest.cs index 6a5065f..2ee7f8f 100644 --- a/src-plugin/src/PluginManifest.cs +++ b/src-plugin/src/PluginManifest.cs @@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin public override string ModuleAuthor => "K4ryuu"; - public override string ModuleVersion => "1.0.0 " + + public override string ModuleVersion => "1.0.1 " + #if RELEASE "(release)"; #else diff --git a/src-shared/K4-RPG-API.dll b/src-shared/K4-RPG-API.dll index 047184ce3d74dab8bb5f964a2a475e2aaae57c5c..0aa9bfdfd651a343b39eeb3cfcaa5709ad4bb411 100644 GIT binary patch delta 384 zcmXv~KS%;`7=7QJ=D+JObjv^q(lG4)`0p06mbMBSq=rz>ySX_UEF2sRI=&%@h&FNU zhL-kLwk+i^>_(bBAr>zaDY zsM=P!g15xgLPu~{cyz&yiNiu!><%g@ fPIAsf)kVer-l9ie`;01 delta 260 zcmZorXi%8Y!P4}|?A^p38AhXtE5rF7s4+13GXQ}D1A_tcL`7kq1O^5kApZzZLT0ie zlg{KwMs-Gu$&EnLVDd`Fa7K~Ij7-9;C2R}~jFSsF%_j#iB`|$q*u06UijnCx!(=@c z2d1YClM7g!7@ut3%)-kopraQNxRT|9%e2bVG0_IGmXj~A-{RzAWMDPaGte`be1}7J z@*fTXAr5t*wNRi0Vk=Bk6a(63%6W5g1EFrklS`RV oV6!auaz