Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
** EPITECH PROJECT, 2024 |
3 |
|
|
** zappy/ai/client |
4 |
|
|
** File description: |
5 |
|
|
** Client.hpp |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
#ifndef CLIENT_HPP_ |
9 |
|
|
#define CLIENT_HPP_ |
10 |
|
|
|
11 |
|
|
#include <iostream> |
12 |
|
|
#include <string> |
13 |
|
|
#include <vector> |
14 |
|
|
#include <sys/socket.h> |
15 |
|
|
#include <netinet/in.h> |
16 |
|
|
#include <arpa/inet.h> |
17 |
|
|
#include <unistd.h> |
18 |
|
|
#include <cstring> |
19 |
|
|
#include <memory> |
20 |
|
|
|
21 |
|
|
#include "../botFactory/BotFactory.hpp" |
22 |
|
|
#include "../constant/Constants.hpp" |
23 |
|
|
|
24 |
|
|
class Client |
25 |
|
|
{ |
26 |
|
|
public: |
27 |
|
|
Client(const std::string &host, const std::string &teamName, int port); |
28 |
|
|
~Client(); |
29 |
|
|
|
30 |
|
|
void run(); |
31 |
|
|
template <typename ConditionFunc, typename ActionFunc> |
32 |
|
|
void interactWithServer(ConditionFunc condition, ActionFunc action); |
33 |
|
|
|
34 |
|
|
class ClientException : public std::exception |
35 |
|
|
{ |
36 |
|
|
public: |
37 |
|
✗ |
ClientException(const std::string &message) : _message(message) {} |
38 |
|
✗ |
const char *what() const noexcept { return _message.c_str(); } |
39 |
|
|
|
40 |
|
|
private: |
41 |
|
|
std::string _message; |
42 |
|
|
}; |
43 |
|
|
|
44 |
|
|
private: |
45 |
|
|
std::string _host; |
46 |
|
|
std::string _teamName; |
47 |
|
|
int _port; |
48 |
|
|
int _sockfd; |
49 |
|
|
fd_set _readfds; |
50 |
|
|
struct timeval _tv; |
51 |
|
|
std::unique_ptr<IBot> _bot; |
52 |
|
|
|
53 |
|
|
// Map Data |
54 |
|
|
unsigned int _slot = 0; |
55 |
|
|
unsigned int _width = 42; |
56 |
|
|
unsigned int _height = 42; |
57 |
|
|
|
58 |
|
|
int _messageToReadBeforeStart = 3; |
59 |
|
|
|
60 |
|
|
// Initialisation |
61 |
|
|
void setupConnection(); |
62 |
|
|
|
63 |
|
|
// Logic |
64 |
|
|
void loop(); |
65 |
|
|
|
66 |
|
|
// Message |
67 |
|
|
void recvMessage(std::string &buffer); |
68 |
|
|
void sendMessage(const std::string &message); |
69 |
|
|
|
70 |
|
|
// Bot |
71 |
|
|
void initBot(const std::string identityMessage); |
72 |
|
|
void authenticate(); |
73 |
|
|
|
74 |
|
|
// Listen |
75 |
|
|
void listenFirstResponse(const std::string &response); |
76 |
|
|
}; |
77 |
|
|
|
78 |
|
|
#endif /* !CLIENT_HPP_ */ |
79 |
|
|
|