Repository navigation
Expand file tree
/
Copy pathCS2-Banner.cs
More file actions
154 lines (123 loc) · 3.97 KB
/
Copy pathCS2-Banner.cs
File metadata and controls
154 lines (123 loc) · 3.97 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using CounterStrikeSharp.API;
using CounterStrikeSharp.API.Core;
using CounterStrikeSharp.API.Modules.Utils;
namespace CS2Banner;
public class CS2Banner : BasePlugin
{
public override string ModuleName => "CS2 Banner Plugin";
public override string ModuleVersion => "1.0.0";
public override string ModuleAuthor => "M1K@c";
private PluginConfig Config = new();
private class User
{
public bool HtmlPrint;
public string Message = "";
public int PrintTime;
}
private readonly User?[] _users = new User?[64];
private bool _showImage = false;
private string? _currentImage = null;
private readonly Random _random = new();
public override void Load(bool hotReload)
{
LoadConfigFile();
RegisterEventHandler<EventPlayerConnectFull>((@event, info) =>
{
var player = @event.Userid;
if (player == null || !player.IsValid || player.IsBot)
return HookResult.Continue;
_users[player.Slot] = new User();
if (!string.IsNullOrEmpty(Config.WelcomeImageUrl))
{
SetHtml(player, $"<img src='{Config.WelcomeImageUrl}' />");
}
return HookResult.Continue;
});
StartImageLoop();
RegisterListener<Listeners.OnTick>(OnTick);
}
private void LoadConfigFile()
{
string path = Path.Combine(ModuleDirectory, "CS2-Banner.json");
if (!File.Exists(path))
{
var json = JsonSerializer.Serialize(Config, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(path, json);
return;
}
string content = File.ReadAllText(path);
var cfg = JsonSerializer.Deserialize<PluginConfig>(content);
if (cfg != null)
Config = cfg;
}
private void StartImageLoop()
{
if (Config.ImageUrls.Count == 0)
return;
ScheduleNextImage();
}
private void ScheduleNextImage()
{
AddTimer(Config.Cooldown, () =>
{
_currentImage = Config.ImageUrls[_random.Next(Config.ImageUrls.Count)];
_showImage = true;
AddTimer(Config.ShowTime, () =>
{
_showImage = false;
ScheduleNextImage();
});
});
}
private void SetHtml(CCSPlayerController player, string message)
{
var user = _users[player.Slot];
if (user == null)
{
_users[player.Slot] = new User();
user = _users[player.Slot];
}
user!.HtmlPrint = true;
user.PrintTime = 0;
user.Message = message;
}
private void OnTick()
{
foreach (var player in Utilities.GetPlayers())
{
if (player == null || !player.IsValid || player.IsBot)
continue;
var pawn = player.Pawn?.Value;
bool alive = pawn != null && pawn.IsValid &&
pawn.LifeState == (byte)LifeState_t.LIFE_ALIVE;
if (_showImage && _currentImage != null)
{
if (!(Config.ShowOnlyToDead && alive))
{
player.PrintToCenterHtml($"<img src='{_currentImage}' />");
}
}
var user = _users[player.Slot];
if (user is not null && user.HtmlPrint)
{
if (!(Config.ShowOnlyToDead && alive))
{
int durationTicks = Config.ShowTime * 64;
if (user.PrintTime < durationTicks)
{
player.PrintToCenterHtml(user.Message);
user.PrintTime++;
}
else
{
user.HtmlPrint = false;
}
}
}
}
}
}