diff --git a/Database.cs b/Database.cs index acbd95a8..028c7143 100644 --- a/Database.cs +++ b/Database.cs @@ -11,18 +11,11 @@ namespace WeaponPaints _dbConnectionString = dbConnectionString; } - public async Task GetConnectionAsync() - { - try - { - var connection = new MySqlConnection(_dbConnectionString); - await connection.OpenAsync(); - return connection; - } - catch (Exception) - { - throw; - } - } + public async Task GetConnectionAsync() + { + var connection = new MySqlConnection(_dbConnectionString); + await connection.OpenAsync(); + return connection; + } } } \ No newline at end of file diff --git a/PlayerExtensions.cs b/PlayerExtensions.cs index 0e8198b7..8b97a559 100644 --- a/PlayerExtensions.cs +++ b/PlayerExtensions.cs @@ -5,11 +5,17 @@ namespace WeaponPaints; public static class PlayerExtensions { - public static void Print(this CCSPlayerController controller, string message) - { - if (WeaponPaints._localizer == null) return; - StringBuilder _message = new(WeaponPaints._localizer["wp_prefix"]); - _message.Append(message); - controller.PrintToChat(_message.ToString()); - } + public static void Print(this CCSPlayerController controller, string message) + { + if (WeaponPaints._localizer == null) + { + controller.PrintToChat(message); + } + else + { + StringBuilder _message = new(WeaponPaints._localizer["wp_prefix"]); + _message.Append(message); + controller.PrintToChat(_message.ToString()); + } + } } \ No newline at end of file diff --git a/PlayerInfo.cs b/PlayerInfo.cs index 5079621d..679bf727 100644 --- a/PlayerInfo.cs +++ b/PlayerInfo.cs @@ -1,12 +1,24 @@ namespace WeaponPaints { - public class PlayerInfo - { - public int Index { get; set; } - public int Slot { get; set; } - public int? UserId { get; set; } - public string? SteamId { get; set; } - public string? Name { get; set; } - public string? IpAddress { get; set; } - } + public class PlayerInfo + { + public int Index { get; set; } + public int Slot { get; set; } + public int? UserId { get; set; } + public string? SteamId { get; set; } + public string? Name { get; set; } + public string? IpAddress { get; set; } + + public PlayerInfo() { } + + public PlayerInfo(int index, int slot, int? userId, string? steamId, string? name, string? ipAddress) + { + Index = index; + Slot = slot; + UserId = userId; + SteamId = steamId; + Name = name; + IpAddress = ipAddress; + } + } } \ No newline at end of file diff --git a/SchemaString.cs b/SchemaString.cs index 9de02790..95c7e0bb 100644 --- a/SchemaString.cs +++ b/SchemaString.cs @@ -10,15 +10,16 @@ public class SchemaString : NativeObject where TSchemaClass : Nati internal SchemaString(TSchemaClass instance, string member) : base(Schema.GetSchemaValue(instance.Handle, typeof(TSchemaClass).Name!, member)) { } - internal unsafe void Set(string str) - { - var bytes = Encoding.UTF8.GetBytes(str); + internal unsafe void Set(string str) + { + var bytes = Encoding.UTF8.GetBytes(str); + var handle = Handle.ToInt64(); - for (var i = 0; i < bytes.Length; i++) - { - Unsafe.Write((void*)(Handle.ToInt64() + i), bytes[i]); - } + for (var i = 0; i < bytes.Length; i++) + { + Unsafe.Write((void*)(handle + i), bytes[i]); + } - Unsafe.Write((void*)(Handle.ToInt64() + bytes.Length), 0); - } + Unsafe.Write((void*)(handle + bytes.Length), 0); + } } \ No newline at end of file diff --git a/WeaponAction.cs b/WeaponAction.cs index 44ceb8de..681692ad 100644 --- a/WeaponAction.cs +++ b/WeaponAction.cs @@ -13,8 +13,7 @@ namespace WeaponPaints internal static void ChangeWeaponAttributes(CBasePlayerWeapon? weapon, CCSPlayerController? player, bool isKnife = false) { if (player is null || weapon is null || !weapon.IsValid || !Utility.IsPlayerValid(player)) return; - - if (!gPlayerWeaponsInfo.ContainsKey(player.Slot)) return; + if (!gPlayerWeaponsInfo.TryGetValue(player.Slot, out _)) return; if (isKnife && !g_playersKnife.ContainsKey(player.Slot) || isKnife && g_playersKnife[player.Slot] == "weapon_knife") return; diff --git a/WeaponInfo.cs b/WeaponInfo.cs index c1a97d58..0ba85db0 100644 --- a/WeaponInfo.cs +++ b/WeaponInfo.cs @@ -1,9 +1,18 @@ namespace WeaponPaints { - public class WeaponInfo - { - public int Paint { get; set; } - public int Seed { get; set; } = 0; - public float Wear { get; set; } = 0f; - } + public class WeaponInfo + { + public int Paint { get; set; } + public int Seed { get; set; } + public float Wear { get; set; } + + public WeaponInfo() : this(0, 0, 0f) { } + + public WeaponInfo(int paint, int seed, float wear) + { + Paint = paint; + Seed = seed; + Wear = wear; + } + } } \ No newline at end of file diff --git a/WeaponPaints.cs b/WeaponPaints.cs index be4dbab9..1f0a596f 100644 --- a/WeaponPaints.cs +++ b/WeaponPaints.cs @@ -180,15 +180,13 @@ public partial class WeaponPaints : BasePlugin, IPluginConfig(query, new { steamid = player.SteamId }); + string query = "SELECT `weapon_defindex`, `weapon_paint_id`, `weapon_wear`, `weapon_seed` FROM `wp_player_skins` WHERE `steamid` = @steamid"; + + var playerSkins = await connection.QueryAsync(query, new { steamid = player.SteamId }); if (playerSkins == null) return; @@ -145,24 +146,31 @@ namespace WeaponPaints { await using var connection = await _database.GetConnectionAsync(); - foreach (var weaponInfoPair in weaponsInfo) - { - int weaponDefIndex = weaponInfoPair.Key; - WeaponInfo weaponInfo = weaponInfoPair.Value; + foreach (var weaponInfoPair in weaponsInfo) + { + int weaponDefIndex = weaponInfoPair.Key; + WeaponInfo weaponInfo = weaponInfoPair.Value; - int paintId = weaponInfo.Paint; - float wear = weaponInfo.Wear; - int seed = weaponInfo.Seed; + int paintId = weaponInfo.Paint; + float wear = weaponInfo.Wear; + int seed = weaponInfo.Seed; - string query = "INSERT INTO `wp_player_skins` (`steamid`, `weapon_defindex`, `weapon_paint_id`, `weapon_wear`, `weapon_seed`) " + - "VALUES (@steamid, @weaponDefIndex, @paintId, @wear, @seed) " + - "ON DUPLICATE KEY UPDATE `weapon_paint_id` = @paintId, `weapon_wear` = @wear, `weapon_seed` = @seed"; + string query = "INSERT INTO `wp_player_skins` (`steamid`, `weapon_defindex`, `weapon_paint_id`, `weapon_wear`, `weapon_seed`) " + + "VALUES (@steamid, @weaponDefIndex, @paintId, @wear, @seed) " + + "ON DUPLICATE KEY UPDATE `weapon_paint_id` = @paintId, `weapon_wear` = @wear, `weapon_seed` = @seed"; - var parameters = new { steamid = player.SteamId, weaponDefIndex, paintId, wear, seed }; - await connection.ExecuteAsync(query, parameters); - } - } - catch (Exception e) + var parameters = new DynamicParameters(); + parameters.Add("@steamid", player.SteamId); + parameters.Add("@weaponDefIndex", weaponDefIndex); + parameters.Add("@paintId", paintId); + parameters.Add("@wear", wear); + parameters.Add("@seed", seed); + + await connection.ExecuteAsync(query, parameters); + } + + } + catch (Exception e) { Utility.Log($"Error syncing weapon paints to database: {e.Message}"); }