Skip to content

Commit

Permalink
ZappyIA: Add current progress debugging communication interferences
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Jun 19, 2023
1 parent 6d89327 commit be84e84
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/AI/zappy_ai.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
10 changes: 6 additions & 4 deletions src/AI/zappy_ia/ClientManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
47 changes: 29 additions & 18 deletions src/AI/zappy_ia/DecisionTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)],
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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

Expand All @@ -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:
Expand All @@ -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)

0 comments on commit be84e84

Please sign in to comment.