-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNitworkClient.hpp
More file actions
194 lines (182 loc) · 7.53 KB
/
NitworkClient.hpp
File metadata and controls
194 lines (182 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
** EPITECH PROJECT, 2023
** R-Bus
** File description:
** NitworkClient
*/
#pragma once
#include "ANitwork.hpp"
#include "ClientNetwork.hpp"
namespace Nitwork {
class NitworkClient : public ANitwork {
public:
~NitworkClient() override = default;
NitworkClient(const NitworkClient &) = delete;
NitworkClient(const NitworkClient &&) = delete;
void operator=(const NitworkClient &) = delete;
void operator=(const NitworkClient &&) = delete;
static NitworkClient &getInstance();
using ANitwork::start;
bool startClient(
int port,
const std::string &ip,
int threadNb = DEFAULT_THREAD_NB,
int tick = TICKS_PER_SECOND);
// Messages creation methods
void addInitMsg();
void addReadyMsg();
void addPositionRelativeMsg(struct position_relative_s pos);
void addPositionAbsoluteMsg(struct position_absolute_s pos);
void addNewBulletMsg(const struct position_absolute_s &pos, const missileTypes_e &missileType);
void addLifeUpdateMsg(n_id_t playerId, const struct health_s &life);
void addEnemyDeathMsg(n_id_t id);
private:
NitworkClient();
bool startNitworkConfig(int port, const std::string &ip) final;
void handleBodyAction(
const struct header_s &header,
const boost::asio::ip::udp::endpoint &endpoint) final;
[[nodiscard]] const std::map<enum n_actionType_t, actionSender> &
getActionToSendHandlers() const final;
protected:
private:
// NOLINTBEGIN(cppcoreguidelines-avoid-non-const-global-variables)
static NitworkClient _instance; // instance of the NitworkClient (singleton)
// NOLINTEND(cppcoreguidelines-avoid-non-const-global-variables)
boost::asio::ip::udp::resolver _resolver; // resolver used to find the server
n_id_t _clientPacketId = 0; // packet id of the client
boost::asio::ip::udp::endpoint _serverEndpoint; // endpoint of the server
// clang-format off
// maps that will be used to handle the actions, in order to send or receive them
std::map<
enum n_actionType_t,
std::pair<handleBodyT, actionHandler>
> _actionsHandlers = {
{
INIT,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgPlayerInit_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receivePlayerInit(any, endpoint);
}
},
},
{
START_WAVE,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgStartWave_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::handleStartWave(any, endpoint);
}
},
},
{
LIFE_UPDATE,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgLifeUpdate_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveLifeUpdate(any, endpoint);
}
}
},
{
ENEMY_DEATH,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgEnemyDeath_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveEnemyDeath(any, endpoint);
}
}
},
{
NEW_ENEMY,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgNewEnemy_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveNewEnemy(any, endpoint);
}
}
},
{
NEW_ALLIE,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgNewAllie_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveNewAllie(any, endpoint);
}
}
},
{
NEW_BULLET,
{
[this](actionHandler &handler, const struct header_s &header) {
handleBody<struct msgNewBullet_s>(handler, header);
},
[](std::any &any, boost::asio::ip::udp::endpoint &endpoint) {
Systems::receiveNewBullet(any, endpoint);
}
}
}
};
std::map<
enum n_actionType_t,
actionSender
> _actionToSendHandlers = {
{
INIT,
[this](Packet &packet) {
sendData<struct packetMsgInit_s>(packet);
}
},
{
READY,
[this](Packet &packet) {
sendData<struct packetMsgReady_s>(packet);
}
},
{
POSITION_RELATIVE,
[this](Packet &packet) {
sendData<struct packetPositionRelative_s>(packet);
}
},
{
POSITION_ABSOLUTE,
[this](Packet &packet) {
sendData<struct packetPositionAbsolute_s>(packet);
}
},
{
NEW_BULLET,
[this](Packet &packet) {
sendData<struct packetNewBullet_s>(packet);
}
},
{
LIFE_UPDATE,
[this](Packet &packet) {
sendData<struct packetLifeUpdate_s>(packet);
}
},
{
ENEMY_DEATH,
[this](Packet &packet) {
sendData<struct packetEnemyDeath_s>(packet);
}
}
};
// clang-format on
};
} // namespace Nitwork