VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CenterHtml.cpp
Go to the documentation of this file.
6#include <string>
7#include <utility>
8
9namespace VoltMod
10{
11
13 : _messages(messages), _scheduler(scheduler), _timers(slots)
14{}
15
16void CenterHtml::Show(int slot, int refreshMs, std::function<std::string(int slot)> render)
17{
18 if (!IsValidSlot(slot) || !render || refreshMs <= 0)
19 {
20 return;
21 }
22
23 Stop(slot);
24
25 // _timers cancels it before `this` goes.
26 auto send = [this, slot, render = std::move(render), sent = std::string(), sentAt = int64_t{0}]() mutable {
27 std::string html = render(slot);
28 const int64_t now = Time::MonotonicMs();
29 if (html == sent && now - sentAt < CenterHtmlKeepAliveMs)
30 {
31 return;
32 }
33 _messages.SendCenterHtml(slot, html);
34 sent = std::move(html);
35 sentAt = now;
36 };
37 send();
38 _timers[slot] = _scheduler.Repeat(refreshMs, std::move(send));
39}
40
41void CenterHtml::Stop(int slot)
42{
43 if (!IsValidSlot(slot) || !_timers[slot])
44 {
45 return;
46 }
47 _timers[slot].Reset();
48 _messages.ClearCenterHtml(slot);
49}
50
52{
53 for (int slot = 0; slot < MaxPlayers; ++slot)
54 {
55 Stop(slot);
56 }
57}
58
59} // namespace VoltMod
CenterHtml(Messages &messages, Scheduler &scheduler, SlotEvents &slots)
void Show(int slot, int refreshMs, std::function< std::string(int slot)> render)
void Stop(int slot)
Send chat, center print, center HTML, and alert messages.
Definition Messages.hpp:30
One-shot delays and repeating timers, run on the game thread.
Definition Scheduler.hpp:18
"The occupant of this slot changed" - raised by PlayerManager, consumed below it.
static int64_t MonotonicMs()
Definition Durations.hpp:35
constexpr bool IsValidSlot(int slot)
Definition Slot.hpp:13
constexpr int64_t CenterHtmlKeepAliveMs
constexpr int MaxPlayers
Definition Slot.hpp:7