| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** zappy/ai | ||
| 4 | ** File description: | ||
| 5 | ** ListenInventoryresponseCopy.cpp | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "../../bots/ABot.hpp" | ||
| 9 | #include "../../utils/StringUtils.hpp" | ||
| 10 | |||
| 11 | ✗ | void ABot::listenInventoryResponse(const std::string &response) | |
| 12 | { | ||
| 13 | ✗ | const std::string responseCopy = response; | |
| 14 | |||
| 15 | ✗ | if (responseCopy.size() < 2 || responseCopy.front() != '[' || responseCopy.back() != ']') | |
| 16 | { | ||
| 17 | ✗ | std::cerr << "Invalid responseCopy format" << std::endl; | |
| 18 | return; | ||
| 19 | } | ||
| 20 | |||
| 21 | ✗ | std::string cleanresponseCopy = responseCopy.substr(1, responseCopy.size() - 2); | |
| 22 | |||
| 23 | ✗ | std::vector<std::string> resources = splitByChar(cleanresponseCopy, ','); | |
| 24 | int foodCount = 0; | ||
| 25 | |||
| 26 | ✗ | for (const auto &resource : resources) | |
| 27 | { | ||
| 28 | ✗ | std::string trimmedResource = resource; | |
| 29 | ✗ | trimmedResource.erase(trimmedResource.begin(), std::find_if(trimmedResource.begin(), trimmedResource.end(), [](unsigned char ch) | |
| 30 | ✗ | { return !std::isspace(ch); })); | |
| 31 | ✗ | trimmedResource.erase(std::find_if(trimmedResource.rbegin(), trimmedResource.rend(), [](unsigned char ch) | |
| 32 | ✗ | { return !std::isspace(ch); }) | |
| 33 | .base(), | ||
| 34 | trimmedResource.end()); | ||
| 35 | |||
| 36 | ✗ | std::vector<std::string> parts = splitByChar(trimmedResource, ' '); | |
| 37 | ✗ | if (parts.size() == 2 && parts[0] == "food") | |
| 38 | { | ||
| 39 | try | ||
| 40 | { | ||
| 41 | ✗ | foodCount = std::stoi(parts[1]); | |
| 42 | } | ||
| 43 | ✗ | catch (const std::invalid_argument &e) | |
| 44 | { | ||
| 45 | ✗ | PRINT_ERROR("Invalid number format: " + parts[1]); | |
| 46 | return; | ||
| 47 | ✗ | } | |
| 48 | ✗ | catch (const std::out_of_range &e) | |
| 49 | { | ||
| 50 | ✗ | PRINT_ERROR("Number out of range: " + parts[1]); | |
| 51 | return; | ||
| 52 | ✗ | } | |
| 53 | break; | ||
| 54 | } | ||
| 55 | ✗ | } | |
| 56 | ✗ | _state.ressources.food = foodCount; | |
| 57 | ✗ | } | |
| 58 |