-
Notifications
You must be signed in to change notification settings - Fork 0
/
RAClient.py
317 lines (265 loc) · 13.8 KB
/
RAClient.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
__author__ = 'AYeager'
from PyQt4 import QtGui, QtCore, QtNetwork
import socket, ssl, sys, time, winsound
import RenA # this is the actual visual setup file
import Q2logging
import RAProtocol # custom message packaging & sending
_Local_Host = socket.gethostname()
_Local_Host = socket.gethostbyname(_Local_Host)# replace with actual host address
_Server_Host = socket.gethostname() #'54.244.118.196' # replace with actual server address
_Login_Port = 60005
class MainDialog(QtGui.QDialog, RenA.Ui_mainDialog):
"""main GUI object"""
def __init__(self, parent=None):
super(MainDialog, self).__init__(parent)
self.log = Q2logging.out_file_instance('logs/GuiClient/PreLogins/')
global _Local_Host
global _Server_Host
global _Login_Port
self.localHost = _Local_Host
self.serverHost = _Server_Host
self.lPort = _Login_Port
self.iPort = ""
self.oPort = ""
self.loggedIn = False
self.name = ""
self.pingTimer = QtCore.QTimer() # Timer loop for sending stay alive pings
self.oldStyle = True # Used to accommodate String/Tuple message format and message object formats
self.setupUi(self) # Sets up Visual aspect of the Gui
self.appendArt()
self.iThread = inThread(self.iPort, self.localHost, self.log) #instantiate the in thread for establishing SIGNAL connections
# Connections/Signals Main Object
self.connect(self.pingTimer, QtCore.SIGNAL("timeout()"), self.pingServer)
self.connect(self, QtCore.SIGNAL("mainDisplay(QString)"), self.appendDisplay, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("inputBox(QObject)"), self.fillTabs, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("artBox(QObject)"), self.appendArt, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("statusBox(QObject)"), self.appendStatus, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("playSound(QObject)"), self.playSound, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("readySend(QString)"), self.sendMessage, QtCore.Qt.DirectConnection)
self.connect(self, QtCore.SIGNAL("sendOld"), self.sendMessage, QtCore.Qt.DirectConnection)
#self.connect(self, QtCore.SIGNAL("readySend(QString)"), self.sendMessage, QtCore.Qt.DirectConnection)
self.inputBox.returnPressed.connect(self.getUserInput)
# Signal Connection for InThread
self.connect(self.iThread, QtCore.SIGNAL("messageReceived(QString)"), self.handleMessageReceived, QtCore.Qt.DirectConnection)
self.mainDisplay.append('\n'*25+" "*10+'Welcome to the Ren Adventure!!'+'\n'*15)
self.inputBox.setFocus()
self.log.write_line('Main Gui object setup complete')
self.main()
def main(self):
# this must be here to initiate Player interaction and properly activate the objects event loop
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), 'Enter User Name')
self.log.write_line('Display initial prompt for game beginning. "Enter user name"')
def outSocket(self):
# Sets up ssl connection to server on appropriate port.
if self.loggedIn == False:
port = self.lPort # login port
self.log.write_line('Out socket being established with login port %s.' % str(self.lPort))
else:
port = self.oPort # out port set when log in is successful
self.log.write_line('Out socket being established with out port %s.' % str(self.oPort))
# Establish socket & connect
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssl_sock = ssl.wrap_socket(sock, certfile = 'cert.pem')
ssl_sock.connect((_Server_Host, port))
self.log.write_line('SSL socket created & connected')
return ssl_sock
def handleMessageReceived(self, message):
# Message router for incoming messages from the server.
if self.oldStyle == True:
if type(message) == 'tuple': # Message is tuple during standard game play
self.log.write_line('message received was a tuple' + str(message))
message = message[1]
if '_play_' in message: # Message for sound is simple string with tag
self.log.write_line('message received was sound type %s' % message)
sound = str(message).split()
sound = sound[1]
self.playSound(sound)
message = "" # makes item not append to screen
else: # messages during login and before entering room are simple strings
self.log.write_line('message is simple string = %s' % str(message))
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), message )
else: # message is an object type with attributes
self.log.write_line('message is message object')
if hasattr(message, "tags"):
if "mainDisplay" in message.tags:
self.log.write_line('body = %s' & str(message.body))
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), message.body )
if "inputBox" in message.tags:
self.log.write_line('tabs = %s' % str(message.tabs))
self.emit(QtCore.SIGNAL("inputBox(RAProtocol.command)"), message.tabs)
if "artBox" in message.tags:
self.log.write_line('art filename = %s' % str(message.art))
self.emit(QtCore.SIGNAL("artBox(QString)"), message.art)
if "statusBox" in message.tags:
self.log.write_line('status = %s' % str(message.status))
self.emit(QtCore.SIGNAL("statusBox(QString)"), message.status)
if "playSound" in message.tags:
self.log.write_line('sound file = %s' % str(message.sound))
self.emit(QtCore.SIGNAL("playSound"), message.sound)
# if "login" in message.tags:
# if message.body == "accepted":
# self.loggedIn = True
# else:
# self.emit(QtCore.SIGNAL("mainDisplay(QString)"), 'Log-in failed.\nEnter User Name.')
def appendDisplay(self, message):
if message != "":
self.mainDisplay.append("")
self.mainDisplay.append(message)
self.log.write_line('"%s" written to main Display.' % message )
self.mainDisplay.moveCursor(QtGui.QTextCursor.End)
self.inputBox.setFocus()
def fillTabs(self):
pass
def appendStatus(self, message):
pass
def appendArt(self):
self.artBox.clear()
file = open('ASCII_art/Kanye1020.txt')
#self.log.write_line('file for ascci Art printing = %s' % str(file))
picString= ""
for line in file:
picString = line.strip()
self.artBox.append(picString)
self.log.write_line('art written to art display')
def playSound(self, sound):
path = 'sounds/%s.wav' % sound
winsound.PlaySound(path, winsound.SND_FILENAME)
self.log.write_line('sound played = %s' % str(sound))
def getUserInput(self):
"""Captures and distributes User input from inputBox"""
line = self.inputBox.displayText()
self.log.write_line('user input captured = "%s"' % line)
if line != "":
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), line )
self.inputBox.clear()
self.inputBox.setFocus()
if self.loggedIn == False:
self.login(line)
self.log.write_line('sent to login function')
else:
if self.oldStyle == True:
message = str(line)
self.log.write_line('message format simple string')
self.emit(QtCore.SIGNAL("readySend(QString)"), message)
else:
message = RAProtocol.QtCommand(name= str(self.name), body=str(line))
self.log.write_line('message format = object')
def sendMessage(self, message):
message = str(message)
#message = pickle.loads(message)
outSocket = self.outSocket()
if self.oldStyle == False:
# convert message object from QObject to regular object so RAP can handle properly
message = RAProtocol.command(name= self.name, body=message)
self.log.write_line('message packaged as object')
RAProtocol.sendMessage(message, outSocket)
outSocket.close()
self.log.write_line('message sent socket closed')
#self.pingTimer.start(4000)
def pingServer(self):
if self.oldStyle == True:
message = '_ping_'
else:
message = '_ping_'
outSocket= self.outSocket()
RAProtocol.sendMessage(message, outSocket)
outSocket.close()
self.log.write_line('server pinged, keep alive')
self.pingTimer.start(10000)
def login(self, line):
if self.name == "":
self.name= line
self.log.write_line('name captured = %s' % str(self.name))
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), "Enter Password" )
else:
self.password = line
self.log.write_line('password captured = %s' % str(self.password))
if self.oldStyle == True:
loginMessage = str(self.name + ' ' + self.password)
self.log.write_line('login string = "%s"' % loginMessage)
else:
loginMessage = RAProtocol.command(tags=['login'], body= str(self.name + " " + self.password))
self.log.write_line('login object created.')
#self.emit(QtCore.SIGNAL("readySend(QObject)"), loginMessage) # Will likely be used in single port build
self.connect_to_server(loginMessage)
def connect_to_server(self, line): # currently used for login purposes only
outSocket = self.outSocket()
RAProtocol.sendMessage(line, outSocket)
self.log.write_line('login message sent to server awaiting response')
message = RAProtocol.receiveMessage(outSocket) #return message from server validating login
self.log.write_line('response recieved = %s' % str(message))
outSocket.close()
self.log.write_line('login socket closed.')
ports = message
if ports in ['invalid', 'banned_name', "affiliation_get"]: # login fails
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), "That login is invalid. Try Again." )
self.name = ""
self.password = ""
self.log.write_line('login attempt invalid. Reset Name and Password')
else: #logIn accepted process
self.log.write_line('Login Valid changing login file to %s' % str(self.name))
self.log = Q2logging.out_file_instance('logs/GuiClient/PlayLogs/%s' % str(self.name))
ports = ports.split()
self.iPort = int(ports[1])
self.oPort = int(ports[0])
self.log.write_line('in/out ports saved in = %d, out = %d' % (self.iPort, self.oPort))
# set port and host for In thread... Start thread & start ping timer
self.iThread.port = self.iPort
self.iThread.host = self.localHost
self.log.write_line('inThread atributes changed to inPort & localHost')
self.iThread.start()
self.pingTimer.start(10000)
# Login True for proper input box operation from this point forward. Emit to print to display.
self.loggedIn = True
self.log.write_line('thread started, Ping timer Started')
self.emit(QtCore.SIGNAL("mainDisplay(QString)"), "You are now logged in...." )
def shutDown(self):
"""function for proper shutdown of client & client/Server relations"""
self.log.write_line('shutdown function started')
self.iThread.quit()
self.iThread.deleteLater()
self.log.write_line('shutdown successful')
class inThread(QtCore.QThread):
""" Primary thread listens for incoming messages, gets message and puts them
on the inQueue to be processed by the main app.
"""
def __init__(self, port, host, log, parent=None):
super(inThread, self).__init__(parent)
self.log = log
self.host = host
self.port = port
self.log.write_line('inThread instantiated')
def run(self):
self.log.write_line('inThread started')
self.iSocket = self.inSocket()
self.iSocket.listen(4)
self.log.write_line('InSocket created, bound and listening port = %d, host = %s' % (self.port, str(self.host)))
while 1:
# Accept Connection from server
conn, addr = self.iSocket.accept()
message = RAProtocol.receiveMessage(conn)
self.log.write_line('message recieved on inThread = %s' % str(message))
self.emit(QtCore.SIGNAL("messageReceived(QString)"), message)
def inSocket(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((self.host, self.port))
ssl_sock = ssl.wrap_socket(sock, certfile = 'cert.pem')
return ssl_sock
class soundThread(QtCore.QThread):
# Not used to play sound at this time done within Gui threads. Leaving in code as it may need to be used at a later date.
def __init__(self, sound, log, parent=None):
super(soundThread, self).__init__(parent)
self.sound = sound
self.log = log
self.log.write_line('sound thread initiated')
def run(self):
path = 'sounds/%s.wav' % self.sound
winsound.PlaySound(path, winsound.SND_FILENAME)
time.sleep(1)
#self.quit()
self.deleteLater()
self.log.write_line('sound played, pause waited exiting thread')
app = QtGui.QApplication(sys.argv)
form = MainDialog()
form.show()
app.exec_()