R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
sessions.hpp
Go to the documentation of this file.
1
3#pragma once
4#include <aegis2d/core/random.hpp>
5#include <aegis2d/net/endpoint.hpp>
6
7#include <rtype/login.hpp>
8#include <rtype/rooms.hpp>
9#include <rtype/tuning.hpp>
10
11#include <cstddef>
12#include <cstdint>
13#include <optional>
14#include <string>
15#include <variant>
16#include <vector>
17
18namespace rtype {
19
21inline constexpr std::uint32_t no_room = 0xFFFFFFFFU;
22
24struct Session {
26 std::uint32_t token = 0;
28 std::uint32_t room = 0;
30 std::uint8_t player = 0;
32 std::string name;
35 std::optional<aegis2d::net::Endpoint> endpoint;
37 bool ready = false;
38};
39
41enum class BindResult : std::uint8_t {
43 Bound,
52 Taken,
54 NoRoom,
55};
56
66public:
69
72 explicit SessionTable(std::uint64_t seed) noexcept;
73
84 [[nodiscard]] std::variant<Session, LoginRejection> login(const LoginRequest& request);
85
90 BindResult bind(std::uint32_t token, const aegis2d::net::Endpoint& endpoint);
91
96 bool end(std::uint32_t token) noexcept;
97
105 [[nodiscard]] std::variant<std::uint8_t, JoinRejection> join(std::uint32_t token,
106 std::uint32_t room);
107
112 std::optional<std::uint32_t> leave(std::uint32_t token);
113
118 bool set_ready(std::uint32_t token, bool ready) noexcept;
119
123 [[nodiscard]] std::vector<Session> members(std::uint32_t room) const;
124
128 [[nodiscard]] const Session* find(std::uint32_t token) const noexcept;
129
133 [[nodiscard]] const Session* find(const aegis2d::net::Endpoint& endpoint) const noexcept;
134
137 [[nodiscard]] std::size_t size() const noexcept;
138
143 void set_rooms(std::uint32_t rooms);
144
147 [[nodiscard]] std::uint32_t rooms() const noexcept;
148
156 [[nodiscard]] std::variant<std::uint8_t, RoomRejection>
158
161 void set_waves(std::vector<std::string> names);
162
167 [[nodiscard]] std::optional<RoomRules> rules(std::uint32_t room) const;
168
172 [[nodiscard]] std::vector<RoomRecord> list_rooms() const;
173
174private:
175 // A room in use.
176 struct Listing {
177 std::string name;
178 std::uint8_t max_players = tuning::max_players;
180 };
181
182 [[nodiscard]] bool has_place(std::uint32_t room) const;
183 [[nodiscard]] std::size_t players_in(std::uint32_t room) const noexcept;
184 [[nodiscard]] std::uint8_t free_number(std::uint32_t room) const noexcept;
185 void end_if_empty(std::uint32_t room) noexcept;
186 [[nodiscard]] std::uint32_t new_token() noexcept;
187 [[nodiscard]] Session* find_mutable(std::uint32_t token) noexcept;
188
189 aegis2d::core::Random random_;
190 std::vector<Session> sessions_;
191 // The rooms in use, by number.
192 std::vector<std::optional<Listing>> listings_ = std::vector<std::optional<Listing>>(1);
193 std::vector<std::string> waves_;
194};
195
196} // namespace rtype
The sessions of the server and the rooms they are in.
Definition sessions.hpp:65
std::variant< std::uint8_t, RoomRejection > create_room(const CreateRoomRequest &request)
Creates a room, empty, for the next logins (CreateRoom, RFC section 3.6).
bool set_ready(std::uint32_t token, bool ready) noexcept
Sets whether a session out of game, in a room, is ready (SetReady).
bool end(std::uint32_t token) noexcept
Ends a session and frees its place, name and endpoint; the last one of its room ends the room.
BindResult bind(std::uint32_t token, const aegis2d::net::Endpoint &endpoint)
Binds the source endpoint of a Connect to the session of its token.
std::vector< Session > members(std::uint32_t room) const
The sessions of a room.
SessionTable(std::uint64_t seed) noexcept
Creates an empty table drawing tokens from a seed, for tests.
std::optional< std::uint32_t > leave(std::uint32_t token)
Moves a session out of game from its room to the lobby (LeaveRoom).
std::optional< RoomRules > rules(std::uint32_t room) const
The rules of a room in use.
void set_waves(std::vector< std::string > names)
Sets the waves files a room may be created with, besides the server's own.
std::variant< Session, LoginRejection > login(const LoginRequest &request)
Decides a login and opens its session if it is accepted.
SessionTable()
Creates an empty table drawing tokens from the system's random source.
std::variant< std::uint8_t, JoinRejection > join(std::uint32_t token, std::uint32_t room)
Moves a session out of game to another room in use (JoinRoom, RFC 3.7).
std::vector< RoomRecord > list_rooms() const
The rooms in use, by number, as a RoomList describes them.
const Session * find(std::uint32_t token) const noexcept
Finds a session.
std::uint32_t rooms() const noexcept
Number of rooms the sessions are spread over.
void set_rooms(std::uint32_t rooms)
Sets how many rooms the sessions are spread over, tuning::max_players in each at most.
const Session * find(const aegis2d::net::Endpoint &endpoint) const noexcept
Finds the session bound to an endpoint.
std::size_t size() const noexcept
Number of open sessions, those of the lobby included.
TCP messages of the session (RFC section 3.2): encoding, decoding and name rules; those of the rooms ...
constexpr std::size_t max_players
Players in one game, as the subject requires.
Definition tuning.hpp:33
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
constexpr std::uint32_t no_room
Room of a session in the lobby, out of every room (LeaveRoom, RFC section 3.7).
Definition sessions.hpp:21
RoomRejection
Why a room is not created, the reason of RoomRejected.
Definition rooms.hpp:28
BindResult
What SessionTable::bind() did with a Connect.
Definition sessions.hpp:41
@ UnknownSession
No session has this token: drop the datagram.
@ NoRoom
The session is in the lobby, with no game to join: drop the datagram.
@ Bound
The endpoint is now bound to the session: answer with Accept.
@ Taken
The session is bound to another endpoint, or the endpoint to another session: drop the datagram,...
@ AlreadyBound
The endpoint was already bound to this session (the Accept was lost): answer with Accept again.
TCP messages of the rooms (RFC sections 3.6 and 3.7): listing them, creating one, joining and leaving...
Content of a CreateRoom.
Definition rooms.hpp:131
Content of a LoginRequest.
Definition login.hpp:104
A room, as a RoomList describes it.
Definition rooms.hpp:113
How a room plays, chosen at its creation (CreateRoom, RFC section 3.6).
Definition rooms.hpp:60
A logged-in player (RFC section 3).
Definition sessions.hpp:24
std::string name
Name given at login, valid as valid_player_name() says.
Definition sessions.hpp:32
std::uint32_t token
Token given in LoginAccepted, never 0.
Definition sessions.hpp:26
std::optional< aegis2d::net::Endpoint > endpoint
UDP endpoint the player plays from, once its Connect arrived (RFC 3.4): the player is then in game.
Definition sessions.hpp:35
bool ready
Whether the player is ready to start the game of its room (SetReady).
Definition sessions.hpp:37
std::uint32_t room
Room the player is in, 0 to SessionTable::rooms() - 1, or no_room in the lobby.
Definition sessions.hpp:28
std::uint8_t player
Player number in its room, 0 to tuning::max_players - 1.
Definition sessions.hpp:30
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.