From be84e84493a788d54e00fb10f7be38b5325879df Mon Sep 17 00:00:00 2001 From: guillaumeAbel Date: Mon, 19 Jun 2023 20:14:13 +0200 Subject: [PATCH] ZappyIA: Add current progress debugging communication interferences --- src/AI/zappy_ai.py | 2 +- src/AI/zappy_ia/ClientManager.py | 10 ++++--- src/AI/zappy_ia/DecisionTree.py | 47 ++++++++++++++++++++------------ 3 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/AI/zappy_ai.py b/src/AI/zappy_ai.py index 1eb12d31..a7a516eb 100644 --- a/src/AI/zappy_ai.py +++ b/src/AI/zappy_ai.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 -from zappy_ia.IA import IA from zappy_ia.Log import clearDirectory +from zappy_ia.IA import IA import argparse import sys diff --git a/src/AI/zappy_ia/ClientManager.py b/src/AI/zappy_ia/ClientManager.py index 7244b9be..e68aff8c 100644 --- a/src/AI/zappy_ia/ClientManager.py +++ b/src/AI/zappy_ia/ClientManager.py @@ -36,12 +36,15 @@ def connect(self): resSetup = self.waitOutput() self._log.info("Received: " + resSetup) - def isMyIdInList(self, list_: List[int]) -> bool: + def isIdInList(self, list_: List[int], toFindId: int) -> bool: for id_ in list_: - if self._id == id_: + if toFindId == id_: return True return False + def isMyIdInList(self, list_: List[int]) -> bool: + return self.isIdInList(list_, self._id) + def checkBroadcast(self) -> List[Tuple[int, str, List[int], int]]: """ This function is call to get received broadcast since last get, @@ -147,8 +150,7 @@ def requestClient( toSendOrd = list(map(ord, toSend)) argToSendOrd = list(map(ord, argToSend)) self._log.info( - f"Server responded ko to : `{toSend}`({toSendOrd}) + `{argToSend}`({argToSendOrd})" - ) + "Server responded ko to : " + toSend + " " + argToSend) return "ko\n" return res diff --git a/src/AI/zappy_ia/DecisionTree.py b/src/AI/zappy_ia/DecisionTree.py index 1b774a48..47a35b50 100644 --- a/src/AI/zappy_ia/DecisionTree.py +++ b/src/AI/zappy_ia/DecisionTree.py @@ -5,8 +5,9 @@ from zappy_ia.ClientManager import ClientManager from zappy_ia.Enums import Message, Element, Command from zappy_ia.Log import LogGood +import time -levelParticpantsNb: List[int] = [0, 0, 1, 1, 3, 3, 5, 5] +levelParticipantsNb: List[int] = [0, 0, 1, 1, 3, 3, 5, 5] levelCosts: List[List[Tuple[Element, int]]] = [ [(Element.LINEMATE, 1)], @@ -106,9 +107,10 @@ def inventory(self): if res == "ko\n": return try: + rescpy = res res = res.split("[")[1].split("]")[0] except IndexError: - print(f"ID : {self._clientManager._id}") + print(f"ID : {self._clientManager._id} crashed in inventory when received : ", rescpy) self._clientManager.stopClient() return @@ -140,9 +142,10 @@ def look(self): if res == "ko\n": return try: + rescpy = res res = res.split("[")[1].split("]")[0] except IndexError: - print(f"ID : {self._clientManager._id}") + print(f"ID : {self._clientManager._id} crashed in look when received : ", rescpy) self._clientManager.stopClient() return @@ -295,7 +298,9 @@ def elevation(self): self._clientManager.requestClient( Command.SET_OBJECT, costTuple[0].value ) - self._clientManager.requestClient(Command.INCANTATION) + res = self._clientManager.requestClient(Command.INCANTATION) + if (res == "ko\n"): + return out = self._clientManager.waitOutput() if out != Message.KO.value + "\n": self.incrementLevel() @@ -312,11 +317,14 @@ def takeClosestFood(self): def checkReceivedMessage( self, participantsId: List[int], res: Tuple[int, str, List[int], int] ) -> List[int]: - if res[1] == Message.OK.value: - if len(participantsId) < levelParticpantsNb[self._level]: + self._log.debug("message: " + res[1]) + if res[1] == Message.OK.value or res[1] == "ok\n": + if len(participantsId) < levelParticipantsNb[self._level]: + self._log.debug("res ok") participantsId.append(res[0]) self._clientManager.sendBroadcast(Message.OK.value, [res[0]]) else: + self._log.debug("res ko") self._clientManager.sendBroadcast(Message.KO.value, [res[0]]) return participantsId @@ -325,21 +333,24 @@ def waitParticipants(self, participantsId: List[int]): self.inventory() res: List[Tuple[int, str, List[int], int]] = [] while ( - readyParticipants < levelParticpantsNb[self._level] + readyParticipants < levelParticipantsNb[self._level] or self._inputTree["mfood"][0] < 13 ): self.takeClosestFood() res = self._clientManager.checkBroadcastWithoutNewElevation() for mess in res: if ( - mess[0] != 0 - and self._clientManager.isMyIdInList(mess[2]) - and mess[1] == Message.OK.value + mess[1] == Message.OK.value ): - readyParticipants += 1 + if self._clientManager.isIdInList(participantsId, mess[0]): + self._log.debug("readyParticipants nb: " + str(readyParticipants)) + readyParticipants += 1 + else: + self._log.debug("resp ko in waitparticipant") + self._clientManager.sendBroadcast(Message.KO.value, [mess[0]]) self.inventory() arrivedParticipants = 0 - while arrivedParticipants < levelParticpantsNb[self._level]: + while arrivedParticipants < levelParticipantsNb[self._level]: self._clientManager.sendBroadcast(Message.COME.value, participantsId) res = self._clientManager.checkBroadcastWithoutNewElevation() for mess in res: @@ -355,9 +366,9 @@ def elevationEmitter(self): self._clientManager.sendBroadcast(list(Message)[self._level].value) participantsId: List[int] = [] res: List[Tuple[int, str, List[int], int]] = [] - while len(participantsId) != levelParticpantsNb[self._level]: - res = self._clientManager.checkBroadcastWithoutNewElevation() - for mess in res: - participantsId = self.checkReceivedMessage(participantsId, mess) - self.takeClosestFood() - self.waitParticipants(participantsId) + 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)