-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
84 lines (79 loc) · 1.97 KB
/
client.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from socket import *
import sys
#host info is args
argv = sys.argv
host = argv[1]
port = int(argv[2])
#command stuff
chunks = []
command = ''
#functions for streamlniing info from server
def numChunks():
global chunks
return len(chunks)
def getMessage(s):
global chunks
global command
message = s.recv(1024)
chunks = message.split('||')
command = chunks[0]
s.send('ok')
#client side sanity checks for command
def checkCommand(str):
if str[0]=='help' and len(str)==1:
return True
elif str[0]=='login' and len(str)==2:
return True
elif str[0]=='remove' and len(str)==3:
if str[1].isdigit() and str[2].isdigit():
return True
else:
return False
elif str[0]=='exit' and len(str)==1:
return True
else:
return False
#method to print help
def printHelp():
print("Commands:")
print("help : Returns a list of commands")
print("login x : attempts to log you into the server with the name 'x'")
print("remove x y : removes x stones from set y")
print("exit : exits from the game")
#launch socket
s = socket(AF_INET, SOCK_STREAM)
print ("Connecting to port:" + str(port))
s.connect((host, port))
playing = True
while playing:
#Connection accepted
try:
#get message
getMessage(s)
#figure out waht to do
if command=='wait':
#if wait, wait
print("Please wait for an opponent.")
elif command=='text':
#print text
print(chunks[1])
elif command=='prompt':
#get prompt
myIn = raw_input("Input:")
check = myIn.split(' ')
send = checkCommand(check)
while send==False:
print("Error in input. Type 'help' for help.")
myIn = raw_input("Input:")
check = myIn.split(' ')
send = checkCommand(check)
if check[0]=='help':
printHelp()
#always send input for prompt if it is valid
s.send(myIn)
except:
#if the user aborts, send an exit prompt and break
s.send('exit')
print("Game closed.")
playing=False
break