Skip to content

Commit 014a9c5

Browse files
authored
Add files via upload
1 parent 7d6ebaa commit 014a9c5

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Plugin.cs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using CounterStrikeSharp.API;
2+
using CounterStrikeSharp.API.Core;
3+
using System.Text.Json;
4+
using System.Text.Json.Serialization;
5+
6+
namespace RandomStartMap;
7+
8+
public class RandomStartMapConfig : BasePluginConfig
9+
{
10+
[JsonPropertyName("SteamApiKey")]
11+
public string SteamApiKey { get; set; } = "your_steam_api_key_here";
12+
13+
[JsonPropertyName("WorkshopCollectionId")]
14+
public string WorkshopCollectionId { get; set; } = "your_collection_id_here";
15+
16+
[JsonPropertyName("ChangeDelay")]
17+
public float ChangeDelay { get; set; } = 2.0f;
18+
}
19+
20+
public class RandomStartMap : BasePlugin, IPluginConfig<RandomStartMapConfig>
21+
{
22+
public override string ModuleName => "RandomStartMap";
23+
public override string ModuleVersion => "1.0";
24+
public override string ModuleAuthor => "RexusOhm";
25+
26+
public RandomStartMapConfig Config { get; set; } = new();
27+
private readonly HttpClient _httpClient = new();
28+
private bool _isFirstLoad = true;
29+
30+
public void OnConfigParsed(RandomStartMapConfig config)
31+
{
32+
Config = config;
33+
}
34+
public override void Load(bool hotReload)
35+
{
36+
_isFirstLoad = !hotReload;
37+
}
38+
39+
public override void OnAllPluginsLoaded(bool hotReload)
40+
{
41+
if (!_isFirstLoad) return;
42+
43+
AddTimer(Config.ChangeDelay, async () => {
44+
await LoadAndChangeToRandomMapAsync();
45+
});
46+
}
47+
48+
private async Task LoadAndChangeToRandomMapAsync()
49+
{
50+
Console.WriteLine("[RandomStartMap] Loading workshop collection...");
51+
52+
try
53+
{
54+
var collectionFormData = new Dictionary<string, string>
55+
{
56+
{ "collectioncount", "1" },
57+
{ "publishedfileids[0]", Config.WorkshopCollectionId }
58+
};
59+
60+
var collectionResponse = await _httpClient.PostAsync(
61+
$"https://api.steampowered.com/ISteamRemoteStorage/GetCollectionDetails/v1/?key={Config.SteamApiKey}",
62+
new FormUrlEncodedContent(collectionFormData));
63+
64+
collectionResponse.EnsureSuccessStatusCode();
65+
var collectionJson = await collectionResponse.Content.ReadAsStringAsync();
66+
using var collectionDoc = JsonDocument.Parse(collectionJson);
67+
68+
var mapIds = collectionDoc.RootElement
69+
.GetProperty("response")
70+
.GetProperty("collectiondetails")[0]
71+
.GetProperty("children")
72+
.EnumerateArray()
73+
.Select(item => item.GetProperty("publishedfileid").GetString()!)
74+
.ToList();
75+
76+
if (mapIds.Count == 0)
77+
{
78+
Console.WriteLine("[RandomStartMap] ERROR: No maps found in collection!");
79+
return;
80+
}
81+
82+
var random = new Random();
83+
string randomMapId = mapIds[random.Next(mapIds.Count)];
84+
85+
Console.WriteLine($"[RandomStartMap] Changing to random map from collection: {randomMapId}");
86+
87+
// Выполняем команду в главном потоке
88+
await Server.NextFrameAsync(() => {
89+
Server.ExecuteCommand($"host_workshop_map {randomMapId}");
90+
});
91+
}
92+
catch (Exception ex)
93+
{
94+
Console.WriteLine($"[RandomStartMap] ERROR: {ex.Message}");
95+
if (ex.InnerException != null)
96+
{
97+
Console.WriteLine($"[RandomStartMap] INNER ERROR: {ex.InnerException.Message}");
98+
}
99+
}
100+
}
101+
}

RandomStartMap.csproj

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

0 commit comments

Comments
 (0)