From c4c1723205b19b1dee7d95357b9f4d544163deea Mon Sep 17 00:00:00 2001 From: Max-Wilhelm Bruker Date: Tue, 7 Sep 2010 03:51:48 +0200 Subject: [PATCH] single player improvements --- cockatrice/src/counter.cpp | 23 +++++++++++--- cockatrice/src/counter.h | 3 ++ cockatrice/src/localserver.cpp | 1 + cockatrice/src/player.cpp | 51 +++++++++++++++++++++++-------- cockatrice/src/player.h | 2 ++ cockatrice/src/tab_game.cpp | 14 +++++++-- cockatrice/src/tab_supervisor.cpp | 24 ++++++++++++--- cockatrice/src/tab_supervisor.h | 1 + cockatrice/src/window_main.cpp | 24 ++++++++++++--- cockatrice/src/window_main.h | 4 ++- common/server.cpp | 3 ++ 11 files changed, 121 insertions(+), 29 deletions(-) diff --git a/cockatrice/src/counter.cpp b/cockatrice/src/counter.cpp index bd6f07c5..c3f7a5d8 100644 --- a/cockatrice/src/counter.cpp +++ b/cockatrice/src/counter.cpp @@ -42,11 +42,24 @@ void Counter::retranslateUi() { if (menu) { aSet->setText(tr("&Set counter...")); - if (name == "life") { - aSet->setShortcut(tr("Ctrl+L")); - aDec->setShortcut(tr("F11")); - aInc->setShortcut(tr("F12")); - } + } +} + +void Counter::setShortcutsActive() +{ + if (name == "life") { + aSet->setShortcut(tr("Ctrl+L")); + aDec->setShortcut(tr("F11")); + aInc->setShortcut(tr("F12")); + } +} + +void Counter::setShortcutsInactive() +{ + if (name == "life") { + aSet->setShortcut(QKeySequence()); + aDec->setShortcut(QKeySequence()); + aInc->setShortcut(QKeySequence()); } } diff --git a/cockatrice/src/counter.h b/cockatrice/src/counter.h index 0556250b..053e75da 100644 --- a/cockatrice/src/counter.h +++ b/cockatrice/src/counter.h @@ -37,6 +37,9 @@ public: QString getName() const { return name; } int getValue() const { return value; } void setValue(int _value); + + void setShortcutsActive(); + void setShortcutsInactive(); }; #endif diff --git a/cockatrice/src/localserver.cpp b/cockatrice/src/localserver.cpp index dabf2c38..e1e98b47 100644 --- a/cockatrice/src/localserver.cpp +++ b/cockatrice/src/localserver.cpp @@ -13,5 +13,6 @@ LocalServer::~LocalServer() LocalServerInterface *LocalServer::newConnection() { LocalServerInterface *lsi = new LocalServerInterface(this); + addClient(lsi); return lsi; } diff --git a/cockatrice/src/player.cpp b/cockatrice/src/player.cpp index c2aa6d3c..520a0aa7 100644 --- a/cockatrice/src/player.cpp +++ b/cockatrice/src/player.cpp @@ -300,19 +300,12 @@ void Player::retranslateUi() aMoveRfgToHand->setText(tr("Move to &hand")); aMoveRfgToGrave->setText(tr("Move to g&raveyard")); aViewLibrary->setText(tr("&View library")); - aViewLibrary->setShortcut(tr("F3")); aViewTopCards->setText(tr("View &top cards of library...")); - aViewTopCards->setShortcut(tr("Ctrl+W")); - aViewGraveyard->setShortcut(tr("F4")); aViewSideboard->setText(tr("&View sideboard")); aDrawCard->setText(tr("&Draw card")); - aDrawCard->setShortcut(tr("Ctrl+D")); aDrawCards->setText(tr("D&raw cards...")); - aDrawCards->setShortcut(tr("Ctrl+E")); - aMulligan->setText(tr("Take &mulligan")); - aMulligan->setShortcut(tr("Ctrl+M")); + aMulligan->setText(tr("Take &mulligan")); aShuffle->setText(tr("&Shuffle")); - aShuffle->setShortcut(tr("Ctrl+S")); handMenu->setTitle(tr("&Hand")); sbMenu->setTitle(tr("&Sideboard")); @@ -320,13 +313,9 @@ void Player::retranslateUi() countersMenu->setTitle(tr("&Counters")); aUntapAll->setText(tr("&Untap all permanents")); - aUntapAll->setShortcut(tr("Ctrl+U")); aRollDie->setText(tr("R&oll die...")); - aRollDie->setShortcut(tr("Ctrl+I")); aCreateToken->setText(tr("&Create token...")); - aCreateToken->setShortcut(tr("Ctrl+T")); aCreateAnotherToken->setText(tr("C&reate another token")); - aCreateAnotherToken->setShortcut(tr("Ctrl+G")); sayMenu->setTitle(tr("S&ay")); QMapIterator counterIterator(counters); @@ -341,6 +330,44 @@ void Player::retranslateUi() zoneIterator.next().value()->retranslateUi(); } +void Player::setShortcutsActive() +{ + aViewLibrary->setShortcut(tr("F3")); + aViewTopCards->setShortcut(tr("Ctrl+W")); + aViewGraveyard->setShortcut(tr("F4")); + aDrawCard->setShortcut(tr("Ctrl+D")); + aDrawCards->setShortcut(tr("Ctrl+E")); + aMulligan->setShortcut(tr("Ctrl+M")); + aShuffle->setShortcut(tr("Ctrl+S")); + aUntapAll->setShortcut(tr("Ctrl+U")); + aRollDie->setShortcut(tr("Ctrl+I")); + aCreateToken->setShortcut(tr("Ctrl+T")); + aCreateAnotherToken->setShortcut(tr("Ctrl+G")); + + QMapIterator counterIterator(counters); + while (counterIterator.hasNext()) + counterIterator.next().value()->setShortcutsActive(); +} + +void Player::setShortcutsInactive() +{ + aViewLibrary->setShortcut(QKeySequence()); + aViewTopCards->setShortcut(QKeySequence()); + aViewGraveyard->setShortcut(QKeySequence()); + aDrawCard->setShortcut(QKeySequence()); + aDrawCards->setShortcut(QKeySequence()); + aMulligan->setShortcut(QKeySequence()); + aShuffle->setShortcut(QKeySequence()); + aUntapAll->setShortcut(QKeySequence()); + aRollDie->setShortcut(QKeySequence()); + aCreateToken->setShortcut(QKeySequence()); + aCreateAnotherToken->setShortcut(QKeySequence()); + + QMapIterator counterIterator(counters); + while (counterIterator.hasNext()) + counterIterator.next().value()->setShortcutsInactive(); +} + void Player::initSayMenu() { sayMenu->clear(); diff --git a/cockatrice/src/player.h b/cockatrice/src/player.h index f4f3c869..e2532513 100644 --- a/cockatrice/src/player.h +++ b/cockatrice/src/player.h @@ -185,6 +185,8 @@ public: QMenu *getCardMenu() const; bool getActive() const { return active; } void setActive(bool _active); + void setShortcutsActive(); + void setShortcutsInactive(); qreal getMinimumWidth() const; void setMirrored(bool _mirrored); diff --git a/cockatrice/src/tab_game.cpp b/cockatrice/src/tab_game.cpp index 697173f9..f8db8b3c 100644 --- a/cockatrice/src/tab_game.cpp +++ b/cockatrice/src/tab_game.cpp @@ -328,8 +328,10 @@ Player *TabGame::addPlayer(int playerId, const QString &playerName) AbstractClient *client; if (clients.size() > 1) client = clients.at(playerId); - else + else { client = clients.first(); + newPlayer->setShortcutsActive(); + } DeckViewContainer *deckView = new DeckViewContainer(client, this); connect(deckView, SIGNAL(newCardAdded(AbstractCardItem *)), this, SLOT(newCardAdded(AbstractCardItem *))); deckViewContainers.insert(playerId, deckView); @@ -590,7 +592,15 @@ Player *TabGame::setActivePlayer(int id) QMapIterator i(players); while (i.hasNext()) { i.next(); - i.value()->setActive(i.value() == player); + if (i.value() == player) { + i.value()->setActive(true); + if (clients.size() > 1) + i.value()->setShortcutsActive(); + } else { + i.value()->setActive(false); + if (clients.size() > 1) + i.value()->setShortcutsInactive(); + } } currentPhase = -1; emit userEvent(); diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 1d151bb4..5effbee6 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -20,6 +20,7 @@ TabSupervisor:: TabSupervisor(QWidget *parent) TabSupervisor::~TabSupervisor() { + stop(); delete tabChangedIcon; } @@ -78,11 +79,22 @@ void TabSupervisor::startLocal(const QList &_clients) void TabSupervisor::stop() { - if (!client) + if ((!client) && localClients.isEmpty()) return; - disconnect(client, 0, this, 0); + if (client) { + disconnect(client, 0, this, 0); + client = 0; + } + if (!localClients.isEmpty()) { + for (int i = 0; i < localClients.size(); ++i) + localClients[i]->deleteLater(); + localClients.clear(); + + emit localGameEnded(); + } + clear(); delete tabServer; @@ -93,12 +105,12 @@ void TabSupervisor::stop() QMapIterator chatChannelIterator(chatChannelTabs); while (chatChannelIterator.hasNext()) - delete chatChannelIterator.next().value(); + chatChannelIterator.next().value()->deleteLater(); chatChannelTabs.clear(); QMapIterator gameIterator(gameTabs); while (gameIterator.hasNext()) - delete gameIterator.next().value(); + gameIterator.next().value()->deleteLater(); gameTabs.clear(); } @@ -128,7 +140,6 @@ void TabSupervisor::localGameJoined(Event_GameJoined *event) setCurrentWidget(tab); for (int i = 1; i < localClients.size(); ++i) { - qDebug("JOINING"); Command_JoinGame *cmd = new Command_JoinGame(event->getGameId()); localClients[i]->sendCommand(cmd); } @@ -140,6 +151,9 @@ void TabSupervisor::gameLeft(TabGame *tab) gameTabs.remove(tab->getGameId()); removeTab(indexOf(tab)); + + if (!localClients.isEmpty()) + stop(); } void TabSupervisor::addChatChannelTab(const QString &channelName) diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index 6c743abb..58ad3c19 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -36,6 +36,7 @@ public: int getGameCount() const { return gameTabs.size(); } signals: void setMenu(QMenu *menu); + void localGameEnded(); private slots: void updateCurrent(int index); void updatePingTime(int value, int max); diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index 6d1c3893..c815c791 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -48,10 +48,12 @@ void MainWindow::statusChanged(ClientStatus _status) break; case StatusDisconnected: tabSupervisor->stop(); + aSinglePlayer->setEnabled(true); aConnect->setEnabled(true); aDisconnect->setEnabled(false); break; case StatusLoggingIn: + aSinglePlayer->setEnabled(false); aConnect->setEnabled(false); aDisconnect->setEnabled(true); break; @@ -85,13 +87,17 @@ void MainWindow::actSinglePlayer() if (!ok) return; - LocalServer *ls = new LocalServer(this); - LocalServerInterface *mainLsi = ls->newConnection(); + aConnect->setEnabled(false); + aSinglePlayer->setEnabled(false); + + localServer = new LocalServer(this); + LocalServerInterface *mainLsi = localServer->newConnection(); LocalClient *mainClient = new LocalClient(mainLsi, tr("Player %1").arg(1), this); + QList localClients; localClients.append(mainClient); for (int i = 0; i < numberPlayers - 1; ++i) { - LocalServerInterface *slaveLsi = ls->newConnection(); + LocalServerInterface *slaveLsi = localServer->newConnection(); LocalClient *slaveClient = new LocalClient(slaveLsi, tr("Player %1").arg(i + 2), this); localClients.append(slaveClient); } @@ -101,6 +107,15 @@ void MainWindow::actSinglePlayer() mainClient->sendCommand(createCommand); } +void MainWindow::localGameEnded() +{ + delete localServer; + localServer = 0; + + aConnect->setEnabled(true); + aSinglePlayer->setEnabled(true); +} + void MainWindow::actDeckEditor() { WndDeckEditor *deckEditor = new WndDeckEditor(this); @@ -214,7 +229,7 @@ void MainWindow::createMenus() } MainWindow::MainWindow(QWidget *parent) - : QMainWindow(parent), tabMenu(0) + : QMainWindow(parent), tabMenu(0), localServer(0) { QPixmapCache::setCacheLimit(200000); @@ -227,6 +242,7 @@ MainWindow::MainWindow(QWidget *parent) tabSupervisor = new TabSupervisor; connect(tabSupervisor, SIGNAL(setMenu(QMenu *)), this, SLOT(updateTabMenu(QMenu *))); + connect(tabSupervisor, SIGNAL(localGameEnded()), this, SLOT(localGameEnded())); setCentralWidget(tabSupervisor); diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index 979994e2..a85d4182 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -27,6 +27,7 @@ class TabSupervisor; class RemoteClient; class LocalClient; +class LocalServer; class MainWindow : public QMainWindow { Q_OBJECT @@ -37,6 +38,7 @@ private slots: void serverError(ResponseCode r); void socketError(const QString &errorStr); void protocolVersionMismatch(int localVersion, int remoteVersion); + void localGameEnded(); void actConnect(); void actDisconnect(); @@ -56,7 +58,7 @@ private: TabSupervisor *tabSupervisor; RemoteClient *client; - QList localClients; + LocalServer *localServer; public: MainWindow(QWidget *parent = 0); protected: diff --git a/common/server.cpp b/common/server.cpp index 25e476d8..9471dc4f 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -22,6 +22,7 @@ #include "server_counter.h" #include "server_chatchannel.h" #include "server_protocolhandler.h" +#include Server::Server(QObject *parent) : QObject(parent), nextGameId(0) @@ -30,6 +31,8 @@ Server::Server(QObject *parent) Server::~Server() { + while (!clients.isEmpty()) + delete clients.takeFirst(); } Server_Game *Server::createGame(const QString &description, const QString &password, int maxPlayers, bool spectatorsAllowed, bool spectatorsNeedPassword, bool spectatorsCanTalk, bool spectatorsSeeEverything, Server_ProtocolHandler *creator)