1 /********************************************************************
3 * Copyright (C) 2008 Davide Pesavento
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 "ServerSocket.h"
23 #include "ServerSettings.h"
25 #define HANDLER_SIGNATURE(token) \
26 void ServerSocket::parse_##token() { \
27 QString elementName = name().toString(); \
28 qDebug() << "parsing" << elementName; \
29 Q_ASSERT_X(elementName == #token, \
30 (QString("ServerSocket::parse_") + #token + "()").toUtf8().constData(), \
31 ("called while parsing " + elementName).toUtf8().constData()); \
34 ServerSocket::ServerSocket(QTcpSocket
*socket
) : StreamSocket(socket
)
37 QList
<PlayerInfo
> infoList
= set
.playersInfo();
38 for (int i
= 0; i
< infoList
.size(); i
++) {
39 if (infoList
.at(i
).type() == "H") {
40 _localPlayerName
= infoList
.at(i
).name();
45 changeState(Connected
);
48 void ServerSocket::acceptJoin(int id
)
50 Q_ASSERT_X(id
>= -1 && id
<= 2, "ServerSocket::acceptJoin()", "invalid id");
52 writeTextElement("joinACK", QString::number(id
));
53 changeState(AwaitingPlayers
);
56 void ServerSocket::refuseJoin(int cause
)
58 writeTextElement("joinNAK", QString::number(cause
));
61 void ServerSocket::sendGameSettings(int d1
, int d2
, int num
, int timer
, bool playing
)
63 writeStartElement("settings");
64 writeAttribute("playing", playing
? "true" : "false");
65 writeTextElement("difficultyOne", QString::number(d1
));
66 writeTextElement("difficultyTwo", QString::number(d2
));
67 writeTextElement("numberOfPlayers", QString::number(num
));
69 writeTextElement("timerDuration", QString::number(timer
));
73 changeState(AwaitingJoinRequest
);
76 void ServerSocket::sendHistory(QList
<Move
> history
)
78 writeStartElement("history");
80 foreach (m
, history
) {
86 void ServerSocket::sendPlayerJoined(int id
, QString name
, QString type
)
88 Q_ASSERT_X(id
>= 0 && id
<= 2, "ServerSocket::sendPlayerJoined()", "invalid id");
92 } else if (type
== "A") {
94 } else if (type
== "R") {
97 type
= type
.toLower();
98 if (type
!= "human" && type
!= "remote" && type
!= "ai") {
99 LOG("refusing to send <playerJoined> with invalid player type");
103 writeStartElement("playerJoined");
104 writeAttribute("id", QString::number(id
));
105 writeTextElement("name", name
);
106 writeTextElement("type", type
);
110 void ServerSocket::sendPlayerLeft(int id
)
112 Q_ASSERT_X(id
>= 0 && id
<= 2, "ServerSocket::sendPlayerLeft()", "invalid id");
114 writeTextElement("playerLeft", QString::number(id
));
117 void ServerSocket::sendStartGame()
119 changeState(Playing
);
121 writeEmptyElement("startGame");
122 writeEmptyElement("foo"); //FIXME
125 BEGIN_TERMINAL_HANDLER(joinRequest
)
126 QString mode
= attributes().value("gameMode").toString().toLower();
128 QString name
= readElementText();
129 if (state() == AwaitingJoinRequest
) {
130 emit
joinRequested(mode
, name
);
132 LOG("dropping <joinRequest> (state = " + stateString() + ")");