R-Type
Networked shoot'em up built on Aegis2D
Loading...
Searching...
No Matches
game_server.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <aegis2d/core/profiler.hpp>
6#include <aegis2d/ecs/waves.hpp>
7#include <aegis2d/net/context.hpp>
8
9#include <rtype/events.hpp>
11#include <rtype/room.hpp>
12#include <rtype/rooms.hpp>
13#include <rtype/scoreboard.hpp>
14#include <rtype/server_game.hpp>
16#include <rtype/simulation.hpp>
17#include <rtype/tuning.hpp>
18
19#include <cstddef>
20#include <cstdint>
21#include <filesystem>
22#include <map>
23#include <memory>
24#include <optional>
25#include <string>
26#include <unordered_map>
27#include <utility>
28#include <vector>
29
30namespace rtype {
31
38[[nodiscard]] GameRules game_rules(const RoomRules& rules) noexcept;
39
65public:
75 GameServer(aegis2d::net::NetworkContext& context, std::optional<std::uint64_t> seed,
76 aegis2d::ecs::WaveSchedule waves, std::uint16_t tcp_port, std::uint16_t udp_port,
77 std::uint32_t rate = tuning::tick_rate, std::uint32_t max_rooms = 1);
78
79 GameServer(const GameServer&) = delete;
80 GameServer& operator=(const GameServer&) = delete;
84 ~GameServer() = default;
85
91 void set_waves(std::map<std::string, aegis2d::ecs::WaveSchedule> waves);
92
99 void keep_scores(std::filesystem::path file);
100
103 [[nodiscard]] const Scoreboard* scoreboard() const noexcept;
104
107 [[nodiscard]] ServerNetwork& network() noexcept;
108
114 [[nodiscard]] Room& room(std::uint32_t id = 0);
115
118 [[nodiscard]] std::uint32_t open_rooms() const noexcept;
119
122 [[nodiscard]] std::uint32_t max_rooms() const noexcept;
123
126 [[nodiscard]] std::uint32_t rate() const noexcept;
127
131 void start_trace() noexcept;
132
138 std::size_t save_trace(const std::filesystem::path& file);
139
143 [[nodiscard]] Simulation& simulation();
144
145private:
146 // A player in a game: its room, its number there and the last rewind posted to the game.
147 struct Seat {
148 std::uint32_t room = 0;
149 std::uint8_t player = 0;
150 std::uint32_t rewind = 0;
151 };
152
153 void after_tick(Room& room, GameInstance& instance);
154 void on_joined(const Session& session);
155 void on_left(const Session& session, LeaveReason reason);
156 void update_rewinds(std::uint32_t id);
157 Room& open_room(std::uint32_t id);
158 void close_room(std::uint32_t id);
159
160 aegis2d::net::NetworkContext& context_;
161 ServerNetwork network_;
162 // What each room's game is built with.
163 std::optional<std::uint64_t> seed_;
164 aegis2d::ecs::WaveSchedule waves_;
165 std::map<std::string, aegis2d::ecs::WaveSchedule> named_waves_;
166 std::uint32_t rate_;
167 std::optional<Scoreboard> scores_;
168 // The players in a game, by token; used by the network thread only.
169 std::unordered_map<std::uint32_t, Seat> seats_;
170 // With start_trace(), the timings of the rooms closed, named after them.
171 bool trace_ = false;
172 std::vector<std::pair<std::string, std::vector<aegis2d::core::TraceEvent>>> traces_;
173 // One slot per room, empty while it is closed; used by the network thread only. Last:
174 // their threads start once the rest is ready, and stop first.
175 std::vector<std::unique_ptr<Room>> rooms_;
176};
177
178} // namespace rtype
The one game of the server, with up to tuning::max_players players.
Definition game_instance.hpp:37
Runs the games of its rooms for the players of its sockets, as r-type_server does.
Definition game_server.hpp:64
GameServer(aegis2d::net::NetworkContext &context, std::optional< std::uint64_t > seed, aegis2d::ecs::WaveSchedule waves, std::uint16_t tcp_port, std::uint16_t udp_port, std::uint32_t rate=tuning::tick_rate, std::uint32_t max_rooms=1)
Opens both ports; the rooms open as their players join.
GameServer(const GameServer &)=delete
void start_trace() noexcept
Records the timings of the ticks of every room opened from now on.
std::uint32_t rate() const noexcept
Ticks per second of each room.
std::size_t save_trace(const std::filesystem::path &file)
Closes every room, then writes the timings recorded in the Chrome trace format, which Perfetto (ui....
void keep_scores(std::filesystem::path file)
Keeps the best scores in a file: at the end of each game of a room, every player of it records its sc...
std::uint32_t open_rooms() const noexcept
Number of rooms open, with a game running.
GameServer & operator=(GameServer &&)=delete
GameServer(GameServer &&)=delete
ServerNetwork & network() noexcept
The sockets and the sessions; use them on the context's thread.
~GameServer()=default
Stops the simulation threads, then closes the sockets.
std::uint32_t max_rooms() const noexcept
Number of rooms played at once.
const Scoreboard * scoreboard() const noexcept
The best scores kept.
void set_waves(std::map< std::string, aegis2d::ecs::WaveSchedule > waves)
Sets the waves files a room may be created with, besides the server's own.
GameServer & operator=(const GameServer &)=delete
Room & room(std::uint32_t id=0)
An open room: its game, thread and event tracker. Use it on the context's thread: the room closes the...
Simulation & simulation()
The thread running the game of the first room, room().simulation().
One game of the server: its GameInstance, the Simulation thread that runs it, the EventTracker of wha...
Definition room.hpp:26
The max_entries best scores, best first, kept in a JSON file.
Definition scoreboard.hpp:34
The two ports the server listens on, and the TCP side of the sessions.
Definition server_network.hpp:81
Runs a GameInstance on its own thread, tuning::tick_rate times per second.
Definition simulation.hpp:30
Gameplay events sent to the clients (RFC section 4.4): spawns, destructions, deaths,...
The game run by the server, and the players of the sessions in it.
constexpr std::uint32_t tick_rate
Simulation rate of the server, in ticks per second.
Definition tuning.hpp:15
R-Type game code, built on the Aegis2D engine.
Definition enemy_scripts.hpp:16
LeaveReason
Why a player left the game, in PlayerLeft.
Definition events.hpp:49
GameRules game_rules(const RoomRules &rules) noexcept
The gameplay of the rules a room was created with.
A game of the server, with the thread that runs it.
TCP messages of the rooms (RFC sections 3.6 and 3.7): listing them, creating one, joining and leaving...
The best scores of the server, kept in a JSON file across restarts.
The R-Type simulation run by the server: world, systems and players.
Sockets of the R-Type server: the TCP port of the logins and the UDP port of the game.
Thread running the server's game at a fixed rate.
How a game plays: the rules of its room, as the gameplay reads them.
Definition server_game.hpp:35
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
Gameplay values of R-Type: play area, speeds, sizes, health, lives and scores.