Debian Codegraph @

src/network/room.cpp

raw symbols SCIP
1 /***************************************************************************
2 * Copyright (C) 2009 by The qGo Project *
3 * *
4 * This file is part of qGo. *
5 * *
6 * qGo is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, see <http://www.gnu.org/licenses/> *
18 * or write to the Free Software Foundation, Inc., *
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 ***************************************************************************/
21
22 #include <QDebug>
23 #include "room.h"
24 #include "listviews.h"
25 #include "talk.h"
26 #include "gamedialog.h"
27 #include "defines.h"
28 #include "connectionwidget.h"
29 #include "playergamelistings.h"
30 #include "boarddispatch.h" //so we can remove observers
31 #include "networkconnection.h"
32
33 #include "ui_connectionwidget.h" // Contains declaration of Ui::ConnectionWidget, needed in Room::Room, FIXME
34
35 Room::Room(NetworkConnection * c)
36 {
37 connection = c; // has to be set before the signals are connected.
38 connectionWidget->room = this;
39
40 gamesListModel = new GamesListModel();
41 gamesView = connectionWidget->ui->gamesView;
42 connectionWidget->gamesListProxyModel->setSourceModel(gamesListModel);
43 gamesView->setColumnWidth ( GC_ID, 40 );
44 gamesView->setColumnWidth ( GC_WHITENAME, 150 );
45 gamesView->setColumnWidth ( GC_WHITERANK, 40 );
46 gamesView->setColumnWidth ( GC_BLACKNAME, 150 );
47 gamesView->setColumnWidth ( GC_BLACKRANK, 40 );
48 gamesView->setColumnWidth ( GC_MOVES, 40 );
49 gamesView->setColumnWidth ( GC_SIZE, 40 );
50 gamesView->setColumnWidth ( GC_HANDICAP, 40 );
51 gamesView->setColumnWidth ( GC_KOMI, 40 );
52 gamesView->setColumnWidth ( GC_BYOMI, 40 );
53 gamesView->setColumnWidth ( GC_FLAGS, 40 );
54 gamesView->setColumnWidth ( GC_OBSERVERS, 40 );
55
56
57 playerListModel = new PlayerListModel();
58 playerView = connectionWidget->ui->playerView;
59 connectionWidget->playerListProxyModel->setSourceModel(playerListModel);
60 playerView->setColumnWidth ( PC_STATUS, 40 );
61 playerView->setColumnWidth ( PC_NAME, 200 );
62 playerView->setColumnWidth ( PC_RANK, 40 );
63 playerView->setColumnWidth ( PC_PLAYING, 40 );
64 playerView->setColumnWidth ( PC_OBSERVING, 40);
65 playerView->setColumnWidth ( PC_WINS, 60 );
66 playerView->setColumnWidth ( PC_LOSSES, 60 );
67 playerView->setColumnWidth ( PC_IDLE, 60 );
68 playerView->setColumnWidth ( PC_COUNTRY, 100 );
69 playerView->horizontalHeader()->setStretchLastSection(true);
70 //playerView->setColumnWidth ( PC_MATCHPREFS, 200 );
71
72 connect(playerView, &QTableView::doubleClicked, this, &Room::slot_playerOpenTalk);
73 connect(playerView, &QTableView::customContextMenuRequested, this, &Room::slot_showPopup);
74 connect(gamesView, &QTableView::doubleClicked, this, &Room::slot_gamesDoubleClicked);
75 connect(gamesView, &QTableView::customContextMenuRequested, this, &Room::slot_showGamesPopup);
76 connect(connectionWidget->ui->refreshPlayersButton, &QPushButton::pressed, this, &Room::slot_refreshPlayers);
77 connect(connectionWidget->ui->refreshGamesButton, &QPushButton::pressed, this, &Room::slot_refreshGames);
78
79 playerView->blockSignals(false);
80 gamesView->blockSignals(false);
81
82 connect(connection,&NetworkConnection::playerListingReceived, this, &Room::recvPlayerListing);
83 connect(connection,&NetworkConnection::gameListingReceived, this, &Room::recvGameListing);
84
85 players = 0;
86 games = 0;
87
88 matchAct = new QAction(tr("Match"), 0);
89 statsAct = new QAction(tr("Stats"), 0);
90 talkAct = new QAction(tr("Talk"), 0);
91 removeFriendAct = new QAction(tr("Remove from Friends"), 0);
92 addFriendAct = new QAction(tr("Add to Friends"), 0);
93 removeWatchAct = new QAction(tr("Remove from Watches"), 0);
94 addWatchAct = new QAction(tr("Add to Watches"), 0);
95 blockAct = new QAction(tr("Block"), 0);
96
97 joinObserveAct = new QAction(tr("Join and Observe"), 0);
98 observeOutsideAct = new QAction(tr("Observe Outside"), 0);
99 }
100
101 Room::~Room()
102 {
103 qDebug("Room::~Room()");
104 /* blockSignals necessary in qt 4.7 otherwise setModel(0) crashes stupidly */
105 playerView->blockSignals(true);
106 gamesView->blockSignals(true);
107 connectionWidget->playerListProxyModel->setSourceModel(NULL);
108 connectionWidget->gamesListProxyModel->setSourceModel(NULL);
109 connectionWidget->room = NULL;
110 while (! playerListModel->items.isEmpty())
111 delete playerListModel->items.takeLast();
112 while (! gamesListModel->items.isEmpty())
113 delete gamesListModel->items.takeLast();
114 delete playerListModel;
115 delete gamesListModel;
116 talkMap.clear();
117 }
118
119 void Room::onError(void)
120 {
121 /* We're going to call this straight from the network dispatch
122 * until we work out any room UI issues */
123 //mainwindow->onConnectionError();
124 }
125
126 void Room::setConnection(NetworkConnection * c)
127 {
128 connection = c;
129 playerListModel->setAccountName(connection->getUsername());
130 }
131
132 void Room::slot_playerOpenTalk(const QModelIndex & index)
133 {
134 QModelIndex sourceIndex = connectionWidget->playerListProxyModel->mapToSource(index);
135 PlayerListing * opponent = playerListModel->playerListingFromIndex(sourceIndex);
136 openTalk(opponent);
137 }
138
139 void Room::slot_showPopup(const QPoint & iPoint)
140 {
141 popup_item = playerView->indexAt(iPoint);
142 if (popup_item == QModelIndex())
143 return;
144 popup_item = connectionWidget->playerListProxyModel->mapToSource(popup_item);
145
146 /* If we have the listing now, we don't need to look it up
147 * again later FIXME */
148 popup_playerlisting = playerListModel->playerListingFromIndex(popup_item);
149 if(popup_playerlisting->name == connection->getUsername())
150 return;
151
152 QMenu * menu = new QMenu(playerView);
153
154 menu->addAction(statsAct);
155 menu->addAction(matchAct);
156 menu->addAction(talkAct);
157 menu->addSeparator();
158 matchAct->setEnabled(!(popup_playerlisting->info.contains("X")));
159 if(popup_playerlisting->friendWatchType == PlayerListing::friended)
160 menu->addAction(removeFriendAct);
161 else
162 menu->addAction(addFriendAct);
163 if(popup_playerlisting->friendWatchType == PlayerListing::watched)
164 menu->addAction(removeWatchAct);
165 else
166 menu->addAction(addWatchAct);
167 menu->addAction(blockAct);
168
169 QAction * result = menu->exec(playerView->mapToGlobal(iPoint));
170 if (result == statsAct)
171 connection->sendStatsRequest(popup_playerlisting);
172 if (result == matchAct)
173 connection->sendMatchInvite(popup_playerlisting);
174 if (result == talkAct)
175 openTalk(popup_playerlisting);
176 if (result == removeFriendAct)
177 connection->removeFriend(popup_playerlisting);
178 if (result == addFriendAct)
179 connection->addFriend(popup_playerlisting);
180 if (result == removeWatchAct)
181 connection->removeWatch(popup_playerlisting);
182 if (result == addWatchAct)
183 connection->addWatch(popup_playerlisting);
184 if (result == blockAct)
185 connection->addBlock(popup_playerlisting);
186 menu->deleteLater();
187 menu = NULL;
188 return;
189 }
190
191 void Room::slot_showGamesPopup(const QPoint & iPoint)
192 {
193 if(!connection)
194 return;
195 popup_item = gamesView->indexAt(iPoint);
196 if (popup_item == QModelIndex())
197 return;
198 popup_item = connectionWidget->gamesListProxyModel->mapToSource(popup_item);
199
200 popup_gamelisting = gamesListModel->gameListingFromIndex(popup_item);
201 if(popup_gamelisting->isRoomOnly)
202 return;
203 QMenu * menu = new QMenu(gamesView);
204 menu->addAction(joinObserveAct);
205 if(connection->supportsObserveOutside())
206 menu->addAction(observeOutsideAct);
207
208 QAction * result = menu->exec(gamesView->mapToGlobal(iPoint));
209 if (result == observeOutsideAct)
210 connection->sendObserveOutside(popup_gamelisting);
211 if (result == joinObserveAct)
212 connection->sendObserve(popup_gamelisting);
213 menu->deleteLater();
214 menu = NULL;
215 return;
216 }
217
218 void Room::openTalk(PlayerListing *listing)
219 {
220 Talk * talk = getTalk(listing);
221 //connection->sendStatsRequest(opponent);
222 /* This is really only for ORO, and let's see if it works but... */
223 if(talk)
224 talk->updatePlayerListing();
225 }
226
227 //observe
228 void Room::slot_gamesDoubleClicked(const QModelIndex & index)
229 {
230 QModelIndex sourceIndex = connectionWidget->gamesListProxyModel->mapToSource(index);
231 const GameListing * g = gamesListModel->gameListingFromIndex(sourceIndex);
232 if(preferences.observe_outside_on_doubleclick &&
233 connection->supportsObserveOutside() && !g->isRoomOnly)
234 connection->sendObserveOutside(g);
235 else
236 connection->sendObserve(g);
237 }
238
239 void Room::slot_refreshGames(void)
240 {
241 connection->sendGamesRequest();
242 }
243
244 void Room::slot_refreshPlayers(void)
245 {
246 connection->sendPlayersRequest();
247 }
248
249 void Room::recvToggle(int type, bool val)
250 {
251 connectionWidget->slot_checkbox(type, val);
252 }
253
254 PlayerListing * Room::getPlayerListing(const QString & name)
255 {
256 //qDebug() << "Room::getPlayerListing() called with argument " << name;
257 PlayerListing * result = playerListModel->getEntry(name);
258 if (result == NULL)
259 {
260 result = new PlayerListing();
261 result->online = true;
262 result->name = name;
263 playerListModel->insertListing(result);
264 }
265 return result;
266 }
267
268 PlayerListing * Room::getPlayerListing(const unsigned int id)
269 {
270 PlayerListing * result = playerListModel->getEntry(id);
271 if (result == NULL)
272 {
273 result = new PlayerListing();
274 result->online = true;
275 result->id = id;
276 playerListModel->insertListing(result);
277 }
278 return result;
279 }
280
281 // The reason to have this function seems to date from performance issues around 2011.
282 /* Here's the deal with this. Tygem has a username and a nickname.
283 * the nickname is displayed and generally present, but not always. The username
284 * seems to always be present. At first glance, it makes sense to lookup by
285 * the username since that seems how the information is stored and it seems
286 * unique to username. However, if we need to display and sort by the
287 * nickname, that introduces overhead in the listviews that are slow enough
288 * already. It might be minor overhead, and it might be really ugly
289 * to search through all the listings to find a particular username like
290 * this, but I think its better since its done rarely. I may regret this
291 * and change it later. */
292 /* FIXME don't we usually say "from" instead of "by" change this */
293 PlayerListing * Room::getPlayerListingByNotNickname(const QString & notnickname)
294 {
295 PlayerListing * result = NULL;
296 /* It is illogical to try to look up things by username first and by "notnickname" second
297 * in this function, but this apparently has some historical reason that I am unaware of. */
298 if (result == NULL)
299 result = playerListModel->getEntry(notnickname);
300 if (result == NULL)
301 playerListModel->getPlayerFromNotNickName(notnickname);
302 if (result == NULL)
303 {
304 result = new PlayerListing();
305 result->online = true;
306 result->notnickname = notnickname;
307 playerListModel->insertListing(result);
308 }
309 return result;
310 }
311
312 GameListing * Room::getGameListing(unsigned int key)
313 {
314 GameListing * result = gamesListModel->getEntry(key);
315 if (result == NULL)
316 {
317 result = new GameListing();
318 result->number = key;
319 gamesListModel->insertListing(result);
320 }
321 return result;
322 }
323
324 /* Called from getNewEntry in BoardDispatchRegistry in networkconnection.cpp */
325 class BoardDispatch * Room::getNewBoardDispatch(unsigned int key)
326 {
327 /* Oro might pass a game code rather than a number here. But
328 * the listings sometimes only have numbers. We need to
329 * look up in game stuff like moves and results by game code.
330 * we don't have the number there. But this means always
331 * passing game codes to this, but then we can't use getGameListing
332 * initially. Even if we use the number on the initial board get/create
333 * and the game code thereafter, assuming there aren't collisions in
334 * the id spaces, we still have to alter this to be aware of the
335 * ORO protocol. Its a fucking mess. I think we better just store
336 * everything by the key and have a separate lookup, code to key */
337 GameListing * listing = getGameListing(key);
338 return new BoardDispatch(connection, listing);
339 }
340
341 void Room::recvPlayerListing(PlayerListing *player)
342 {
343 playerListModel->updateEntry(player);
344
345 if(!player->online)
346 {
347 if(player->playing)
348 {
349 /* FIXME, something should clear this when games end */
350 GameListing * g = getGameListing(player->playing);
351 if(g->black == player)
352 {
353 g->black = NULL;
354 g->_black_name = player->name;
355 g->_black_rank = player->rank;
356 }
357 else if(g->white == player)
358 {
359 g->white = NULL;
360 g->_white_name = player->name;
361 g->_white_rank = player->rank;
362 }
363 }
364 if(player->observing)
365 {
366 /* FIXME remove this check, room_list should be sufficient */
367 /* FIXME, something should clear this when games end */
368 BoardDispatch * b = connection->getIfBoardDispatch(player->observing);
369 if(b)
370 {
371 b->recvObserver(player, false);
372 }
373 if(player->dialog_opened)
374 closeTalk(player);
375 }
376 std::vector<unsigned short>::iterator room_listit = player->room_list.begin();
377 while(room_listit != player->room_list.end())
378 {
379 BoardDispatch * boarddispatch = connection->getIfBoardDispatch(*room_listit);
380 if(boarddispatch)
381 boarddispatch->recvObserver(player, false);
382 else
383 player->room_list.erase(room_listit);
384 room_listit = player->room_list.begin();
385 }
386 if(player->friendWatchType != PlayerListing::none)
387 connection->getAndSetFriendWatchType(player); //removes
388 }
389
390 if(player->online)
391 connection->getAndSetFriendWatchType(player);
392
393 if(player->online && player->dialog_opened)
394 {
395 Talk * t = getIfTalk(player);
396 if(t)
397 t->updatePlayerListing();
398 else
399 qDebug("dialog_opened flag set but no talk dialog");
400 }
401 }
402
403 void Room::recvGameListing(GameListing * game)
404 {
405 gamesListModel->updateListing(game);
406 /* This WAS iffy, we WERE using the other ID registry's
407 * existence to indicate an ORO connection in order
408 * to use a different key. Ugly. Once this works,
409 * we need to find a way to hide this within the
410 * connection. It shouldn't be running around here */
411 /* FIXME, turns out that game_number is better for listings
412 * even if we need the code for joining, etc. Its unique
413 * anyway, so who cares */
414 //if(playerListingIDRegistry)
415 // key = game->game_code;
416 //else
417
418 if(game->running)
419 {
420 connection->checkGameWatched(game);
421 }
422 }
423
424 Talk * Room::getTalk(PlayerListing * opponent)
425 {
426 Talk * result = getIfTalk(opponent);
427 if (result == NULL)
428 {
429 // Create if it does not exist
430 result = new Talk(connection, opponent, this);
431 /* Here's how the talk stuff works for future reference. FIXME.
432 * double clicking on a name or typing a console command (maybe) opens
433 * a talk dialog through the room which also, at that time, might request
434 * stats from the network connection. We also notify
435 * the mainwindow to add the new dialog to the list of talk dialogs */
436 connectionWidget->talkOpened(result);
437 talkMap.insert(opponent, result);
438 }
439 return result;
440 }
441
442 Talk * Room::getIfTalk(const PlayerListing * opponent)
443 {
444 QMap <const PlayerListing *, Talk *>::const_iterator i = talkMap.find(opponent);
445 if(i == talkMap.end())
446 return NULL;
447 else
448 return i.value();
449 }
450
451 void Room::closeTalk(const PlayerListing * opponent)
452 {
453 QMap <const PlayerListing *, Talk *>::iterator i = talkMap.find(opponent);
454 if(i == talkMap.end())
455 return;
456
457 i.value()->deleteLater();
458 talkMap.erase(i);
459 }