R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
rooms.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <cstddef>
6#include <cstdint>
7#include <optional>
8#include <span>
9#include <string>
10#include <string_view>
11#include <utility>
12#include <vector>
13
14namespace rtype {
15
17inline constexpr std::size_t max_listed_rooms = 64;
18
20enum class RoomState : std::uint8_t {
22 Waiting = 0,
24 Playing = 1,
25};
26
28enum class RoomRejection : std::uint8_t {
30 NoRoomLeft = 1,
32 BadName = 2,
34 BadSize = 3,
36 BadLives = 4,
38 BadDifficulty = 5,
40 UnknownWaves = 6,
43};
44
46inline constexpr std::uint8_t max_room_lives = 9;
47
49enum class Difficulty : std::uint8_t {
51 Easy = 0,
53 Normal = 1,
55 Hard = 2,
56};
57
60struct RoomRules {
62 std::uint8_t lives = 3;
64 std::uint8_t difficulty = static_cast<std::uint8_t>(Difficulty::Normal);
67 std::string waves;
69 std::uint8_t friendly_fire = 0;
70
73 bool operator==(const RoomRules&) const = default;
74};
75
77enum class JoinRejection : std::uint8_t {
79 NoSuchRoom = 1,
81 RoomFull = 2,
83 InGame = 3,
84};
85
87struct RoomPlayer {
89 std::uint8_t player = 0;
91 std::string name;
93 bool ready = false;
94
97 bool operator==(const RoomPlayer&) const = default;
98};
99
103 std::uint8_t room = 0;
105 std::vector<RoomPlayer> players;
106
109 bool operator==(const RoomPlayers&) const = default;
110};
111
115 std::uint8_t room = 0;
117 std::string name;
119 std::uint8_t players = 0;
121 std::uint8_t max_players = 0;
123 std::uint8_t state = 0;
124
127 bool operator==(const RoomRecord&) const = default;
128};
129
133 std::string name;
135 std::uint8_t max_players = 0;
138
141 bool operator==(const CreateRoomRequest&) const = default;
142};
143
147[[nodiscard]] std::string_view to_string(RoomRejection reason) noexcept;
148
152[[nodiscard]] bool read_list_rooms(std::span<const std::byte> message) noexcept;
153
159[[nodiscard]] std::optional<std::vector<RoomRecord>>
160read_room_list(std::span<const std::byte> message);
161
165[[nodiscard]] std::optional<CreateRoomRequest> read_create_room(std::span<const std::byte> message);
166
171[[nodiscard]] std::optional<std::uint8_t>
172read_room_created(std::span<const std::byte> message) noexcept;
173
178[[nodiscard]] std::optional<std::uint8_t>
179read_room_rejected(std::span<const std::byte> message) noexcept;
180
184[[nodiscard]] std::string_view to_string(JoinRejection reason) noexcept;
185
190[[nodiscard]] std::optional<std::uint8_t>
191read_join_room(std::span<const std::byte> message) noexcept;
192
197[[nodiscard]] std::optional<std::pair<std::uint8_t, std::uint8_t>>
198read_room_joined(std::span<const std::byte> message) noexcept;
199
204[[nodiscard]] std::optional<std::uint8_t>
205read_join_rejected(std::span<const std::byte> message) noexcept;
206
210[[nodiscard]] bool read_leave_room(std::span<const std::byte> message) noexcept;
211
216[[nodiscard]] std::optional<bool> read_set_ready(std::span<const std::byte> message) noexcept;
217
223[[nodiscard]] std::optional<RoomPlayers> read_room_players(std::span<const std::byte> message);
224
228[[nodiscard]] bool read_game_start(std::span<const std::byte> message) noexcept;
229
232[[nodiscard]] std::vector<std::byte> write_list_rooms();
233
238[[nodiscard]] std::vector<std::byte> write_room_list(std::span<const RoomRecord> rooms);
239
243[[nodiscard]] std::vector<std::byte> write_create_room(const CreateRoomRequest& request);
244
248[[nodiscard]] std::vector<std::byte> write_room_created(std::uint8_t room);
249
253[[nodiscard]] std::vector<std::byte> write_room_rejected(RoomRejection reason);
254
258[[nodiscard]] std::vector<std::byte> write_join_room(std::uint8_t room);
259
264[[nodiscard]] std::vector<std::byte> write_room_joined(std::uint8_t room, std::uint8_t player);
265
269[[nodiscard]] std::vector<std::byte> write_join_rejected(JoinRejection reason);
270
273[[nodiscard]] std::vector<std::byte> write_leave_room();
274
278[[nodiscard]] std::vector<std::byte> write_set_ready(bool ready);
279
284[[nodiscard]] std::vector<std::byte> write_room_players(const RoomPlayers& players);
285
288[[nodiscard]] std::vector<std::byte> write_game_start();
289
290} // namespace rtype
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
@ BadName
The name is empty, too long, not UTF-8 or has control characters.
std::vector< std::byte > write_join_room(std::uint8_t room)
Encodes a JoinRoom.
std::vector< std::byte > write_room_players(const RoomPlayers &players)
Encodes a RoomPlayers.
std::optional< std::uint8_t > read_join_rejected(std::span< const std::byte > message) noexcept
Reads a JoinRejected.
JoinRejection
Why a room is not joined, the reason of JoinRejected.
Definition rooms.hpp:77
@ NoSuchRoom
No room in use has this number.
@ RoomFull
The room has as many players as it takes.
@ InGame
The player is in game: it plays in its room until its session ends.
std::optional< std::uint8_t > read_room_created(std::span< const std::byte > message) noexcept
Reads a RoomCreated.
std::vector< std::byte > write_room_rejected(RoomRejection reason)
Encodes a RoomRejected.
std::vector< std::byte > write_game_start()
Encodes a GameStart.
bool read_list_rooms(std::span< const std::byte > message) noexcept
Tells whether a message is a well-formed ListRooms: its kind alone.
std::optional< std::pair< std::uint8_t, std::uint8_t > > read_room_joined(std::span< const std::byte > message) noexcept
Reads a RoomJoined.
std::vector< std::byte > write_create_room(const CreateRoomRequest &request)
Encodes a CreateRoom.
Difficulty
How hard the enemies of a room are: how often they come and how much they take.
Definition rooms.hpp:49
@ Hard
More enemies, harder to destroy.
@ Easy
Fewer enemies, easier to destroy.
@ Normal
The game as tuned.
std::vector< std::byte > write_room_list(std::span< const RoomRecord > rooms)
Encodes a RoomList.
constexpr std::size_t max_listed_rooms
Most rooms a server may have, and a RoomList may hold.
Definition rooms.hpp:17
constexpr std::uint8_t max_room_lives
Most lives a room may give its players.
Definition rooms.hpp:46
bool read_leave_room(std::span< const std::byte > message) noexcept
Tells whether a message is a well-formed LeaveRoom: its kind alone.
RoomRejection
Why a room is not created, the reason of RoomRejected.
Definition rooms.hpp:28
@ UnknownWaves
The server has no waves file of this name.
@ BadSize
The number of players is not 1 to 4.
@ BadDifficulty
The difficulty is not a Difficulty.
@ BadLives
The lives are not 1 to max_room_lives.
@ NoRoomLeft
Every room of the server is in use.
@ BadFriendlyFire
The friendly fire is not 0 or 1.
std::optional< std::vector< RoomRecord > > read_room_list(std::span< const std::byte > message)
Reads a RoomList.
std::vector< std::byte > write_set_ready(bool ready)
Encodes a SetReady.
std::vector< std::byte > write_room_joined(std::uint8_t room, std::uint8_t player)
Encodes a RoomJoined.
std::vector< std::byte > write_list_rooms()
Encodes a ListRooms.
std::string_view to_string(ChatRejection reason) noexcept
Short name of a rejection, for logs.
std::optional< CreateRoomRequest > read_create_room(std::span< const std::byte > message)
Reads a CreateRoom.
RoomState
What a room is doing, the state of a RoomList record.
Definition rooms.hpp:20
@ Waiting
None of its players is in game yet.
@ Playing
Its game is running.
std::optional< RoomPlayers > read_room_players(std::span< const std::byte > message)
Reads a RoomPlayers.
std::optional< std::uint8_t > read_join_room(std::span< const std::byte > message) noexcept
Reads a JoinRoom.
std::vector< std::byte > write_join_rejected(JoinRejection reason)
Encodes a JoinRejected.
std::vector< std::byte > write_room_created(std::uint8_t room)
Encodes a RoomCreated.
bool read_game_start(std::span< const std::byte > message) noexcept
Tells whether a message is a well-formed GameStart: its kind alone.
std::optional< std::uint8_t > read_room_rejected(std::span< const std::byte > message) noexcept
Reads a RoomRejected.
std::vector< std::byte > write_leave_room()
Encodes a LeaveRoom.
std::optional< bool > read_set_ready(std::span< const std::byte > message) noexcept
Reads a SetReady.
Content of a CreateRoom.
Definition rooms.hpp:131
RoomRules rules
How the room plays, not checked yet.
Definition rooms.hpp:137
std::string name
Name of the room, not checked yet: see valid_player_name(), whose rules apply.
Definition rooms.hpp:133
bool operator==(const CreateRoomRequest &) const =default
Field-by-field equality.
std::uint8_t max_players
Most players the room takes, not checked yet: 1 to 4 are valid.
Definition rooms.hpp:135
A player of a room, as a RoomPlayers describes it.
Definition rooms.hpp:87
bool operator==(const RoomPlayer &) const =default
Field-by-field equality.
std::uint8_t player
Number in the room, 0 to 3.
Definition rooms.hpp:89
std::string name
Name given at login.
Definition rooms.hpp:91
bool ready
Whether it is ready to start.
Definition rooms.hpp:93
Content of a RoomPlayers.
Definition rooms.hpp:101
std::uint8_t room
Number of the room.
Definition rooms.hpp:103
std::vector< RoomPlayer > players
Its players, at most 4, by number.
Definition rooms.hpp:105
bool operator==(const RoomPlayers &) const =default
Field-by-field equality.
A room, as a RoomList describes it.
Definition rooms.hpp:113
std::uint8_t room
Number of the room, 0 to max_listed_rooms - 1.
Definition rooms.hpp:115
bool operator==(const RoomRecord &) const =default
Field-by-field equality.
std::string name
Name of the room.
Definition rooms.hpp:117
std::uint8_t max_players
Most players the room takes, 1 to 4.
Definition rooms.hpp:121
std::uint8_t players
Players in the room.
Definition rooms.hpp:119
std::uint8_t state
State as sent: a RoomState value, or an unknown one.
Definition rooms.hpp:123
How a room plays, chosen at its creation (CreateRoom, RFC section 3.6).
Definition rooms.hpp:60
std::uint8_t lives
Lives of each player, 1 to max_room_lives.
Definition rooms.hpp:62
std::uint8_t difficulty
A Difficulty value.
Definition rooms.hpp:64
bool operator==(const RoomRules &) const =default
Field-by-field equality.
std::uint8_t friendly_fire
1 if the players' missiles hit the other players, 0 if not.
Definition rooms.hpp:69
std::string waves
Name of the waves file to play, among those the server offers, without its .json; empty for the serve...
Definition rooms.hpp:67