Skip to content

Commit

Permalink
Patch v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed May 15, 2024
1 parent 908d460 commit 47a86fb
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
18 changes: 12 additions & 6 deletions src-plugin/src/Models/LevelModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,28 @@ 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;
}

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++;
}

Expand Down
4 changes: 2 additions & 2 deletions src-plugin/src/Models/PlayerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;";

Expand Down
5 changes: 5 additions & 0 deletions src-plugin/src/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 76 additions & 1 deletion src-plugin/src/PluginDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Dapper;
using Microsoft.Extensions.Logging;
using K4RPG.Models;
using CounterStrikeSharp.API;

namespace K4RPG;

Expand Down Expand Up @@ -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;";

Expand Down Expand Up @@ -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<dynamic, dynamic, RPGPlayer>(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;
Expand Down
2 changes: 1 addition & 1 deletion src-plugin/src/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 => "1.0.0 " +
public override string ModuleVersion => "1.0.1 " +
#if RELEASE
"(release)";
#else
Expand Down
Binary file modified src-shared/K4-RPG-API.dll
Binary file not shown.

0 comments on commit 47a86fb

Please sign in to comment.