main.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import numpy as np
  2. import socket
  3. host = "84.237.21.36"
  4. port = 5152
  5. def rcvall(sock, n):
  6. data = bytearray()
  7. while len(data) < n:
  8. pack = sock.recv(n - len(data))
  9. data.extend(pack)
  10. return data
  11. def square(b, y, x):
  12. return b[y-1:y+2, x-1:x+2].flatten()
  13. def lmax(data):
  14. pos1, pos2 = None, None
  15. for y in range(1, data.shape[0] - 1):
  16. for x in range(1, data.shape[1] - 1):
  17. v = data[y, x]
  18. if v < 3: continue
  19. if any([n > v for n in square(data, y, x)]): continue
  20. if pos1 is None: pos1 = (x, y)
  21. elif pos2 is None: pos2 = (x, y)
  22. else: break
  23. return pos1, pos2
  24. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
  25. sock.connect((host, port))
  26. for i in range(10):
  27. sock.send(b"get")
  28. rc = rcvall(sock, 40002)
  29. img = np.frombuffer(rc[2:40002], dtype="uint8").reshape(200, 200)
  30. pos1, pos2 = lmax(img)
  31. res = np.sqrt((pos1[0] - pos2[0]) ** 2 + (pos1[1] - pos2[1]) ** 2)
  32. sock.send(f"{res:.1f}".encode())
  33. print(sock.recv(20).decode())
  34. sock.send(b"beat")
  35. print(sock.recv(20).decode())