From 634a8fc6fee80929f52066104a9f226b93f39b1c Mon Sep 17 00:00:00 2001 From: Marius-P1 Date: Sat, 22 Jun 2024 17:55:23 +0200 Subject: [PATCH] feat: Improve server communication using a buffer --- ai/src/AI.py | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ai/src/AI.py b/ai/src/AI.py index 52b1929e..c0d47e96 100644 --- a/ai/src/AI.py +++ b/ai/src/AI.py @@ -60,6 +60,7 @@ def __init__(self, host, port, teamName): self.creationTime = time.time_ns() self.myuuid = str(uuid.uuid4()) self.isRunning = True + self.buffer = "" def serverCommunicationInThread(self): @@ -75,7 +76,12 @@ def serverCommunicationInThread(self): self.player.currentCallback = self.player.callbacks[0] self.api.sendData(self.player.currentCommand) while self.player.currentAction != Action.NONE: - responses = self.api.receiveData().split("\n") + responses = self.buffer + self.api.receiveData() + responses = responses.split("\n") + self.buffer = "" + if responses[-1] != "": + self.buffer = responses[-1] + responses.pop() for response in responses: if response == '': continue @@ -86,8 +92,13 @@ def serverCommunicationInThread(self): print("Regrouping Start", flush=True, file=sys.stderr) while self.isRunning: responses = self.api.receiveData(0.1) - if responses is not None : + if responses is not None: + responses = self.buffer + responses responses = responses.split("\n") + self.buffer = "" + if responses[-1] != "": + self.buffer = responses[-1] + responses.pop() for response in responses: if response == '': continue @@ -98,7 +109,12 @@ def serverCommunicationInThread(self): self.player.currentCallback = self.player.callbacks[0] self.api.sendData(self.player.currentCommand) while self.player.currentAction != Action.NONE: - responses = self.api.receiveData().split("\n") + responses = self.buffer + self.api.receiveData() + responses = responses.split("\n") + self.buffer = "" + if responses[-1] != "": + self.buffer = responses[-1] + responses.pop() for response in responses: if response == '': continue