mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-18 02:41:54 +00:00
using transaction to sync killstreaks
This commit is contained in:
64
Events.cs
64
Events.cs
@@ -5,6 +5,7 @@ using CounterStrikeSharp.API.Modules.Entities;
|
||||
using CounterStrikeSharp.API.Modules.Memory;
|
||||
using CounterStrikeSharp.API.Modules.Memory.DynamicFunctions;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Collections.Concurrent;
|
||||
|
||||
namespace WeaponPaints
|
||||
{
|
||||
@@ -68,6 +69,21 @@ namespace WeaponPaints
|
||||
|
||||
if (player is null || !player.IsValid || player.IsBot) return HookResult.Continue;
|
||||
|
||||
var playerInfo = new PlayerInfo
|
||||
{
|
||||
UserId = player.UserId,
|
||||
Slot = player.Slot,
|
||||
Index = (int)player.Index,
|
||||
SteamId = player.SteamID.ToString(),
|
||||
Name = player.PlayerName,
|
||||
IpAddress = player.IpAddress?.Split(":")[0]
|
||||
};
|
||||
|
||||
if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var weaponInfos))
|
||||
return HookResult.Continue;
|
||||
|
||||
_ = Task.Run(async () => await SyncStatTrakForPlayer(playerInfo, weaponInfos));
|
||||
|
||||
if (Config.Additional.SkinEnabled)
|
||||
{
|
||||
GPlayerWeaponsInfo.TryRemove(player.Slot, out _);
|
||||
@@ -97,36 +113,28 @@ namespace WeaponPaints
|
||||
|
||||
CommandsCooldown.Remove(player.Slot);
|
||||
|
||||
var playerInfo = new PlayerInfo
|
||||
{
|
||||
UserId = player.UserId,
|
||||
Slot = player.Slot,
|
||||
Index = (int)player.Index,
|
||||
SteamId = player.SteamID.ToString(),
|
||||
Name = player.PlayerName,
|
||||
IpAddress = player.IpAddress?.Split(":")[0]
|
||||
};
|
||||
|
||||
if (!GPlayerWeaponsInfo.TryGetValue(player.Slot, out var weaponInfos))
|
||||
return HookResult.Continue;
|
||||
|
||||
foreach (var weapon in weaponInfos)
|
||||
{
|
||||
var weaponDefIndex = weapon.Key;
|
||||
var weaponInfo = weapon.Value;
|
||||
|
||||
if (weaponInfo.StatTrak)
|
||||
{
|
||||
if (WeaponSync != null)
|
||||
{
|
||||
|
||||
_ = Task.Run(async () => await WeaponSync.SyncStatTrakToDatabase(playerInfo, weaponInfo.StatTrakCount, weaponDefIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return HookResult.Continue;
|
||||
}
|
||||
|
||||
public async Task SyncStatTrakForPlayer(PlayerInfo playerInfo, ConcurrentDictionary<int, WeaponInfo> weaponInfos)
|
||||
{
|
||||
if (WeaponSync == null || weaponInfos == null || weaponInfos.Count == 0) return;
|
||||
|
||||
var statTrakWeapons = weaponInfos
|
||||
.Where(w => w.Value.StatTrak)
|
||||
.ToDictionary(w => w.Key, w => w.Value.StatTrakCount);
|
||||
|
||||
if (statTrakWeapons.Count == 0) return;
|
||||
|
||||
try
|
||||
{
|
||||
await WeaponSync.SyncStatTrakToDatabase(playerInfo, statTrakWeapons);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Utility.Log($"Error syncing StatTrak for player {playerInfo.SteamId}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OnMapStart(string mapName)
|
||||
{
|
||||
|
||||
@@ -373,34 +373,37 @@ namespace WeaponPaints
|
||||
}
|
||||
}
|
||||
|
||||
internal async Task SyncStatTrakToDatabase(PlayerInfo player, int StatTrakCount, int defindex)
|
||||
internal async Task SyncStatTrakToDatabase(PlayerInfo player, Dictionary<int, int> weaponStatTrakCounts)
|
||||
{
|
||||
if (string.IsNullOrEmpty(player.SteamId) || !WeaponPaints.GPlayerWeaponsInfo.TryGetValue(player.Slot, out var weaponsInfo))
|
||||
if (string.IsNullOrEmpty(player.SteamId) || weaponStatTrakCounts == null || weaponStatTrakCounts.Count == 0)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
await using var connection = await _database.GetConnectionAsync();
|
||||
await using var transaction = await connection.BeginTransactionAsync();
|
||||
|
||||
const string queryCheckExistence = "SELECT COUNT(*) FROM `wp_player_skins` WHERE `steamid` = @steamid AND `weapon_defindex` = @weaponDefIndex";
|
||||
foreach (var weapon in weaponStatTrakCounts)
|
||||
{
|
||||
int defindex = weapon.Key;
|
||||
int statTrakCount = weapon.Value;
|
||||
|
||||
var existingRecordCount = await connection.ExecuteScalarAsync<int>(queryCheckExistence, new { steamid = player.SteamId, weaponDefIndex = defindex });
|
||||
const string query = @"
|
||||
INSERT INTO `wp_player_skins` (`steamid`, `weapon_defindex`, `weapon_stattrak_count`)
|
||||
VALUES (@steamid, @weaponDefIndex, @StatTrakCount)
|
||||
ON DUPLICATE KEY UPDATE `weapon_stattrak_count` = @StatTrakCount";
|
||||
|
||||
string query = string.Empty;
|
||||
object? parameters = null;
|
||||
|
||||
if (existingRecordCount > 0)
|
||||
var parameters = new
|
||||
{
|
||||
query = "UPDATE `wp_player_skins` SET `weapon_stattrak_count` = @StatTrakCount WHERE `steamid` = @steamid AND `weapon_defindex` = @weaponDefIndex";
|
||||
parameters = new { steamid = player.SteamId, weaponDefIndex = defindex, StatTrakCount };
|
||||
}
|
||||
else
|
||||
{
|
||||
query = "INSERT INTO `wp_player_skins` (`steamid`, `weapon_defindex`, `weapon_stattrak_count`) VALUES (@steamid, @weaponDefIndex, @StatTrakCount)";
|
||||
parameters = new { steamid = player.SteamId, weaponDefIndex = defindex, StatTrakCount };
|
||||
}
|
||||
steamid = player.SteamId,
|
||||
weaponDefIndex = defindex,
|
||||
StatTrakCount = statTrakCount
|
||||
};
|
||||
|
||||
await connection.ExecuteAsync(query, parameters);
|
||||
await connection.ExecuteAsync(query, parameters, transaction);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user