use basic config

+ configurable !wp seconds cooldows
+ configurable website in !ws
This commit is contained in:
Nereziel
2023-11-13 17:35:43 +01:00
parent 26211cc92b
commit b7b8055587
3 changed files with 41 additions and 53 deletions

View File

@@ -1,59 +1,32 @@
using CounterStrikeSharp.API.Modules.Utils; using CounterStrikeSharp.API.Core;
using System.Reflection; using System.Text.Json.Serialization;
using System.Text.Json;
namespace WeaponPaints namespace WeaponPaints
{ {
internal class Cfg public class WeaponPaintsConfig : BasePluginConfig
{ {
public static Config config = new(); public override int Version { get; set; } = 1;
public void CheckConfig(string moduleDirectory)
{
string path = Path.Join(moduleDirectory, "config.json");
if (!File.Exists(path)) [JsonPropertyName("DatabaseHost")]
{ public string DatabaseHost { get; set; } = "localhost";
CreateAndWriteFile(path);
}
using FileStream fs = new(path, FileMode.Open, FileAccess.Read); [JsonPropertyName("DatabasePort")]
using StreamReader sr = new(fs); public int DatabasePort { get; set; } = 3306;
// Deserialize the JSON from the file and load the configuration.
config = JsonSerializer.Deserialize<Config>(sr.ReadToEnd());
}
private static void CreateAndWriteFile(string path)
{
using (FileStream fs = File.Create(path)) [JsonPropertyName("DatabaseUser")]
{ public string DatabaseUser { get; set; } = "dbuser";
// File is created, and fs will automatically be disposed when the using block exits.
}
Console.WriteLine($"File created: {File.Exists(path)}"); [JsonPropertyName("DatabasePassword")]
public string DatabasePassword { get; set; } = "dbuserpw";
config = new Config [JsonPropertyName("DatabaseName")]
{ public string DatabaseName { get; set; } = "dbname";
DatabaseHost = "localhost",
DatabasePort = 3306,
DatabaseUser = "dbuser",
DatabasePassword = "dbpassword",
DatabaseName = "database"
};
// Serialize the config object to JSON and write it to the file. [JsonPropertyName("CmdRefreshCooldownSeconds")]
string jsonConfig = JsonSerializer.Serialize(config, new JsonSerializerOptions() public int CmdRefreshCooldownSeconds { get; set; } = 60;
{
WriteIndented = true [JsonPropertyName("WebSite")]
}); public string WebSite { get; set; } = "http://wp.example.com";
File.WriteAllText(path, jsonConfig);
}
}
internal class Config
{
public string? DatabaseHost { get; set; }
public uint DatabasePort { get; set; }
public string? DatabaseUser { get; set; }
public string? DatabasePassword { get; set; }
public string? DatabaseName { get; set; }
} }
} }

View File

@@ -4,15 +4,19 @@ using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Commands; using CounterStrikeSharp.API.Modules.Commands;
using CounterStrikeSharp.API.Modules.Entities; using CounterStrikeSharp.API.Modules.Entities;
using CounterStrikeSharp.API.Modules.Memory; using CounterStrikeSharp.API.Modules.Memory;
using CounterStrikeSharp.API.Modules.Utils;
using Nexd.MySQL; using Nexd.MySQL;
using static CounterStrikeSharp.API.Core.Listeners;
namespace WeaponPaints; namespace WeaponPaints;
public class WeaponPaints : BasePlugin public class WeaponPaints : BasePlugin, IPluginConfig<WeaponPaintsConfig>
{ {
public override string ModuleName => "WeaponPaints"; public override string ModuleName => "WeaponPaints";
public override string ModuleDescription => "Connector for web-based player chosen wepaon paints."; public override string ModuleDescription => "Connector for web-based player chosen wepaon paints.";
public override string ModuleAuthor => "Nereziel"; public override string ModuleAuthor => "Nereziel";
public override string ModuleVersion => "0.7"; public override string ModuleVersion => "0.7";
public WeaponPaintsConfig Config { get; set; } = new();
MySqlDb? MySql = null; MySqlDb? MySql = null;
public DateTime[] commandCooldown = new DateTime[Server.MaxPlayers]; public DateTime[] commandCooldown = new DateTime[Server.MaxPlayers];
private Dictionary<ulong, Dictionary<nint, int>> g_playersSkins = new Dictionary<ulong, Dictionary<nint, int>>(); private Dictionary<ulong, Dictionary<nint, int>> g_playersSkins = new Dictionary<ulong, Dictionary<nint, int>>();
@@ -54,13 +58,17 @@ public class WeaponPaints : BasePlugin
public override void Load(bool hotReload) public override void Load(bool hotReload)
{ {
new Cfg().CheckConfig(ModuleDirectory); MySql = new MySqlDb(Config.DatabaseHost!, Config.DatabaseUser!, Config.DatabasePassword!, Config.DatabaseName!, Config.DatabasePort);
MySql = new MySqlDb(Cfg.config.DatabaseHost!, Cfg.config.DatabaseUser!, Cfg.config.DatabasePassword!, Cfg.config.DatabaseName!, (int)Cfg.config.DatabasePort);
RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned); RegisterListener<Listeners.OnEntitySpawned>(OnEntitySpawned);
RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized); RegisterListener<Listeners.OnClientAuthorized>(OnClientAuthorized);
RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect); RegisterListener<Listeners.OnClientDisconnect>(OnClientDisconnect);
} }
public void OnConfigParsed(WeaponPaintsConfig config)
{
Config = config;
}
private void OnClientAuthorized(int playerSlot, SteamID steamId) private void OnClientAuthorized(int playerSlot, SteamID steamId)
{ {
int slot = playerSlot; int slot = playerSlot;
Server.NextFrame(() => Server.NextFrame(() =>
@@ -109,12 +117,19 @@ public class WeaponPaints : BasePlugin
} }
}); });
} }
[ConsoleCommand("css_ws", "weaponskins")]
public void OnCommandWS(CCSPlayerController? player, CommandInfo command)
{
if (player == null) return;
player.PrintToChat($"Change weapon skins at {ChatColors.Purple}{Config.WebSite}");
player.PrintToChat($"To synchronize weapon paints type {ChatColors.Purple}!wp");
}
[ConsoleCommand("css_wp", "refreshskins")] [ConsoleCommand("css_wp", "refreshskins")]
public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command) public void OnCommandRefresh(CCSPlayerController? player, CommandInfo command)
{ {
if (player == null) return; if (player == null) return;
int playerSlot = (int)player.EntityIndex!.Value.Value - 1; int playerSlot = (int)player.EntityIndex!.Value.Value - 1;
if (DateTime.UtcNow >= commandCooldown[playerSlot].AddMinutes(2)) if (DateTime.UtcNow >= commandCooldown[playerSlot].AddSeconds(Config.CmdRefreshCooldownSeconds))
{ {
commandCooldown[playerSlot] = DateTime.UtcNow; commandCooldown[playerSlot] = DateTime.UtcNow;
Task.Run(async () => await GetWeaponPaintsFromDatabase(playerSlot)); Task.Run(async () => await GetWeaponPaintsFromDatabase(playerSlot));
@@ -159,4 +174,4 @@ public class WeaponPaints : BasePlugin
return; return;
} }
} }
} }

View File

@@ -8,7 +8,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="CounterStrikeSharp.API" Version="1.0.30" /> <PackageReference Include="CounterStrikeSharp.API" Version="1.0.50" />
<PackageReference Include="Nexd.MySQL" Version="1.0.1" /> <PackageReference Include="Nexd.MySQL" Version="1.0.1" />
</ItemGroup> </ItemGroup>