-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
114 lines (86 loc) · 2.7 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'''
Important Note:
The server is intended to function as a dedicated server.
It is recommended that a client is not run on the same machine as a server.
'''
import socket
import sys
def getinput():
isCleanInput = False
options = ('R', 'P', 'S')
while(isCleanInput is False):
clientInput = raw_input("Enter R, P, or S: ").upper()
if clientInput in options:
isCleanInput = True
return clientInput
def usage():
print 'USAGE: python client.py <ADDRESS> <PORT> <BUFFERSIZE>'
exit(0)
if len(sys.argv) > 1:
if sys.argv[1] in ('-h', '-H', '--help', '--HELP'):
if(sys.argv[1].isDigit()):
address = int(sys.argv[1])
elif(not sys.argv[1].isDigit()):
address = sys.argv[1]
else:
usage()
else:
if len(sys.argv) > 2:
address = sys.argv[1]
if sys.argv[2].isdigit():
port = int(sys.argv[2])
if port < 1000 or port > 65535:
usage()
else:
usage()
if len(sys.argv) > 3:
if sys.argv[3].isdigit():
bufferSize = int(sys.argv[3])
if bufferSize < 32 or bufferSize > 99999:
usage()
else:
bufferSize = 1024
else:
usage()
choices = ('R', 'P', 'S')
target = (address, port)
clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientSocket.connect(target)
status = clientSocket.recv(bufferSize)
player = '-1'
if '0' in status:
player = '1'
elif '1' in status:
player = '2'
print(status)
if 'queue' in status:
while status == 'queue':
print 'The room is full, you have been added to the queue.'
status = clientSocket.recv(bufferSize)
print(status)
print 'You are now connected!'
isPlaying = True
while isPlaying:
clientInput = getinput()
if clientInput:
clientSocket.send(clientInput + str(player))
result = clientSocket.recv(bufferSize)
if 'wait' in result:
print 'Waiting for your opponent!'
result = clientSocket.recv(bufferSize)
result = clientSocket.recv(bufferSize)
if '0' in result:
print 'The match was a draw!'
elif '1' in result and player == '1':
print 'You won!'
elif '1' in result and player == '2':
print 'You lost!'
elif '2' in result and player == '1':
print 'You lost!'
elif '2' in result and player == '2':
print 'You Won!'
print 'Disconnecting to make room for other players. Thank you for playing SPEED-RPS!'
clientSocket.close()
isPlaying = False
def reQueue():
isPlaying = False