|
| 1 | +import socket |
| 2 | +import struct |
| 3 | +import json |
| 4 | +import time |
| 5 | +from enum import Enum |
| 6 | + |
| 7 | +# Connection constants |
| 8 | +IPC_HOST = "127.0.0.1" |
| 9 | +IPC_PORT = 27185 |
| 10 | +BYTECODE_LIMIT = 20000 # Default, updated by game |
| 11 | + |
| 12 | + |
| 13 | +class CrossPlayException(Exception): |
| 14 | + def __init__(self, message): |
| 15 | + super().__init__( |
| 16 | + message |
| 17 | + + " (If you are a competitor, please report this to the Battlecode staff.)" |
| 18 | + ) |
| 19 | + |
| 20 | + |
| 21 | +class CrossPlayMethod(Enum): |
| 22 | + INVALID = 0 |
| 23 | + START_TURN = 1 |
| 24 | + END_TURN = 2 |
| 25 | + RC_GET_ROUND_NUM = 3 |
| 26 | + RC_GET_MAP_WIDTH = 4 |
| 27 | + RC_GET_MAP_HEIGHT = 5 |
| 28 | + LOG = 6 |
| 29 | + |
| 30 | + |
| 31 | +class CrossPlayClient: |
| 32 | + def __init__(self): |
| 33 | + self.sock = None |
| 34 | + |
| 35 | + def connect(self): |
| 36 | + while True: |
| 37 | + try: |
| 38 | + self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 | + self.sock.connect((IPC_HOST, IPC_PORT)) |
| 40 | + break |
| 41 | + except ConnectionRefusedError: |
| 42 | + time.sleep(0.1) |
| 43 | + except Exception as e: |
| 44 | + raise CrossPlayException(f"Failed to connect to Java server: {e}") |
| 45 | + |
| 46 | + def send_json(self, data): |
| 47 | + if self.sock is None: |
| 48 | + raise CrossPlayException("Socket not connected") |
| 49 | + |
| 50 | + json_bytes = json.dumps(data).encode("utf-8") |
| 51 | + length = len(json_bytes) |
| 52 | + try: |
| 53 | + self.sock.sendall(struct.pack(">I", length)) |
| 54 | + self.sock.sendall(json_bytes) |
| 55 | + except Exception as e: |
| 56 | + raise CrossPlayException(f"Failed to send data: {e}") |
| 57 | + |
| 58 | + def receive_json(self): |
| 59 | + if self.sock is None: |
| 60 | + raise CrossPlayException("Socket not connected") |
| 61 | + |
| 62 | + try: |
| 63 | + length_bytes = self.recv_exactly(4) |
| 64 | + length = struct.unpack(">I", length_bytes)[0] |
| 65 | + data_bytes = self.recv_exactly(length) |
| 66 | + return json.loads(data_bytes.decode("utf-8")) |
| 67 | + except Exception as e: |
| 68 | + raise CrossPlayException(f"Failed to receive data: {e}") |
| 69 | + |
| 70 | + def recv_exactly(self, n): |
| 71 | + if self.sock is None: |
| 72 | + raise CrossPlayException("Socket not connected") |
| 73 | + |
| 74 | + data = b"" |
| 75 | + while len(data) < n: |
| 76 | + packet = self.sock.recv(n - len(data)) |
| 77 | + if not packet: |
| 78 | + raise CrossPlayException("Socket connection closed unexpectedly") |
| 79 | + data += packet |
| 80 | + return data |
| 81 | + |
| 82 | + def close(self): |
| 83 | + if self.sock: |
| 84 | + self.sock.close() |
| 85 | + self.sock = None |
| 86 | + |
| 87 | + |
| 88 | +# Global client instance |
| 89 | +_client = CrossPlayClient() |
| 90 | + |
| 91 | + |
| 92 | +def connect(): |
| 93 | + _client.connect() |
| 94 | + |
| 95 | + |
| 96 | +def close(): |
| 97 | + _client.close() |
| 98 | + |
| 99 | + |
| 100 | +def send(method: CrossPlayMethod, params=None): |
| 101 | + if params is None: |
| 102 | + params = [] |
| 103 | + |
| 104 | + message = {"method": method.value, "params": params} |
| 105 | + |
| 106 | + _client.send_json(message) |
| 107 | + |
| 108 | + |
| 109 | +def send_and_wait(method: CrossPlayMethod, params=None): |
| 110 | + send(method, params) |
| 111 | + return _client.receive_json() |
| 112 | + |
| 113 | + |
| 114 | +def receive(): |
| 115 | + return _client.receive_json() |
| 116 | + |
0 commit comments