Skip to content

Commit

Permalink
0.4
Browse files Browse the repository at this point in the history
* Fixed bug when setting got incorrectly
* Replacing with async methods
* Renamed sqlite filename and tables struct
  • Loading branch information
NickFox007 authored May 16, 2024
1 parent 5b887b1 commit 9919102
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 22 deletions.
21 changes: 18 additions & 3 deletions PlayerSettings/CPlayerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ namespace PlayerSettings
internal class CPlayerSettings
{
private int userid;
private static Dictionary<string, string> cached_values;
private CCSPlayerController player;
private Dictionary<string, string> cached_values;

public CPlayerSettings(int _userid)
public CPlayerSettings(CCSPlayerController _player)
{
userid = _userid;
player = _player;
userid = Storage.GetUserId(player);
cached_values = new Dictionary<string, string>();
}

Expand All @@ -41,6 +43,19 @@ public int UserId()
return userid;
}

public bool EqualPlayer(CCSPlayerController _player)
{
return (player == _player);
}

internal void ParseLoadedSettings(List<List<string>> rows)
{
foreach (var row in rows)
{
cached_values[row[0]] = row[1];
}
}


}
}
11 changes: 10 additions & 1 deletion PlayerSettings/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)";

Expand All @@ -21,5 +23,12 @@ public override void Load(bool hotReload)
_api = new SettingsApi();
Capabilities.RegisterPluginCapability(_pluginCapability, () => _api);
Storage.Init(this);

RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
}

private void OnClientAuthorized(int slot, SteamID steamID)
{
((SettingsApi)_api).LoadOnConnect(Utilities.GetPlayerFromSlot(slot));
}
}
35 changes: 26 additions & 9 deletions PlayerSettings/SettingsApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,51 @@ namespace PlayerSettings

internal class SettingsApi : ISettingsApi
{
List<CPlayerSettings> settings;
CPlayerSettings[] settings;
public SettingsApi()
{
settings = new List<CPlayerSettings>();
settings = Array.Empty<CPlayerSettings>();
}

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

}
}
25 changes: 16 additions & 9 deletions PlayerSettings/Storage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,46 @@ 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<string>([steamid.ToString()]));
var res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
if(res.Count == 0)
{
db.Query("insert into \"users\" (\"steam\") values (\"{ARG}\")", new List<string>([steamid.ToString()]), true);
res = db.Query("select \"id\" from \"users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
db.Query("insert into \"settings_users\" (\"steam\") values (\"{ARG}\")", new List<string>([steamid.ToString()]), true);
res = db.Query("select \"id\" from \"settings_users\" where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()]));
}
return int.Parse(res[0][0]);
}

internal static void LoadSettings(int userid, Action<List<List<string>>> action)
{
db.QueryAsync("select \"param\",\"value\" from \"settings_values\" where \"user_id\" = {ARG}", new List<string>([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<string>([userid.ToString(), param]));
var res = db.Query("select \"value\" from \"settings_values\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([userid.ToString(), param]));
if (res.Count == 0)
{
db.Query("insert into \"settings\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true);
db.Query("insert into \"settings_values\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true);
return default_value;
}
return res[0][0];
}

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<string>([value, userid.ToString(), param]), true);
db.QueryAsync("update \"settings_values\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([value, userid.ToString(), param]), null, true);
}


Expand Down

0 comments on commit 9919102

Please sign in to comment.