Skip to content

Commit

Permalink
feat: Trade and Game ban filters
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Nov 10, 2023
1 parent a73182c commit b0637a9
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/CFG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,6 @@ internal class Config
public int MinimumLevelPrime { get; set; }
public bool BlockPrivateProfile { get; set; }
public bool BlockNonPrime { get; set; }
public bool BlockTradeBanned { get; set; }
public bool BlockGameBanned { get; set; }
}
84 changes: 78 additions & 6 deletions src/K4ryuuSteamRestrict.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using System.Text;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Core.Attributes;
using CounterStrikeSharp.API.Core.Attributes.Registration;
using CounterStrikeSharp.API.Modules.Utils;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using CounterStrikeSharp.API;

Expand All @@ -19,10 +15,12 @@ public class SteamUserInfo
public int CSGOPlaytime { get; set; }
public bool IsPrivate { get; set; }
public bool HasPrime { get; set; }
public bool IsTradeBanned { get; set; }
public bool IsGameBanned { get; set; }
}

public override string ModuleName => "Steam Restrict";
public override string ModuleVersion => "1.0.0";
public override string ModuleVersion => "1.1.0";
public override string ModuleAuthor => "K4ryuu";

public override void Load(bool hotReload)
Expand Down Expand Up @@ -103,6 +101,32 @@ private async Task<SteamUserInfo> FetchSteamUserInfo(CCSPlayerController player)
{
Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\"");
}

string tradeBanUrl = $"https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key={steamWebAPIKey}&steamids={steamId}";
HttpResponseMessage tradeBanResponse = await httpClient.GetAsync(tradeBanUrl);

if (tradeBanResponse.IsSuccessStatusCode)
{
string tradeBanJson = await tradeBanResponse.Content.ReadAsStringAsync();
ParseTradeBanStatus(tradeBanJson, userInfo);
}
else
{
userInfo.IsTradeBanned = false;
}

string gameBanUrl = $"https://api.steampowered.com/ISteamUser/GetUserGameBan/v1/?key={steamWebAPIKey}&steamids={steamId}";
HttpResponseMessage gameBanResponse = await httpClient.GetAsync(gameBanUrl);

if (gameBanResponse.IsSuccessStatusCode)
{
string gameBanJson = await gameBanResponse.Content.ReadAsStringAsync();
ParseGameBanStatus(gameBanJson, userInfo);
}
else
{
userInfo.IsGameBanned = false;
}
}

return userInfo;
Expand Down Expand Up @@ -134,6 +158,16 @@ private bool IsRestrictionViolated(SteamUserInfo userInfo)
isViolated = true;
}

if (CFG.config.BlockTradeBanned && userInfo.IsTradeBanned)
{
isViolated = true;
}

if (CFG.config.BlockGameBanned && userInfo.IsGameBanned)
{
isViolated = true;
}

return isViolated;
}

Expand Down Expand Up @@ -202,5 +236,43 @@ private void ParseSteamUserInfo(string json, SteamUserInfo userInfo)
Console.WriteLine($"Error parsing Steam user info: {ex.Message}");
}
}

private void ParseTradeBanStatus(string json, SteamUserInfo userInfo)
{
try
{
JObject data = JObject.Parse(json);
JArray playerBans = (data["players"] as JArray)!;

if (playerBans != null && playerBans.Count > 0)
{
var playerBan = playerBans[0];
userInfo.IsTradeBanned = (bool)playerBan["CommunityBanned"]!;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing trade ban status: {ex.Message}");
}
}

private void ParseGameBanStatus(string json, SteamUserInfo userInfo)
{
try
{
JObject data = JObject.Parse(json);
JArray userGameBans = (data["players"] as JArray)!;

if (userGameBans != null && userGameBans.Count > 0)
{
var userGameBan = userGameBans[0];
userInfo.IsGameBanned = (bool)userGameBan["IsGameBanned"]!;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error parsing game ban status: {ex.Message}");
}
}
}
}

0 comments on commit b0637a9

Please sign in to comment.