10#include <cstrike15_usermessages.pb.h>
11#include <engine2/igameeventsystem.h>
12#include <networksystem/inetworkmessages.h>
38 : _interfaces(interfaces), _entities(entities), _players(players), _events(events), _scheduler(scheduler)
40 _leaving = players.
Disconnected += [
this](
Player& player) { DropVoter(player.Slot()); };
43CRecipientFilter Vote::Recipients()
const
45 CRecipientFilter filter;
46 filter.MakeReliable();
47 for (
const Player* player : _players.
All())
51 filter.AddRecipient(player->Slot());
64 if (_running || !request.
Passed)
69 Ballot ballot{.Request = std::move(request)};
70 for (
const Player* player : _players.All())
72 if (!player->IsBot() &&
IsValidSlot(player->Slot()))
74 ballot.Waiting[player->Slot()] =
true;
85 controller.SetPotentialVotes(ballot.Tally.
Eligible);
86 controller.SetIsYesNoVote(
true);
88 controller.SetOnlyTeamToVote(
AllTeams);
92 Ballot& running = _running.emplace(std::move(ballot));
93 PublishCounts(running.Tally);
95 running.Timeout = _scheduler.Delay(running.Request.
DurationMs, [
this] { Finish(VoteEndReason::TimeUp); });
106 Ballot& ballot = *_running;
112 if (option ==
"option1")
117 else if (option ==
"option2")
127 ballot.Waiting[slot] =
false;
128 PublishCounts(ballot.Tally);
133void Vote::DropVoter(
int slot)
135 if (!_running || !
IsValidSlot(slot) || !_running->Waiting[slot])
141 _running->Waiting[slot] =
false;
142 --_running->Tally.Eligible;
143 PublishCounts(_running->Tally);
147void Vote::CloseIfAllVoted()
149 if (_running->Tally.Cast() < _running->Tally.Eligible)
169 Ballot ballot = std::move(*_running);
175 SendOutcome(ballot.Request, passed);
179 controller.SetActiveIssueIndex(
NoIssue);
182 if (ballot.Request.Finished)
184 ballot.Request.Finished(passed, reason);
188void Vote::PublishBallot(
int slot,
int option)
197 event->SetInt(
"vote_option", option);
199 event->SetPlayer(
"userid", CPlayerSlot(slot));
200 _events.FireEvent(event);
203void Vote::PublishCounts(
const VoteTally& tally)
212 event->SetInt(
"vote_option1", tally.Yes);
213 event->SetInt(
"vote_option2", tally.No);
214 event->SetInt(
"vote_option3", 0);
215 event->SetInt(
"vote_option4", 0);
216 event->SetInt(
"vote_option5", 0);
217 event->SetInt(
"potentialVotes", tally.Eligible);
218 _events.FireEvent(event);
221void Vote::SendStart(
const Ballot& ballot)
223 CRecipientFilter filter = Recipients();
225 auto* start = raw->ToPB<CCSUsrMsg_VoteStart>();
227 start->set_player_slot(ballot.Request.Caller);
228 start->set_vote_type(-1);
229 start->set_disp_str(ballot.Request.Title);
230 start->set_details_str(ballot.Request.Detail);
231 start->set_is_yes_no_vote(
true);
236void Vote::SendOutcome(
const VoteRequest& request,
bool passed)
239 auto& cached = passed ? _votePassType : _voteFailedType;
240 CRecipientFilter filter = Recipients();
243 [&request, passed](CNetMessage* raw) {
246 auto* pass = raw->ToPB<CCSUsrMsg_VotePass>();
248 pass->set_vote_type(-1);
249 pass->set_disp_str(request.Title);
250 pass->set_details_str(request.Detail);
254 auto* fail = raw->ToPB<CCSUsrMsg_VoteFailed>();
A player's identity behind a slot: name, money, team, scoreboard row. It survives death and respawn; ...
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
Creates, fires and listens to game events.
The roster: every connected player, and the signals for the connection lifecycle.
std::span< Player *const > All() const
Event< Player & > Disconnected
A player is leaving. Raised while they are still in the roster, so the handler can read their identit...
One connected player, owned by PlayerManager for the length of the connection.
One-shot delays and repeating timers, run on the game thread.
Vote(Interfaces &interfaces, EntitySystem &entities, PlayerManager &players, GameEvents &events, Scheduler &scheduler)
bool TryCastBallot(int slot, std::string_view option)
bool Start(VoteRequest request)
void End(VoteEndReason reason)
bool PostUserMessage(Interfaces &interfaces, NetMessageInfo_t *&cached, std::string_view partialName, IRecipientFilter &filter, const std::function< bool(CNetMessage *)> &fill)
static constexpr int NoOption
static constexpr std::string_view ControllerClass
static constexpr std::string_view VoteFailedMessage
static constexpr int YesNoIssueIndex
static constexpr std::string_view VotePassMessage
constexpr bool IsValidSlot(int slot)
@ Cancelled
an admin (or a peer feature) called it off
@ AllVoted
every eligible player cast a ballot
static constexpr std::string_view VoteStartMessage
static constexpr int NoIssue
static constexpr int AllTeams
static constexpr int YesOption
Centralized holder for all SDK interface pointers.
static constexpr std::string_view EventName
static constexpr std::string_view EventName
std::function< bool(const VoteTally &)> Passed
int Eligible
players who could vote