Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit aaa8acb

Browse files
committed
Publish
1 parent 6795d87 commit aaa8acb

File tree

6 files changed

+291
-18
lines changed

6 files changed

+291
-18
lines changed

README.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
<a name="readme-top"></a>
22

3-
![GitHub tag (with filter)](https://img.shields.io/github/v/tag/K4ryuu/Project_Template?style=for-the-badge&label=Version)
4-
![GitHub Repo stars](https://img.shields.io/github/stars/K4ryuu/Project_Template?style=for-the-badge)
5-
![GitHub issues](https://img.shields.io/github/issues/K4ryuu/Project_Template?style=for-the-badge)
6-
![GitHub](https://img.shields.io/github/license/K4ryuu/Project_Template?style=for-the-badge)
7-
![GitHub contributors](https://img.shields.io/github/contributors/K4ryuu/Project_Template?style=for-the-badge)
8-
![GitHub all releases](https://img.shields.io/github/downloads/K4ryuu/Project_Template/total?style=for-the-badge)
9-
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/K4ryuu/Project_Template/dev?style=for-the-badge)
3+
![GitHub tag (with filter)](https://img.shields.io/github/v/tag/K4ryuu/CS2_SteamRestrict?style=for-the-badge&label=Version)
4+
![GitHub Repo stars](https://img.shields.io/github/stars/K4ryuu/CS2_SteamRestrict?style=for-the-badge)
5+
![GitHub issues](https://img.shields.io/github/issues/K4ryuu/CS2_SteamRestrict?style=for-the-badge)
6+
![GitHub](https://img.shields.io/github/license/K4ryuu/CS2_SteamRestrict?style=for-the-badge)
7+
![GitHub contributors](https://img.shields.io/github/contributors/K4ryuu/CS2_SteamRestrict?style=for-the-badge)
8+
![GitHub all releases](https://img.shields.io/github/downloads/K4ryuu/CS2_SteamRestrict/total?style=for-the-badge)
9+
![GitHub last commit (branch)](https://img.shields.io/github/last-commit/K4ryuu/CS2_SteamRestrict/dev?style=for-the-badge)
1010

1111
<!-- PROJECT LOGO -->
1212
<br />
1313
<div align="center">
1414
<h1 align="center">K4ryuu</h1>
15-
<h3 align="center">PLACE OF TITLE</h3>
15+
<h3 align="center">CS2 Chat Relay</h3>
1616

1717
<p align="center">
1818
An awesome CS2 server addon created with <a href="https://github.com/roflmuffin/CounterStrikeSharp"><strong>CounterStrikeSharp</strong></a>
1919
<br />
2020
<br />
21-
<a href="https://github.com/K4ryuu/Project_Template/releases">Download</a>
21+
<a href="https://github.com/K4ryuu/CS2_SteamRestrict/releases">Download</a>
2222
·
23-
<a href="https://github.com/K4ryuu/Project_Template/issues/new?assignees=K4ryuu&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D">Report Bug</a>
23+
<a href="https://github.com/K4ryuu/CS2_SteamRestrict/issues/new?assignees=K4ryuu&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D">Report Bug</a>
2424
·
25-
<a href="https://github.com/K4ryuu/Project_Template/issues/new?assignees=K4ryuu&labels=enhancement&projects=&template=feature_request.md&title=%5BREQ%5D">Request Feature</a>
25+
<a href="https://github.com/K4ryuu/CS2_SteamRestrict/issues/new?assignees=K4ryuu&labels=enhancement&projects=&template=feature_request.md&title=%5BREQ%5D">Request Feature</a>
2626
</p>
2727
</div>
2828

@@ -62,7 +62,7 @@
6262

6363
## About The Project
6464

65-
DESCRIPTION HERE
65+
Enhance your game server's control with this plugin. Fetch and analyze player data from their Steam profiles, enforce minimum CS:GO playtime and Steam level requirements, and customize restrictions for Prime and non-Prime players. Flexible and powerful server management tool.
6666

6767
<p align="right">(<a href="#readme-top">back to top</a>)</p>
6868

@@ -71,7 +71,6 @@ DESCRIPTION HERE
7171
To use this server addon, you'll need the following dependencies installed:
7272

7373
- [**CounterStrikeSharp**](https://github.com/roflmuffin/CounterStrikeSharp/actions/workflows/cmake-single-platform.yml): CounterStrikeSharp allows you to write server plugins in C# for Counter-Strike 2/Source2/CS2
74-
- **MySQL Database (Version 5.2 or higher):** This project requires a MySQL database to store and manage data. You can host your own MySQL server or use a third-party hosting service. Make sure it's at least version 5.2 or higher.
7574

7675
<p align="right">(<a href="#readme-top">back to top</a>)</p>
7776

@@ -106,13 +105,12 @@ Before you begin, ensure you have the following prerequisites:
106105

107106
- A working CS2 (Counter-Strike 2) server.
108107
- CounterStrikeSharp is up to date and is running on your server.
109-
- A compatible MySQL database (Version 5.2 or higher) set up and configured.
110108

111109
<p align="right">(<a href="#readme-top">back to top</a>)</p>
112110

113111
### Installation
114112

115-
1. **Download the Addon:** Start by downloading the addon from the [GitHub Releases Page](https://github.com/K4ryuu/Project_Template/releases). Choose the latest release version.
113+
1. **Download the Addon:** Start by downloading the addon from the [GitHub Releases Page](https://github.com/K4ryuu/CS2_SteamRestrict/releases). Choose the latest release version.
116114

117115
2. **Extract the Addon:** After downloading, extract the contents of the addon to the counterstrikesharp/plugins directory on your server. Inside the plugins folder, you should have a folder named exactly as the project dll. From the releases, you find it pre zipped with the correct name.
118116

@@ -128,21 +126,21 @@ The addon provides several commands and console variables (convars) to customize
128126

129127
### Commands
130128

131-
- **Command 1:** Describe the first command and how to use it.
129+
- None
132130

133131
<p align="right">(<a href="#readme-top">back to top</a>)</p>
134132

135133
### Console Variables (Convars)
136134

137-
- **Convar 1:** Describe the first convar and how to set its value.
135+
- None
138136

139137
<p align="right">(<a href="#readme-top">back to top</a>)</p>
140138

141139
<!-- ROADMAP -->
142140

143141
## Roadmap
144142

145-
- [ ] No plans for now
143+
- [ ] No plans yet
146144

147145
<p align="right">(<a href="#readme-top">back to top</a>)</p>
148146

src/.gitkeep

Whitespace-only changes.

src/CFG.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System.Text.Json;
2+
3+
internal class CFG
4+
{
5+
public static Config config = new();
6+
7+
public void CheckConfig(string moduleDirectory)
8+
{
9+
string path = Path.Join(moduleDirectory, "config.json");
10+
11+
if (!File.Exists(path))
12+
{
13+
CreateAndWriteFile(path);
14+
}
15+
16+
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
17+
using (StreamReader sr = new StreamReader(fs))
18+
{
19+
// Deserialize the JSON from the file and load the configuration.
20+
config = JsonSerializer.Deserialize<Config>(sr.ReadToEnd())!;
21+
}
22+
}
23+
24+
private static void CreateAndWriteFile(string path)
25+
{
26+
27+
using (FileStream fs = File.Create(path))
28+
{
29+
// File is created, and fs will automatically be disposed when the using block exits.
30+
}
31+
32+
Console.WriteLine($"File created: {File.Exists(path)}");
33+
34+
Config config = new Config
35+
{
36+
SteamWebAPI = "-",
37+
};
38+
39+
// Serialize the config object to JSON and write it to the file.
40+
string jsonConfig = JsonSerializer.Serialize(config, new JsonSerializerOptions()
41+
{
42+
WriteIndented = true
43+
});
44+
File.WriteAllText(path, jsonConfig);
45+
}
46+
}
47+
48+
internal class Config
49+
{
50+
public string? SteamWebAPI { get; set; }
51+
public int MinimumHourNonPrime { get; set; }
52+
public int MinimumHourPrime { get; set; }
53+
public int MinimumLevelNonPrime { get; set; }
54+
public int MinimumLevelPrime { get; set; }
55+
public bool BlockPrivateProfile { get; set; }
56+
public bool BlockNonPrime { get; set; }
57+
}

src/CounterStrikeSharp.API.dll

959 KB
Binary file not shown.

src/K4ryuuSteamRestrict.cs

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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+
}

src/K4ryuuSteamRestrict.csproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<Reference Include="CounterStrikeSharp.API" />
10+
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
11+
</ItemGroup>
12+
</Project>

0 commit comments

Comments
 (0)