import numpy as np
import socket

host = "84.237.21.36"
port = 5152


def recvall(sock, n):
    data = bytearray()
    while len(data) < n:
        packet = sock.recv(n - len(data))
        if not packet:
            return
        data.extend(packet)
    return data


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.connect((host, port))

    for _ in range(10):
        sock.send(b"get")
        bits = recvall(sock, 40002)
        print(len(bits))

        img = np.frombuffer(bits[2:], dtype="uint8").reshape(bits[0], bits[1])

        centers = []
        for y in range(1,img.shape[0]-2):
            for x in range(1,img.shape[1]-2):
                if img[y, x] > img[y, x-1] and img[y, x] > img[y, x+1] and img[y, x] > img[y+1, x] and img[y, x] > img[y-1, x]:
                    centers.append(np.array([y,x]))

    
        res = ((centers[0][0] - centers[1][0]) ** 2 + (centers[0][1] - centers[1][1]) ** 2) ** 0.5
        sock.send(f"{res:.1f}".encode())
        ye = sock.recv(20)
        print(ye)

    sock.send(b'beat')
    print("done:",sock.recv(20))