-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKarutaClient.py
65 lines (57 loc) · 1.47 KB
/
KarutaClient.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
import socket
import sys
import time
class KarutaClient:
def __init__(self, HOST, PORT, player):
self.player = ''
self.HOST = HOST
self.PORT = PORT
received = self.sendMessage('join')
if received[:2] == 'p1':
self.player = 'p1'
cards = received[3:].split(',')
orders = [int(i) for i in cards]
self.cards = orders[:100]
self.order = orders[100:]
print 'connected as p1'
elif received[:2] == 'p2':
self.player = 'p2'
cards = received[3:].split(',')
orders = [int(i) for i in cards]
self.cards = orders[:100]
self.order = orders[100:]
print 'connected as p2'
else:
self.player = player
cards = received[3:].split(',')
orders = [int(i) for i in cards]
self.cards = orders[:100]
self.order = orders[100:]
print 'connected as ' + self.player
def sendMessage(self,message):
if message == 'join' or not self.player == '':
message = self.player+message
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
# Connect to server and send data
sock.connect((self.HOST, self.PORT))
sock.sendall(message)
# Receive data from the server and shut down
received = sock.recv(1024)
finally:
sock.close()
return received
else:
return ''
'''
HOST, PORT = "localhost", 9999
cl = KarutaClient()
cl.sendMessage('hello')
cl.sendMessage('wassup')
response = True
count = 0
while not response == '':
response = cl.sendMessage('get,'+str(count))
print response
count = count + 1
'''