Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 47a86fb

Browse files
committed
Patch v1.0.1
1 parent 908d460 commit 47a86fb

File tree

7 files changed

+101
-10
lines changed

7 files changed

+101
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- 2024.05.15 - v1.0.1
2+
3+
- fix: Load player properly on reload
4+
- fix: Start the levelling from level 1 instead of 0
5+
- fix: Experience data shows 0 to all value on first level

src-plugin/src/Models/LevelModel.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,28 @@ public sealed partial class Plugin : BasePlugin
1212
{
1313
public int GetExperienceForLevel(int level)
1414
{
15+
if (level <= 1)
16+
return 0;
17+
1518
int totalExperience = 0;
16-
for (int i = 1; i < level; i++)
19+
for (int i = 2; i <= level; i++)
1720
{
18-
totalExperience += (int)(Config.LevelSettings.BaseExperience * Math.Pow(i, Config.LevelSettings.ExperienceMultiplier));
21+
totalExperience += (int)(Config.LevelSettings.BaseExperience * Math.Pow(i - 1, Config.LevelSettings.ExperienceMultiplier));
1922
}
2023

2124
return totalExperience;
2225
}
2326

2427
public int GetLevelForExperience(long experience)
2528
{
26-
int level = 1;
27-
long totalExperience = 0;
28-
while (totalExperience < experience)
29+
if (experience < Config.LevelSettings.BaseExperience)
30+
return 1;
31+
32+
int level = 2;
33+
long totalExperience = Config.LevelSettings.BaseExperience;
34+
while (totalExperience <= experience)
2935
{
30-
totalExperience += (long)(Config.LevelSettings.BaseExperience * Math.Pow(level, Config.LevelSettings.ExperienceMultiplier));
36+
totalExperience += (long)(Config.LevelSettings.BaseExperience * Math.Pow(level - 1, Config.LevelSettings.ExperienceMultiplier));
3137
level++;
3238
}
3339

src-plugin/src/Models/PlayerModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public async Task LoadPlayerDataAsync()
7676
string tablePrefix = Plugin.Config.DatabaseSettings.TablePrefix;
7777

7878
string insertOrUpdateQuery = @$"
79-
INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `LastSeen`)
80-
VALUES (@SteamID, CURRENT_TIMESTAMP)
79+
INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `Level`, `LastSeen`)
80+
VALUES (@SteamID, 1, CURRENT_TIMESTAMP)
8181
ON DUPLICATE KEY UPDATE
8282
`LastSeen` = CURRENT_TIMESTAMP;";
8383

src-plugin/src/Plugin.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public override void Load(bool hotReload)
4444
Initialize_Events();
4545
Initialize_Commands();
4646
Initialize_DynamicEvents();
47+
48+
if (hotReload)
49+
{
50+
Task.Run(LoadAllPlayersDataAsync);
51+
}
4752
}
4853

4954
public override void Unload(bool hotReload)

src-plugin/src/PluginDatabase.cs

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Dapper;
55
using Microsoft.Extensions.Logging;
66
using K4RPG.Models;
7+
using CounterStrikeSharp.API;
78

89
namespace K4RPG;
910

@@ -40,7 +41,7 @@ public async Task CreateTableAsync()
4041
CREATE TABLE IF NOT EXISTS `{tablePrefix}k4-rpg_playerskills` (
4142
`SkillID` VARCHAR(255),
4243
`PlayerSteamID` BIGINT UNSIGNED,
43-
`Level` INT,
44+
`Level` INT DEFAULT 1,
4445
FOREIGN KEY (`PlayerSteamID`) REFERENCES `{tablePrefix}k4-rpg_players`(`SteamID`)
4546
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";
4647

@@ -87,6 +88,80 @@ DELETE FROM `{tablePrefix}k4-rpg_players`
8788
}
8889
}
8990

91+
public void LoadAllPlayersDataAsync()
92+
{
93+
Utilities.GetPlayers().Where(p => p.IsValid && !p.IsBot && !p.IsHLTV && p.Connected == PlayerConnectedState.PlayerConnected).ToList().ForEach(p =>
94+
{
95+
RPGPlayer newPlayer = new RPGPlayer(this, p);
96+
RPGPlayers.Add(newPlayer);
97+
});
98+
99+
if (RPGPlayers.Count == 0)
100+
return;
101+
102+
string tablePrefix = Config.DatabaseSettings.TablePrefix;
103+
string query = @$"
104+
INSERT INTO `{tablePrefix}k4-rpg_players` (`SteamID`, `Level`, `LastSeen`)
105+
SELECT @SteamID, 1, CURRENT_TIMESTAMP
106+
FROM DUAL
107+
WHERE NOT EXISTS (
108+
SELECT 1
109+
FROM `{tablePrefix}k4-rpg_players`
110+
WHERE `SteamID` = @SteamID
111+
);
112+
113+
SELECT
114+
p.`SteamID`,
115+
p.`Experience`,
116+
p.`SkillPoints`,
117+
s.`SkillID`,
118+
s.`Level`
119+
FROM `{tablePrefix}k4-rpg_players` p
120+
LEFT JOIN `{tablePrefix}k4-rpg_playerskills` s ON p.`SteamID` = s.`PlayerSteamID`
121+
WHERE p.`SteamID` IN ({string.Join(",", RPGPlayers.Select(p => p.SteamID))});";
122+
123+
Task.Run(async () =>
124+
{
125+
using MySqlConnection connection = CreateConnection(Config);
126+
await connection.OpenAsync();
127+
128+
try
129+
{
130+
var players = (await connection.QueryAsync<dynamic, dynamic, RPGPlayer>(query, (player, skill) =>
131+
{
132+
RPGPlayer? cPlayer = RPGPlayers.Find(p => p.SteamID == player.SteamID);
133+
if (cPlayer != null)
134+
{
135+
cPlayer.Experience = player.Experience;
136+
cPlayer.SkillPoints = player.SkillPoints;
137+
cPlayer.KnownLevel = GetLevelForExperience(cPlayer.Experience);
138+
if (skill != null)
139+
{
140+
RPGSkill? rpgSkill = RPGSkills.Find(s => s.ID == skill.SkillID);
141+
if (rpgSkill != null)
142+
{
143+
cPlayer.Skills.Add(rpgSkill.ID, skill.Level);
144+
}
145+
else
146+
Server.NextWorldUpdate(() => Logger.LogError($"Failed to load skill data for skill ID {skill.SkillID} due to missing skill."));
147+
}
148+
}
149+
if (cPlayer != null)
150+
{
151+
return cPlayer;
152+
}
153+
else
154+
throw new Exception("Failed to find RPGPlayer with the specified SteamID.");
155+
}, splitOn: "SkillID")).ToList();
156+
RPGPlayers = players.Where(p => p != null).ToList();
157+
}
158+
catch (Exception ex)
159+
{
160+
Logger.LogError($"Error loading all players data: {ex.Message}");
161+
}
162+
});
163+
}
164+
90165
public bool IsDatabaseConfigDefault(PluginConfig config)
91166
{
92167
DatabaseSettings _settings = config.DatabaseSettings;

src-plugin/src/PluginManifest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public sealed partial class Plugin : BasePlugin
1010

1111
public override string ModuleAuthor => "K4ryuu";
1212

13-
public override string ModuleVersion => "1.0.0 " +
13+
public override string ModuleVersion => "1.0.1 " +
1414
#if RELEASE
1515
"(release)";
1616
#else

src-shared/K4-RPG-API.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)