1 /********************************************************************
3 * Copyright (C) 2008 Daniele Battaglia
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
22 #include <QColorDialog>
23 #include <QMessageBox>
25 #include "ServerSettingsDialog.h"
26 #include "ServerSettings.h"
27 #include "MainWindow.h"
28 #include "GameServer.h"
30 ServerSettingsDialog::ServerSettingsDialog(QWidget
*parent
)
32 , Ui_ServerSettingsDialog(*this)
37 _alreadyJoined
= false;
39 d2Box
->setCurrentIndex(set
.difficulty2()-1);
40 d1Box
->setCurrentIndex((set
.difficulty1() - 5) / 2);
42 QList
<PlayerInfo
> info
= set
.playersInfo();
43 numPlayersBox
->setCurrentIndex(info
.size() - 2);
45 playername_0
->setText(info
[0].name());
46 playername_1
->setText(info
[1].name());
48 color_0
->setPalette(QPalette(info
[0].color()));
49 color_1
->setPalette(QPalette(info
[1].color()));
51 if (info
[0].type() == "H") {
55 if (info
[0].type() == "A") {
63 if (info
[1].type() == "H") {
67 if (info
[1].type() == "A") {
75 if (info
.size() == 3) {
76 frame_3
->setEnabled(true);
77 playername_2
->setText(info
[2].name());
78 color_2
->setPalette(QPalette(info
[2].color()));
79 if (info
[2].type() == "H") {
83 if (info
[2].type() == "A") {
92 frame_3
->setEnabled(false);
95 if (set
.timerDuration() != 0) {
96 timerCheckBox
->click();
97 timerSpinBox
->setValue(set
.timerDuration());
100 myname
->setText(set
.myName());
101 serverPortLineEdit
->setText(QString::number(set
.serverPort()));
102 QObject::connect(numPlayersBox
, SIGNAL(currentIndexChanged(int)), this, SLOT(playersChanged(int)));
103 QObject::connect(color_0
, SIGNAL( clicked() ), this, SLOT(colorSlot()));
104 QObject::connect(color_1
, SIGNAL(clicked()), this, SLOT(colorSlot()));
105 QObject::connect(color_2
, SIGNAL(clicked()), this, SLOT(colorSlot()));
108 void ServerSettingsDialog::accept()
110 quint16 serverPort
= serverPortLineEdit
->text().toUShort();
111 if (serverPort
<= 1024) {
112 QMessageBox::warning(this, tr("Warning"), tr("Invalid port number."), QMessageBox::Ok
);
116 QString p0Name
= playername_0
->text();
117 QString p1Name
= playername_1
->text();
118 QString p2Name
= playername_2
->text();
119 bool b0
= p0Name
== "" && !remote_0
->isChecked();
120 bool b1
= p1Name
== "" && !remote_1
->isChecked();
121 bool b2
= p2Name
== "" && !remote_2
->isChecked();
122 if (b0
|| b1
|| (numPlayersBox
->currentText().toInt() == 3 && b2
)) {
123 QMessageBox::warning(this, tr("Warning"), tr("Human and AI players cannot have empty name"), QMessageBox::Ok
);
126 if (myname
->isEnabled() && myname
->text() == "") {
127 QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok
);
133 set
.setDifficulty1(d1Box
->currentText().toInt());
134 set
.setDifficulty2(d2Box
->currentText().toInt());
136 QColor p0Color
= (color_0
->palette()).color(QPalette::Button
);
137 QColor p1Color
= (color_1
->palette()).color(QPalette::Button
);
142 if (human_0
->isChecked()) {
146 if (ai_0
->isChecked()) {
153 if (human_1
->isChecked()) {
157 if (ai_1
->isChecked()) {
164 QList
<PlayerInfo
> info
;
165 info
.append(PlayerInfo(p0Name
, p0Color
, p0Type
));
166 info
.append(PlayerInfo(p1Name
, p1Color
, p1Type
));
167 if (numPlayersBox
->currentText().toInt() == 3) {
168 QColor p2Color
= (color_2
->palette()).color(QPalette::Button
);
170 if (human_2
->isChecked()) {
174 if (ai_2
->isChecked()) {
181 info
.append(PlayerInfo(p2Name
, p2Color
, p2Type
));
183 set
.setPlayersInfo(info
);
184 if (timerCheckBox
->isChecked()) {
185 set
.setTimerDuration(timerSpinBox
->value());
188 set
.setTimerDuration(0);
190 uint port
= serverPortLineEdit
->text().toUInt();
192 set
.setServerPort(port
);
194 if (spectator
->isEnabled() && spectator
->isChecked()) {
195 set
.setMyName(myname
->text());
198 MainWindow
*mainwin
= qobject_cast
<MainWindow
*>(parent());
199 Q_ASSERT(mainwin
->_net
== 0);
200 mainwin
->_net
= new GameServer(this, mainwin
->_history
);
202 //disabilito piĆ¹ o meno tutto
203 color_0
->setEnabled(false);
204 color_1
->setEnabled(false);
205 color_2
->setEnabled(false);
206 playername_0
->setEnabled(false);
207 playername_1
->setEnabled(false);
208 playername_2
->setEnabled(false);
209 human_0
->setEnabled(false);
210 human_1
->setEnabled(false);
211 human_2
->setEnabled(false);
212 remote_0
->setEnabled(false);
213 remote_1
->setEnabled(false);
214 remote_2
->setEnabled(false);
215 ai_0
->setEnabled(false);
216 ai_1
->setEnabled(false);
217 ai_2
->setEnabled(false);
218 numPlayersBox
->setEnabled(false);
219 d1Box
->setEnabled(false);
220 d2Box
->setEnabled(false);
221 timerCheckBox
->setEnabled(false);
222 timerSpinBox
->setEnabled(false);
223 spectator
->setEnabled(false);
224 myname
->setEnabled(false);
225 dedicatedserver
->setEnabled(false);
226 serverPortLineEdit
->setEnabled(false);
227 buttonBox
->button(QDialogButtonBox::Ok
)->setEnabled(false);
230 void ServerSettingsDialog::playersChanged(int number
)
233 frame_3
->setEnabled(false);
236 frame_3
->setEnabled(true);
240 void ServerSettingsDialog::colorSlot()
242 QPushButton
*button
= qobject_cast
<QPushButton
*>(sender());
247 QRgb chosenColor
= QColorDialog::getRgba(button
->palette().color(QPalette::Button
).rgba(), &ok
, this);
249 button
->setPalette(QPalette(QColor::fromRgba(chosenColor
)));
253 void ServerSettingsDialog::removePlayer(int id
)
256 playername_0
->clear();
259 playername_1
->clear();
262 playername_2
->clear();
266 void ServerSettingsDialog::addPlayer(int id
, QString name
, QString type
)
269 playername_0
->setText(name
);
271 remote_0
->setChecked(false);
272 ai_0
->setChecked(true);
275 remote_0
->setChecked(true);
276 ai_0
->setChecked(false);
280 playername_1
->setText(name
);
282 remote_1
->setChecked(false);
283 ai_1
->setChecked(true);
286 remote_1
->setChecked(true);
287 ai_1
->setChecked(false);
291 playername_2
->setText(name
);
293 remote_2
->setChecked(false);
294 ai_2
->setChecked(true);
297 remote_2
->setChecked(true);
298 ai_2
->setChecked(false);
303 void ServerSettingsDialog::gameStarted()