1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 This module contains miscealeanous facilities used by BZoo component.
19
20 """
21
22 import math
23 from math import radians, cos, sin
24 import struct
25 import time
26 import socket
27
29 """
30 Returns the current date and time.
31 @return: a formated string of current date and time.
32 @rtype: string.
33 """
34 return (time.strftime("%d/%m/%Y %H:%M"))
35
37 """
38 Displays a list of bytes contained in data using hexa format.
39
40 This fonction is typically called when wanting to debug protocol.
41
42 @param data: The data to be displayed.
43 @type data: Byte array.
44 """
45 chaine = "\n>>>> "
46 data_len = len(data)
47 for ii in range (data_len):
48 octet = struct.unpack ('B', data[ii])
49 chaine = chaine + "%02x "%(octet)
50 if (((ii+1) % 16) == 0):
51 chaine = chaine + "\n>>>> "
52 return chaine
53
55 """
56 Converts euler angles into a 3*3 rotation matrix.
57
58 This fonction is used to lower size of packet sent between client and server.
59
60 @param eul: The euler angle vector.
61 @type eul: 3*1 vector.
62 @return: the corresponding rotation matrix
63 @rtype: 3*3 matrix
64 """
65 [DAlpha, DBeta, DPhi] = eul
66 [Alpha, Beta, Phi] = [radians(DAlpha), radians(DBeta), radians(DPhi)]
67 try:
68 mat = [[cos(Beta)*cos(Phi), cos(Beta)*sin(Phi), -(sin(Beta))],
69 [-cos(Alpha)*sin(Phi)+cos(Phi)*sin(Beta)*sin(Alpha),cos(Alpha)*cos(Phi)+sin(Beta)*sin(Phi)*sin(Alpha),cos(Beta)*sin(Alpha)],
70 [cos(Alpha)*cos(Phi)*sin(Beta)+sin(Phi)*sin(Alpha),cos(Alpha)*sin(Beta)*sin(Phi)-cos(Phi)*sin(Alpha),cos(Alpha)*cos(Beta)]
71 ]
72 except ValueError:
73 print "BzProtocol ERROR: ValueError: [Alpha, Beta, Phi] =", [Alpha, Beta, Phi]
74 mat =[[1.0, 0.0, 0.0],
75 [0.0, 1.0, 0.0],
76 [0.0, 0.0, 1.0]
77 ]
78 return mat
79
81 """
82 Converts a 3*3 rotation matrix into a euler angle vector.
83
84 @param mat: The matrix to convert.
85 @type mat: 3*3 Matrix
86 @return: euler angles [RotX, RotY, RotZ] corresponding to 3*3 matrix.
87 @rtype: 3*1 Vector.
88 """
89
90 if abs((1.0 - abs(mat[0][2])))> 0.001:
91 DBeta = -math.degrees(math.asin(mat[0][2]))
92 cosBeta = math.cos(math.radians(DBeta))
93 DPhi = math.degrees(math.atan2(mat[0][1]/cosBeta, mat[0][0]/cosBeta))
94 DAlpha = math.degrees(math.atan2(mat[1][2]/cosBeta, mat[2][2]/cosBeta))
95 else:
96 if (mat[0][2] > 0.0):
97 sinBeta = -1.0
98 else:
99 sinBeta = 1.0
100 DBeta = math.degrees(math.atan2(sinBeta, 0.0))
101 cosBeta = math.cos(math.radians(DBeta))
102 DPhi= 0.0
103 DAlpha = math.degrees(math.atan2(mat[1][2]/cosBeta, mat[2][2]/cosBeta))
104
105 return [DAlpha, DBeta, DPhi]
106
108 """
109 Opens a connection to IRC server.
110
111 @param pseudo: the pseudo to use for session.
112 @type pseudo: string.
113 @param network: The name of the network to connect to.
114 @type network: string
115 @param channel: The name of the channel to connect to.
116 @type channel: string
117 @param port: the port number to open connection on.
118 @type port: integer
119 @return: the opened socket with the IRC server or None if connection failed.
120 @rtype: socket.socket.
121 """
122
123 carryon = True
124 IRCsocket = None
125
126 try:
127 IRCsocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
128 print "IRC socket created"
129 except socket.error, msg:
130
131 print msg
132 carryon = False
133
134 if carryon == True:
135 try:
136 IRCsocket.connect ( ( network, port ) )
137 print "IRC connection established"
138 except socket.error, msg:
139 IRCsocket.close()
140 IRCsocket = None
141 carryon = False
142 print msg
143
144
145 if carryon == True:
146 try:
147 trash = IRCsocket.recv ( 4096 )
148 IRCsocket.send ( 'NICK BZ%s\r\n'%pseudo )
149 IRCsocket.send ( 'USER BZ%s 0 * :BZoo User\r\n'%pseudo )
150 IRCsocket.send ( 'JOIN %s\r\n'%channel )
151 print "IRC joined as BZ%s"%pseudo
152
153
154 IRCsocket.setblocking(0)
155 except socket.error, (errno, msg):
156 print "IRC Exception %s %s while receiving "%(errno,msg)
157 carryon=False
158
159 return IRCsocket
160
162 """
163 This object is a facility to display message in a multiline form.
164 It is typically used to split long text strings into several lines of text.
165
166 Exemple::
167 # initialisation
168 aDisplay = bzDisplay()
169 aDisplay.QueueMessage ('Hi there ..','JiBe')
170 aDisplay.QueueMessage ('Hi Jibe, nice to see you in here. How are you ?','LOD')
171 aDisplay.QueueMessage ('I'm fine, thank you .. what about you ?','JiBe')
172
173 # display
174 print aDisplay.GetDisplay (nb_lines = 8, nb_car= 28)
175
176 will give the following result::
177
178 JiBe: Hi there ..
179 LOD: Hi Jibe, nice to see you in
180 here. How are you ?
181 JiBe: I'm fine, thank you ..
182 what about you ?
183
184 @ivar messages: list of message [pseudo, text].
185 @type messages: list of [string, string].
186 """
187
189 """
190 @return: N/A
191 @rtype: L{bzDisplay}
192 """
193 self.messages = []
194
195
198
200 nbdeb, nbfin = 0,0
201 idxLigne=0
202 lignes = []
203
204 while idxLigne < maxLigne:
205 nbdeb = nbfin
206 nbfin = nbdeb + maxCar
207
208 while (nbfin < len(string)) and (string[nbfin] != ' ') and (string[nbfin] != '\n'):
209 nbfin = nbfin - 1
210
211
212
213 if (string[nbdeb:nbfin] != ''):
214 lignes.append (string[nbdeb:nbfin])
215
216 idxLigne = idxLigne + 1
217 nbfin = nbfin +1
218 return (lignes)
219
221 """
222 Append a message to the list of message to be displayed.
223 This fonction is typically called when a new incoming message has been received
224 @param pseudo: The pseudo of the player who sent the message.
225 @type pseudo: String.
226 @param message: The message itself.
227 @type message: String
228 """
229
230 self.messages.append ([pseudo, message])
231
232
233
235 """
236 Compute a list of character string showing received messages
237
238 Messages are cesurated so that lines never end in the mid of a word.
239 Messages are formatted so that each received messages starts at the beginning of a line.
240 If messages are too numerous to be displayed on nb_lines lines, only the last nb_lines lines are processed.
241
242 This fonction is typically called when a refresh of a displayed is needed
243
244 @param nb_lines: The pseudo of the player who sent the message.
245 @type nb_lines: Integer.
246 @param nb_car: The message itself.
247 @type nb_car: Integer
248 @return: a list of formatted strings ..
249 @rtype: Array of nb_lines String
250 """
251
252
253 displayed_lines = []
254 nb_remaining_lines = nb_lines
255 nb_used_lines = 0
256
257 if len(self.messages)> 0:
258
259 idx_message = len(self.messages)-1
260 if self.messages[idx_message][0] != '':
261 message = self.messages[idx_message][0] + " : " + self.messages[idx_message][1]
262 else:
263 message = self.messages[idx_message][1]
264
265
266 lines = self.__cesurate__(message, nb_lines, nb_car)
267
268 while (len(lines) <= nb_remaining_lines) and (idx_message > -1):
269
270 lines.reverse()
271 for li in lines:
272 displayed_lines.insert(0,li)
273 nb_remaining_lines = nb_remaining_lines - 1
274 nb_used_lines = nb_used_lines + 1
275 idx_message = idx_message -1
276 if self.messages[idx_message][0] != '':
277 message = self.messages[idx_message][0] + " : " + self.messages[idx_message][1]
278 else:
279 message = self.messages[idx_message][1]
280 lines = self.__cesurate__(message, nb_lines, nb_car)
281
282 return displayed_lines
283
285 """
286 Cleans the display
287 """
288
289 self.messages=[]
290
291
292
293 if __name__ == "__main__":
294
295 print "bzTools"
296 mat = eul2mat([90.0,0.0,0.0])
297 eul = mat2eul(mat)
298