From 9919102c06999663b36a5fe1851e330b6e94bffe Mon Sep 17 00:00:00 2001 From: Nick Fox <66554932+NickFox007@users.noreply.github.com> Date: Thu, 16 May 2024 23:28:25 +0300 Subject: [PATCH] 0.4 * Fixed bug when setting got incorrectly * Replacing with async methods * Renamed sqlite filename and tables struct --- PlayerSettings/CPlayerSettings.cs | 21 ++++++++++++++++--- PlayerSettings/Main.cs | 11 +++++++++- PlayerSettings/SettingsApi.cs | 35 +++++++++++++++++++++++-------- PlayerSettings/Storage.cs | 25 ++++++++++++++-------- 4 files changed, 70 insertions(+), 22 deletions(-) diff --git a/PlayerSettings/CPlayerSettings.cs b/PlayerSettings/CPlayerSettings.cs index 3122e30..31585c0 100644 --- a/PlayerSettings/CPlayerSettings.cs +++ b/PlayerSettings/CPlayerSettings.cs @@ -10,11 +10,13 @@ namespace PlayerSettings internal class CPlayerSettings { private int userid; - private static Dictionary cached_values; + private CCSPlayerController player; + private Dictionary cached_values; - public CPlayerSettings(int _userid) + public CPlayerSettings(CCSPlayerController _player) { - userid = _userid; + player = _player; + userid = Storage.GetUserId(player); cached_values = new Dictionary(); } @@ -41,6 +43,19 @@ public int UserId() return userid; } + public bool EqualPlayer(CCSPlayerController _player) + { + return (player == _player); + } + + internal void ParseLoadedSettings(List> rows) + { + foreach (var row in rows) + { + cached_values[row[0]] = row[1]; + } + } + } } diff --git a/PlayerSettings/Main.cs b/PlayerSettings/Main.cs index d8a9ec7..693aaba 100644 --- a/PlayerSettings/Main.cs +++ b/PlayerSettings/Main.cs @@ -3,14 +3,16 @@ using CounterStrikeSharp.API.Core.Attributes.Registration; using CounterStrikeSharp.API.Core.Capabilities; using CounterStrikeSharp.API.Modules.Commands; +using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Menu; +using static CounterStrikeSharp.API.Core.Listeners; namespace PlayerSettings; public class PlayerSettingsCore : BasePlugin { public override string ModuleName => "PlayerSettings [Core]"; - public override string ModuleVersion => "0.1"; + public override string ModuleVersion => "0.4"; public override string ModuleAuthor => "Nick Fox"; public override string ModuleDescription => "One storage for player's settings (aka ClientCookies)"; @@ -21,5 +23,12 @@ public override void Load(bool hotReload) _api = new SettingsApi(); Capabilities.RegisterPluginCapability(_pluginCapability, () => _api); Storage.Init(this); + + RegisterListener(OnClientAuthorized); + } + + private void OnClientAuthorized(int slot, SteamID steamID) + { + ((SettingsApi)_api).LoadOnConnect(Utilities.GetPlayerFromSlot(slot)); } } \ No newline at end of file diff --git a/PlayerSettings/SettingsApi.cs b/PlayerSettings/SettingsApi.cs index 8f96361..1abc513 100644 --- a/PlayerSettings/SettingsApi.cs +++ b/PlayerSettings/SettingsApi.cs @@ -11,34 +11,51 @@ namespace PlayerSettings internal class SettingsApi : ISettingsApi { - List settings; + CPlayerSettings[] settings; public SettingsApi() { - settings = new List(); + settings = Array.Empty(); } - private CPlayerSettings FindUser(int userid) + private CPlayerSettings FindUser(CCSPlayerController player) { foreach (var item in this.settings) { - if (item.UserId() == userid) + if (item.EqualPlayer(player)) { + //Console.WriteLine($"Returned found: {item.UserId()}"); return item; } - } - var newInst = new CPlayerSettings(userid); - settings.Add(newInst); + } + var newInst = new CPlayerSettings(player); + AddPlayerInst(newInst); return newInst; } + private void AddPlayerInst(CPlayerSettings inst) + { + Array.Resize(ref settings, settings.Length + 1); + settings[settings.Length - 1] = inst; + } + + public string GetPlayerSettingsValue(CCSPlayerController player, string param, string default_value) { - return FindUser(Storage.GetUserId(player)).GetValue(param, default_value); + return FindUser(player).GetValue(param, default_value); } + public void SetPlayerSettingsValue(CCSPlayerController player, string param, string value) { - FindUser(Storage.GetUserId(player)).SetValue(param, value); + FindUser(player).SetValue(param, value); } + + internal void LoadOnConnect(CCSPlayerController player) + { + var user = FindUser(player); + + Storage.LoadSettings(user.UserId(), user.ParseLoadedSettings); + } + } } diff --git a/PlayerSettings/Storage.cs b/PlayerSettings/Storage.cs index 114397d..64d6c8b 100644 --- a/PlayerSettings/Storage.cs +++ b/PlayerSettings/Storage.cs @@ -18,31 +18,38 @@ internal static class Storage public static void Init(BasePlugin plugin, string driver = "sqlite") { db = CAnyBase.Base(driver); - db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"menusets")); + db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"settings")); db.Init(); - db.Query("create table if not exists \"users\" (\"id\" integer primary key AUTOINCREMENT, \"steam\" varchar(255) not null)", null, true); - db.Query("create table if not exists \"settings\" (\"user_id\" int, \"param\" varchar(255) not null, \"value\" varchar(255) not null)", null, true); + db.QueryAsync("create table if not exists \"settings_users\" (\"id\" integer primary key AUTOINCREMENT, \"steam\" varchar(255) not null)", null, null, true); + db.QueryAsync("create table if not exists \"settings_values\" (\"user_id\" int, \"param\" varchar(255) not null, \"value\" varchar(255) not null)", null, null, true); } public static int GetUserId(CCSPlayerController player) { var steamid = player.SteamID; - var res = db.Query("select \"id\" from users where \"steam\" = \"{ARG}\"", new List([steamid.ToString()])); + var res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List([steamid.ToString()])); if(res.Count == 0) { - db.Query("insert into \"users\" (\"steam\") values (\"{ARG}\")", new List([steamid.ToString()]), true); - res = db.Query("select \"id\" from \"users\" where \"steam\" = \"{ARG}\"", new List([steamid.ToString()])); + db.Query("insert into \"settings_users\" (\"steam\") values (\"{ARG}\")", new List([steamid.ToString()]), true); + res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List([steamid.ToString()])); } return int.Parse(res[0][0]); } + internal static void LoadSettings(int userid, Action>> action) + { + db.QueryAsync("select \"param\",\"value\" from \"settings_values\" where \"user_id\" = {ARG}", new List([userid.ToString()]), action); + } + + + public static string GetUserSettingValue(int userid, string param, string default_value) { - var res = db.Query("select \"value\" from \"settings\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List([userid.ToString(), param])); + var res = db.Query("select \"value\" from \"settings_values\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List([userid.ToString(), param])); if (res.Count == 0) { - db.Query("insert into \"settings\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List([userid.ToString(), param, default_value]), true); + db.Query("insert into \"settings_values\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List([userid.ToString(), param, default_value]), true); return default_value; } return res[0][0]; @@ -50,7 +57,7 @@ public static string GetUserSettingValue(int userid, string param, string defaul public static void SetUserSettingValue(int userid, string param, string value) { - db.Query("update \"settings\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List([value, userid.ToString(), param]), true); + db.QueryAsync("update \"settings_values\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List([value, userid.ToString(), param]), null, true); }