mirror of
https://github.com/Nereziel/cs2-WeaponPaints.git
synced 2026-02-17 18:39:07 +00:00
221 lines
6.7 KiB
C#
221 lines
6.7 KiB
C#
using Dapper;
|
|
using Newtonsoft.Json.Linq;
|
|
using System.Collections.Concurrent;
|
|
|
|
namespace WeaponPaints
|
|
{
|
|
internal class WeaponSynchronization
|
|
{
|
|
private readonly WeaponPaintsConfig _config;
|
|
private readonly Database _database;
|
|
private readonly Uri _globalShareApi;
|
|
private readonly int _globalShareServerId;
|
|
|
|
internal WeaponSynchronization(Database database, WeaponPaintsConfig config, Uri globalShareApi, int globalShareServerId)
|
|
{
|
|
_database = database;
|
|
_config = config;
|
|
_globalShareApi = globalShareApi;
|
|
_globalShareServerId = globalShareServerId;
|
|
}
|
|
|
|
internal async Task GetKnifeFromDatabase(PlayerInfo player)
|
|
{
|
|
if (!_config.Additional.KnifeEnabled) return;
|
|
if (player.SteamId == null || player.Index == 0) return;
|
|
try
|
|
{
|
|
if (_config.GlobalShare)
|
|
{
|
|
var values = new Dictionary<string, string>
|
|
{
|
|
{ "server_id", _globalShareServerId.ToString() },
|
|
{ "steamid", player.SteamId },
|
|
{ "knife", "1" }
|
|
};
|
|
|
|
UriBuilder builder = new UriBuilder(_globalShareApi);
|
|
builder.Query = string.Join("&", values.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}"));
|
|
|
|
using (var httpClient = new HttpClient())
|
|
{
|
|
httpClient.BaseAddress = _globalShareApi;
|
|
var formContent = new FormUrlEncodedContent(values);
|
|
HttpResponseMessage response = await httpClient.GetAsync(builder.Uri);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string result = await response.Content.ReadAsStringAsync();
|
|
if (!string.IsNullOrEmpty(result))
|
|
{
|
|
WeaponPaints.g_playersKnife[player.Index] = result;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
await using (var connection = await _database.GetConnectionAsync())
|
|
{
|
|
string query = "SELECT `knife` FROM `wp_player_knife` WHERE `steamid` = @steamid";
|
|
string? playerKnife = await connection.QueryFirstOrDefaultAsync<string>(query, new { steamid = player.SteamId });
|
|
|
|
if (playerKnife != null)
|
|
{
|
|
WeaponPaints.g_playersKnife[player.Index] = playerKnife;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Utility.Log(e.Message);
|
|
return;
|
|
}
|
|
}
|
|
|
|
internal async Task GetWeaponPaintsFromDatabase(PlayerInfo player)
|
|
{
|
|
if (!_config.Additional.SkinEnabled) return;
|
|
if (player.SteamId == null || player.Index == 0) return;
|
|
|
|
if (!WeaponPaints.gPlayerWeaponsInfo.TryGetValue(player.Index, out _))
|
|
{
|
|
WeaponPaints.gPlayerWeaponsInfo[player.Index] = new ConcurrentDictionary<int, WeaponInfo>();
|
|
}
|
|
try
|
|
{
|
|
if (_config.GlobalShare)
|
|
{
|
|
var values = new Dictionary<string, string>
|
|
{
|
|
{ "server_id", _globalShareServerId.ToString() },
|
|
{ "steamid", player.SteamId },
|
|
{ "skins", "1" }
|
|
};
|
|
UriBuilder builder = new UriBuilder(_globalShareApi);
|
|
builder.Query = string.Join("&", values.Select(p => $"{Uri.EscapeDataString(p.Key)}={Uri.EscapeDataString(p.Value)}"));
|
|
|
|
using (var httpClient = new HttpClient())
|
|
{
|
|
httpClient.BaseAddress = _globalShareApi;
|
|
var formContent = new FormUrlEncodedContent(values);
|
|
HttpResponseMessage response = await httpClient.GetAsync(builder.Uri);
|
|
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
string responseBody = await response.Content.ReadAsStringAsync();
|
|
JArray jsonArray = JArray.Parse(responseBody);
|
|
if (jsonArray != null && jsonArray.Count > 0)
|
|
{
|
|
foreach (var weapon in jsonArray)
|
|
{
|
|
int? weaponDefIndex = weapon["weapon_defindex"]?.Value<int>();
|
|
int? weaponPaintId = weapon["weapon_paint_id"]?.Value<int>();
|
|
float? weaponWear = weapon["weapon_wear"]?.Value<float>();
|
|
int? weaponSeed = weapon["weapon_seed"]?.Value<int>();
|
|
|
|
if (weaponDefIndex != null && weaponPaintId != null && weaponWear != null && weaponSeed != null)
|
|
{
|
|
WeaponInfo weaponInfo = new WeaponInfo
|
|
{
|
|
Paint = weaponPaintId.Value,
|
|
Seed = weaponSeed.Value,
|
|
Wear = weaponWear.Value
|
|
};
|
|
WeaponPaints.gPlayerWeaponsInfo[player.Index][weaponDefIndex.Value] = weaponInfo;
|
|
}
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
await using (var connection = await _database.GetConnectionAsync())
|
|
{
|
|
string query = "SELECT * FROM `wp_player_skins` WHERE `steamid` = @steamid";
|
|
var playerSkins = await connection.QueryAsync(query, new { steamid = player.SteamId });
|
|
|
|
foreach (var row in playerSkins)
|
|
{
|
|
int weaponDefIndex = row.weapon_defindex ?? default;
|
|
int weaponPaintId = row.weapon_paint_id ?? default;
|
|
float weaponWear = row.weapon_wear ?? default;
|
|
int weaponSeed = row.weapon_seed ?? default;
|
|
|
|
WeaponInfo weaponInfo = new WeaponInfo
|
|
{
|
|
Paint = weaponPaintId,
|
|
Seed = weaponSeed,
|
|
Wear = weaponWear
|
|
};
|
|
WeaponPaints.gPlayerWeaponsInfo[player.Index][weaponDefIndex] = weaponInfo;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Utility.Log(e.Message);
|
|
return;
|
|
}
|
|
}
|
|
|
|
internal async Task SyncKnifeToDatabase(PlayerInfo player, string knife)
|
|
{
|
|
if (!_config.Additional.KnifeEnabled) return;
|
|
if (player.SteamId == null || player.Index == 0) return;
|
|
|
|
try
|
|
{
|
|
await using var connection = await _database.GetConnectionAsync();
|
|
string query = "INSERT INTO `wp_player_knife` (`steamid`, `knife`) VALUES(@steamid, @newKnife) ON DUPLICATE KEY UPDATE `knife` = @newKnife";
|
|
await connection.ExecuteAsync(query, new { steamid = player.SteamId, newKnife = knife });
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Utility.Log(e.Message);
|
|
}
|
|
}
|
|
|
|
internal async Task SyncWeaponPaintsToDatabase(PlayerInfo player)
|
|
{
|
|
if (player == null || player.Index <= 0 || player.SteamId == null) return;
|
|
|
|
await using var connection = await _database.GetConnectionAsync();
|
|
|
|
if (!WeaponPaints.gPlayerWeaponsInfo.ContainsKey(player.Index))
|
|
return;
|
|
|
|
foreach (var weaponInfoPair in WeaponPaints.gPlayerWeaponsInfo[player.Index])
|
|
{
|
|
int weaponDefIndex = weaponInfoPair.Key;
|
|
WeaponInfo weaponInfo = weaponInfoPair.Value;
|
|
|
|
int paintId = weaponInfo.Paint;
|
|
float wear = weaponInfo.Wear;
|
|
int seed = weaponInfo.Seed;
|
|
|
|
string updateSql = "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 updateParams = new { steamid = player.SteamId, weaponDefIndex, paintId, wear, seed };
|
|
await connection.ExecuteAsync(updateSql, updateParams);
|
|
}
|
|
}
|
|
}
|
|
} |