Skip to content

Commit 140aeae

Browse files
authored
v 0.9
1 parent 0faba13 commit 140aeae

File tree

6 files changed

+24
-21
lines changed

6 files changed

+24
-21
lines changed

PlayerSettings/Main.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void OnConfigParsed(PluginConfig config)
2929
}
3030

3131
public override string ModuleName => "PlayerSettings [Core]";
32-
public override string ModuleVersion => "0.8.1";
32+
public override string ModuleVersion => "0.9";
3333
public override string ModuleAuthor => "Nick Fox";
3434
public override string ModuleDescription => "One storage for player's settings (aka ClientCookies)";
3535

@@ -55,7 +55,6 @@ private void OnClientAuthorized(int slot, SteamID steamID)
5555
{
5656
((SettingsApi)_api).LoadOnConnect(Utilities.GetPlayerFromSlot(slot));
5757
}
58-
5958
}
6059

6160
public struct DatabaseParams
@@ -64,16 +63,18 @@ public struct DatabaseParams
6463
public string Name { get; set; }
6564
public string User { get; set; }
6665
public string Password { get; set; }
66+
public string Table { get; set; }
6767

6868
public DatabaseParams()
6969
{
7070
Host = "127.0.0.1:3306";
7171
Name = "";
7272
User = "";
7373
Password = "";
74+
Table = "settings_";
7475
}
7576

76-
public bool IsDefault()
77+
public bool IsLocal()
7778
{
7879
return (Host == "127.0.0.1:3306" && Name == "" && User == "" && Password == "") || Host == "";
7980
}

PlayerSettings/SettingItems.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ internal static void AddTogglable(string name, string viewName)
2121
Items.Add(new SettingItem(SettingType.Togglable, name, viewName));
2222
}
2323

24-
internal static void AddSelecting(string name, string viewName, List<string> values)
24+
internal static void AddSelecting(string name, string viewName, Dictionary<string, string> values)
2525
{
2626
DeleteIfExist(name);
2727
Items.Add(new SettingItem(SettingType.Selecting, name, viewName, values));
@@ -33,7 +33,7 @@ internal static void DeleteIfExist(string name)
3333
if (Items[i].Name == name)
3434
{
3535
Items.RemoveAt(i);
36-
break;
36+
i--;
3737
}
3838
}
3939
}

PlayerSettings/SettingsApi.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void RegisterTogglableSetting(string name, string viewName)
7676
SettingItems.AddTogglable(name, viewName);
7777
}
7878

79-
public void RegisterSelectingSetting(string name, string viewName, List<string> values)
79+
public void RegisterSelectingSetting(string name, string viewName, Dictionary<string, string> values)
8080
{
8181
SettingItems.AddSelecting(name, viewName, values);
8282
}

PlayerSettings/Storage.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ namespace PlayerSettings
1414
internal static class Storage
1515
{
1616
private static IAnyBase db;
17+
private static string table;
1718

1819
public static void Init()
1920
{
20-
var is_sqlite = PlayerSettingsCore.plugin.Config.DatabaseParams.IsDefault();
21+
table = PlayerSettingsCore.plugin.Config.DatabaseParams.Table;
22+
var is_sqlite = PlayerSettingsCore.plugin.Config.DatabaseParams.IsLocal();
2123
if (is_sqlite)
2224
{
2325
db = CAnyBase.Base("sqlite");
@@ -30,9 +32,9 @@ public static void Init()
3032
}
3133

3234
db.Init();
33-
db.QueryAsync("CREATE TABLE IF NOT EXISTS `settings_users` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `steam` VARCHAR(255) NOT NULL)", null, (_) =>
35+
db.QueryAsync($"CREATE TABLE IF NOT EXISTS `{table}users` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `steam` VARCHAR(255) NOT NULL)", null, (_) =>
3436
{
35-
db.QueryAsync("CREATE TABLE IF NOT EXISTS `settings_values` (`user_id` INT, `param` VARCHAR(255) NOT NULL, `value` VARCHAR(255) NOT NULL)", null, (_) =>
37+
db.QueryAsync($"CREATE TABLE IF NOT EXISTS `{table}values` (`user_id` INT, `param` VARCHAR(255) NOT NULL, `value` VARCHAR(255) NOT NULL)", null, (_) =>
3638
{
3739
if (!is_sqlite) Migrate.Init(db);
3840
}, true);
@@ -56,21 +58,21 @@ public static int GetUserId(CCSPlayerController player)
5658
public static void GetUserIdAsync(CCSPlayerController player, Action<int> callback)
5759
{
5860
var steamid = player.SteamID;
59-
db.QueryAsync("SELECT `id` FROM `settings_users` WHERE `steam` = '{ARG}'", new List<string>([steamid.ToString()]), (data) => {
61+
db.QueryAsync("SELECT `id` FROM `" + table + "users` WHERE `steam` = '{ARG}'", new List<string>([steamid.ToString()]), (data) => {
6062
if (data.Count > 0)
6163
{
6264
callback(int.Parse(data[0][0]));
6365
}
6466
else
65-
db.QueryAsync("INSERT INTO `settings_users` (`steam`) VALUES ('{ARG}')", new List<string>([steamid.ToString()]), (data) => GetUserIdAsync(player, callback), true);
67+
db.QueryAsync("INSERT INTO `" + table + "users` (`steam`) VALUES ('{ARG}')", new List<string>([steamid.ToString()]), (data) => GetUserIdAsync(player, callback), true);
6668
});
6769

6870

6971
}
7072

7173
internal static void LoadSettings(int userid, Action<List<List<string>>> action)
7274
{
73-
db.QueryAsync("SELECT `param`, `value` FROM `settings_values` WHERE `user_id` = {ARG}", new List<string>([userid.ToString()]), action);
75+
db.QueryAsync("SELECT `param`, `value` FROM `" + table + "values` WHERE `user_id` = {ARG}", new List<string>([userid.ToString()]), action);
7476
}
7577

7678

@@ -88,15 +90,15 @@ public static string GetUserSettingValue(int userid, string param, string defaul
8890

8991
public static void SetUserSettingValue(int userid, string param, string value)
9092
{
91-
db.QueryAsync("SELECT `value` FROM `settings_values` WHERE `user_id` = {ARG} AND `param` = '{ARG}'", new List<string>([userid.ToString(), param]), (data) => SetUserSettingValuePost(userid, param, value, data.Count));
93+
db.QueryAsync("SELECT `value` FROM `" + table + "values` WHERE `user_id` = {ARG} AND `param` = '{ARG}'", new List<string>([userid.ToString(), param]), (data) => SetUserSettingValuePost(userid, param, value, data.Count));
9294
}
9395

9496
private static void SetUserSettingValuePost(int userid, string param, string value, int co)
9597
{
9698
if (co == 0)
97-
db.QueryAsync("INSERT INTO `settings_values` (`user_id`, `param`, `value`) VALUES ({ARG}, '{ARG}', '{ARG}')", new List<string>([userid.ToString(), param, value]), null, true);
99+
db.QueryAsync("INSERT INTO `" + table + "values` (`user_id`, `param`, `value`) VALUES ({ARG}, '{ARG}', '{ARG}')", new List<string>([userid.ToString(), param, value]), null, true);
98100
else
99-
db.QueryAsync("UPDATE `settings_values` SET `value` = '{ARG}' WHERE `user_id` = {ARG} AND `param` = '{ARG}'", new List<string>([value, userid.ToString(), param]), null, true);
101+
db.QueryAsync("UPDATE `" + table + "values` SET `value` = '{ARG}' WHERE `user_id` = {ARG} AND `param` = '{ARG}'", new List<string>([value, userid.ToString(), param]), null, true);
100102
}
101103

102104
public static void Close()

PlayerSettingsApi/ISettingsApi.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public interface ISettingsApi
1414
public void AddHook(Action<CCSPlayerController> action);
1515
public void RemHook(Action<CCSPlayerController> action);
1616

17-
// Coming soon...
18-
//public void RegisterTogglableSetting(string name, string viewName);
19-
//public void RegisterSelectingSetting(string name, string viewName, List<string> values);
20-
//public List<SettingItem> GetSettingItems();
17+
18+
public void RegisterTogglableSetting(string name, string viewName);
19+
public void RegisterSelectingSetting(string name, string viewName, Dictionary<string, string> values);
20+
public List<SettingItem> GetSettingItems();
2121
}
2222
}

PlayerSettingsApi/Structs.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ public class SettingItem
1010
{
1111
public string Name;
1212
public string ViewName;
13-
public List<string> Values;
13+
public Dictionary<string, string> Values;
1414
public SettingType Type;
1515

16-
public SettingItem(SettingType type, string name, string viewName, List<string> values = null)
16+
public SettingItem(SettingType type, string name, string viewName, Dictionary<string, string> values = null)
1717
{
1818
if(type != SettingType.Togglable && values == null)
1919
throw new ArgumentException("Non-togglable settings must have values");

0 commit comments

Comments
 (0)