From 802fcfbdb2b204146c558a4bba78010abf8b1e40 Mon Sep 17 00:00:00 2001 From: Davide Pesavento Date: Thu, 20 Mar 2008 00:20:55 +0100 Subject: [PATCH] Connect more signals on the server side. More descriptive warnings in OnlineDialog. --- TODO | 1 + src/gui/MainWindow.cpp | 15 ++++++++++++--- src/gui/MainWindow.h | 1 + src/gui/OnlineDialog.cpp | 6 +++--- src/gui/ServerSettingsDialog.cpp | 20 +++++++++++++++----- src/network/GameServer.cpp | 6 +++++- src/network/GameServer.h | 2 ++ src/network/Network.h | 2 +- 8 files changed, 40 insertions(+), 13 deletions(-) diff --git a/TODO b/TODO index 7e50056..a906066 100644 --- a/TODO +++ b/TODO @@ -5,3 +5,4 @@ *) joinRefused deve passare alla GUI direttamente una QString tradotta *) trasformare i radio groups di OnlineDialog in QLabel col tipo del giocatore *) spostare _localPlayer in Network? +*) perché non c'è più root->unref() nel distruttore?? diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 70c255d..4557356 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -309,7 +309,6 @@ void MainWindow::newOnlineGame() OnlineDialog dialog(this); dialog.exec(); if (dialog.result() == QDialog::Rejected) { - //FIXME e' giusto deallocare _net? if (_net != 0) { delete _net; _net = 0; @@ -400,6 +399,8 @@ void MainWindow::newOnlineGame() p->moveToThread(_loop); } + _net->setupGameLoop(_loop); + //connetto connect(_loop, SIGNAL(turn(int)), this, SLOT(turn(int))); connect(_loop, SIGNAL(turn(int)), _playersWidget, SLOT(updateTurn(int))); @@ -425,13 +426,19 @@ void MainWindow::newServerGame() ServerSettingsDialog dialog(this); dialog.exec(); if (dialog.result() == QDialog::Rejected) { - //TODO FIXME dealloca _net? + if (_net != 0) { + delete _net; + _net = 0; + } return; } + int d1 = dialog.d1Box->currentText().toInt(); int d2 = dialog.d2Box->currentText().toInt(); int numPlayers = dialog.numPlayersBox->currentText().toInt(); + GameMatrix::create(d1, d2, numPlayers); + QString type; QList players; if (dialog.human_0->isChecked()) { @@ -497,6 +504,8 @@ void MainWindow::newServerGame() } connectAISkillSlider(players); + _net->setupGameLoop(_loop); + bool noHuman = true; for (int i = 0; i < _playersInfo.size(); i++) { if (_playersInfo.at(i).type() == "H") { @@ -535,7 +544,7 @@ void MainWindow::newServerGame() connect(_loop, SIGNAL(draw(QList)), this, SLOT(playersDraw(QList))); } - connect(_loop, SIGNAL(turn(int)), _playersWidget, SLOT(updateTurn(int))); + connect(_loop, SIGNAL(turn(int)), _playersWidget, SLOT(updateTurn(int))); //FIXME: perché qui? _loop->start(); } diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index ad8a734..ca5a6b9 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -48,6 +48,7 @@ class MainWindow : public QMainWindow, private Ui_MainWindow Q_OBJECT friend class OnlineDialog; + friend class ServerSettingsDialog; public: MainWindow(); diff --git a/src/gui/OnlineDialog.cpp b/src/gui/OnlineDialog.cpp index 12fa84f..b29a1cf 100644 --- a/src/gui/OnlineDialog.cpp +++ b/src/gui/OnlineDialog.cpp @@ -238,9 +238,9 @@ void OnlineDialog::connectToServer() ipLineEdit->setEnabled(false); portLineEdit->setEnabled(false); - MainWindow *mw = qobject_cast(parent()); - Q_ASSERT(mw->_net == 0); - mw->_net = new GameClient(this, ipLineEdit->text(), serverPort); + MainWindow *mainwin = qobject_cast(parent()); + Q_ASSERT(mainwin->_net == 0); + mainwin->_net = new GameClient(this, ipLineEdit->text(), serverPort); PlSettings->setEnabled(true); } diff --git a/src/gui/ServerSettingsDialog.cpp b/src/gui/ServerSettingsDialog.cpp index e22fda2..9c163e7 100644 --- a/src/gui/ServerSettingsDialog.cpp +++ b/src/gui/ServerSettingsDialog.cpp @@ -24,6 +24,8 @@ #include "ServerSettingsDialog.h" #include "ServerSettings.h" +#include "MainWindow.h" +#include "GameServer.h" ServerSettingsDialog::ServerSettingsDialog(QWidget *parent) : QDialog(parent), Ui_ServerSettingsDialog(*this) @@ -114,11 +116,14 @@ void ServerSettingsDialog::accept() bool b0 = p0Name == "" && !remote_0->isChecked(); bool b1 = p1Name == "" && !remote_1->isChecked(); bool b2 = p2Name == "" && !remote_2->isChecked(); - bool b3 = myname->isEnabled() && myname->text() == ""; - if (b0 || b1 || (numPlayersBox->currentText().toInt() == 3 && b2) || b3) { + if (b0 || b1 || (numPlayersBox->currentText().toInt() == 3 && b2)) { QMessageBox::warning(this, tr("Warning"), tr("Human and AI players cannot have empty name"), QMessageBox::Ok); return; } + if (myname->isEnabled() && myname->text() == "") { + QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok); + return; + } ServerSettings set; @@ -186,8 +191,14 @@ void ServerSettingsDialog::accept() if (spectator->isEnabled() && spectator->isChecked()) { set.setMyName(myname->text()); } - //QDialog::accept(); - //disabilito più o meno tutto + //QDialog::accept(); FIXME ??? + + MainWindow *mainwin = qobject_cast(parent()); + Q_ASSERT(mainwin->_net == 0); + // FIXME: _history è a zero quando lo passo al costruttore e fallisce la Q_ASSERT + //mainwin->_net = new GameServer(this, mainwin->_history); + + //disabilito più o meno tutto color_0->setEnabled(false); color_1->setEnabled(false); color_2->setEnabled(false); @@ -212,7 +223,6 @@ void ServerSettingsDialog::accept() myname->setEnabled(false); dedicatedserver->setEnabled(false); serverPortLineEdit->setEnabled(false); - buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); } diff --git a/src/network/GameServer.cpp b/src/network/GameServer.cpp index f256bd4..7e4909f 100644 --- a/src/network/GameServer.cpp +++ b/src/network/GameServer.cpp @@ -51,6 +51,10 @@ GameServer::GameServer(QWidget *gui, HistoryModel *history) : Network(gui) Q_ASSERT(_pendingConnections.size() == 0); Q_ASSERT(_names.size() == _numberOfPlayers); + connect(this, SIGNAL(playerJoined(int, QString, QString)), gui, SLOT(addPlayer(int, QString, QString))); + connect(this, SIGNAL(playerLeft(int)), gui, SLOT(removePlayer(int))); + connect(this, SIGNAL(startGame()), gui, SLOT(gameStarted())); + // start listening for incoming connections connect(_listener, SIGNAL(newConnection()), this, SLOT(handleIncomingConnection())); if (!_listener->listen(QHostAddress::Any, _settings->serverPort())) { @@ -154,8 +158,8 @@ void GameServer::setupChat(ChatWidget *widget) void GameServer::setupGameLoop(GameLoop *gameLoop) { + connect(gameLoop, SIGNAL(turn(int)), this, SLOT(setTurn(int))); connect(gameLoop, SIGNAL(moved(Move)), this, SLOT(broadcastMove(Move))); - //TODO } void GameServer::handleIncomingConnection() diff --git a/src/network/GameServer.h b/src/network/GameServer.h index ef8bce1..957d429 100644 --- a/src/network/GameServer.h +++ b/src/network/GameServer.h @@ -68,6 +68,8 @@ class GameServer : public Network void setTurn(int playerId); signals: + void playerJoined(int id, QString name, QString type); + void playerLeft(int id); void startGame(); }; diff --git a/src/network/Network.h b/src/network/Network.h index 9ad8647..050f5d7 100644 --- a/src/network/Network.h +++ b/src/network/Network.h @@ -45,7 +45,7 @@ class Network : public QObject public slots: virtual void broadcastMove(Move move) = 0; - private: + protected: QWidget *_gui; }; -- 2.11.4.GIT