Updated Glossario.tex
[GoMoku3D.git] / src / network / GameClient.cpp
blob7d0a0c3ac409785cad627a162ec56637aa53e199
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 <QTcpSocket>
24 #include "GameClient.h"
25 #include "GameLoop.h"
26 #include "ChatWidget.h"
27 #include "SyncSharedCondition.h"
29 GameClient::GameClient(QWidget *gui, QString serverAddress, quint16 serverPort) : Network(gui)
31 Q_ASSERT_X(gui != 0, "GameClient::GameClient()", "gui must not be null");
33 QTcpSocket *s = new QTcpSocket();
34 _server = new ClientSocket(s);
36 connect(_server, SIGNAL(statusChanged(QString)), gui, SLOT(setStatus(QString)));
37 connect(_server, SIGNAL(receivedGameSettings(int, int, int, int, bool)), gui, SLOT(displaySettings(int, int, int, int, bool)));
38 connect(_server, SIGNAL(joinAccepted(int)), this, SLOT(setLocalPlayer(int)));
39 connect(_server, SIGNAL(joinAccepted(int)), gui, SLOT(acceptedJoinRequest(int)));
40 connect(_server, SIGNAL(joinRefused(QString)), gui, SLOT(refusedJoinRequest(QString)));
41 connect(_server, SIGNAL(playerJoined(int, QString, QString)), gui, SLOT(addPlayer(int, QString, QString)));
42 connect(_server, SIGNAL(playerLeft(int)), gui, SLOT(removePlayer(int)));
43 connect(_server, SIGNAL(receivedHistory(QList<Move>)), gui->parent(), SLOT(updateHistory(QList<Move>)));
44 connect(_server, SIGNAL(startGame()), gui, SLOT(gameStarted()));
45 connect(_server, SIGNAL(receivedMove(Move)), this, SLOT(broadcastMove(Move)));
46 connect(_server, SIGNAL(protocolError(QString)), this, SIGNAL(error(QString)));
47 connect(this, SIGNAL(error(QString)), gui, SLOT(networkError(QString)));
48 connect(gui, SIGNAL(sendJoin(QString, QString)), _server, SLOT(joinGame(QString, QString)));
50 _server->changeState(StreamSocket::Connecting);
51 s->connectToHost(serverAddress, serverPort);
54 GameClient::~GameClient()
56 if (_server) {
57 delete _server;
61 void GameClient::broadcastMove(Move m)
63 // GameLoop sent the move
64 GameLoop *gl = qobject_cast<GameLoop*>(sender());
65 if (gl && (m.playerId() == _localPlayer)) {
66 _server->sendMove(m);
69 // the server sent the move
70 ClientSocket *socket = qobject_cast<ClientSocket*>(sender());
71 if (socket) {
72 QMutexLocker lock(SyncSharedCondition::instance());
73 SyncSharedCondition::instance()->notifyMove(m.point());
77 Point GameClient::requestMove()
79 Point p = _server->takeFirstMove().point();
80 if (p.isNull()) {
81 _server->changeState(StreamSocket::AwaitingMove);
83 return p;
86 void GameClient::setupChat(ChatWidget *widget)
88 if (widget) {
89 connect(_server, SIGNAL(receivedChatMessage(QString, QString)), widget, SLOT(displayMessage(QString, QString)));
90 connect(widget, SIGNAL(textEntered(QString, QString)), _server, SLOT(sendChatMessage(QString, QString)));
94 void GameClient::setupGameLoop(GameLoop *gameLoop)
96 connect(gameLoop, SIGNAL(moved(Move)), this, SLOT(broadcastMove(Move)));
99 void GameClient::setLocalPlayer(int id)
101 Q_ASSERT_X(id >= -1 && id <= 2, "GameClient::setLocalPlayer()", "invalid id");
102 Q_ASSERT_X(_server->state() == StreamSocket::AwaitingJoinAnswer,
103 "GameClient::setLocalPlayer()", "called outside 'AwaitingJoinAnswer' state");
105 _localPlayer = id;