Enable server-side network mode.
[GoMoku3D.git] / src / gui / ServerSettingsDialog.cpp
blob511adb90716ed8bc58f0d762020d83da4450dcb3
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)
31 : QDialog(parent)
32 , Ui_ServerSettingsDialog(*this)
34 setupUi(this);
35 ServerSettings set;
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") {
52 human_0->click();
54 else {
55 if (info[0].type() == "A") {
56 ai_0->click();
58 else {
59 remote_0->click();
63 if (info[1].type() == "H") {
64 human_1->click();
66 else {
67 if (info[1].type() == "A") {
68 ai_1->click();
70 else {
71 remote_1->click();
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") {
80 human_2->click();
82 else {
83 if (info[2].type() == "A") {
84 ai_2->click();
86 else {
87 remote_2->click();
91 else {
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);
113 return;
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);
124 return;
126 if (myname->isEnabled() && myname->text() == "") {
127 QMessageBox::warning(this, tr("Warning"), tr("You cannot have an empty name"), QMessageBox::Ok);
128 return;
131 ServerSettings set;
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);
139 QString p0Type;
140 QString p1Type;
142 if (human_0->isChecked()) {
143 p0Type = "H";
145 else {
146 if (ai_0->isChecked()) {
147 p0Type = "A";
149 else {
150 p0Type = "R";
153 if (human_1->isChecked()) {
154 p1Type = "H";
156 else {
157 if (ai_1->isChecked()) {
158 p1Type = "A";
160 else {
161 p1Type = "R";
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);
169 QString p2Type;
170 if (human_2->isChecked()) {
171 p2Type = "H";
173 else {
174 if (ai_2->isChecked()) {
175 p2Type = "A";
177 else {
178 p2Type = "R";
181 info.append(PlayerInfo(p2Name, p2Color, p2Type));
183 set.setPlayersInfo(info);
184 if (timerCheckBox->isChecked()) {
185 set.setTimerDuration(timerSpinBox->value());
187 else {
188 set.setTimerDuration(0);
190 uint port = serverPortLineEdit->text().toUInt();
191 if (port != 0) {
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)
232 if (number == 0) {
233 frame_3->setEnabled(false);
235 else {
236 frame_3->setEnabled(true);
240 void ServerSettingsDialog::colorSlot()
242 QPushButton *button = qobject_cast<QPushButton*>(sender());
243 if (!button) {
244 return;
246 bool ok;
247 QRgb chosenColor = QColorDialog::getRgba(button->palette().color(QPalette::Button).rgba(), &ok, this);
248 if (ok) {
249 button->setPalette(QPalette(QColor::fromRgba(chosenColor)));
253 void ServerSettingsDialog::removePlayer(int id)
255 if (id == 0) {
256 playername_0->clear();
258 if (id == 1) {
259 playername_1->clear();
261 if (id == 2) {
262 playername_2->clear();
266 void ServerSettingsDialog::addPlayer(int id, QString name, QString type)
268 if (id == 0) {
269 playername_0->setText(name);
270 if (type == "A") {
271 remote_0->setChecked(false);
272 ai_0->setChecked(true);
274 else {
275 remote_0->setChecked(true);
276 ai_0->setChecked(false);
279 if (id == 1) {
280 playername_1->setText(name);
281 if (type == "A") {
282 remote_1->setChecked(false);
283 ai_1->setChecked(true);
285 else {
286 remote_1->setChecked(true);
287 ai_1->setChecked(false);
290 if (id == 2) {
291 playername_2->setText(name);
292 if (type == "A") {
293 remote_2->setChecked(false);
294 ai_2->setChecked(true);
296 else {
297 remote_2->setChecked(true);
298 ai_2->setChecked(false);
303 void ServerSettingsDialog::gameStarted()
305 QDialog::accept();