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