-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmia_client.py
More file actions
52 lines (44 loc) · 1.36 KB
/
Copy pathmia_client.py
File metadata and controls
52 lines (44 loc) · 1.36 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env python
import socket
import random
SERVERHOST = '192.168.0.14'
SERVERPORT = 5000
LOCALIP = '127.0.0.1'
LOCALPORT = 5010
LOCALNAME = "Python Starter Bot"
def connect_to_miaserver(sock):
sock.settimeout(2)
while True:
sock.sendto("REGISTER;" + LOCALNAME, (SERVERHOST, SERVERPORT))
try:
data = sock.recv(5010)
if "REGISTERED" in data:
break
else:
print "Received '" + data + "'"
except socket.timeout:
print "MIA Server does not respond, retrying"
print "Registered at MIA Server"
sock.setblocking(1)
def play_mia(sock):
announced = None
while True:
data = sock.recv(1024)
if data.startswith("ROUND STARTING;"):
_, _, token = data.strip().partition(";")
sock.sendto("JOIN_ROUND;" + token, (SERVERHOST, SERVERPORT))
announced = None
elif data.startswith("ANNOUNCED;"):
d1, _, d2 = data.strip().split(";")[2].partition(",")
announced = (d1, d2)
elif data.startswith("YOUR TURN;"):
_, _, token = data.strip().partition(";")
if announced == None or random.uniform(0,100) > 30.0:
sock.sendto("ROLL;" + token, (SERVERHOST, SERVERPORT))
def mia_client_start():
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((LOCALIP, LOCALPORT))
connect_to_miaserver(sock)
play_mia(sock)
if __name__ == "__main__":
mia_client_start()