import numpy as np
import socket

host = "84.237.21.36"
port = 5152

def rcvall(sock, n):
    data = bytearray()
    while len(data) < n:
        pack = sock.recv(n - len(data))
        data.extend(pack)
    return data

def square(b, y, x):
    return b[y-1:y+2, x-1:x+2].flatten()

def lmax(data):
    pos1, pos2 = None, None
    for y in range(1, data.shape[0] - 1):
        for x in range(1, data.shape[1] - 1):
            v = data[y, x]
            if v < 3: continue
            if any([n > v for n in square(data, y, x)]): continue
            if pos1 is None: pos1 = (x, y)
            elif pos2 is None: pos2 = (x, y)
            else: break
    return pos1, pos2

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
    sock.connect((host, port))
    for i in range(10):
        sock.send(b"get")
        rc = rcvall(sock, 40002)
        img = np.frombuffer(rc[2:40002], dtype="uint8").reshape(200, 200)
        pos1, pos2 = lmax(img)
        res = np.sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)
        sock.send(f"{res:.1f}".encode())
        print(sock.recv(20).decode())
    sock.send(b"beat")
    print(sock.recv(20).decode())