VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Player.hpp
Go to the documentation of this file.
1#pragma once
2
6#include <chrono>
7#include <cstdint>
8#include <string>
9#include <string_view>
10#include <utility>
11
12namespace VoltMod
13{
14
15/**
16 * @brief One connected player, owned by @ref PlayerManager for the length of the connection.
17 *
18 * A `Player&` stays valid while the player is connected. Store a @ref PlayerRef across anything
19 * that can outlive that, and resolve it with `runtime.Players.Get(ref)`. Identity only: keep
20 * plugin state in your own managers, keyed by SteamID.
21 */
22class Player
23{
24public:
25 /** @internal Built by @ref PlayerManager. @p connectName is the fallback for @ref Name;
26 * @p entities is null only in the SDK-free unit tests. */
27 Player(int slot, int64_t steamId, std::string connectName, std::string ip, EntitySystem* entities)
28 : _slot(slot),
29 _steamId(steamId),
30 _connectName(std::move(connectName)),
31 _ip(std::move(ip)),
32 _entities(entities),
33 _connectTime(Time::Now())
34 {}
35
36 // One Player is one connection: copying would make a second identity for the same slot.
37 Player(const Player&) = delete;
38 Player& operator=(const Player&) = delete;
39
40 int Slot() const noexcept { return _slot; }
41 int64_t SteamId() const noexcept { return _steamId; }
42
43 /** True for engine bots and the SourceTV relay, which connect without a real SteamID;
44 * @ref IsSourceTV tells them apart. */
45 bool IsBot() const noexcept { return _steamId == 0; }
46
47 /** True for the SourceTV relay: it takes a player slot, but nobody plays it. Read from the
48 * controller, so false until the relay has one. */
49 bool IsSourceTV() const;
50
51 /** This player as a storable reference. */
52 PlayerRef Ref() const noexcept { return {.Slot = _slot, .SteamId = _steamId}; }
53
54 /** The scoreboard name, read from the controller. Falls back to the connect-time name while
55 * there is no controller yet - between ClientConnected and the first spawn. */
56 std::string Name() const;
57
58 /** The IP the player connected from, captured at connect because the engine offers it only
59 * there. Empty for bots. */
60 std::string_view Ip() const noexcept { return _ip; }
61
62 /** How long this connection has lasted. */
63 std::chrono::seconds Playtime() const { return std::chrono::seconds{Time::Now() - _connectTime}; }
64
65 /** @{ This player's entities, valid for this frame; falsy when there are none. Include
66 * <VoltMod/Entities/EntitySystem.hpp> or <VoltMod/Api.hpp> to use them. */
68 VoltMod::Pawn Pawn() const;
69 /** @} */
70
71private:
72 int _slot;
73 int64_t _steamId;
74 std::string _connectName;
75 std::string _ip;
76 /** The wrapper factory, owned by the Runtime that owns the roster. */
77 EntitySystem* _entities;
78 int64_t _connectTime;
79};
80
81} // namespace VoltMod
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...
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
Player(int slot, int64_t steamId, std::string connectName, std::string ip, EntitySystem *entities)
Definition Player.hpp:27
std::string_view Ip() const noexcept
Definition Player.hpp:60
std::chrono::seconds Playtime() const
Definition Player.hpp:63
int64_t SteamId() const noexcept
Definition Player.hpp:41
bool IsSourceTV() const
Definition Player.cpp:17
Player & operator=(const Player &)=delete
int Slot() const noexcept
Definition Player.hpp:40
bool IsBot() const noexcept
Definition Player.hpp:45
Player(const Player &)=delete
VoltMod::Pawn Pawn() const
Definition Player.cpp:12
PlayerRef Ref() const noexcept
Definition Player.hpp:52
std::string Name() const
Definition Player.cpp:22
VoltMod::Controller Controller() const
Definition Player.cpp:7
static int64_t Now()
Definition Durations.cpp:10
A storable reference to a player: the slot plus the SteamID that occupied it.
Definition PlayerRef.hpp:19