R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
LeftRoom.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 14/01/2026.
4** File description:
5** LeftRoom.hpp
6*/
7
8#pragma once
9
10#include <capnp/message.h>
11#include <capnp/serialize.h>
12#include <cstdint>
13#include <string>
14#include <vector>
15#include "schemas/s2c_messages.capnp.h"
16
17namespace RType::Messages::S2C {
18
23 enum class LeftRoomReason : uint8_t {
24 VOLUNTARY_LEAVE = 0, // Player pressed "back" or left voluntarily
25 KICKED = 1, // Player was kicked by host
26 ROOM_CLOSED = 2, // Room was closed/deleted
27 SERVER_SHUTDOWN = 3 // Server is shutting down
28 };
29
38 class LeftRoom {
39 public:
40 uint32_t playerId;
42 std::string message;
43
44 LeftRoom() = default;
45 LeftRoom(uint32_t pId, LeftRoomReason r, const std::string &msg = "")
46 : playerId(pId), reason(r), message(msg) {}
47
51 [[nodiscard]] std::vector<uint8_t> serialize() const {
52 capnp::MallocMessageBuilder messageBuilder;
53 auto builder = messageBuilder.initRoot<::LeftRoom>();
54
55 builder.setPlayerId(playerId);
56
57 // Convert enum to Cap'n Proto enum
58 ::LeftRoomReason capnpReason;
59 switch (reason) {
62 break;
64 capnpReason = ::LeftRoomReason::KICKED;
65 break;
68 break;
71 break;
72 default:
74 break;
75 }
76 builder.setReason(capnpReason);
77 builder.setMessage(message);
78
79 auto bytes = capnp::messageToFlatArray(messageBuilder);
80 auto byteArray = bytes.asBytes();
81 return std::vector<uint8_t>(byteArray.begin(), byteArray.end());
82 }
83
87 static LeftRoom deserialize(const std::vector<uint8_t> &data) {
88 KJ_REQUIRE(data.size() % sizeof(capnp::word) == 0,
89 "Serialized data size must be a multiple of capnp::word");
90 auto aligned = kj::heapArray<uint8_t>(data.size());
91 memcpy(aligned.begin(), data.data(), data.size());
92 kj::ArrayPtr<const capnp::word> words(reinterpret_cast<const capnp::word *>(aligned.begin()),
93 data.size() / sizeof(capnp::word));
94
95 capnp::FlatArrayMessageReader messageReader(words);
96 auto reader = messageReader.getRoot<::LeftRoom>();
97
98 LeftRoom result;
99 result.playerId = reader.getPlayerId();
100
101 // Convert Cap'n Proto enum to C++ enum
102 auto capnpReason = reader.getReason();
103 switch (capnpReason) {
104 case ::LeftRoomReason::VOLUNTARY_LEAVE:
105 result.reason = LeftRoomReason::VOLUNTARY_LEAVE;
106 break;
107 case ::LeftRoomReason::KICKED:
108 result.reason = LeftRoomReason::KICKED;
109 break;
110 case ::LeftRoomReason::ROOM_CLOSED:
111 result.reason = LeftRoomReason::ROOM_CLOSED;
112 break;
113 case ::LeftRoomReason::SERVER_SHUTDOWN:
114 result.reason = LeftRoomReason::SERVER_SHUTDOWN;
115 break;
116 }
117
118 result.message = reader.getMessage().cStr();
119
120 return result;
121 }
122 };
123
124} // namespace RType::Messages::S2C
Notification that a player has left a room.
Definition LeftRoom.hpp:38
static LeftRoom deserialize(const std::vector< uint8_t > &data)
Deserialize from byte vector.
Definition LeftRoom.hpp:87
std::vector< uint8_t > serialize() const
Serialize to byte vector.
Definition LeftRoom.hpp:51
LeftRoom(uint32_t pId, LeftRoomReason r, const std::string &msg="")
Definition LeftRoom.hpp:45
Server-to-Client messages.
Definition Server.hpp:34
LeftRoomReason
Reason why a player left a room.
Definition LeftRoom.hpp:23