forked from FAForever/client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatter.py
422 lines (321 loc) · 15.7 KB
/
chatter.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#-------------------------------------------------------------------------------
# Copyright (c) 2012 Gael Honorez.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the GNU Public License v3.0
# which accompanies this distribution, and is available at
# http://www.gnu.org/licenses/gpl.html
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#-------------------------------------------------------------------------------
from PyQt4 import QtGui, QtCore
from PyQt4.QtNetwork import QNetworkRequest
from chat._avatarWidget import avatarWidget
from chat import user2name
from fa.replay import replay
import util
import fa
import client
class Chatter(QtGui.QTableWidgetItem):
SORT_COLUMN = 2
AVATAR_COLUMN = 1
RANK_COLUMN = 0
STATUS_COLUMN = 3
RANK_ELEVATION = 0
RANK_FRIEND = 1
RANK_USER = 2
RANK_FOE = 3
RANK_NONPLAYER = 4
'''
A chatter is the representation of a person on IRC, in a channel's nick list. There are multiple chatters per channel.
There can be multiple chatters for every Player in the Client.
'''
def __init__(self, parent, user, lobby, *args, **kwargs):
QtGui.QTableWidgetItem.__init__(self, *args, **kwargs)
# TODO: for now, userflags and ranks aren't properly interpreted :-/ This is impractical if an operator reconnects too late.
self.parent = parent
self.lobby = lobby
if user[0] in self.lobby.OPERATOR_COLORS:
self.elevation = user[0]
else:
self.elevation = None
self.name = user2name(user)
self.avatar = None
self.status = None
self.rating = None
self.country = None
self.league = None
self.clan = ""
self.avatarTip = ""
self.setup()
def setup(self):
self.setText(self.name)
self.setFlags(QtCore.Qt.ItemIsEnabled)
self.setTextAlignment(QtCore.Qt.AlignLeft)
row = self.parent.rowCount()
self.parent.insertRow(row)
self.parent.setItem(row, Chatter.SORT_COLUMN, self)
self.avatarItem = QtGui.QTableWidgetItem()
self.avatarItem.setFlags(QtCore.Qt.ItemIsEnabled)
self.avatarItem.setTextAlignment(QtCore.Qt.AlignHCenter)
self.rankItem = QtGui.QTableWidgetItem()
self.rankItem.setFlags(QtCore.Qt.ItemIsEnabled)
self.rankItem.setTextAlignment(QtCore.Qt.AlignHCenter)
self.statusItem = QtGui.QTableWidgetItem()
self.statusItem.setFlags(QtCore.Qt.ItemIsEnabled)
self.statusItem.setTextAlignment(QtCore.Qt.AlignHCenter)
self.parent.setItem(self.row(), Chatter.RANK_COLUMN, self.rankItem)
self.parent.setItem(self.row(), Chatter.AVATAR_COLUMN, self.avatarItem)
self.parent.setItem(self.row(), Chatter.STATUS_COLUMN, self.statusItem)
self.update()
def isFiltered(self, filter):
if filter in self.clan.lower() or filter in self.name.lower():
return True
return False
def setVisible(self, visible):
if visible:
self.tableWidget().showRow(self.row())
else:
self.tableWidget().hideRow(self.row())
def __ge__(self, other):
''' Comparison operator used for item list sorting '''
return not self.__lt__(other)
def __lt__(self, other):
''' Comparison operator used for item list sorting '''
firstStatus = self.getUserRank(self)
secondStatus = self.getUserRank(other)
# if not same rank sort
if firstStatus != secondStatus:
return firstStatus < secondStatus
# List self below all friends and above every other player
if self.name == self.lobby.client.login: return True
if other.name == self.lobby.client.login: return False
# Default: Alphabetical
return self.name.lower() < other.name.lower()
def getUserRank(self, user):
# TODO: Add subdivision for admin?
if user.elevation:
return self.RANK_ELEVATION
if self.lobby.client.isFriend(user.name):
return self.RANK_FRIEND
if self.lobby.client.isFoe(user.name):
return self.RANK_FOE
if self.lobby.client.isPlayer(user.name):
return self.RANK_USER
return self.RANK_NONPLAYER
def updateAvatar(self):
if self.avatar:
self.avatarTip = self.avatar["tooltip"]
url = self.avatar["url"]
avatarPix = util.respix(url)
if avatarPix :
self.avatarItem.setIcon(QtGui.QIcon(avatarPix))
self.avatarItem.setToolTip(self.avatarTip)
else:
if util.addcurDownloadAvatar(url, self.name) :
self.lobby.nam.get(QNetworkRequest(QtCore.QUrl(url)))
else:
# No avatar set.
self.avatarItem.setIcon(QtGui.QIcon())
self.avatarItem.setToolTip(None)
def update(self):
'''
updates the appearance of this chatter in the nicklist according to its lobby and irc states
'''
country = self.lobby.client.getUserCountry(self.name)
if country != None :
self.setIcon(util.icon("chat/countries/%s.png" % country.lower()))
self.setToolTip(country)
if self.lobby.client.getUserAvatar(self.name) != self.avatar:
self.avatar = self.lobby.client.getUserAvatar(self.name)
self.updateAvatar()
self.rating = self.lobby.client.getUserRanking(self.name)
self.clan = self.lobby.client.getUserClan(self.name)
if self.clan != "":
self.setText("[%s]%s" % (self.clan, self.name))
# Color handling
self.setChatUserColor(self.name)
rating = self.rating
# Status icon handling
if self.name in client.instance.urls:
url = client.instance.urls[self.name]
if url:
if url.scheme() == "fafgame":
self.statusItem.setIcon(util.icon("chat/status/lobby.png"))
self.statusItem.setToolTip("In Game Lobby<br/>" + url.toString())
elif url.scheme() == "faflive":
self.statusItem.setIcon(util.icon("chat/status/playing.png"))
self.statusItem.setToolTip("Playing Game<br/>" + url.toString())
else:
self.statusItem.setIcon(QtGui.QIcon())
self.statusItem.setToolTip("Idle")
# Rating icon choice
# TODO: These are very basic and primitive
if rating != None:
league = self.lobby.client.getUserLeague(self.name)
self.rankItem.setToolTip("Global Rating: " + str(int(rating)))
if league != None :
self.rankItem.setToolTip("Division : " + league["division"] + "\nGlobal Rating: " + str(int(rating)))
if league["league"] == 1 :
self.league = "chat/rank/Aeon_Scout.png"
self.rankItem.setIcon(util.icon("chat/rank/Aeon_Scout.png"))
elif league["league"] == 2 :
self.league = "chat/rank/Aeon_T1.png"
self.rankItem.setIcon(util.icon("chat/rank/Aeon_T1.png"))
elif league["league"] == 3 :
self.league = "chat/rank/Aeon_T2.png"
self.rankItem.setIcon(util.icon("chat/rank/Aeon_T2.png"))
elif league["league"] == 4 :
self.league = "chat/rank/Aeon_T3.png"
self.rankItem.setIcon(util.icon("chat/rank/Aeon_T3.png"))
elif league["league"] == 5 :
self.league = "chat/rank/Aeon_XP.png"
self.rankItem.setIcon(util.icon("chat/rank/Aeon_XP.png"))
else :
self.league = "chat/rank/newplayer.png"
self.rankItem.setIcon(util.icon("chat/rank/newplayer.png"))
else:
self.rankItem.setIcon(util.icon("chat/rank/civilian.png"))
self.rankItem.setToolTip("IRC User")
def setChatUserColor(self, username):
if self.lobby.client.isFriend(username):
if self.elevation in self.lobby.OPERATOR_COLORS:
self.setTextColor(QtGui.QColor(self.lobby.client.getColor("friend_mod")))
return
self.setTextColor(QtGui.QColor(self.lobby.client.getColor("friend")))
return
if self.elevation in self.lobby.OPERATOR_COLORS:
self.setTextColor(QtGui.QColor(self.lobby.OPERATOR_COLORS[self.elevation]))
return
if self.name in self.lobby.client.colors :
self.setTextColor(QtGui.QColor(self.lobby.client.getColor(self.name)))
return
self.setTextColor(QtGui.QColor(self.lobby.client.getUserColor(self.name)))
def doubleClicked(self, item):
# filter yourself
if self.lobby.client.login == self.name:
return
# Chatter name clicked
if item == self:
self.lobby.openQuery(self.name, True) # open and activate query window
elif item == self.statusItem:
if self.name in client.instance.urls:
url = client.instance.urls[self.name]
if url.scheme() == "fafgame":
self.lobby.client.api.joinInGame(self.name)
elif url.scheme() == "faflive":
self.lobby.client.api.viewLiveReplay(self.name)
def pressed(self, item):
menu = QtGui.QMenu(self.parent)
# Actions for stats
actionStats = QtGui.QAction("View Player statistics", menu)
actionSelectAvatar = QtGui.QAction("Select Avatar", menu)
# Actions for Games and Replays
actionReplay = QtGui.QAction("View Live Replay", menu)
actionVaultReplay = QtGui.QAction("View Replays in Vault", menu)
actionJoin = QtGui.QAction("Join in Game", menu)
# actionInvite = QtGui.QAction("Invite to Game", menu)
# Default is all disabled, we figure out what we can do after this
actionReplay.setDisabled(True)
actionJoin.setDisabled(True)
# actionInvite.setDisabled(True)
# Don't allow self to be invited to a game, or join one
if self.lobby.client.login != self.name:
if self.name in client.instance.urls:
url = client.instance.urls[self.name]
if url.scheme() == "fafgame":
actionJoin.setEnabled(True)
elif url.scheme() == "faflive":
actionReplay.setEnabled(True)
# Triggers
actionStats.triggered.connect(lambda : self.lobby.client.api.viewPlayerStats(self.name))
actionSelectAvatar.triggered.connect(lambda : self.lobby.client.api.selectAvatar(self.name))
actionReplay.triggered.connect(lambda : self.lobby.client.api.viewLiveReplay(self.name))
actionVaultReplay.triggered.connect(lambda : self.lobby.client.api.viewVaultReplay(self.name))
actionJoin.triggered.connect(lambda : self.lobby.client.api.joinInGame(self.name))
# actionInvite.triggered.connect(self.invite)
# only for us. Either way, it will display our avatar, not anyone avatar.
if self.lobby.client.login == self.name :
menu.addAction(actionSelectAvatar)
menu.addSeparator()
# power menu
if self.lobby.client.power > 1 :
# admin and mod menus
actionAddAvatar = QtGui.QAction("Assign avatar", menu)
menu.addAction(actionAddAvatar)
actionAddAvatar.triggered.connect(lambda: self.lobby.client.admin_api.addAvatar(self.name))
actionJoinChannel = QtGui.QAction("Join Channel", menu)
menu.addAction(actionJoinChannel)
actionJoinChannel.triggered.connect(lambda: self.lobby.client.admin_api.joinChannel(self.name))
actionKick = QtGui.QAction("Kick", menu)
menu.addAction(actionKick)
actionKick.triggered.connect(lambda: self.lobby.client.admin_api.kick(self.name))
actionKick.setDisabled(1)
if self.lobby.client.power == 2 :
actionCloseFA = QtGui.QAction("Close FA", menu)
menu.addAction(actionCloseFA)
actionCloseFA.triggered.connect(lambda : self.lobby.client.admin_api.closeFA(self.name))
actionCloseLobby = QtGui.QAction("Kick from Lobby", menu)
menu.addAction(actionCloseLobby)
actionCloseLobby.triggered.connect(lambda : self.lobby.client.admin_api.closeLobby(self.name))
menu.addSeparator()
# Adding to menu
menu.addAction(actionStats)
menu.addSeparator()
menu.addAction(actionReplay)
menu.addAction(actionVaultReplay)
menu.addSeparator()
menu.addAction(actionJoin)
# Actions for teams
# actionInviteToTeam = QtGui.QAction("Invite to Team", menu)
# actionInviteToTeam.triggered.connect(self.invite)
# menu.addAction(actionInviteToTeam)
# menu.addSeparator()
# Actions for the Friends List
actionAddFriend = QtGui.QAction("Add friend", menu)
actionRemFriend = QtGui.QAction("Remove friend", menu)
# Actions for the Foes List
actionAddFoe = QtGui.QAction("Add foe", menu)
actionRemFoe = QtGui.QAction("Remove foe", menu)
# Don't allow self to be added or removed from friends or foes
if self.lobby.client.login == self.name:
actionAddFriend.setDisabled(1)
actionRemFriend.setDisabled(1)
actionAddFoe.setDisabled(1)
actionRemFoe.setDisabled(1)
# Enable / Disable actions according to friend status
if self.lobby.client.isFriend(self.name):
actionAddFriend.setDisabled(1)
actionRemFoe.setDisabled(1)
actionAddFoe.setDisabled(1)
else :
actionRemFriend.setDisabled(1)
if self.lobby.client.isFoe(self.name):
actionAddFoe.setDisabled(1)
actionAddFriend.setDisabled(1)
actionRemFriend.setDisabled(1)
else :
actionRemFoe.setDisabled(1)
# Triggers
actionAddFriend.triggered.connect(lambda : self.lobby.client.api.addFriend(self.name))
actionRemFriend.triggered.connect(lambda : self.lobby.client.api.remFriend(self.name))
actionAddFoe.triggered.connect(lambda : self.lobby.client.api.addFoe(self.name))
actionRemFoe.triggered.connect(lambda : self.lobby.client.api.remFoe(self.name))
# Adding to menu
menu.addAction(actionAddFriend)
menu.addAction(actionRemFriend)
menu.addSeparator()
menu.addAction(actionAddFoe)
menu.addAction(actionRemFoe)
# Finally: Show the popup
menu.popup(QtGui.QCursor.pos())
@QtCore.pyqtSlot()
def invite(self):
self.lobby.client.invite(self.name)