|
| 1 | +using System.Text; |
| 2 | +using CounterStrikeSharp.API.Core; |
| 3 | +using CounterStrikeSharp.API.Core.Attributes; |
| 4 | +using CounterStrikeSharp.API.Core.Attributes.Registration; |
| 5 | +using CounterStrikeSharp.API.Modules.Utils; |
| 6 | +using System.Net.Http; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using CounterStrikeSharp.API; |
| 10 | + |
| 11 | +namespace K4ryuuSteamRestrict |
| 12 | +{ |
| 13 | + [MinimumApiVersion(16)] |
| 14 | + public class SteamRestrictPlugin : BasePlugin |
| 15 | + { |
| 16 | + public class SteamUserInfo |
| 17 | + { |
| 18 | + public int SteamLevel { get; set; } |
| 19 | + public int CSGOPlaytime { get; set; } |
| 20 | + public bool IsPrivate { get; set; } |
| 21 | + public bool HasPrime { get; set; } |
| 22 | + } |
| 23 | + |
| 24 | + public override string ModuleName => "Steam Restrict"; |
| 25 | + public override string ModuleVersion => "1.0.0"; |
| 26 | + public override string ModuleAuthor => "K4ryuu"; |
| 27 | + |
| 28 | + public override void Load(bool hotReload) |
| 29 | + { |
| 30 | + new CFG().CheckConfig(ModuleDirectory); |
| 31 | + } |
| 32 | + |
| 33 | + [GameEventHandler] |
| 34 | + public HookResult OnClientConnect(EventPlayerConnectFull @event, GameEventInfo info) |
| 35 | + { |
| 36 | + CCSPlayerController player = @event.Userid; |
| 37 | + |
| 38 | + if (player == null || !player.IsValid || player.IsBot || CFG.config.SteamWebAPI == "-") |
| 39 | + return HookResult.Continue; |
| 40 | + |
| 41 | + _ = FetchSteamUserInfo(player); |
| 42 | + |
| 43 | + |
| 44 | + return HookResult.Continue; |
| 45 | + } |
| 46 | + |
| 47 | + private async Task<SteamUserInfo> FetchSteamUserInfo(CCSPlayerController player) |
| 48 | + { |
| 49 | + SteamUserInfo userInfo = new SteamUserInfo(); |
| 50 | + |
| 51 | + using (HttpClient httpClient = new HttpClient()) |
| 52 | + { |
| 53 | + string steamId = player.SteamID.ToString(); |
| 54 | + string steamWebAPIKey = CFG.config.SteamWebAPI!; |
| 55 | + |
| 56 | + string gamesUrl = $"https://api.steampowered.com/IPlayerService/GetOwnedGames/v1/?key={steamWebAPIKey}&steamid={steamId}&format=json"; |
| 57 | + HttpResponseMessage gamesResponse = await httpClient.GetAsync(gamesUrl); |
| 58 | + |
| 59 | + if (gamesResponse.IsSuccessStatusCode) |
| 60 | + { |
| 61 | + string gamesJson = await gamesResponse.Content.ReadAsStringAsync(); |
| 62 | + userInfo.CSGOPlaytime = ParseCS2Playtime(gamesJson) / 60; |
| 63 | + |
| 64 | + JArray games = (JObject.Parse(gamesJson)["response"]!["games"] as JArray)!; |
| 65 | + bool hasPrime = games.Any(game => (int)game["appid"]! == 54029); |
| 66 | + userInfo.HasPrime = hasPrime; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + userInfo.HasPrime = false; |
| 71 | + userInfo.CSGOPlaytime = 0; |
| 72 | + } |
| 73 | + |
| 74 | + // Fetch Steam level |
| 75 | + string levelUrl = $"http://api.steampowered.com/IPlayerService/GetSteamLevel/v1/?key={steamWebAPIKey}&steamid={steamId}"; |
| 76 | + HttpResponseMessage levelResponse = await httpClient.GetAsync(levelUrl); |
| 77 | + |
| 78 | + if (levelResponse.IsSuccessStatusCode) |
| 79 | + { |
| 80 | + string levelJson = await levelResponse.Content.ReadAsStringAsync(); |
| 81 | + userInfo.SteamLevel = ParseSteamLevel(levelJson); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + userInfo.SteamLevel = 0; |
| 86 | + } |
| 87 | + |
| 88 | + // Fetch other user information |
| 89 | + string userUrl = $"https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key={steamWebAPIKey}&steamids={steamId}"; |
| 90 | + HttpResponseMessage userResponse = await httpClient.GetAsync(userUrl); |
| 91 | + |
| 92 | + if (userResponse.IsSuccessStatusCode) |
| 93 | + { |
| 94 | + string userJson = await userResponse.Content.ReadAsStringAsync(); |
| 95 | + ParseSteamUserInfo(userJson, userInfo); |
| 96 | + } |
| 97 | + else |
| 98 | + { |
| 99 | + userInfo.IsPrivate = false; |
| 100 | + } |
| 101 | + |
| 102 | + if (IsRestrictionViolated(userInfo)) |
| 103 | + { |
| 104 | + Server.ExecuteCommand($"kickid {player.UserId} \"You have been kicked for not meeting the minimum requirements.\""); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + return userInfo; |
| 109 | + } |
| 110 | + |
| 111 | + private bool IsRestrictionViolated(SteamUserInfo userInfo) |
| 112 | + { |
| 113 | + bool isViolated = false; |
| 114 | + |
| 115 | + if (userInfo.HasPrime) |
| 116 | + { |
| 117 | + if (CFG.config.MinimumHourPrime != -1 && userInfo.CSGOPlaytime < CFG.config.MinimumHourPrime |
| 118 | + || CFG.config.MinimumLevelPrime != -1 && userInfo.SteamLevel < CFG.config.MinimumLevelPrime) |
| 119 | + { |
| 120 | + isViolated = true; |
| 121 | + } |
| 122 | + } |
| 123 | + else |
| 124 | + { |
| 125 | + if (CFG.config.MinimumHourNonPrime != -1 && userInfo.CSGOPlaytime < CFG.config.MinimumHourNonPrime |
| 126 | + || CFG.config.MinimumLevelNonPrime != -1 && userInfo.SteamLevel < CFG.config.MinimumLevelNonPrime) |
| 127 | + { |
| 128 | + isViolated = true; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if (CFG.config.BlockPrivateProfile && userInfo.IsPrivate) |
| 133 | + { |
| 134 | + isViolated = true; |
| 135 | + } |
| 136 | + |
| 137 | + return isViolated; |
| 138 | + } |
| 139 | + |
| 140 | + private int ParseCS2Playtime(string json) |
| 141 | + { |
| 142 | + int csPlaytime = 0; |
| 143 | + |
| 144 | + try |
| 145 | + { |
| 146 | + JObject data = JObject.Parse(json); |
| 147 | + JArray games = (data["response"]!["games"] as JArray)!; |
| 148 | + |
| 149 | + if (games != null) |
| 150 | + { |
| 151 | + foreach (var game in games) |
| 152 | + { |
| 153 | + int appId = (int)game["appid"]!; |
| 154 | + if (appId == 730) |
| 155 | + { |
| 156 | + csPlaytime = (int)game["playtime_forever"]!; |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + catch (Exception ex) |
| 162 | + { |
| 163 | + Console.WriteLine($"Error parsing CS:GO playtime: {ex.Message}"); |
| 164 | + } |
| 165 | + |
| 166 | + return csPlaytime; |
| 167 | + } |
| 168 | + |
| 169 | + private int ParseSteamLevel(string json) |
| 170 | + { |
| 171 | + int steamLevel = 0; |
| 172 | + |
| 173 | + try |
| 174 | + { |
| 175 | + JObject data = JObject.Parse(json); |
| 176 | + steamLevel = (int)data["response"]!["player_level"]!; |
| 177 | + } |
| 178 | + catch (Exception ex) |
| 179 | + { |
| 180 | + Console.WriteLine($"Error parsing Steam level: {ex.Message}"); |
| 181 | + } |
| 182 | + |
| 183 | + return steamLevel; |
| 184 | + } |
| 185 | + |
| 186 | + private void ParseSteamUserInfo(string json, SteamUserInfo userInfo) |
| 187 | + { |
| 188 | + try |
| 189 | + { |
| 190 | + JObject data = JObject.Parse(json); |
| 191 | + JArray players = (data["response"]!["players"] as JArray)!; |
| 192 | + |
| 193 | + if (players != null && players.Count > 0) |
| 194 | + { |
| 195 | + var player = players[0]; |
| 196 | + |
| 197 | + userInfo.IsPrivate = (int)player["communityvisibilitystate"]! != 3; |
| 198 | + } |
| 199 | + } |
| 200 | + catch (Exception ex) |
| 201 | + { |
| 202 | + Console.WriteLine($"Error parsing Steam user info: {ex.Message}"); |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | +} |
0 commit comments