Skip to content

Commit 9919102

Browse files
authored
0.4
* Fixed bug when setting got incorrectly * Replacing with async methods * Renamed sqlite filename and tables struct
1 parent 5b887b1 commit 9919102

File tree

4 files changed

+70
-22
lines changed

4 files changed

+70
-22
lines changed

PlayerSettings/CPlayerSettings.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ namespace PlayerSettings
1010
internal class CPlayerSettings
1111
{
1212
private int userid;
13-
private static Dictionary<string, string> cached_values;
13+
private CCSPlayerController player;
14+
private Dictionary<string, string> cached_values;
1415

15-
public CPlayerSettings(int _userid)
16+
public CPlayerSettings(CCSPlayerController _player)
1617
{
17-
userid = _userid;
18+
player = _player;
19+
userid = Storage.GetUserId(player);
1820
cached_values = new Dictionary<string, string>();
1921
}
2022

@@ -41,6 +43,19 @@ public int UserId()
4143
return userid;
4244
}
4345

46+
public bool EqualPlayer(CCSPlayerController _player)
47+
{
48+
return (player == _player);
49+
}
50+
51+
internal void ParseLoadedSettings(List<List<string>> rows)
52+
{
53+
foreach (var row in rows)
54+
{
55+
cached_values[row[0]] = row[1];
56+
}
57+
}
58+
4459

4560
}
4661
}

PlayerSettings/Main.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
using CounterStrikeSharp.API.Core.Attributes.Registration;
44
using CounterStrikeSharp.API.Core.Capabilities;
55
using CounterStrikeSharp.API.Modules.Commands;
6+
using CounterStrikeSharp.API.Modules.Entities;
67
using CounterStrikeSharp.API.Modules.Menu;
8+
using static CounterStrikeSharp.API.Core.Listeners;
79

810

911
namespace PlayerSettings;
1012
public class PlayerSettingsCore : BasePlugin
1113
{
1214
public override string ModuleName => "PlayerSettings [Core]";
13-
public override string ModuleVersion => "0.1";
15+
public override string ModuleVersion => "0.4";
1416
public override string ModuleAuthor => "Nick Fox";
1517
public override string ModuleDescription => "One storage for player's settings (aka ClientCookies)";
1618

@@ -21,5 +23,12 @@ public override void Load(bool hotReload)
2123
_api = new SettingsApi();
2224
Capabilities.RegisterPluginCapability(_pluginCapability, () => _api);
2325
Storage.Init(this);
26+
27+
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
28+
}
29+
30+
private void OnClientAuthorized(int slot, SteamID steamID)
31+
{
32+
((SettingsApi)_api).LoadOnConnect(Utilities.GetPlayerFromSlot(slot));
2433
}
2534
}

PlayerSettings/SettingsApi.cs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,34 +11,51 @@ namespace PlayerSettings
1111

1212
internal class SettingsApi : ISettingsApi
1313
{
14-
List<CPlayerSettings> settings;
14+
CPlayerSettings[] settings;
1515
public SettingsApi()
1616
{
17-
settings = new List<CPlayerSettings>();
17+
settings = Array.Empty<CPlayerSettings>();
1818
}
1919

20-
private CPlayerSettings FindUser(int userid)
20+
private CPlayerSettings FindUser(CCSPlayerController player)
2121
{
2222
foreach (var item in this.settings)
2323
{
24-
if (item.UserId() == userid)
24+
if (item.EqualPlayer(player))
2525
{
26+
//Console.WriteLine($"Returned found: {item.UserId()}");
2627
return item;
2728
}
28-
}
29-
var newInst = new CPlayerSettings(userid);
30-
settings.Add(newInst);
29+
}
30+
var newInst = new CPlayerSettings(player);
31+
AddPlayerInst(newInst);
3132
return newInst;
3233
}
3334

35+
private void AddPlayerInst(CPlayerSettings inst)
36+
{
37+
Array.Resize(ref settings, settings.Length + 1);
38+
settings[settings.Length - 1] = inst;
39+
}
40+
41+
3442
public string GetPlayerSettingsValue(CCSPlayerController player, string param, string default_value)
3543
{
36-
return FindUser(Storage.GetUserId(player)).GetValue(param, default_value);
44+
return FindUser(player).GetValue(param, default_value);
3745
}
46+
3847

3948
public void SetPlayerSettingsValue(CCSPlayerController player, string param, string value)
4049
{
41-
FindUser(Storage.GetUserId(player)).SetValue(param, value);
50+
FindUser(player).SetValue(param, value);
4251
}
52+
53+
internal void LoadOnConnect(CCSPlayerController player)
54+
{
55+
var user = FindUser(player);
56+
57+
Storage.LoadSettings(user.UserId(), user.ParseLoadedSettings);
58+
}
59+
4360
}
4461
}

PlayerSettings/Storage.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,39 +18,46 @@ internal static class Storage
1818
public static void Init(BasePlugin plugin, string driver = "sqlite")
1919
{
2020
db = CAnyBase.Base(driver);
21-
db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"menusets"));
21+
db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"settings"));
2222

2323
db.Init();
24-
db.Query("create table if not exists \"users\" (\"id\" integer primary key AUTOINCREMENT, \"steam\" varchar(255) not null)", null, true);
25-
db.Query("create table if not exists \"settings\" (\"user_id\" int, \"param\" varchar(255) not null, \"value\" varchar(255) not null)", null, true);
24+
db.QueryAsync("create table if not exists \"settings_users\" (\"id\" integer primary key AUTOINCREMENT, \"steam\" varchar(255) not null)", null, null, true);
25+
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);
2626
}
2727

2828
public static int GetUserId(CCSPlayerController player)
2929
{
3030
var steamid = player.SteamID;
31-
var res = db.Query("select \"id\" from users where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
31+
var res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
3232
if(res.Count == 0)
3333
{
34-
db.Query("insert into \"users\" (\"steam\") values (\"{ARG}\")", new List<string>([steamid.ToString()]), true);
35-
res = db.Query("select \"id\" from \"users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
34+
db.Query("insert into \"settings_users\" (\"steam\") values (\"{ARG}\")", new List<string>([steamid.ToString()]), true);
35+
res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
3636
}
3737
return int.Parse(res[0][0]);
3838
}
3939

40+
internal static void LoadSettings(int userid, Action<List<List<string>>> action)
41+
{
42+
db.QueryAsync("select \"param\",\"value\" from \"settings_values\" where \"user_id\" = {ARG}", new List<string>([userid.ToString()]), action);
43+
}
44+
45+
46+
4047
public static string GetUserSettingValue(int userid, string param, string default_value)
4148
{
42-
var res = db.Query("select \"value\" from \"settings\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([userid.ToString(), param]));
49+
var res = db.Query("select \"value\" from \"settings_values\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([userid.ToString(), param]));
4350
if (res.Count == 0)
4451
{
45-
db.Query("insert into \"settings\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true);
52+
db.Query("insert into \"settings_values\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true);
4653
return default_value;
4754
}
4855
return res[0][0];
4956
}
5057

5158
public static void SetUserSettingValue(int userid, string param, string value)
5259
{
53-
db.Query("update \"settings\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([value, userid.ToString(), param]), true);
60+
db.QueryAsync("update \"settings_values\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([value, userid.ToString(), param]), null, true);
5461
}
5562

5663

0 commit comments

Comments
 (0)