RType
MenuScene.cpp
Go to the documentation of this file.
1 /*
2 ** EPITECH PROJECT, 2023
3 ** R-type
4 ** File description:
5 ** R-type
6 */
7 
8 #include "MenuScene.hpp"
9 #include "ClientCore.hpp"
10 
11 #include <utility>
12 
13 /**
14  * @brief Construct a new Menu Scene:: Menu Scene object
15  *
16  * @param clientCore
17  * @param socket
18  */
19 MenuScene::MenuScene(ClientCore *clientCore, std::shared_ptr<ClientSocket> socket) :
20  AScene(clientCore),
22 {
24  _pingTime.tv_sec = 0;
25  _pingTime.tv_usec = 0;
26 }
27 
28 /**
29  * @brief init_scene, initialize the scene
30  *
31  */
33 {
34  std::shared_ptr<TextComponent> text = std::make_shared<TextComponent>(_clientCore, _socket);
35  std::shared_ptr<ButtonComponent> button = std::make_shared<ButtonComponent>(_clientCore, _socket);
36  std::shared_ptr<TextComponent> text_button = std::make_shared<TextComponent>(_clientCore, _socket);
37  std::shared_ptr<InputComponent> address_input = std::make_shared<InputComponent>(_clientCore, _socket);
38  std::shared_ptr<InputComponent> port_input = std::make_shared<InputComponent>(_clientCore, _socket);
39  std::shared_ptr<SoundComponent> sound = std::make_shared<SoundComponent>(_clientCore, _socket);
40  std::shared_ptr<TextComponent> text_ping = std::make_shared<TextComponent>(_clientCore, _socket);
41 
42  text->setText("");
43 
44  button->addActionTarget(text);
45  button->addActionTarget(address_input);
46  button->addActionTarget(port_input);
47  button->addActionTarget(sound);
48  std::function<void()> handleClick = std::bind(&ButtonComponent::handleClickInitServer, button);
49  button->setCallback(handleClick);
50 
51  text_button->setText("Init Server Connection");
52  text_button->setPosition(sf::Vector2f(275, 190));
53 
54  address_input->setPosition(sf::Vector2f(150, 380));
55  port_input->setPosition(sf::Vector2f(150, 475));
56 
57  address_input->addActionTarget(port_input);
58  port_input->addActionTarget(address_input);
59 
60  address_input->setAttribute("address");
61  address_input->_textEntry = "127.0.0.1";
62  address_input->setText();
63  port_input->setAttribute("port");
64  port_input->_textEntry = "4242";
65  port_input->setText();
66  text->setAttribute("text add serv");
67 
68  text_ping->setAttribute("text ping");
69  text_ping->setText("Ping: 0ms");
70  text_ping->setPosition(sf::Vector2f(0, 550));
71 
72  addComponent(text);
73  addComponent(button);
74  addComponent(text_button);
75  addComponent(address_input);
76  addComponent(port_input);
77 // addComponent(sound);
78  addComponent(text_ping);
79 }
80 
81 /**
82  * @brief handleEvent, handle the event
83  * @param event
84  * @param window
85  */
86 void MenuScene::handleEvent(const sf::Event &event, sf::RenderWindow &window)
87 {
88  while (window.pollEvent(const_cast<sf::Event &>(event))) {
89  if (event.type == sf::Event::Closed) {
90  window.close();
91  return;
92  }
93  if (event.type == sf::Event::KeyPressed) {
94  if (event.key.code == sf::Keyboard::Escape) {
96  return;
97  }
98  }
99  for (auto &component : _components) {
100  component->handleEvent(event, window);
101  }
102  }
103 }
104 
105 /**
106  * @brief receiveData, receive data from the server
107  */
109 {
110  std::tuple<std::unique_ptr<Packet>, int> packet = _socket->receive();
111  std::unique_ptr<Packet> p = std::move(std::get<0>(packet));
112 
113  if (p != nullptr) {
114  if (p->code == MESSAGE) {
115  std::string message = static_cast<char *>(p->data);
116  if (message == "connection accepted") {
117  _socket->setInit(true);
119  for (auto &component : _components) {
120  if (component->getType() == ComponentType::TEXT) {
121  if (component->getAttribute() == "text add serv") {
122  dynamic_cast<TextComponent *>(component.get())->setText("Connection accepted");
123  }
124  }
125  }
126  } else {
127  std::cout << "Message: " << message << std::endl;
128  }
129  }
130  if (p->code == HEARTBEAT) {
131  timeval timerecv{};
132  timeval now{};
133  gettimeofday(&now, nullptr);
134  timerecv = *static_cast<timeval *>(p->data);
135  timersub(&now, &timerecv, &_pingTime);
136 
137  for (auto &component : _components) {
138  if (component->getType() == ComponentType::TEXT) {
139  if (component->getAttribute() == "text ping") {
140  dynamic_cast<TextComponent *>(component.get())
141  ->setText("Ping: " + std::to_string(_pingTime.tv_sec * 1000 + _pingTime.tv_usec / 1000) +
142  ", " + std::to_string(_pingTime.tv_usec % 1000) + "ms");
143  }
144  }
145  }
146  }
147  free(p->data);
148  }
149 }