diff --git a/cockatrice/cockatrice.pro b/cockatrice/cockatrice.pro index 330f612c..1faaa3de 100644 --- a/cockatrice/cockatrice.pro +++ b/cockatrice/cockatrice.pro @@ -5,7 +5,7 @@ INCLUDEPATH += . src ../common MOC_DIR = build OBJECTS_DIR = build RESOURCES = cockatrice.qrc -QT += network svg +QT += network svg webkit HEADERS += src/counter.h \ src/dlg_creategame.h \ diff --git a/cockatrice/src/abstractclient.h b/cockatrice/src/abstractclient.h index d5b4dc96..dae91752 100644 --- a/cockatrice/src/abstractclient.h +++ b/cockatrice/src/abstractclient.h @@ -46,6 +46,7 @@ signals: void listRoomsEventReceived(Event_ListRooms *event); void gameJoinedEventReceived(Event_GameJoined *event); void messageEventReceived(Event_Message *event); + void userInfoChanged(ServerInfo_User *userInfo); protected slots: void processProtocolItem(ProtocolItem *item); protected: diff --git a/cockatrice/src/remoteclient.cpp b/cockatrice/src/remoteclient.cpp index 4e3f9724..dd7c12d9 100644 --- a/cockatrice/src/remoteclient.cpp +++ b/cockatrice/src/remoteclient.cpp @@ -42,12 +42,17 @@ void RemoteClient::slotConnected() setStatus(StatusAwaitingWelcome); } -void RemoteClient::loginResponse(ResponseCode response) +void RemoteClient::loginResponse(ProtocolResponse *response) { - if (response == RespOk) + Response_Login *resp = qobject_cast(response); + if (!resp) + disconnectFromServer(); + + if (resp->getResponseCode() == RespOk) { setStatus(StatusLoggedIn); - else { - emit serverError(response); + emit userInfoChanged(resp->getUserInfo()); + } else { + emit serverError(resp->getResponseCode()); setStatus(StatusDisconnecting); } } @@ -78,7 +83,7 @@ void RemoteClient::readData() setStatus(StatusLoggingIn); Command_Login *cmdLogin = new Command_Login(userName, password); - connect(cmdLogin, SIGNAL(finished(ResponseCode)), this, SLOT(loginResponse(ResponseCode))); + connect(cmdLogin, SIGNAL(finished(ProtocolResponse *)), this, SLOT(loginResponse(ProtocolResponse *))); sendCommand(cmdLogin); } } diff --git a/cockatrice/src/remoteclient.h b/cockatrice/src/remoteclient.h index 10d7c417..30313fbb 100644 --- a/cockatrice/src/remoteclient.h +++ b/cockatrice/src/remoteclient.h @@ -2,7 +2,6 @@ #define REMOTECLIENT_H #include -#include "protocol_datastructures.h" #include "abstractclient.h" class QTimer; @@ -22,7 +21,7 @@ private slots: void readData(); void slotSocketError(QAbstractSocket::SocketError error); void ping(); - void loginResponse(ResponseCode response); + void loginResponse(ProtocolResponse *response); private: static const int maxTimeout = 10; diff --git a/cockatrice/src/tab_room.cpp b/cockatrice/src/tab_room.cpp index fbe101a8..381adb78 100644 --- a/cockatrice/src/tab_room.cpp +++ b/cockatrice/src/tab_room.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include "dlg_creategame.h" #include "tab_room.h" #include "userlist.h" @@ -17,6 +18,8 @@ #include "protocol_items.h" #include "gamesmodel.h" +#include + GameSelector::GameSelector(AbstractClient *_client, int _roomId, QWidget *parent) : QGroupBox(parent), client(_client), roomId(_roomId) { @@ -119,14 +122,50 @@ void GameSelector::processGameInfo(ServerInfo_Game *info) gameListModel->updateGameList(info); } -TabRoom::TabRoom(AbstractClient *_client, ServerInfo_Room *info) - : Tab(), client(_client), roomId(info->getRoomId()), roomName(info->getName()) +ChatView::ChatView(const QString &_ownName, QWidget *parent) + : QTextEdit(parent), ownName(_ownName) +{ + setTextInteractionFlags(Qt::TextSelectableByMouse); + + QTextTableFormat format; + format.setBorderStyle(QTextFrameFormat::BorderStyle_None); + table = textCursor().insertTable(1, 3, format); +} + +void ChatView::appendMessage(const QString &sender, const QString &message) +{ + QTextCursor cellCursor = table->cellAt(table->rows() - 1, 0).lastCursorPosition(); + cellCursor.insertText(QDateTime::currentDateTime().toString("[hh:mm]")); + QTextTableCell senderCell = table->cellAt(table->rows() - 1, 1); + QTextCharFormat senderFormat; + if (sender == ownName) { + senderFormat.setFontWeight(QFont::Bold); + senderFormat.setForeground(Qt::red); + } else + senderFormat.setForeground(Qt::blue); + senderCell.setFormat(senderFormat); + cellCursor = senderCell.lastCursorPosition(); + cellCursor.insertText(sender); + QTextTableCell messageCell = table->cellAt(table->rows() - 1, 2); + QTextCharFormat messageFormat; + if (sender.isEmpty()) + messageFormat.setForeground(Qt::darkGreen); + messageCell.setFormat(messageFormat); + cellCursor = messageCell.lastCursorPosition(); + cellCursor.insertText(message); + + table->appendRows(1); + + verticalScrollBar()->setValue(verticalScrollBar()->maximum()); +} + +TabRoom::TabRoom(AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info) + : Tab(), client(_client), roomId(info->getRoomId()), roomName(info->getName()), ownName(_ownName) { gameSelector = new GameSelector(client, roomId); userList = new UserList(false); - textEdit = new QTextEdit; - textEdit->setReadOnly(true); + chatView = new ChatView(ownName); sayLabel = new QLabel; sayEdit = new QLineEdit; sayLabel->setBuddy(sayEdit); @@ -137,7 +176,7 @@ TabRoom::TabRoom(AbstractClient *_client, ServerInfo_Room *info) sayHbox->addWidget(sayEdit); QVBoxLayout *chatVbox = new QVBoxLayout; - chatVbox->addWidget(textEdit); + chatVbox->addWidget(chatView); chatVbox->addLayout(sayHbox); chatGroupBox = new QGroupBox; @@ -221,23 +260,18 @@ void TabRoom::processListGamesEvent(Event_ListGames *event) void TabRoom::processJoinRoomEvent(Event_JoinRoom *event) { - textEdit->append(tr("%1 has joined the room.").arg(sanitizeHtml(event->getUserInfo()->getName()))); + chatView->appendMessage(QString(), tr("%1 has joined the room.").arg(event->getUserInfo()->getName())); userList->processUserInfo(event->getUserInfo()); - emit userEvent(); } void TabRoom::processLeaveRoomEvent(Event_LeaveRoom *event) { - textEdit->append(tr("%1 has left the room.").arg(sanitizeHtml(event->getPlayerName()))); + chatView->appendMessage(QString(), tr("%1 has left the room.").arg(event->getPlayerName())); userList->deleteUser(event->getPlayerName()); - emit userEvent(); } void TabRoom::processSayEvent(Event_RoomSay *event) { - if (event->getPlayerName().isEmpty()) - textEdit->append(QString("%1getMessage()))); - else - textEdit->append(QString("%1: %2").arg(sanitizeHtml(event->getPlayerName())).arg(sanitizeHtml(event->getMessage()))); + chatView->appendMessage(event->getPlayerName(), event->getMessage()); emit userEvent(); } diff --git a/cockatrice/src/tab_room.h b/cockatrice/src/tab_room.h index e61a3831..541b2c57 100644 --- a/cockatrice/src/tab_room.h +++ b/cockatrice/src/tab_room.h @@ -4,6 +4,7 @@ #include "tab.h" #include "protocol_datastructures.h" #include +#include class AbstractClient; class UserList; @@ -12,6 +13,7 @@ class QTextEdit; class QLineEdit; class QTreeView; class QPushButton; +class QTextTable; class QCheckBox; class GamesModel; class GamesProxyModel; @@ -47,16 +49,27 @@ public: void processGameInfo(ServerInfo_Game *info); }; +class ChatView : public QTextEdit { + Q_OBJECT; +private: + QTextTable *table; + QString ownName; +public: + ChatView(const QString &_ownName, QWidget *parent = 0); + void appendMessage(const QString &sender, const QString &message); +}; + class TabRoom : public Tab { Q_OBJECT private: AbstractClient *client; int roomId; QString roomName; + QString ownName; GameSelector *gameSelector; UserList *userList; - QTextEdit *textEdit; + ChatView *chatView; QLabel *sayLabel; QLineEdit *sayEdit; QGroupBox *chatGroupBox; @@ -74,7 +87,7 @@ private slots: void processLeaveRoomEvent(Event_LeaveRoom *event); void processSayEvent(Event_RoomSay *event); public: - TabRoom(AbstractClient *_client, ServerInfo_Room *info); + TabRoom(AbstractClient *_client, const QString &_ownName, ServerInfo_Room *info); ~TabRoom(); void retranslateUi(); void processRoomEvent(RoomEvent *event); diff --git a/cockatrice/src/tab_server.cpp b/cockatrice/src/tab_server.cpp index 438c2281..55972704 100644 --- a/cockatrice/src/tab_server.cpp +++ b/cockatrice/src/tab_server.cpp @@ -116,32 +116,7 @@ void RoomSelector::joinFinished(ProtocolResponse *r) emit roomJoined(resp->getRoomInfo()); } -ServerMessageLog::ServerMessageLog(AbstractClient *_client, QWidget *parent) - : QGroupBox(parent) -{ - textEdit = new QTextEdit; - textEdit->setReadOnly(true); - - QVBoxLayout *vbox = new QVBoxLayout; - vbox->addWidget(textEdit); - - setLayout(vbox); - retranslateUi(); - - connect(_client, SIGNAL(serverMessageEventReceived(Event_ServerMessage *)), this, SLOT(processServerMessageEvent(Event_ServerMessage *))); -} - -void ServerMessageLog::retranslateUi() -{ - setTitle(tr("Server messages")); -} - -void ServerMessageLog::processServerMessageEvent(Event_ServerMessage *event) -{ - textEdit->append(event->getMessage()); -} - -UserInfoBox::UserInfoBox(AbstractClient *_client, QWidget *parent) +UserInfoBox::UserInfoBox(ServerInfo_User *userInfo, QWidget *parent) : QWidget(parent) { avatarLabel = new QLabel; @@ -169,9 +144,7 @@ UserInfoBox::UserInfoBox(AbstractClient *_client, QWidget *parent) setLayout(mainLayout); - Command_GetUserInfo *cmd = new Command_GetUserInfo; - connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(processResponse(ProtocolResponse *))); - _client->sendCommand(cmd); + updateInfo(userInfo); } void UserInfoBox::retranslateUi() @@ -180,12 +153,8 @@ void UserInfoBox::retranslateUi() userLevelLabel1->setText(tr("User level:")); } -void UserInfoBox::processResponse(ProtocolResponse *response) +void UserInfoBox::updateInfo(ServerInfo_User *user) { - Response_GetUserInfo *resp = qobject_cast(response); - if (!resp) - return; - ServerInfo_User *user = resp->getUserInfo(); int userLevel = user->getUserLevel(); QPixmap avatarPixmap; @@ -208,21 +177,20 @@ void UserInfoBox::processResponse(ProtocolResponse *response) userLevelLabel3->setText(userLevelText); } -TabServer::TabServer(AbstractClient *_client, QWidget *parent) +TabServer::TabServer(AbstractClient *_client, ServerInfo_User *userInfo, QWidget *parent) : Tab(parent), client(_client) { roomSelector = new RoomSelector(client); - serverMessageLog = new ServerMessageLog(client); - userInfoBox = new UserInfoBox(client); + serverInfoBox = new QTextBrowser; + userInfoBox = new UserInfoBox(userInfo); userList = new UserList(true); -// connect(gameSelector, SIGNAL(gameJoined(int)), this, SIGNAL(gameJoined(int))); connect(roomSelector, SIGNAL(roomJoined(ServerInfo_Room *)), this, SIGNAL(roomJoined(ServerInfo_Room *))); connect(userList, SIGNAL(openMessageDialog(const QString &, bool)), this, SIGNAL(openMessageDialog(const QString &, bool))); - connect(userList, SIGNAL(userLeft(const QString &)), this, SIGNAL(userLeft(const QString &))); connect(client, SIGNAL(userJoinedEventReceived(Event_UserJoined *)), this, SLOT(processUserJoinedEvent(Event_UserJoined *))); connect(client, SIGNAL(userLeftEventReceived(Event_UserLeft *)), this, SLOT(processUserLeftEvent(Event_UserLeft *))); + connect(client, SIGNAL(serverMessageEventReceived(Event_ServerMessage *)), this, SLOT(processServerMessageEvent(Event_ServerMessage *))); Command_ListUsers *cmd = new Command_ListUsers; connect(cmd, SIGNAL(finished(ProtocolResponse *)), this, SLOT(processListUsersResponse(ProtocolResponse *))); @@ -230,7 +198,7 @@ TabServer::TabServer(AbstractClient *_client, QWidget *parent) QVBoxLayout *vbox = new QVBoxLayout; vbox->addWidget(roomSelector); - vbox->addWidget(serverMessageLog); + vbox->addWidget(serverInfoBox); QVBoxLayout *vbox2 = new QVBoxLayout; vbox2->addWidget(userInfoBox); @@ -246,11 +214,15 @@ TabServer::TabServer(AbstractClient *_client, QWidget *parent) void TabServer::retranslateUi() { roomSelector->retranslateUi(); - serverMessageLog->retranslateUi(); userInfoBox->retranslateUi(); userList->retranslateUi(); } +void TabServer::processServerMessageEvent(Event_ServerMessage *event) +{ + serverInfoBox->setHtml(event->getMessage()); +} + void TabServer::processListUsersResponse(ProtocolResponse *response) { Response_ListUsers *resp = qobject_cast(response); diff --git a/cockatrice/src/tab_server.h b/cockatrice/src/tab_server.h index 47003983..1e883a07 100644 --- a/cockatrice/src/tab_server.h +++ b/cockatrice/src/tab_server.h @@ -3,8 +3,9 @@ #include #include +#include #include "tab.h" -#include "protocol_datastructures.h" +//#include "protocol_datastructures.h" class AbstractClient; class QTextEdit; @@ -17,6 +18,8 @@ class Event_ServerMessage; class Event_UserJoined; class Event_UserLeft; class ProtocolResponse; +class ServerInfo_User; +class ServerInfo_Room; class RoomSelector : public QGroupBox { Q_OBJECT @@ -37,25 +40,13 @@ public: void retranslateUi(); }; -class ServerMessageLog : public QGroupBox { - Q_OBJECT -private: - QTextEdit *textEdit; -private slots: - void processServerMessageEvent(Event_ServerMessage *event); -public: - ServerMessageLog(AbstractClient *_client, QWidget *parent = 0); - void retranslateUi(); -}; - class UserInfoBox : public QWidget { Q_OBJECT private: QLabel *avatarLabel, *nameLabel, *countryLabel1, *countryLabel2, *userLevelLabel1, *userLevelLabel2, *userLevelLabel3; -private slots: - void processResponse(ProtocolResponse *response); + void updateInfo(ServerInfo_User *user); public: - UserInfoBox(AbstractClient *_client, QWidget *parent = 0); + UserInfoBox(ServerInfo_User *userInfo, QWidget *parent = 0); void retranslateUi(); }; @@ -63,21 +54,21 @@ class TabServer : public Tab { Q_OBJECT signals: void roomJoined(ServerInfo_Room *info); -// void gameJoined(int gameId); void openMessageDialog(const QString &userName, bool focus); void userLeft(const QString &userName); private slots: void processListUsersResponse(ProtocolResponse *response); void processUserJoinedEvent(Event_UserJoined *event); void processUserLeftEvent(Event_UserLeft *event); + void processServerMessageEvent(Event_ServerMessage *event); private: AbstractClient *client; RoomSelector *roomSelector; - ServerMessageLog *serverMessageLog; + QTextBrowser *serverInfoBox; UserList *userList; UserInfoBox *userInfoBox; public: - TabServer(AbstractClient *_client, QWidget *parent = 0); + TabServer(AbstractClient *_client, ServerInfo_User *userInfo, QWidget *parent = 0); void retranslateUi(); QString getTabText() const { return tr("Server"); } }; diff --git a/cockatrice/src/tab_supervisor.cpp b/cockatrice/src/tab_supervisor.cpp index 084e9e35..36c7de53 100644 --- a/cockatrice/src/tab_supervisor.cpp +++ b/cockatrice/src/tab_supervisor.cpp @@ -51,16 +51,18 @@ void TabSupervisor::myAddTab(Tab *tab) addTab(tab, tab->getTabText()); } -void TabSupervisor::start(AbstractClient *_client) +void TabSupervisor::start(AbstractClient *_client, ServerInfo_User *userInfo) { client = _client; + userName = userInfo->getName(); + connect(client, SIGNAL(roomEventReceived(RoomEvent *)), this, SLOT(processRoomEvent(RoomEvent *))); connect(client, SIGNAL(gameEventContainerReceived(GameEventContainer *)), this, SLOT(processGameEventContainer(GameEventContainer *))); connect(client, SIGNAL(gameJoinedEventReceived(Event_GameJoined *)), this, SLOT(gameJoined(Event_GameJoined *))); connect(client, SIGNAL(messageEventReceived(Event_Message *)), this, SLOT(processMessageEvent(Event_Message *))); connect(client, SIGNAL(maxPingTime(int, int)), this, SLOT(updatePingTime(int, int))); - tabServer = new TabServer(client); + tabServer = new TabServer(client, userInfo); connect(tabServer, SIGNAL(roomJoined(ServerInfo_Room *)), this, SLOT(addRoomTab(ServerInfo_Room *))); connect(tabServer, SIGNAL(openMessageDialog(const QString &, bool)), this, SLOT(addMessageTab(const QString &, bool))); connect(tabServer, SIGNAL(userLeft(const QString &)), this, SLOT(processUserLeft(const QString &))); @@ -162,7 +164,7 @@ void TabSupervisor::gameLeft(TabGame *tab) void TabSupervisor::addRoomTab(ServerInfo_Room *info) { - TabRoom *tab = new TabRoom(client, info); + TabRoom *tab = new TabRoom(client, userName, info); connect(tab, SIGNAL(roomClosing(TabRoom *)), this, SLOT(roomLeft(TabRoom *))); myAddTab(tab); roomTabs.insert(info->getRoomId(), tab); @@ -177,12 +179,15 @@ void TabSupervisor::roomLeft(TabRoom *tab) removeTab(indexOf(tab)); } -TabMessage *TabSupervisor::addMessageTab(const QString &userName, bool focus) +TabMessage *TabSupervisor::addMessageTab(const QString &receiverName, bool focus) { - TabMessage *tab = new TabMessage(client, userName); + if (receiverName == userName) + return 0; + + TabMessage *tab = new TabMessage(client, receiverName); connect(tab, SIGNAL(talkClosing(TabMessage *)), this, SLOT(talkLeft(TabMessage *))); myAddTab(tab); - messageTabs.insert(userName, tab); + messageTabs.insert(receiverName, tab); if (focus) setCurrentWidget(tab); return tab; @@ -230,6 +235,8 @@ void TabSupervisor::processMessageEvent(Event_Message *event) tab = messageTabs.value(event->getReceiverName()); if (!tab) tab = addMessageTab(event->getSenderName(), false); + if (!tab) + return; tab->processMessageEvent(event); } diff --git a/cockatrice/src/tab_supervisor.h b/cockatrice/src/tab_supervisor.h index 1dd11463..e7925968 100644 --- a/cockatrice/src/tab_supervisor.h +++ b/cockatrice/src/tab_supervisor.h @@ -17,10 +17,12 @@ class GameEventContainer; class Event_GameJoined; class Event_Message; class ServerInfo_Room; +class ServerInfo_User; class TabSupervisor : public QTabWidget { Q_OBJECT private: + QString userName; QIcon *tabChangedIcon; AbstractClient *client; QList localClients; @@ -34,7 +36,7 @@ public: TabSupervisor(QWidget *parent = 0); ~TabSupervisor(); void retranslateUi(); - void start(AbstractClient *_client); + void start(AbstractClient *_client, ServerInfo_User *userInfo); void startLocal(const QList &_clients); void stop(); int getGameCount() const { return gameTabs.size(); } diff --git a/cockatrice/src/window_main.cpp b/cockatrice/src/window_main.cpp index e6d53e7c..26b3ac88 100644 --- a/cockatrice/src/window_main.cpp +++ b/cockatrice/src/window_main.cpp @@ -64,15 +64,18 @@ void MainWindow::statusChanged(ClientStatus _status) aConnect->setEnabled(false); aDisconnect->setEnabled(true); break; - case StatusLoggedIn: { - tabSupervisor->start(client); + case StatusLoggedIn: break; - } default: break; } } +void MainWindow::userInfoReceived(ServerInfo_User *info) +{ + tabSupervisor->start(client, info); +} + // Actions void MainWindow::actConnect() @@ -260,6 +263,7 @@ MainWindow::MainWindow(QWidget *parent) connect(client, SIGNAL(serverTimeout()), this, SLOT(serverTimeout())); connect(client, SIGNAL(statusChanged(ClientStatus)), this, SLOT(statusChanged(ClientStatus))); connect(client, SIGNAL(protocolVersionMismatch(int, int)), this, SLOT(protocolVersionMismatch(int, int))); + connect(client, SIGNAL(userInfoChanged(ServerInfo_User *)), this, SLOT(userInfoReceived(ServerInfo_User *))); tabSupervisor = new TabSupervisor; connect(tabSupervisor, SIGNAL(setMenu(QMenu *)), this, SLOT(updateTabMenu(QMenu *))); diff --git a/cockatrice/src/window_main.h b/cockatrice/src/window_main.h index adb4e20e..e6bcb155 100644 --- a/cockatrice/src/window_main.h +++ b/cockatrice/src/window_main.h @@ -28,6 +28,7 @@ class TabSupervisor; class RemoteClient; class LocalClient; class LocalServer; +class ServerInfo_User; class MainWindow : public QMainWindow { Q_OBJECT @@ -38,6 +39,7 @@ private slots: void serverError(ResponseCode r); void socketError(const QString &errorStr); void protocolVersionMismatch(int localVersion, int remoteVersion); + void userInfoReceived(ServerInfo_User *userInfo); void localGameEnded(); void actConnect(); diff --git a/common/protocol.cpp b/common/protocol.cpp index 1a1a2f55..b6060f2e 100644 --- a/common/protocol.cpp +++ b/common/protocol.cpp @@ -44,6 +44,7 @@ void ProtocolItem::initializeHash() registerSerializableItem("respdeck_download", Response_DeckDownload::newItem); registerSerializableItem("respdeck_upload", Response_DeckUpload::newItem); registerSerializableItem("respdump_zone", Response_DumpZone::newItem); + registerSerializableItem("resplogin", Response_Login::newItem); registerSerializableItem("room_eventlist_games", Event_ListGames::newItem); registerSerializableItem("room_eventjoin_room", Event_JoinRoom::newItem); @@ -281,6 +282,14 @@ Response_DumpZone::Response_DumpZone(int _cmdId, ResponseCode _responseCode, Ser insertItem(_zone); } +Response_Login::Response_Login(int _cmdId, ResponseCode _responseCode, ServerInfo_User *_userInfo) + : ProtocolResponse(_cmdId, _responseCode, "login") +{ + if (!_userInfo) + _userInfo = new ServerInfo_User; + insertItem(_userInfo); +} + GameEvent::GameEvent(const QString &_eventName, int _playerId) : ProtocolItem("game_event", _eventName) { diff --git a/common/protocol.h b/common/protocol.h index 307a15e7..97fe77e4 100644 --- a/common/protocol.h +++ b/common/protocol.h @@ -44,6 +44,7 @@ enum ItemId { ItemId_Response_DeckUpload = ItemId_Other + 304, ItemId_Response_DumpZone = ItemId_Other + 305, ItemId_Response_JoinRoom = ItemId_Other + 306, + ItemId_Response_Login = ItemId_Other + 307, ItemId_Invalid = ItemId_Other + 1000 }; @@ -268,6 +269,15 @@ public: ServerInfo_Zone *getZone() const { return static_cast(itemMap.value("zone")); } }; +class Response_Login : public ProtocolResponse { + Q_OBJECT +public: + Response_Login(int _cmdId = -1, ResponseCode _responseCode = RespOk, ServerInfo_User *_userInfo = 0); + int getItemId() const { return ItemId_Response_Login; } + static SerializableItem *newItem() { return new Response_Login; } + ServerInfo_User *getUserInfo() const { return static_cast(itemMap.value("user")); } +}; + // -------------- // --- EVENTS --- // -------------- diff --git a/common/server.cpp b/common/server.cpp index 5e7d98ff..4890f04f 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -108,6 +108,11 @@ void Server::broadcastRoomUpdate() delete event; } +void Server::gameCreated(Server_Game *game) +{ + games.insert(game->getGameId(), game); +} + void Server::gameClosing(int gameId) { qDebug("Server::gameClosing"); diff --git a/common/server.h b/common/server.h index b2ed7815..d382626a 100644 --- a/common/server.h +++ b/common/server.h @@ -18,6 +18,7 @@ class Server : public QObject signals: void pingClockTimeout(); private slots: + void gameCreated(Server_Game *game); void gameClosing(int gameId); void broadcastRoomUpdate(); public: diff --git a/common/server_protocolhandler.cpp b/common/server_protocolhandler.cpp index 114b6bd3..d63fb981 100644 --- a/common/server_protocolhandler.cpp +++ b/common/server_protocolhandler.cpp @@ -25,6 +25,10 @@ Server_ProtocolHandler::~Server_ProtocolHandler() // so it will not receive the game update event. server->removeClient(this); + QMapIterator roomIterator(rooms); + while (roomIterator.hasNext()) + roomIterator.next().value()->removeClient(this); + QMapIterator > gameIterator(games); while (gameIterator.hasNext()) { gameIterator.next(); @@ -37,10 +41,6 @@ Server_ProtocolHandler::~Server_ProtocolHandler() p->setProtocolHandler(0); } - QMapIterator roomIterator(rooms); - while (roomIterator.hasNext()) - roomIterator.next().value()->removeClient(this); - delete userInfo; } @@ -234,8 +234,9 @@ ResponseCode Server_ProtocolHandler::cmdLogin(Command_Login *cmd, CommandContain } } } - - return RespOk; + + cont->setResponse(new Response_Login(cont->getCmdId(), RespOk, new ServerInfo_User(userInfo, true))); + return RespNothing; } ResponseCode Server_ProtocolHandler::cmdMessage(Command_Message *cmd, CommandContainer *cont) @@ -302,6 +303,8 @@ ResponseCode Server_ProtocolHandler::cmdJoinRoom(Command_JoinRoom *cmd, CommandC r->addClient(this); rooms.insert(r->getId(), r); + enqueueProtocolItem(new Event_RoomSay(r->getId(), QString(), r->getJoinMessage())); + cont->setResponse(new Response_JoinRoom(cont->getCmdId(), RespOk, r->getInfo(true))); return RespNothing; } diff --git a/common/server_room.cpp b/common/server_room.cpp index ff13ea29..865c0cd7 100644 --- a/common/server_room.cpp +++ b/common/server_room.cpp @@ -17,7 +17,6 @@ ServerInfo_Room *Server_Room::getInfo(bool complete) const { QList gameList; QList userList; - qDebug() << "getInfo: complete=" << complete; if (complete) { QMapIterator gameIterator(games); while (gameIterator.hasNext()) @@ -79,6 +78,7 @@ Server_Game *Server_Room::createGame(const QString &description, const QString & broadcastGameListUpdate(newGame); emit gameCreated(newGame); + emit roomInfoChanged(); return newGame; } @@ -90,4 +90,5 @@ void Server_Room::removeGame() games.remove(game->getGameId()); emit gameClosing(game->getGameId()); + emit roomInfoChanged(); } diff --git a/common/server_room.h b/common/server_room.h index a22893b8..3df55b87 100644 --- a/common/server_room.h +++ b/common/server_room.h @@ -34,6 +34,7 @@ public: QString getName() const { return name; } QString getDescription() const { return description; } bool getAutoJoin() const { return autoJoin; } + QString getJoinMessage() const { return joinMessage; } const QMap &getGames() const { return games; } Server *getServer() const; ServerInfo_Room *getInfo(bool complete) const;