| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** zappy/ai/run/listeners/broadcast | ||
| 4 | ** File description: | ||
| 5 | ** ListenGroupBroadcast.cpp | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "../../../bots/ABot.hpp" | ||
| 9 | |||
| 10 | #include <iostream> | ||
| 11 | #include <string> | ||
| 12 | #include <sstream> | ||
| 13 | |||
| 14 | ✗ | void ABot::listenGroupBroadcast(const std::string &message) | |
| 15 | { | ||
| 16 | ✗ | if (_state.metadata["should_group"] != "true") // TODO: rename it in process_group ? | |
| 17 | { | ||
| 18 | try | ||
| 19 | { | ||
| 20 | ✗ | size_t firstUnderscore = message.find('_'); | |
| 21 | ✗ | size_t secondUnderscore = message.find('_', firstUnderscore + 1); | |
| 22 | |||
| 23 | ✗ | if (firstUnderscore == std::string::npos || secondUnderscore == std::string::npos) | |
| 24 | { | ||
| 25 | ✗ | throw std::invalid_argument("Invalid format: underscores not found"); | |
| 26 | } | ||
| 27 | |||
| 28 | ✗ | std::string levelStr = message.substr(firstUnderscore + 1, secondUnderscore - firstUnderscore - 1); | |
| 29 | |||
| 30 | ✗ | std::string currentIdGroup = message.substr(secondUnderscore + 1); | |
| 31 | |||
| 32 | ✗ | unsigned int askLevel = std::stoi(levelStr); | |
| 33 | |||
| 34 | ✗ | _state.metadata["id_group"] = currentIdGroup; | |
| 35 | |||
| 36 | ✗ | if (_state.level + 1 == askLevel) | |
| 37 | { | ||
| 38 | ✗ | queue.clear(); | |
| 39 | ✗ | _state.metadata["should_group"] = "true"; | |
| 40 | ✗ | _state.state = ACT_ON_BROADCAST; | |
| 41 | } | ||
| 42 | } | ||
| 43 | ✗ | catch (std::invalid_argument &e) | |
| 44 | { | ||
| 45 | ✗ | PRINT_ERROR(e.what()); | |
| 46 | ✗ | } | |
| 47 | ✗ | catch (std::out_of_range &e) | |
| 48 | { | ||
| 49 | ✗ | PRINT_ERROR(e.what()); | |
| 50 | ✗ | } | |
| 51 | } | ||
| 52 | ✗ | } | |
| 53 | |||
| 54 | ✗ | void ABot::listenGroupJoinedBroadcast(const std::string &message) | |
| 55 | { | ||
| 56 | ✗ | size_t underscorePos = message.find('_'); | |
| 57 | ✗ | std::string idGroup = "0"; | |
| 58 | |||
| 59 | ✗ | if (underscorePos != std::string::npos) | |
| 60 | { | ||
| 61 | ✗ | idGroup = message.substr(underscorePos + 1); | |
| 62 | } | ||
| 63 | else | ||
| 64 | { | ||
| 65 | return; | ||
| 66 | } | ||
| 67 | ✗ | if (idGroup == _state.metadata["id_group"]) | |
| 68 | ✗ | _state.nbAlly++; | |
| 69 | ✗ | if (_state.level == 2 || _state.level == 3) | |
| 70 | { | ||
| 71 | ✗ | if (_state.nbAlly >= 1) | |
| 72 | { | ||
| 73 | ✗ | _state.metadata["should_incant"] = "true"; | |
| 74 | ✗ | _state.metadata["ask_for_group"] = "false"; | |
| 75 | } | ||
| 76 | } | ||
| 77 | ✗ | else if (_state.level == 4 || _state.level == 5) | |
| 78 | { | ||
| 79 | ✗ | if (_state.nbAlly >= 3) | |
| 80 | { | ||
| 81 | ✗ | _state.metadata["should_incant"] = "true"; | |
| 82 | ✗ | _state.metadata["ask_for_group"] = "false"; | |
| 83 | ✗ | _state.nbAlly = 0; | |
| 84 | } | ||
| 85 | } | ||
| 86 | ✗ | else if (_state.level == 6 || _state.level == 7) | |
| 87 | { | ||
| 88 | ✗ | if (_state.nbAlly >= 5) | |
| 89 | { | ||
| 90 | ✗ | _state.metadata["should_incant"] = "true"; | |
| 91 | ✗ | _state.metadata["ask_for_group"] = "false"; | |
| 92 | ✗ | _state.nbAlly = 0; | |
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | ✗ | void ABot::listenMeetingDoneBroadcast(const std::string &message) | |
| 98 | { | ||
| 99 | ✗ | const std::string prefix = "meeting_"; | |
| 100 | ✗ | const std::string suffix = "_done"; | |
| 101 | ✗ | std::string messageCopy = message; | |
| 102 | |||
| 103 | ✗ | if (messageCopy.find(prefix) == 0 && messageCopy.rfind(suffix) == (messageCopy.length() - suffix.length())) | |
| 104 | { | ||
| 105 | ✗ | std::string idGroup = messageCopy.substr(prefix.length(), messageCopy.length() - prefix.length() - suffix.length()); | |
| 106 | ✗ | if (idGroup == _state.metadata["id_group"]) | |
| 107 | { | ||
| 108 | ✗ | _state.metadata["should_group"] = "false"; | |
| 109 | ✗ | _state.state = STANDARD; | |
| 110 | } | ||
| 111 | } | ||
| 112 | ✗ | } | |
| 113 | |||
| 114 | ✗ | void ABot::listenWarnsBroadcast(const std::string &message) | |
| 115 | { | ||
| 116 | ✗ | const std::string prefix = "warns_will_incant_"; | |
| 117 | |||
| 118 | ✗ | if (message.rfind(prefix, 0) == 0) | |
| 119 | { | ||
| 120 | ✗ | std::string idGroup = message.substr(prefix.length()); | |
| 121 | ✗ | if (idGroup == _state.metadata["id_group"]) | |
| 122 | { | ||
| 123 | ✗ | _state.metadata["should_group"] = "false"; | |
| 124 | ✗ | _state.state = STANDARD; | |
| 125 | } | ||
| 126 | } | ||
| 127 | ✗ | } | |
| 128 |