Skip to content

Commit

Permalink
ZappyIA: Add new elevation protocol step
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumeAbel committed Jun 19, 2023
1 parent be84e84 commit 9aa11b8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 19 deletions.
28 changes: 15 additions & 13 deletions src/AI/zappy_ia/DecisionTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
35 changes: 31 additions & 4 deletions src/AI/zappy_ia/ElevationParticipant.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions src/SERVER/src/trantorien/commands/command_incantation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "
Expand Down Expand Up @@ -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]);
}

Expand All @@ -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;
Expand Down

0 comments on commit 9aa11b8

Please sign in to comment.