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, which connect without a real SteamID. */
44 bool IsBot() const noexcept { return _steamId == 0; }
45
46 /** This player as a storable reference. */
47 PlayerRef Ref() const noexcept { return {.Slot = _slot, .SteamId = _steamId}; }
48
49 /** The scoreboard name, read from the controller. Falls back to the connect-time name while
50 * there is no controller yet - between ClientConnected and the first spawn. */
51 std::string Name() const;
52
53 /** The IP the player connected from, captured at connect because the engine offers it only
54 * there. Empty for bots. */
55 std::string_view Ip() const noexcept { return _ip; }
56
57 /** How long this connection has lasted. */
58 std::chrono::seconds Playtime() const { return std::chrono::seconds{Time::Now() - _connectTime}; }
59
60 /** @{ This player's entities, valid for this frame; falsy when there are none. Include
61 * <VoltMod/Entities/EntitySystem.hpp> or <VoltMod/Api.hpp> to use them. */
63 VoltMod::Pawn Pawn() const;
64 /** @} */
65
66private:
67 int _slot;
68 int64_t _steamId;
69 std::string _connectName;
70 std::string _ip;
71 /** The wrapper factory, owned by the Runtime that owns the roster. */
72 EntitySystem* _entities;
73 int64_t _connectTime;
74};
75
76} // 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:19
One connected player, owned by PlayerManager for the length of the connection.
Definition Player.hpp:23
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:55
std::chrono::seconds Playtime() const
Definition Player.hpp:58
int64_t SteamId() const noexcept
Definition Player.hpp:41
Player & operator=(const Player &)=delete
int Slot() const noexcept
Definition Player.hpp:40
bool IsBot() const noexcept
Definition Player.hpp:44
Player(const Player &)=delete
VoltMod::Pawn Pawn() const
Definition Player.cpp:12
PlayerRef Ref() const noexcept
Definition Player.hpp:47
std::string Name() const
Definition Player.cpp:17
VoltMod::Controller Controller() const
Definition Player.cpp:7
static int64_t Now()
Definition Durations.cpp:10
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
A storable reference to a player: the slot plus the SteamID that occupied it.
Definition PlayerRef.hpp:19