-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpython_player_helper
31 lines (29 loc) · 1.16 KB
/
python_player_helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import socket
def recvall(sock, n):
# Helper function to recv n bytes or return None if EOF is hit
data = b''
while len(data) < n:
packet = sock.recv(n - len(data))
if not packet:
return None
data += packet
return data
def main():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
if color == 'white':
# Connect the socket to the port where the server is listening
server_address = ('localhost', 5800)
elif color == 'black':
# Connect the socket to the port where the server is listening
server_address = ('localhost', 5801)
else:
raise Exception("Se giochi o sei bianco oppure sei nero")
sock.connect(server_address)
sock.send(struct.pack('>i', len(player_name)))
sock.send(player_name.encode())
len_bytes = struct.unpack('>i', recvall(sock, 4))[0]
current_state_server_bytes = sock.recv(len_bytes)
json_current_state_server = json.loads(current_state_server_bytes)
move_for_server = convert_move_for_server(move, color)
sock.send(struct.pack('>i', len(move_for_server)))
sock.send(move_for_server.encode())