|
| 1 | +using AnyBaseLib; |
| 2 | +using CounterStrikeSharp.API.Core; |
| 3 | +using CounterStrikeSharp.API.Modules.Entities; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Net.WebSockets; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | + |
| 12 | +namespace PlayerSettings |
| 13 | +{ |
| 14 | + internal static class Storage |
| 15 | + { |
| 16 | + private static IAnyBase db; |
| 17 | + |
| 18 | + public static void Init(BasePlugin plugin, string driver = "sqlite") |
| 19 | + { |
| 20 | + db = CAnyBase.Base(driver); |
| 21 | + db.Set(AnyBaseLib.Bases.CommitMode.AutoCommit, Path.Combine(plugin.ModuleDirectory,"menusets")); |
| 22 | + |
| 23 | + 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); |
| 26 | + } |
| 27 | + |
| 28 | + public static int GetUserId(CCSPlayerController player) |
| 29 | + { |
| 30 | + var steamid = player.SteamID; |
| 31 | + var res = db.Query("select \"id\" from users where \"steam\" = \"{ARG}\"", new List<string>([steamid.ToString()])); |
| 32 | + if(res.Count == 0) |
| 33 | + { |
| 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()])); |
| 36 | + } |
| 37 | + return int.Parse(res[0][0]); |
| 38 | + } |
| 39 | + |
| 40 | + public static string GetUserSettingValue(int userid, string param, string default_value) |
| 41 | + { |
| 42 | + var res = db.Query("select \"value\" from \"settings\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([userid.ToString(), param])); |
| 43 | + if (res.Count == 0) |
| 44 | + { |
| 45 | + db.Query("insert into \"settings\" (\"user_id\", \"param\",\"value\") values ({ARG},\"{ARG}\", \"{ARG}\")", new List<string>([userid.ToString(), param, default_value]), true); |
| 46 | + return default_value; |
| 47 | + } |
| 48 | + return res[0][0]; |
| 49 | + } |
| 50 | + |
| 51 | + public static void SetUserSettingValue(int userid, string param, string value) |
| 52 | + { |
| 53 | + db.Query("update \"settings\" set \"value\" = \"{ARG}\" where \"user_id\" = {ARG} and \"param\" = \"{ARG}\"", new List<string>([value, userid.ToString(), param]), true); |
| 54 | + } |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + } |
| 60 | +} |
0 commit comments