diff --git a/src/AI/zappy_ia/DecisionTree.py b/src/AI/zappy_ia/DecisionTree.py index 47a35b50..f7f0486d 100644 --- a/src/AI/zappy_ia/DecisionTree.py +++ b/src/AI/zappy_ia/DecisionTree.py @@ -109,14 +109,12 @@ def inventory(self): try: rescpy = res res = res.split("[")[1].split("]")[0] - except IndexError: + for elem in res.split(","): + parsedElem = elem.strip().split(" ") + self._inputTree["m" + parsedElem[0]][0] = int(parsedElem[1]) + except (IndexError, ValueError) as error: print(f"ID : {self._clientManager._id} crashed in inventory when received : ", rescpy) self._clientManager.stopClient() - return - - for elem in res.split(","): - parsedElem = elem.strip().split(" ") - self._inputTree["m" + parsedElem[0]][0] = int(parsedElem[1]) def lookForTree(self): """ @@ -300,10 +298,13 @@ def elevation(self): ) res = self._clientManager.requestClient(Command.INCANTATION) if (res == "ko\n"): + self._clientManager.requestClient(Command.FORWARD) return out = self._clientManager.waitOutput() if out != Message.KO.value + "\n": self.incrementLevel() + else: + self._clientManager.requestClient(Command.FORWARD) def takeClosestFood(self): self.look() @@ -363,12 +364,13 @@ def elevationEmitter(self): This function is call by decision tree when the ia have the stones for elevation, the ia call others to try elevation """ - self._clientManager.sendBroadcast(list(Message)[self._level].value) participantsId: List[int] = [] res: List[Tuple[int, str, List[int], int]] = [] - time.sleep(1) - res = self._clientManager.checkBroadcastWithoutNewElevation() - for mess in res: - participantsId = self.checkReceivedMessage(participantsId, mess) - if len(participantsId) == levelParticipantsNb[self._level]: - self.waitParticipants(participantsId) + while len(participantsId) < levelParticipantsNb[self._level]: + self._clientManager.sendBroadcast(list(Message)[self._level].value) + res = self._clientManager.checkBroadcastWithoutNewElevation() + for mess in res: + participantsId = self.checkReceivedMessage(participantsId, mess) + self.takeClosestFood() + self._clientManager.sendBroadcast(Message.OK.value, participantsId) + self.waitParticipants(participantsId) diff --git a/src/AI/zappy_ia/ElevationParticipant.py b/src/AI/zappy_ia/ElevationParticipant.py index 5bd09d16..10c8427a 100644 --- a/src/AI/zappy_ia/ElevationParticipant.py +++ b/src/AI/zappy_ia/ElevationParticipant.py @@ -53,11 +53,38 @@ def joinEmitter(self): else: return self.errorReturn() + def checkBroadcastEmitter(self) -> List[Tuple[int, str, List[int], int]]: + broadcasts = self._clientManager.checkBroadcast() + res: List[Tuple[int, str, List[int], int]] = [] + for mess in broadcasts: + if mess[2][0] == 0: + if mess[0] == self._emitter: + continue + else: + self._clientManager.sendBroadcast(Message.KO.value, [mess[0]]) + elif mess[0] == self._emitter: + res.append(mess) + else: + self._log.debug("weird message in check broadcast emitter: " + mess[1]) + return res + + def waitNextStep(self) -> bool: + okNb = 0 + res: Tuple[int, str, List[int], int] = [0, "", [], 0] + while okNb < 2: + res = self.checkBroadcastEmitter() + for mess in res: + if mess[1] == Message.KO.value: + return False + elif mess[1] == Message.OK.value: + okNb += 1 + else: + self._log.debug("weird message in wait next step: " + mess[1]) + self._decisionTree.takeClosestFood() + return True + def elevationParticipant(self) -> bool: - res = self._clientManager.checkBroadcastResponse() - while res[1] == "": - res = self._clientManager.checkBroadcastResponse() - if res[1] == Message.KO.value: + if self.waitNextStep() == False: return self.errorReturn() haveToCome = False ready = False diff --git a/src/SERVER/src/trantorien/commands/command_incantation.c b/src/SERVER/src/trantorien/commands/command_incantation.c index e972d4f8..5bb03c64 100644 --- a/src/SERVER/src/trantorien/commands/command_incantation.c +++ b/src/SERVER/src/trantorien/commands/command_incantation.c @@ -15,7 +15,10 @@ #include "client.h" #include "command_reponses.h" -static const char *format_error_level = "Incantation: too many/less " +static const char *format_error_too_many_level = "Incantation: too many" + "trantoriens at level %d\n"; + +static const char *format_error_too_less_level = "Incantation: too less " "trantoriens at level %d\n"; static const char *format_error_resource = "Incantation: too less " @@ -56,6 +59,11 @@ static bool check_incantation_lvl_availability(trantorien_t *ref_trnt, nb_trantorien_lvl += 1; } } + if (nb_trantorien_lvl < nb_level_players[trantorien_lvl - 1]) { + fprintf(stderr, format_error_too_less_level, trantorien_lvl); + } else if (nb_trantorien_lvl > nb_level_players[trantorien_lvl - 1]) { + fprintf(stderr, format_error_too_many_level, trantorien_lvl); + } return (nb_trantorien_lvl == nb_level_players[trantorien_lvl - 1]); } @@ -78,7 +86,6 @@ bool check_incantation_availability(trantorien_t *trantorien, map_t *map, } } if (check_incantation_lvl_availability(trantorien, ntw) == false) { - fprintf(stderr, format_error_level, trantorien->level); return false; } return true;