diff --git a/Code/Communication/APIRequests.py b/Code/Communication/APIRequests.py index 3c27918..7bd0e27 100644 --- a/Code/Communication/APIRequests.py +++ b/Code/Communication/APIRequests.py @@ -8,11 +8,11 @@ from Code.Communication.Member import Member class APIRequests: - def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> Member: + def connectToMember(self, own_process: Member, ip, port, direction: Direction) -> (Member,int): body = asdict(own_process) response = requests.post(f"http://{ip}:{port}/connect/{direction.name}", json=body,allow_redirects=True) jsonValue = response.json() - return Member(jsonValue["ip"], jsonValue["port"]) + return Member(jsonValue["ip"], jsonValue["port"]),int(jsonValue["counter"]) def get_edge(self, target: Member, direction: Direction,counter:int): print(f"Getting {direction} edge from {target} with url http://{target.ip}:{target.port}/border/{direction.name}") diff --git a/Code/Communication/Neighbours.py b/Code/Communication/Neighbours.py index a0cc020..ef49af7 100644 --- a/Code/Communication/Neighbours.py +++ b/Code/Communication/Neighbours.py @@ -11,10 +11,11 @@ class Neighbours: self.own_process = own_process self.api = APIRequests() - def connect(self, direction, ip, port): + def connect(self, direction, ip, port) -> int: print(f"connecting to {ip}:{port} on {direction} side") - new_neighbour = self.api.connectToMember(self.own_process, ip, port, mirror(direction)) + new_neighbour,counter = self.api.connectToMember(self.own_process, ip, port, mirror(direction)) self.neighbours[direction] = new_neighbour + return counter def accept_connection(self, direction: Direction, ip, port) -> tuple[Member, bool]: if direction in self.neighbours: diff --git a/Code/Communication/RequestHandler.py b/Code/Communication/RequestHandler.py index 0e0b484..b5d5269 100644 --- a/Code/Communication/RequestHandler.py +++ b/Code/Communication/RequestHandler.py @@ -37,14 +37,14 @@ class RequestHandler(BaseHTTPRequestHandler): self.end_headers() return - """ - /connect/right - /connect/left - - with body : {ip:"string",port:number} - """ def do_POST(self): + """ + /connect/right + /connect/left + + with body : {ip:"string",port:number} + """ if self.path.startswith("/connect/"): direction = re.findall("/connect/(left|right)", self.path, flags=re.IGNORECASE)[0] data_string = self.rfile.read(int(self.headers['Content-Length'])) @@ -59,7 +59,9 @@ class RequestHandler(BaseHTTPRequestHandler): self.send_response(200) self.send_header('Content-Type', 'application/json') self.end_headers() - self.wfile.write(json.dumps(asdict(neighbour)).encode('utf8')) + body = asdict(neighbour) + body["counter"]= self.game_state.counter + self.wfile.write(json.dumps(body).encode('utf8')) return else: self.send_response(307) diff --git a/Code/Main.py b/Code/Main.py index b477a7b..ec6ba36 100644 --- a/Code/Main.py +++ b/Code/Main.py @@ -33,16 +33,18 @@ if __name__ == "__main__": own_port = 8080 neighbours = Neighbours(own_process=Member(own_ip, own_port)) - game_state = GameState(neighbours) - server = Server(neighbours, game_state) + counter = 0 n_direction = Direction.LEFT if len(args) > 5: n_direction = args[5] if len(args) >= 5: n_ip = args[3] n_port = int(args[4]) - neighbours.connect(Direction[n_direction], n_ip, n_port) + counter= neighbours.connect(Direction[n_direction], n_ip, n_port) + + game_state = GameState(neighbours,counter) + server = Server(neighbours, game_state) serverThread = threading.Thread(target=server.start) serverThread.start() diff --git a/Code/UI/PlayingField.py b/Code/UI/PlayingField.py index 6d6e83f..9c29468 100644 --- a/Code/UI/PlayingField.py +++ b/Code/UI/PlayingField.py @@ -11,10 +11,10 @@ from Code.UI.Shape import Shape class GameState: - def __init__(self, neighbours: Neighbours): + def __init__(self, neighbours: Neighbours,counter): self.neighbours = neighbours - self.counter = 0 + self.counter = counter self.run = True self.is_evolving = False self.field = Field() @@ -75,7 +75,7 @@ class GameState: self.counter_old = self.counter -def run_game(game_state: GameState): +def run_game(game_state: GameState,counter = 0): pygame.init() pygame.display.set_caption(GeneralConfig.window_caption) window = pygame.display.set_mode((GeneralConfig.width, GeneralConfig.height))