| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** zappy/ai | ||
| 4 | ** File description: | ||
| 5 | ** ABotPattern.hpp | ||
| 6 | */ | ||
| 7 | |||
| 8 | #include "ABotPattern.hpp" | ||
| 9 | |||
| 10 | ✗ | void ABotPattern::init(int sockfd, const std::string &teamName, const std::string &host, int port, int id, int idMessage) | |
| 11 | { | ||
| 12 | ✗ | _sockfd = sockfd; | |
| 13 | ✗ | _teamName = teamName; | |
| 14 | ✗ | _host = host; | |
| 15 | ✗ | _port = port; | |
| 16 | ✗ | _id = id; | |
| 17 | ✗ | _currentMessageId = idMessage; | |
| 18 | ✗ | initChild(); | |
| 19 | ✗ | saveActionsFile += teamName + std::to_string(id) + ".txt"; | |
| 20 | ✗ | } | |
| 21 | |||
| 22 | ✗ | void ABotPattern::run(const std::string &response) | |
| 23 | { | ||
| 24 | ✗ | std::string responseServer = ""; | |
| 25 | ✗ | std::string responseBroadcast = ""; | |
| 26 | ✗ | _message.content = ""; | |
| 27 | ✗ | _allyMessage.content = ""; | |
| 28 | ✗ | _enemyMessage.content = ""; | |
| 29 | |||
| 30 | // separe servers and broadcast, decrypt message | ||
| 31 | ✗ | separateServerBroadcast(response, responseServer, responseBroadcast); | |
| 32 | |||
| 33 | ✗ | verifyServerIsRunning(response); | |
| 34 | ✗ | debugBotRun(); | |
| 35 | |||
| 36 | // Debug | ||
| 37 | ✗ | debugResponses(responseServer, responseBroadcast); | |
| 38 | |||
| 39 | ✗ | if (!responseServer.empty()) | |
| 40 | ✗ | _canAct = true; | |
| 41 | |||
| 42 | ✗ | if (!responseServer.empty() && _state.state != WAIT_FOR_BROADCAST_RESPONSE) | |
| 43 | { | ||
| 44 | ✗ | listen(responseServer); | |
| 45 | } | ||
| 46 | ✗ | if (!responseBroadcast.empty() && _state.state != WAIT_FOR_SERVER_RESPONSE) | |
| 47 | { | ||
| 48 | ✗ | listenBroadcastResponse(); | |
| 49 | } | ||
| 50 | ✗ | if (_state.state != WAIT_FOR_BROADCAST_RESPONSE && _state.state != WAIT_FOR_SERVER_RESPONSE) | |
| 51 | { | ||
| 52 | ✗ | react(responseServer, responseBroadcast); | |
| 53 | } | ||
| 54 | ✗ | debugState(); | |
| 55 | ✗ | debugMetadata(); | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | void ABotPattern::react(const std::string &responseServer, const std::string &responseBroadcast) | |
| 59 | { | ||
| 60 | ✗ | if (_canAct) | |
| 61 | { | ||
| 62 | ✗ | if (!responseServer.empty() && !responseBroadcast.empty()) | |
| 63 | { | ||
| 64 | ✗ | PRINT_ALERT("GET RESPONSES FROM SERVER AND BROADCAST\n"); | |
| 65 | } | ||
| 66 | ✗ | if (!responseServer.empty() && _state.state != ACT_ON_BROADCAST) | |
| 67 | { | ||
| 68 | if (_state.state != ACT_ON_BROADCAST) | ||
| 69 | ✗ | act(); | |
| 70 | } | ||
| 71 | ✗ | else if (!responseBroadcast.empty() && _state.state != ACT_ON_SERVER) | |
| 72 | { | ||
| 73 | ✗ | act(); | |
| 74 | } | ||
| 75 | } | ||
| 76 | ✗ | } | |
| 77 | |||
| 78 | ✗ | void ABotPattern::act() | |
| 79 | { | ||
| 80 | ✗ | if (queue.empty()) | |
| 81 | { | ||
| 82 | ✗ | updateStrategy(); | |
| 83 | } | ||
| 84 | ✗ | if (!queue.empty()) | |
| 85 | { | ||
| 86 | ✗ | queue.front().first(); | |
| 87 | queue.erase(queue.begin()); | ||
| 88 | ✗ | _canAct = false; | |
| 89 | ✗ | _iteration++; | |
| 90 | } | ||
| 91 | ✗ | if (_iteration % 20 == 0) | |
| 92 | ✗ | saveDataActions(saveActionsFile); | |
| 93 | ✗ | } | |
| 94 | |||
| 95 | // Always put state listener before listener for actions | ||
| 96 | ✗ | void ABotPattern::listen(const std::string &response) | |
| 97 | { | ||
| 98 | ✗ | if (_state.state == WAIT_FOR_SERVER_RESPONSE) | |
| 99 | ✗ | listenCancel(response); | |
| 100 | ✗ | if (_state.state == WAIT_FOR_SERVER_RESPONSE && isConcernedByIncantation()) | |
| 101 | ✗ | listenIncantationReturnResponse(response); | |
| 102 | ✗ | else if (_state.lastAction.action == INVENTORY) | |
| 103 | ✗ | listenInventoryResponse(response); | |
| 104 | ✗ | else if (_state.lastAction.action == LOOK) | |
| 105 | ✗ | listenLookResponse(response); | |
| 106 | ✗ | else if (_state.lastAction.action == TAKE) | |
| 107 | ✗ | listenTakeResponse(response); | |
| 108 | ✗ | else if (isConcernedByIncantation()) | |
| 109 | ✗ | listenIncantationResponse(response); | |
| 110 | ✗ | else if (_state.lastAction.action == CONNECT_NBR) | |
| 111 | ✗ | listenConnectNbrResponse(response); | |
| 112 | ✗ | } | |
| 113 | |||
| 114 | ✗ | void ABotPattern::verifyServerIsRunning(const std::string &response) | |
| 115 | { | ||
| 116 | ✗ | if (response.find("dead") != std::string::npos) | |
| 117 | { | ||
| 118 | ✗ | debugState(); | |
| 119 | ✗ | PRINT_ALERT("END OF BOT\n"); | |
| 120 | ✗ | exit(0); | |
| 121 | } | ||
| 122 | ✗ | } | |
| 123 |