Major redesign: Using game ids and player ids in gui (associated with tree view items).

Player list of all network games is now dynamically updated in lobby (no polling, server sends events).
This commit is contained in:
lotodore
2007-09-02 16:36:52 +00:00
parent 1b8616d079
commit bd53403752
23 changed files with 421 additions and 238 deletions
+8 -8
View File
@@ -108,18 +108,18 @@ void ServerGuiWrapper::logPlayerWinGame(std::string playerName, int gameID) {}
void ServerGuiWrapper::SignalNetClientConnect(int actionID) { if (myClientcb) myClientcb->SignalNetClientConnect(actionID); } void ServerGuiWrapper::SignalNetClientConnect(int actionID) { if (myClientcb) myClientcb->SignalNetClientConnect(actionID); }
void ServerGuiWrapper::SignalNetClientGameInfo(int actionID) { if (myClientcb) myClientcb->SignalNetClientGameInfo(actionID); } void ServerGuiWrapper::SignalNetClientGameInfo(int actionID) { if (myClientcb) myClientcb->SignalNetClientGameInfo(actionID); }
void ServerGuiWrapper::SignalNetClientError(int errorID, int osErrorID) { if (myClientcb) myClientcb->SignalNetClientError(errorID, osErrorID); } void ServerGuiWrapper::SignalNetClientError(int errorID, int osErrorID) { if (myClientcb) myClientcb->SignalNetClientError(errorID, osErrorID); }
void ServerGuiWrapper::SignalNetClientSelfJoined(const string &playerName, PlayerRights rights) { if (myClientcb) myClientcb->SignalNetClientSelfJoined(playerName, rights); } void ServerGuiWrapper::SignalNetClientSelfJoined(unsigned playerId, const std::string &playerName, PlayerRights rights) { if (myClientcb) myClientcb->SignalNetClientSelfJoined(playerId, playerName, rights); }
void ServerGuiWrapper::SignalNetClientPlayerJoined(const string &playerName, PlayerRights rights) { if (myClientcb) myClientcb->SignalNetClientPlayerJoined(playerName, rights); } void ServerGuiWrapper::SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, PlayerRights rights) { if (myClientcb) myClientcb->SignalNetClientPlayerJoined(playerId, playerName, rights); }
void ServerGuiWrapper::SignalNetClientPlayerChanged(const std::string &oldPlayerName, const std::string &newPlayerName) { if (myClientcb) myClientcb->SignalNetClientPlayerChanged(oldPlayerName, newPlayerName); } void ServerGuiWrapper::SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName) { if (myClientcb) myClientcb->SignalNetClientPlayerChanged(playerId, newPlayerName); }
void ServerGuiWrapper::SignalNetClientPlayerLeft(const string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerLeft(playerName); } void ServerGuiWrapper::SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName) { if (myClientcb) myClientcb->SignalNetClientPlayerLeft(playerId, playerName); }
void ServerGuiWrapper::SignalNetClientGameListNew(const string &gameName) { if (myClientcb) myClientcb->SignalNetClientGameListNew(gameName); } void ServerGuiWrapper::SignalNetClientGameListNew(unsigned gameId, const string &gameName) { if (myClientcb) myClientcb->SignalNetClientGameListNew(gameId, gameName); }
void ServerGuiWrapper::SignalNetClientGameListRemove(const string &gameName) { if (myClientcb) myClientcb->SignalNetClientGameListRemove(gameName); } void ServerGuiWrapper::SignalNetClientGameListRemove(unsigned gameId, const string &gameName) { if (myClientcb) myClientcb->SignalNetClientGameListRemove(gameId, gameName); }
void ServerGuiWrapper::SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId) { if (myClientcb) myClientcb->SignalNetClientGameListPlayerJoined(gameId, playerId); }
void ServerGuiWrapper::SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId) { if (myClientcb) myClientcb->SignalNetClientGameListPlayerLeft(gameId, playerId); }
void ServerGuiWrapper::SignalNetClientGameStart(boost::shared_ptr<Game> game) { if (myClientcb) myClientcb->SignalNetClientGameStart(game); } void ServerGuiWrapper::SignalNetClientGameStart(boost::shared_ptr<Game> game) { if (myClientcb) myClientcb->SignalNetClientGameStart(game); }
void ServerGuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { if (myClientcb) myClientcb->SignalNetClientChatMsg(playerName, msg); } void ServerGuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { if (myClientcb) myClientcb->SignalNetClientChatMsg(playerName, msg); }
void ServerGuiWrapper::SignalNetClientWaitDialog() { if (myClientcb) myClientcb->SignalNetClientWaitDialog(); } void ServerGuiWrapper::SignalNetClientWaitDialog() { if (myClientcb) myClientcb->SignalNetClientWaitDialog(); }
void ServerGuiWrapper::SignalNetServerSuccess(int actionID) { if (myServercb) myServercb->SignalNetServerSuccess(actionID); } void ServerGuiWrapper::SignalNetServerSuccess(int actionID) { if (myServercb) myServercb->SignalNetServerSuccess(actionID); }
void ServerGuiWrapper::SignalNetServerError(int errorID, int osErrorID) { if (myServercb) myServercb->SignalNetServerError(errorID, osErrorID); } void ServerGuiWrapper::SignalNetServerError(int errorID, int osErrorID) { if (myServercb) myServercb->SignalNetServerError(errorID, osErrorID); }
void ServerGuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerJoined(playerName); }
void ServerGuiWrapper::SignalNetServerPlayerLeft(const string &playerName) { if (myServercb) myServercb->SignalNetServerPlayerLeft(playerName); }
+8 -8
View File
@@ -92,21 +92,21 @@ public:
void SignalNetClientConnect(int actionID); void SignalNetClientConnect(int actionID);
void SignalNetClientGameInfo(int actionID); void SignalNetClientGameInfo(int actionID);
void SignalNetClientError(int errorID, int osErrorID); void SignalNetClientError(int errorID, int osErrorID);
void SignalNetClientSelfJoined(const std::string &playerName, PlayerRights rights); void SignalNetClientSelfJoined(unsigned playerId, const std::string &playerName, PlayerRights rights);
void SignalNetClientPlayerJoined(const std::string &playerName, PlayerRights rights); void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, PlayerRights rights);
void SignalNetClientPlayerChanged(const std::string &oldPlayerName, const std::string &newPlayerName); void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName);
void SignalNetClientPlayerLeft(const std::string &playerName); void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName);
void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg); void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg);
void SignalNetClientWaitDialog(); void SignalNetClientWaitDialog();
void SignalNetClientGameListNew(const std::string &gameName); void SignalNetClientGameListNew(unsigned gameId, const std::string &gameName);
void SignalNetClientGameListRemove(const std::string &gameName); void SignalNetClientGameListRemove(unsigned gameId, const std::string &gameName);
void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId);
void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId);
void SignalNetClientGameStart(boost::shared_ptr<Game> game); void SignalNetClientGameStart(boost::shared_ptr<Game> game);
void SignalNetServerSuccess(int actionID); void SignalNetServerSuccess(int actionID);
void SignalNetServerError(int errorID, int osErrorID); void SignalNetServerError(int errorID, int osErrorID);
void SignalNetServerPlayerJoined(const std::string &playerName);
void SignalNetServerPlayerLeft(const std::string &playerName);
private: private:
@@ -21,26 +21,12 @@ gameLobbyDialogImpl::gameLobbyDialogImpl(QWidget *parent, ConfigFile *c)
connect( pushButton_CreateGame, SIGNAL( clicked() ), this, SLOT( createGame() ) ); connect( pushButton_CreateGame, SIGNAL( clicked() ), this, SLOT( createGame() ) );
connect( pushButton_JoinGame, SIGNAL( clicked() ), this, SLOT( joinGame() ) ); connect( pushButton_JoinGame, SIGNAL( clicked() ), this, SLOT( joinGame() ) );
connect( treeWidget_GameList, SIGNAL( itemClicked ( QTreeWidgetItem*, int) ), this, SLOT( gameSelected(QTreeWidgetItem*, int) ) ); connect( treeWidget_GameList, SIGNAL( currentItemChanged ( QTreeWidgetItem*, QTreeWidgetItem*) ), this, SLOT( gameSelected(QTreeWidgetItem*, QTreeWidgetItem*) ) );
connect( treeWidget_GameList, SIGNAL( clear () ), this, SLOT( clearGames() ) );
pushButton_JoinGame->setEnabled(false);
pushButton_Leave->hide();
pushButton_Kick->hide();
pushButton_StartGame->hide();
treeWidget_GameList->setColumnWidth(0,250);
treeWidget_GameList->setColumnWidth(1,75);
treeWidget_GameList->setColumnWidth(2,70);
lineEdit_ChatInput->setFocus();
} }
void gameLobbyDialogImpl::exec() void gameLobbyDialogImpl::exec()
{ {
clearDialog();
QDialog::exec(); QDialog::exec();
} }
@@ -95,7 +81,9 @@ void gameLobbyDialogImpl::joinGame()
} }
} }
void gameLobbyDialogImpl::gameSelected(QTreeWidgetItem* item, int) void gameLobbyDialogImpl::gameSelected(QTreeWidgetItem* item, QTreeWidgetItem*)
{
if (item)
{ {
pushButton_JoinGame->setEnabled(true); pushButton_JoinGame->setEnabled(true);
@@ -105,31 +93,70 @@ void gameLobbyDialogImpl::gameSelected(QTreeWidgetItem* item, int)
groupBox_GameInfo->setTitle(tr("Game Info") + " - " + currentGameName); groupBox_GameInfo->setTitle(tr("Game Info") + " - " + currentGameName);
assert(mySession); assert(mySession);
GameInfo info = mySession->getClientGameInfo(currentGameName.toUtf8().constData()); GameInfo info(mySession->getClientGameInfo(item->data(0, Qt::UserRole).toUInt()));
label_SmallBlind->setText(QString::number(info.data.smallBlind)); label_SmallBlind->setText(QString::number(info.data.smallBlind));
label_StartCash->setText(QString::number(info.data.startMoney)); label_StartCash->setText(QString::number(info.data.startMoney));
label_MaximumNumberOfPlayers->setText(QString::number(info.data.maxNumberOfPlayers)); label_MaximumNumberOfPlayers->setText(QString::number(info.data.maxNumberOfPlayers));
label_HandsToRaiseSmallBlind->setText(QString::number(info.data.handsBeforeRaise)); label_HandsToRaiseSmallBlind->setText(QString::number(info.data.handsBeforeRaise));
label_TimeoutForPlayerAction->setText(QString::number(info.data.playerActionTimeoutSec)); label_TimeoutForPlayerAction->setText(QString::number(info.data.playerActionTimeoutSec));
treeWidget_connectedPlayers->clear();
PlayerIdList::const_iterator i = info.players.begin();
PlayerIdList::const_iterator end = info.players.end();
while (i != end)
{
PlayerInfo info(mySession->getClientPlayerInfo(*i));
addConnectedPlayer(*i, QString::fromUtf8(info.playerName.c_str()), PLAYER_RIGHTS_NORMAL);
++i;
}
}
} }
void gameLobbyDialogImpl::addGame(QString gameName) void gameLobbyDialogImpl::addGame(unsigned gameId, QString gameName)
{ {
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget_GameList, 0); QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget_GameList, 0);
item->setData(0, 0, gameName); item->setData(0, Qt::UserRole, gameId);
item->setData(0, Qt::DisplayRole, gameName);
} }
void gameLobbyDialogImpl::removeGame(QString gameName) void gameLobbyDialogImpl::removeGame(unsigned gameId, QString)
{ {
QList<QTreeWidgetItem *> list = treeWidget_GameList->findItems(gameName, Qt::MatchExactly, 0); QTreeWidgetItemIterator it(treeWidget_GameList);
if(!list.empty()) { while (*it) {
treeWidget_GameList->takeTopLevelItem(treeWidget_GameList->indexOfTopLevelItem(list[0])); if ((*it)->data(0, Qt::UserRole) == gameId)
{
treeWidget_GameList->takeTopLevelItem(treeWidget_GameList->indexOfTopLevelItem(*it));
break;
}
++it;
} }
} }
void gameLobbyDialogImpl::clearGames() void gameLobbyDialogImpl::gameAddPlayer(unsigned gameId, unsigned playerId)
{
QTreeWidgetItem *item = treeWidget_GameList->currentItem();
if (item && item->data(0, Qt::UserRole) == gameId)
{
assert(mySession);
PlayerInfo info(mySession->getClientPlayerInfo(playerId));
addConnectedPlayer(playerId, QString::fromUtf8(info.playerName.c_str()), PLAYER_RIGHTS_NORMAL);
}
}
void gameLobbyDialogImpl::gameRemovePlayer(unsigned gameId, unsigned playerId)
{
QTreeWidgetItem *item = treeWidget_GameList->currentItem();
if (item && item->data(0, Qt::UserRole) == gameId)
{
assert(mySession);
PlayerInfo info(mySession->getClientPlayerInfo(playerId));
removePlayer(playerId, QString::fromUtf8(info.playerName.c_str()));
}
}
void gameLobbyDialogImpl::clearDialog()
{ {
pushButton_JoinGame->setEnabled(false);
groupBox_GameInfo->setTitle(tr("Game Info")); groupBox_GameInfo->setTitle(tr("Game Info"));
groupBox_GameInfo->setEnabled(false); groupBox_GameInfo->setEnabled(false);
currentGameName = ""; currentGameName = "";
@@ -139,6 +166,20 @@ void gameLobbyDialogImpl::clearGames()
label_MaximumNumberOfPlayers->setText(""); label_MaximumNumberOfPlayers->setText("");
label_HandsToRaiseSmallBlind->setText(""); label_HandsToRaiseSmallBlind->setText("");
label_TimeoutForPlayerAction->setText(""); label_TimeoutForPlayerAction->setText("");
treeWidget_GameList->clear();
treeWidget_connectedPlayers->clear();
pushButton_JoinGame->setEnabled(false);
pushButton_Leave->hide();
pushButton_Kick->hide();
pushButton_StartGame->hide();
treeWidget_GameList->setColumnWidth(0,250);
treeWidget_GameList->setColumnWidth(1,75);
treeWidget_GameList->setColumnWidth(2,70);
lineEdit_ChatInput->setFocus();
} }
void gameLobbyDialogImpl::checkPlayerQuantity() { void gameLobbyDialogImpl::checkPlayerQuantity() {
@@ -152,19 +193,20 @@ void gameLobbyDialogImpl::checkPlayerQuantity() {
} }
void gameLobbyDialogImpl::joinedNetworkGame(QString playerName, int rights) { void gameLobbyDialogImpl::joinedNetworkGame(unsigned playerId, QString playerName, int rights) {
isAdmin = rights == PLAYER_RIGHTS_ADMIN; isAdmin = rights == PLAYER_RIGHTS_ADMIN;
addConnectedPlayer(playerName, rights); addConnectedPlayer(playerId, playerName, rights);
} }
void gameLobbyDialogImpl::addConnectedPlayer(QString playerName, int rights) { void gameLobbyDialogImpl::addConnectedPlayer(unsigned playerId, QString playerName, int rights) {
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget_connectedPlayers, 0); QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget_connectedPlayers, 0);
item->setData(0, 0, playerName); item->setData(0, Qt::UserRole, playerId);
item->setData(0, Qt::DisplayRole, playerName);
assert(mySession); /* assert(mySession);
GameInfo info = mySession->getClientGameInfo(currentGameName.toUtf8().constData()); GameInfo info = mySession->getClientGameInfo(currentGameName.toUtf8().constData());
if(treeWidget_connectedPlayers->topLevelItemCount() != info.data.maxNumberOfPlayers) { if(treeWidget_connectedPlayers->topLevelItemCount() != info.data.maxNumberOfPlayers) {
@@ -172,25 +214,36 @@ void gameLobbyDialogImpl::addConnectedPlayer(QString playerName, int rights) {
} }
else { else {
myW->getMySDLPlayer()->playSound("onlinegameready", 0); myW->getMySDLPlayer()->playSound("onlinegameready", 0);
}*/
checkPlayerQuantity();
}
void gameLobbyDialogImpl::updatePlayer(unsigned playerId, QString newPlayerName) {
QTreeWidgetItemIterator it(treeWidget_connectedPlayers);
while (*it) {
if ((*it)->data(0, Qt::UserRole) == playerId)
{
(*it)->setData(0, Qt::DisplayRole, newPlayerName);
break;
}
++it;
}
}
void gameLobbyDialogImpl::removePlayer(unsigned playerId, QString) {
QTreeWidgetItemIterator it(treeWidget_connectedPlayers);
while (*it) {
if ((*it)->data(0, Qt::UserRole) == playerId)
{
treeWidget_connectedPlayers->takeTopLevelItem(treeWidget_connectedPlayers->indexOfTopLevelItem(*it));
break;
}
++it;
} }
checkPlayerQuantity(); checkPlayerQuantity();
} }
void gameLobbyDialogImpl::updatePlayer(QString oldPlayerName, QString newPlayerName) {
QList<QTreeWidgetItem *> list = treeWidget_connectedPlayers->findItems(oldPlayerName, Qt::MatchExactly, 0);
if(!list.empty()) {
list[0]->setText(0, newPlayerName);
}
}
void gameLobbyDialogImpl::removePlayer(QString playerName) {
QList<QTreeWidgetItem *> list = treeWidget_connectedPlayers->findItems(playerName, Qt::MatchExactly, 0);
if(!list.empty()) {
treeWidget_connectedPlayers->takeTopLevelItem(treeWidget_connectedPlayers->indexOfTopLevelItem(list[0]));
}
}
@@ -45,21 +45,24 @@ public slots:
void createGame(); void createGame();
void joinGame(); void joinGame();
void gameSelected(QTreeWidgetItem*, int); void gameSelected(QTreeWidgetItem*, QTreeWidgetItem*);
void addGame(QString gameName); void addGame(unsigned gameId, QString gameName);
void removeGame(QString gameName); void removeGame(unsigned gameId, QString gameName);
void gameAddPlayer(unsigned gameId, unsigned playerId);
void gameRemovePlayer(unsigned gameId, unsigned playerId);
void setCurrentGameName ( const QString& theValue ) { currentGameName = theValue; } void setCurrentGameName ( const QString& theValue ) { currentGameName = theValue; }
QString getCurrentGameName() const { return currentGameName; } QString getCurrentGameName() const { return currentGameName; }
void clearGames();
void checkPlayerQuantity(); void checkPlayerQuantity();
void joinedNetworkGame(QString, int); void joinedNetworkGame(unsigned, QString, int);
void addConnectedPlayer(QString, int); void addConnectedPlayer(unsigned, QString, int);
void updatePlayer(QString, QString); void updatePlayer(unsigned, QString);
void removePlayer(QString); void removePlayer(unsigned, QString);
void clearDialog();
private: private:
+10 -10
View File
@@ -111,22 +111,24 @@ void GuiWrapper::logPlayerWinGame(std::string playerName, int gameID) { myLog->s
void GuiWrapper::SignalNetClientConnect(int actionID) { myW->signalNetClientConnect(actionID); } void GuiWrapper::SignalNetClientConnect(int actionID) { myW->signalNetClientConnect(actionID); }
void GuiWrapper::SignalNetClientGameInfo(int actionID) { myW->signalNetClientGameInfo(actionID); } void GuiWrapper::SignalNetClientGameInfo(int actionID) { myW->signalNetClientGameInfo(actionID); }
void GuiWrapper::SignalNetClientError(int errorID, int osErrorID) { myW->signalNetClientError(errorID, osErrorID); } void GuiWrapper::SignalNetClientError(int errorID, int osErrorID) { myW->signalNetClientError(errorID, osErrorID); }
void GuiWrapper::SignalNetClientSelfJoined(const string &playerName, PlayerRights rights) { myW->signalNetClientSelfJoined(QString::fromUtf8(playerName.c_str()), rights); } void GuiWrapper::SignalNetClientSelfJoined(unsigned playerId, const string &playerName, PlayerRights rights) { myW->signalNetClientSelfJoined(playerId, QString::fromUtf8(playerName.c_str()), rights); }
void GuiWrapper::SignalNetClientPlayerJoined(const string &playerName, PlayerRights rights) { myW->signalNetClientPlayerJoined(QString::fromUtf8(playerName.c_str()), rights); } void GuiWrapper::SignalNetClientPlayerJoined(unsigned playerId, const string &playerName, PlayerRights rights) { myW->signalNetClientPlayerJoined(playerId, QString::fromUtf8(playerName.c_str()), rights); }
void GuiWrapper::SignalNetClientPlayerChanged(const string &oldPlayerName, const string &newPlayerName) void GuiWrapper::SignalNetClientPlayerChanged(unsigned playerId, const string &newPlayerName)
{ {
myW->signalNetClientPlayerChanged(QString::fromUtf8(oldPlayerName.c_str()), QString::fromUtf8(newPlayerName.c_str())); myW->signalNetClientPlayerChanged(playerId, QString::fromUtf8(newPlayerName.c_str()));
} }
void GuiWrapper::SignalNetClientPlayerLeft(const string &playerName) void GuiWrapper::SignalNetClientPlayerLeft(unsigned playerId, const string &playerName)
{ {
QString tmpName(QString::fromUtf8(playerName.c_str())); QString tmpName(QString::fromUtf8(playerName.c_str()));
myW->signalNetClientPlayerLeft(tmpName); myW->signalNetClientPlayerLeft(playerId, tmpName);
if (!playerName.empty() && playerName[0] != '#') if (!playerName.empty() && playerName[0] != '#')
myLog->signalLogPlayerLeftMsg(tmpName); myLog->signalLogPlayerLeftMsg(tmpName);
} }
void GuiWrapper::SignalNetClientGameListNew(const string &gameName) { myW->signalNetClientGameListNew(QString::fromUtf8(gameName.c_str())); } void GuiWrapper::SignalNetClientGameListNew(unsigned gameId, const string &gameName) { myW->signalNetClientGameListNew(gameId, QString::fromUtf8(gameName.c_str())); }
void GuiWrapper::SignalNetClientGameListRemove(const string &gameName) { myW->signalNetClientGameListRemove(QString::fromUtf8(gameName.c_str())); } void GuiWrapper::SignalNetClientGameListRemove(unsigned gameId, const string &gameName) { myW->signalNetClientGameListRemove(gameId, QString::fromUtf8(gameName.c_str())); }
void GuiWrapper::SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId) { myW->signalNetClientGameListPlayerJoined(gameId, playerId); }
void GuiWrapper::SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId) { myW->signalNetClientGameListPlayerLeft(gameId, playerId); }
void GuiWrapper::SignalNetClientGameStart(boost::shared_ptr<Game> game) { myW->signalNetClientGameStart(game); } void GuiWrapper::SignalNetClientGameStart(boost::shared_ptr<Game> game) { myW->signalNetClientGameStart(game); }
void GuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { myChat->signalChatMessage(QString::fromUtf8(playerName.c_str()), QString::fromUtf8(msg.c_str())); } void GuiWrapper::SignalNetClientChatMsg(const string &playerName, const string &msg) { myChat->signalChatMessage(QString::fromUtf8(playerName.c_str()), QString::fromUtf8(msg.c_str())); }
@@ -134,6 +136,4 @@ void GuiWrapper::SignalNetClientWaitDialog() { myW->signalShowNetworkStartDialog
void GuiWrapper::SignalNetServerSuccess(int actionID) { } void GuiWrapper::SignalNetServerSuccess(int actionID) { }
void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { myW->signalNetServerError(errorID, osErrorID); } void GuiWrapper::SignalNetServerError(int errorID, int osErrorID) { myW->signalNetServerError(errorID, osErrorID); }
void GuiWrapper::SignalNetServerPlayerJoined(const string &playerName) { }
void GuiWrapper::SignalNetServerPlayerLeft(const string &playerName) { }
+8 -8
View File
@@ -99,22 +99,22 @@ public:
void SignalNetClientConnect(int actionID); void SignalNetClientConnect(int actionID);
void SignalNetClientGameInfo(int actionID); void SignalNetClientGameInfo(int actionID);
void SignalNetClientError(int errorID, int osErrorID); void SignalNetClientError(int errorID, int osErrorID);
void SignalNetClientSelfJoined(const std::string &playerName, PlayerRights rights); void SignalNetClientSelfJoined(unsigned playerId, const std::string &playerName, PlayerRights rights);
void SignalNetClientPlayerJoined(const std::string &playerName, PlayerRights rights); void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, PlayerRights rights);
void SignalNetClientPlayerChanged(const std::string &oldPlayerName, const std::string &newPlayerName); void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName);
void SignalNetClientPlayerLeft(const std::string &playerName); void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName);
void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg); void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg);
void SignalNetClientWaitDialog(); void SignalNetClientWaitDialog();
void SignalNetClientGameListNew(const std::string &gameName); void SignalNetClientGameListNew(unsigned gameId, const std::string &gameName);
void SignalNetClientGameListRemove(const std::string &gameName); void SignalNetClientGameListRemove(unsigned gameId, const std::string &gameName);
void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId);
void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId);
void SignalNetClientGameStart(boost::shared_ptr<Game> game); void SignalNetClientGameStart(boost::shared_ptr<Game> game);
void SignalNetServerSuccess(int actionID); void SignalNetServerSuccess(int actionID);
void SignalNetServerError(int errorID, int osErrorID); void SignalNetServerError(int errorID, int osErrorID);
void SignalNetServerPlayerJoined(const std::string &playerName);
void SignalNetServerPlayerLeft(const std::string &playerName);
private: private:
+14 -16
View File
@@ -670,17 +670,21 @@ mainWindowImpl::mainWindowImpl(ConfigFile *c, QMainWindow *parent)
connect(this, SIGNAL(signalNetClientConnect(int)), myConnectToServerDialog, SLOT(refresh(int))); connect(this, SIGNAL(signalNetClientConnect(int)), myConnectToServerDialog, SLOT(refresh(int)));
connect(this, SIGNAL(signalNetClientGameInfo(int)), myStartNetworkGameDialog, SLOT(refresh(int))); connect(this, SIGNAL(signalNetClientGameInfo(int)), myStartNetworkGameDialog, SLOT(refresh(int)));
connect(this, SIGNAL(signalNetClientSelfJoined(QString, int)), myStartNetworkGameDialog, SLOT(joinedNetworkGame(QString, int)));
connect(this, SIGNAL(signalNetClientPlayerJoined(QString, int)), myStartNetworkGameDialog, SLOT(addConnectedPlayer(QString, int)));
connect(this, SIGNAL(signalNetClientPlayerChanged(QString, QString)), myStartNetworkGameDialog, SLOT(updatePlayer(QString, QString)));
connect(this, SIGNAL(signalNetClientPlayerLeft(QString)), myStartNetworkGameDialog, SLOT(removePlayer(QString)));
connect(this, SIGNAL(signalNetClientSelfJoined(QString, int)), myGameLobbyDialog, SLOT(joinedNetworkGame(QString, int))); connect(this, SIGNAL(signalNetClientSelfJoined(unsigned, QString, int)), myStartNetworkGameDialog, SLOT(joinedNetworkGame(unsigned, QString, int)));
connect(this, SIGNAL(signalNetClientPlayerJoined(QString, int)), myGameLobbyDialog, SLOT(addConnectedPlayer(QString, int))); connect(this, SIGNAL(signalNetClientPlayerJoined(unsigned, QString, int)), myStartNetworkGameDialog, SLOT(addConnectedPlayer(unsigned, QString, int)));
connect(this, SIGNAL(signalNetClientPlayerChanged(QString, QString)), myGameLobbyDialog, SLOT(updatePlayer(QString, QString))); connect(this, SIGNAL(signalNetClientPlayerChanged(unsigned, QString)), myStartNetworkGameDialog, SLOT(updatePlayer(unsigned, QString)));
connect(this, SIGNAL(signalNetClientPlayerLeft(QString)), myGameLobbyDialog, SLOT(removePlayer(QString))); connect(this, SIGNAL(signalNetClientPlayerLeft(unsigned, QString)), myStartNetworkGameDialog, SLOT(removePlayer(unsigned, QString)));
connect(this, SIGNAL(signalNetClientGameListNew(QString)), myGameLobbyDialog, SLOT(addGame(QString)));
connect(this, SIGNAL(signalNetClientGameListRemove(QString)), myGameLobbyDialog, SLOT(removeGame(QString))); connect(this, SIGNAL(signalNetClientSelfJoined(unsigned, QString, int)), myGameLobbyDialog, SLOT(joinedNetworkGame(unsigned, QString, int)));
connect(this, SIGNAL(signalNetClientPlayerJoined(unsigned, QString, int)), myGameLobbyDialog, SLOT(addConnectedPlayer(unsigned, QString, int)));
connect(this, SIGNAL(signalNetClientPlayerChanged(unsigned, QString)), myGameLobbyDialog, SLOT(updatePlayer(unsigned, QString)));
connect(this, SIGNAL(signalNetClientPlayerLeft(unsigned, QString)), myGameLobbyDialog, SLOT(removePlayer(unsigned, QString)));
connect(this, SIGNAL(signalNetClientGameListNew(unsigned, QString)), myGameLobbyDialog, SLOT(addGame(unsigned, QString)));
connect(this, SIGNAL(signalNetClientGameListRemove(unsigned, QString)), myGameLobbyDialog, SLOT(removeGame(unsigned, QString)));
connect(this, SIGNAL(signalNetClientGameListPlayerJoined(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameAddPlayer(unsigned, unsigned)));
connect(this, SIGNAL(signalNetClientGameListPlayerLeft(unsigned, unsigned)), myGameLobbyDialog, SLOT(gameRemovePlayer(unsigned, unsigned)));
// Errors are handled globally, not within one dialog. // Errors are handled globally, not within one dialog.
connect(this, SIGNAL(signalNetClientError(int, int)), this, SLOT(networkError(int, int))); connect(this, SIGNAL(signalNetClientError(int, int)), this, SLOT(networkError(int, int)));
@@ -789,9 +793,7 @@ void mainWindowImpl::callCreateNetworkGameDialog() {
gameData.playerActionTimeoutSec = myCreateNetworkGameDialog->spinBox_netTimeOutPlayerAction->value(); gameData.playerActionTimeoutSec = myCreateNetworkGameDialog->spinBox_netTimeOutPlayerAction->value();
myGameLobbyDialog->setSession(&getSession()); myGameLobbyDialog->setSession(&getSession());
myGameLobbyDialog->treeWidget_GameList->clear();
myStartNetworkGameDialog->setSession(&getSession()); myStartNetworkGameDialog->setSession(&getSession());
myStartNetworkGameDialog->treeWidget->clear();
myServerGuiInterface->getSession().startNetworkServer(); myServerGuiInterface->getSession().startNetworkServer();
mySession->startNetworkClientForLocalServer(gameData); mySession->startNetworkClientForLocalServer(gameData);
@@ -816,9 +818,7 @@ void mainWindowImpl::callJoinNetworkGameDialog() {
myServerGuiInterface->getSession().terminateNetworkServer(); myServerGuiInterface->getSession().terminateNetworkServer();
myGameLobbyDialog->setSession(&getSession()); myGameLobbyDialog->setSession(&getSession());
myGameLobbyDialog->treeWidget_GameList->clear();
myStartNetworkGameDialog->setSession(&getSession()); myStartNetworkGameDialog->setSession(&getSession());
myStartNetworkGameDialog->treeWidget->clear();
// Maybe use QUrl::toPunycode. // Maybe use QUrl::toPunycode.
mySession->startNetworkClient( mySession->startNetworkClient(
myJoinNetworkGameDialog->lineEdit_ipAddress->text().toUtf8().constData(), myJoinNetworkGameDialog->lineEdit_ipAddress->text().toUtf8().constData(),
@@ -851,8 +851,6 @@ void mainWindowImpl::callGameLobbyDialog() {
myServerGuiInterface->getSession().terminateNetworkServer(); myServerGuiInterface->getSession().terminateNetworkServer();
myGameLobbyDialog->setSession(&getSession()); myGameLobbyDialog->setSession(&getSession());
myGameLobbyDialog->treeWidget_GameList->clear();
myStartNetworkGameDialog->treeWidget->clear();
myStartNetworkGameDialog->setSession(&getSession()); myStartNetworkGameDialog->setSession(&getSession());
// Start client for dedicated server. // Start client for dedicated server.
+8 -6
View File
@@ -127,12 +127,14 @@ signals:
void signalNetClientGameInfo(int actionID); void signalNetClientGameInfo(int actionID);
void signalNetClientError(int errorID, int osErrorID); void signalNetClientError(int errorID, int osErrorID);
void signalNetServerError(int errorID, int osErrorID); void signalNetServerError(int errorID, int osErrorID);
void signalNetClientSelfJoined(QString playerName, int rights); void signalNetClientSelfJoined(unsigned playerId, QString playerName, int rights);
void signalNetClientPlayerJoined(QString playerName, int rights); void signalNetClientPlayerJoined(unsigned playerId, QString playerName, int rights);
void signalNetClientPlayerChanged(QString oldPlayerName, QString newPlayerName); void signalNetClientPlayerChanged(unsigned playerId, QString newPlayerName);
void signalNetClientPlayerLeft(QString playerName); void signalNetClientPlayerLeft(unsigned playerId, QString playerName);
void signalNetClientGameListNew(QString gameName); void signalNetClientGameListNew(unsigned gameId, QString gameName);
void signalNetClientGameListRemove(QString gameName); void signalNetClientGameListRemove(unsigned gameId, QString gameName);
void signalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId);
void signalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId);
void signalNetClientGameStart(boost::shared_ptr<Game> game); void signalNetClientGameStart(boost::shared_ptr<Game> game);
public slots: public slots:
@@ -30,16 +30,14 @@ startNetworkGameDialogImpl::startNetworkGameDialogImpl(QWidget *parent, ConfigFi
connect( pushButton_cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) ); connect( pushButton_cancel, SIGNAL( clicked() ), this, SLOT( cancel() ) );
connect( pushButton_startGame, SIGNAL( clicked() ), this, SLOT( startGame() ) ); connect( pushButton_startGame, SIGNAL( clicked() ), this, SLOT( startGame() ) );
connect( pushButton_Kick, SIGNAL( clicked() ), this, SLOT( kickPlayer() ) ); connect( pushButton_Kick, SIGNAL( clicked() ), this, SLOT( kickPlayer() ) );
connect( treeWidget, SIGNAL( itemClicked ( QTreeWidgetItem*, int) ), this, SLOT( playerSelected(QTreeWidgetItem*, int) ) ); connect( treeWidget, SIGNAL( currentItemChanged ( QTreeWidgetItem*, QTreeWidgetItem*) ), this, SLOT( playerSelected(QTreeWidgetItem*, QTreeWidgetItem*) ) );
connect( treeWidget, SIGNAL( clear () ), this, SLOT( clearPlayers() ) );
pushButton_Kick->setEnabled(false);
pushButton_startGame->setEnabled(false);
} }
void startNetworkGameDialogImpl::exec() { void startNetworkGameDialogImpl::exec() {
GameInfo info = mySession->getClientGameInfo("default"); clearDialog();
GameInfo info = mySession->getClientGameInfo(0);
label_maxPlayerNumber->setText(QString::number(info.data.maxNumberOfPlayers)); label_maxPlayerNumber->setText(QString::number(info.data.maxNumberOfPlayers));
QDialog::exec(); QDialog::exec();
@@ -62,16 +60,17 @@ void startNetworkGameDialogImpl::refresh(int actionID) {
} }
} }
void startNetworkGameDialogImpl::joinedNetworkGame(QString playerName, int rights) { void startNetworkGameDialogImpl::joinedNetworkGame(unsigned playerId, QString playerName, int rights) {
isAdmin = rights == PLAYER_RIGHTS_ADMIN; isAdmin = rights == PLAYER_RIGHTS_ADMIN;
addConnectedPlayer(playerName, rights); addConnectedPlayer(playerId, playerName, rights);
} }
void startNetworkGameDialogImpl::addConnectedPlayer(QString playerName, int rights) { void startNetworkGameDialogImpl::addConnectedPlayer(unsigned playerId, QString playerName, int rights) {
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, 0); QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget, 0);
item->setData(0, 0, playerName); item->setData(0, Qt::UserRole, playerId);
item->setData(0, Qt::DisplayRole, playerName);
if(treeWidget->topLevelItemCount() != maxPlayerNumber) { if(treeWidget->topLevelItemCount() != maxPlayerNumber) {
myW->getMySDLPlayer()->playSound("playerconnected", 0); myW->getMySDLPlayer()->playSound("playerconnected", 0);
@@ -83,26 +82,37 @@ void startNetworkGameDialogImpl::addConnectedPlayer(QString playerName, int righ
checkPlayerQuantity(); checkPlayerQuantity();
} }
void startNetworkGameDialogImpl::updatePlayer(QString oldPlayerName, QString newPlayerName) void startNetworkGameDialogImpl::updatePlayer(unsigned playerId, QString newPlayerName)
{ {
QList<QTreeWidgetItem *> list = treeWidget->findItems(oldPlayerName, Qt::MatchExactly, 0); QTreeWidgetItemIterator it(treeWidget);
if(!list.empty()) { while (*it) {
list[0]->setText(0, newPlayerName); if ((*it)->data(0, Qt::UserRole) == playerId)
{
(*it)->setData(0, Qt::DisplayRole, newPlayerName);
break;
}
++it;
} }
} }
void startNetworkGameDialogImpl::removePlayer(QString playerName) { void startNetworkGameDialogImpl::removePlayer(unsigned playerId, QString) {
QList<QTreeWidgetItem *> list = treeWidget->findItems(playerName, Qt::MatchExactly, 0); QTreeWidgetItemIterator it(treeWidget);
if(!list.empty()) { while (*it) {
treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(list[0])); if ((*it)->data(0, Qt::UserRole) == playerId)
{
treeWidget->takeTopLevelItem(treeWidget->indexOfTopLevelItem(*it));
break;
}
++it;
} }
checkPlayerQuantity(); checkPlayerQuantity();
} }
void startNetworkGameDialogImpl::playerSelected(QTreeWidgetItem*, int) { void startNetworkGameDialogImpl::playerSelected(QTreeWidgetItem* item, QTreeWidgetItem*) {
if (item)
pushButton_Kick->setEnabled(isAdmin); pushButton_Kick->setEnabled(isAdmin);
} }
@@ -137,9 +147,11 @@ void startNetworkGameDialogImpl::checkPlayerQuantity() {
} }
void startNetworkGameDialogImpl::clearPlayers() void startNetworkGameDialogImpl::clearDialog()
{ {
pushButton_Kick->setEnabled(false); pushButton_Kick->setEnabled(false);
pushButton_startGame->setEnabled(false);
treeWidget->clear();
} }
void startNetworkGameDialogImpl::setSession(Session *session) void startNetworkGameDialogImpl::setSession(Session *session)
@@ -45,14 +45,14 @@ public slots:
void startGame(); void startGame();
void cancel(); void cancel();
void refresh(int actionID); void refresh(int actionID);
void joinedNetworkGame(QString playerName, int rights); void joinedNetworkGame(unsigned playerId, QString playerName, int rights);
void addConnectedPlayer(QString playerName, int rights); void addConnectedPlayer(unsigned playerId, QString playerName, int rights);
void updatePlayer(QString oldPlayerName, QString newPlayerName); void updatePlayer(unsigned playerId, QString newPlayerName);
void removePlayer(QString playerName); void removePlayer(unsigned playerId, QString playerName);
void playerSelected(QTreeWidgetItem*, int); void playerSelected(QTreeWidgetItem*, QTreeWidgetItem*);
void kickPlayer(); void kickPlayer();
void checkPlayerQuantity(); void checkPlayerQuantity();
void clearPlayers(); void clearDialog();
void keyPressEvent ( QKeyEvent*); void keyPressEvent ( QKeyEvent*);
+8 -6
View File
@@ -36,14 +36,16 @@ public:
virtual void SignalNetClientGameInfo(int actionID) = 0; virtual void SignalNetClientGameInfo(int actionID) = 0;
virtual void SignalNetClientError(int errorID, int osErrorID) = 0; virtual void SignalNetClientError(int errorID, int osErrorID) = 0;
virtual void SignalNetClientGameListNew(const std::string &gameName) = 0; virtual void SignalNetClientGameListNew(unsigned gameId, const std::string &gameName) = 0;
virtual void SignalNetClientGameListRemove(const std::string &gameName) = 0; virtual void SignalNetClientGameListRemove(unsigned gameId, const std::string &gameName) = 0;
virtual void SignalNetClientGameListPlayerJoined(unsigned gameId, unsigned playerId) = 0;
virtual void SignalNetClientGameListPlayerLeft(unsigned gameId, unsigned playerId) = 0;
virtual void SignalNetClientGameStart(boost::shared_ptr<Game> game) = 0; virtual void SignalNetClientGameStart(boost::shared_ptr<Game> game) = 0;
virtual void SignalNetClientSelfJoined(const std::string &playerName, PlayerRights rights) = 0; virtual void SignalNetClientSelfJoined(unsigned playerId, const std::string &playerName, PlayerRights rights) = 0;
virtual void SignalNetClientPlayerJoined(const std::string &playerName, PlayerRights rights) = 0; virtual void SignalNetClientPlayerJoined(unsigned playerId, const std::string &playerName, PlayerRights rights) = 0;
virtual void SignalNetClientPlayerChanged(const std::string &oldPlayerName, const std::string &newPlayerName) = 0; virtual void SignalNetClientPlayerChanged(unsigned playerId, const std::string &newPlayerName) = 0;
virtual void SignalNetClientPlayerLeft(const std::string &playerName) = 0; virtual void SignalNetClientPlayerLeft(unsigned playerId, const std::string &playerName) = 0;
virtual void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg) = 0; virtual void SignalNetClientChatMsg(const std::string &playerName, const std::string &msg) = 0;
virtual void SignalNetClientWaitDialog() = 0; virtual void SignalNetClientWaitDialog() = 0;
+11 -5
View File
@@ -62,7 +62,8 @@ public:
void SendJoinGame(const std::string &name, const std::string &password); void SendJoinGame(const std::string &name, const std::string &password);
void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password); void SendCreateGame(const GameData &gameData, const std::string &name, const std::string &password);
GameInfo GetGameInfo(const std::string &game) const; GameInfo GetGameInfo(unsigned playerId) const;
PlayerInfo GetPlayerInfo(unsigned playerId) const;
ClientCallback &GetCallback(); ClientCallback &GetCallback();
GuiInterface &GetGui(); GuiInterface &GetGui();
@@ -78,8 +79,8 @@ protected:
void AddPacket(boost::shared_ptr<NetPacket> packet); void AddPacket(boost::shared_ptr<NetPacket> packet);
void SendPacketLoop(); void SendPacketLoop();
PlayerInfo GetCachedPlayerInfo(unsigned id) const; bool GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const;
void RequestPlayerInfo(unsigned id, const PlayerInfo &tempInfo); void RequestPlayerInfo(unsigned id);
void SetPlayerInfo(unsigned id, const PlayerInfo &info); void SetPlayerInfo(unsigned id, const PlayerInfo &info);
const ClientContext &GetContext() const; const ClientContext &GetContext() const;
@@ -112,8 +113,10 @@ protected:
void RemoveDisconnectedPlayers(); void RemoveDisconnectedPlayers();
unsigned GetGameIdByName(const std::string &name) const; unsigned GetGameIdByName(const std::string &name) const;
void AddGameInfo(unsigned id, const GameInfo &info); void AddGameInfo(unsigned gameId, const GameInfo &info);
void RemoveGameInfo(unsigned id); void RemoveGameInfo(unsigned gameId);
void ModifyGameInfoAddPlayer(unsigned gameId, unsigned playerId);
void ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId);
bool IsSessionEstablished() const; bool IsSessionEstablished() const;
void SetSessionEstablished(bool flag); void SetSessionEstablished(bool flag);
@@ -139,7 +142,10 @@ private:
mutable boost::mutex m_gameInfoMapMutex; mutable boost::mutex m_gameInfoMapMutex;
boost::shared_ptr<Game> m_game; boost::shared_ptr<Game> m_game;
PlayerInfoMap m_playerInfoMap; PlayerInfoMap m_playerInfoMap;
mutable boost::mutex m_playerInfoMapMutex;
PlayerIdList m_playerInfoRequestList;
unsigned m_curGameId; unsigned m_curGameId;
unsigned m_guiPlayerId; unsigned m_guiPlayerId;
+40 -8
View File
@@ -485,6 +485,21 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptr<Net
// A new game was created on the server. // A new game was created on the server.
NetPacketGameListNew::Data gameListNewData; NetPacketGameListNew::Data gameListNewData;
packet->ToNetPacketGameListNew()->GetData(gameListNewData); packet->ToNetPacketGameListNew()->GetData(gameListNewData);
// Request player info for players if needed.
PlayerIdList::const_iterator i = gameListNewData.gameInfo.players.begin();
PlayerIdList::const_iterator end = gameListNewData.gameInfo.players.end();
while (i != end)
{
PlayerInfo info;
if (!client.GetCachedPlayerInfo(*i, info))
{
// Request player info.
client.RequestPlayerInfo(*i);
}
++i;
}
client.AddGameInfo(gameListNewData.gameId, gameListNewData.gameInfo); client.AddGameInfo(gameListNewData.gameId, gameListNewData.gameInfo);
} }
else if (packet->ToNetPacketGameListUpdate()) else if (packet->ToNetPacketGameListUpdate())
@@ -495,6 +510,24 @@ ClientStateWaitJoin::InternalProcess(ClientThread &client, boost::shared_ptr<Net
if (gameListUpdateData.gameMode == GAME_MODE_CLOSED) if (gameListUpdateData.gameMode == GAME_MODE_CLOSED)
client.RemoveGameInfo(gameListUpdateData.gameId); client.RemoveGameInfo(gameListUpdateData.gameId);
} }
else if (packet->ToNetPacketGameListPlayerJoined())
{
NetPacketGameListPlayerJoined::Data playerJoinedData;
packet->ToNetPacketGameListPlayerJoined()->GetData(playerJoinedData);
client.ModifyGameInfoAddPlayer(playerJoinedData.gameId, playerJoinedData.playerId);
// Request player info if needed.
PlayerInfo info;
if (!client.GetCachedPlayerInfo(playerJoinedData.playerId, info))
{
client.RequestPlayerInfo(playerJoinedData.playerId);
}
}
else if (packet->ToNetPacketGameListPlayerLeft())
{
NetPacketGameListPlayerLeft::Data playerLeftData;
packet->ToNetPacketGameListPlayerLeft()->GetData(playerLeftData);
client.ModifyGameInfoRemovePlayer(playerLeftData.gameId, playerLeftData.playerId);
}
else if (packet->ToNetPacketJoinGameAck()) else if (packet->ToNetPacketJoinGameAck())
{ {
// Successfully joined a game. // Successfully joined a game.
@@ -573,25 +606,24 @@ ClientStateWaitGame::InternalProcess(ClientThread &client, boost::shared_ptr<Net
packet->ToNetPacketPlayerJoined()->GetData(netPlayerData); packet->ToNetPacketPlayerJoined()->GetData(netPlayerData);
boost::shared_ptr<PlayerData> playerData; boost::shared_ptr<PlayerData> playerData;
try PlayerInfo info;
if (client.GetCachedPlayerInfo(netPlayerData.playerId, info))
{ {
PlayerInfo info = client.GetCachedPlayerInfo(netPlayerData.playerId);
playerData.reset( playerData.reset(
new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights)); new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights));
playerData->SetName(info.playerName); playerData->SetName(info.playerName);
} catch (const NetException &) }
else
{ {
ostringstream name; ostringstream name;
name << "#" << netPlayerData.playerId; name << "#" << netPlayerData.playerId;
// Request player info. // Request player info.
PlayerInfo info; client.RequestPlayerInfo(netPlayerData.playerId);
info.playerName = name.str();
client.RequestPlayerInfo(netPlayerData.playerId, info);
// Use temporary data until the PlayerInfo request is completed. // Use temporary data until the PlayerInfo request is completed.
playerData.reset( playerData.reset(
new PlayerData(netPlayerData.playerId, 0, info.ptype, netPlayerData.prights)); new PlayerData(netPlayerData.playerId, 0, PLAYER_TYPE_HUMAN, netPlayerData.prights));
playerData->SetName(info.playerName); playerData->SetName(name.str());
} }
client.AddPlayerData(playerData); client.AddPlayerData(playerData);
} }
+84 -43
View File
@@ -28,6 +28,7 @@
#include <game.h> #include <game.h>
#include <boost/lambda/lambda.hpp> #include <boost/lambda/lambda.hpp>
#include <sstream>
#include <cassert> #include <cassert>
using namespace std; using namespace std;
@@ -217,25 +218,33 @@ ClientThread::SendCreateGame(const GameData &gameData, const std::string &name,
} }
GameInfo GameInfo
ClientThread::GetGameInfo(const string &game) const ClientThread::GetGameInfo(unsigned gameId) const
{ {
GameInfo tmpInfo; GameInfo tmpInfo;
try
{
unsigned id = GetGameIdByName(game);
boost::mutex::scoped_lock lock(m_gameInfoMapMutex); boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
GameInfoMap::const_iterator pos = m_gameInfoMap.find(id); GameInfoMap::const_iterator pos = m_gameInfoMap.find(gameId);
if (pos != m_gameInfoMap.end()) if (pos != m_gameInfoMap.end())
{ {
tmpInfo = pos->second; tmpInfo = pos->second;
} }
} catch (const NetException &)
{
}
return tmpInfo; return tmpInfo;
} }
PlayerInfo
ClientThread::GetPlayerInfo(unsigned playerId) const
{
boost::mutex::scoped_lock lock(m_playerInfoMapMutex);
PlayerInfo info;
if (!GetCachedPlayerInfo(playerId, info))
{
ostringstream name;
name << "#" << playerId;
info.playerName = name.str();
}
return info;
}
ClientCallback & ClientCallback &
ClientThread::GetCallback() ClientThread::GetCallback()
{ {
@@ -320,17 +329,25 @@ ClientThread::SendPacketLoop()
} }
} }
PlayerInfo bool
ClientThread::GetCachedPlayerInfo(unsigned id) const ClientThread::GetCachedPlayerInfo(unsigned id, PlayerInfo &info) const
{ {
bool retVal = false;
boost::mutex::scoped_lock lock(m_playerInfoMapMutex);
PlayerInfoMap::const_iterator pos = m_playerInfoMap.find(id); PlayerInfoMap::const_iterator pos = m_playerInfoMap.find(id);
if (pos == m_playerInfoMap.end()) if (pos != m_playerInfoMap.end())
throw NetException(ERR_NET_UNKNOWN_PLAYER_ID, 0); {
return pos->second; info = pos->second;
retVal = true;
}
return retVal;
} }
void void
ClientThread::RequestPlayerInfo(unsigned id, const PlayerInfo &tempInfo) ClientThread::RequestPlayerInfo(unsigned id)
{
if (find(m_playerInfoRequestList.begin(), m_playerInfoRequestList.end(), id) == m_playerInfoRequestList.end())
{ {
boost::shared_ptr<NetPacket> req(new NetPacketRetrievePlayerInfo); boost::shared_ptr<NetPacket> req(new NetPacketRetrievePlayerInfo);
NetPacketRetrievePlayerInfo::Data reqData; NetPacketRetrievePlayerInfo::Data reqData;
@@ -338,25 +355,18 @@ ClientThread::RequestPlayerInfo(unsigned id, const PlayerInfo &tempInfo)
static_cast<NetPacketRetrievePlayerInfo *>(req.get())->SetData(reqData); static_cast<NetPacketRetrievePlayerInfo *>(req.get())->SetData(reqData);
GetSender().Send(GetContext().GetSocket(), req); GetSender().Send(GetContext().GetSocket(), req);
m_playerInfoMap[id] = tempInfo; m_playerInfoRequestList.push_back(id);
}
} }
void void
ClientThread::SetPlayerInfo(unsigned id, const PlayerInfo &info) ClientThread::SetPlayerInfo(unsigned id, const PlayerInfo &info)
{ {
PlayerInfoMap::iterator pos = m_playerInfoMap.find(id);
// Update info cache.
if (pos != m_playerInfoMap.end())
{ {
GetCallback().SignalNetClientPlayerChanged(pos->second.playerName, info.playerName); boost::mutex::scoped_lock lock(m_playerInfoMapMutex);
m_playerInfoMap[id] = info;
pos->second = info;
}
else
{
m_playerInfoMap.insert(PlayerInfoMap::value_type(id, info));
} }
GetCallback().SignalNetClientPlayerChanged(id, info.playerName);
// Update player data for current game. // Update player data for current game.
boost::shared_ptr<PlayerData> playerData = GetPlayerDataByUniqueId(id); boost::shared_ptr<PlayerData> playerData = GetPlayerDataByUniqueId(id);
@@ -464,16 +474,16 @@ ClientThread::AddPlayerData(boost::shared_ptr<PlayerData> playerData)
{ {
m_playerDataList.push_back(playerData); m_playerDataList.push_back(playerData);
if (playerData->GetUniqueId() == GetGuiPlayerId()) if (playerData->GetUniqueId() == GetGuiPlayerId())
GetCallback().SignalNetClientSelfJoined(playerData->GetName(), playerData->GetRights()); GetCallback().SignalNetClientSelfJoined(playerData->GetUniqueId(), playerData->GetName(), playerData->GetRights());
else else
GetCallback().SignalNetClientPlayerJoined(playerData->GetName(), playerData->GetRights()); GetCallback().SignalNetClientPlayerJoined(playerData->GetUniqueId(), playerData->GetName(), playerData->GetRights());
} }
} }
void void
ClientThread::RemovePlayerData(unsigned playerId) ClientThread::RemovePlayerData(unsigned playerId)
{ {
string playerName; boost::shared_ptr<PlayerData> tmpData;
PlayerDataList::iterator i = m_playerDataList.begin(); PlayerDataList::iterator i = m_playerDataList.begin();
PlayerDataList::iterator end = m_playerDataList.end(); PlayerDataList::iterator end = m_playerDataList.end();
@@ -481,20 +491,17 @@ ClientThread::RemovePlayerData(unsigned playerId)
{ {
if ((*i)->GetUniqueId() == playerId) if ((*i)->GetUniqueId() == playerId)
{ {
playerName = (*i)->GetName(); tmpData = *i;
m_playerDataList.erase(i); m_playerDataList.erase(i);
break; break;
} }
++i; ++i;
} }
if (!playerName.empty()) if (tmpData.get())
{ {
// Remove name and id string. // Remove player from gui.
GetCallback().SignalNetClientPlayerLeft(playerName); GetCallback().SignalNetClientPlayerLeft(tmpData->GetUniqueId(), tmpData->GetName());
ostringstream name;
name << "#" << playerId;
GetCallback().SignalNetClientPlayerLeft(name.str());
} }
} }
@@ -623,22 +630,22 @@ ClientThread::GetGameIdByName(const std::string &name) const
} }
void void
ClientThread::AddGameInfo(unsigned id, const GameInfo &info) ClientThread::AddGameInfo(unsigned gameId, const GameInfo &info)
{ {
{ {
boost::mutex::scoped_lock lock(m_gameInfoMapMutex); boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
m_gameInfoMap.insert(GameInfoMap::value_type(id, info)); m_gameInfoMap.insert(GameInfoMap::value_type(gameId, info));
} }
GetCallback().SignalNetClientGameListNew(info.name); GetCallback().SignalNetClientGameListNew(gameId, info.name);
} }
void void
ClientThread::RemoveGameInfo(unsigned id) ClientThread::RemoveGameInfo(unsigned gameId)
{ {
string name; string name;
{ {
boost::mutex::scoped_lock lock(m_gameInfoMapMutex); boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
GameInfoMap::iterator pos = m_gameInfoMap.find(id); GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
if (pos != m_gameInfoMap.end()) if (pos != m_gameInfoMap.end())
{ {
name = pos->second.name; name = pos->second.name;
@@ -646,7 +653,41 @@ ClientThread::RemoveGameInfo(unsigned id)
} }
} }
if (!name.empty()) if (!name.empty())
GetCallback().SignalNetClientGameListRemove(name); GetCallback().SignalNetClientGameListRemove(gameId, name);
}
void
ClientThread::ModifyGameInfoAddPlayer(unsigned gameId, unsigned playerId)
{
bool playerAdded = false;
{
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
if (pos != m_gameInfoMap.end())
{
pos->second.players.push_back(playerId);
playerAdded = true;
}
}
if (playerAdded)
GetCallback().SignalNetClientGameListPlayerJoined(gameId, playerId);
}
void
ClientThread::ModifyGameInfoRemovePlayer(unsigned gameId, unsigned playerId)
{
bool playerRemoved = false;
{
boost::mutex::scoped_lock lock(m_gameInfoMapMutex);
GameInfoMap::iterator pos = m_gameInfoMap.find(gameId);
if (pos != m_gameInfoMap.end())
{
pos->second.players.remove(playerId);
playerRemoved = true;
}
}
if (playerRemoved)
GetCallback().SignalNetClientGameListPlayerLeft(gameId, playerId);
} }
bool bool
+1 -1
View File
@@ -1091,7 +1091,7 @@ NetPacketGameListNew::GetData(NetPacketGameListNew::Data &outData) const
// Store all available players. // Store all available players.
for (int i = 0; i < curNumPlayers; i++) for (int i = 0; i < curNumPlayers; i++)
{ {
outData.gameInfo.players.push_back(*tmpPlayer); outData.gameInfo.players.push_back(ntohl(*tmpPlayer));
++tmpPlayer; ++tmpPlayer;
} }
} }
+4
View File
@@ -19,6 +19,7 @@
#include <net/servergamestate.h> #include <net/servergamestate.h>
#include <net/servergamethread.h> #include <net/servergamethread.h>
#include <net/serverlobbythread.h>
#include <net/receiverhelper.h> #include <net/receiverhelper.h>
#include <net/senderthread.h> #include <net/senderthread.h>
#include <net/netpacket.h> #include <net/netpacket.h>
@@ -279,6 +280,9 @@ ServerGameStateInit::HandleNewSession(ServerGameThread &server, SessionWrapper s
session.sessionData->SetState(SessionData::Game); session.sessionData->SetState(SessionData::Game);
// Accept session. // Accept session.
server.GetSessionManager().AddSession(session); server.GetSessionManager().AddSession(session);
// Notify lobby.
server.GetLobbyThread().NotifyPlayerJoinedGame(server.GetId(), session.playerData->GetUniqueId());
} }
} }
} }
+1 -2
View File
@@ -227,11 +227,10 @@ ServerGameThread::CloseSessionDelayed(SessionWrapper session)
thisPlayerLeftData.playerId = tmpPlayerData->GetUniqueId(); thisPlayerLeftData.playerId = tmpPlayerData->GetUniqueId();
static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData); static_cast<NetPacketPlayerLeft *>(thisPlayerLeft.get())->SetData(thisPlayerLeftData);
GetSessionManager().SendToAllSessions(GetSender(), thisPlayerLeft, SessionData::Game); GetSessionManager().SendToAllSessions(GetSender(), thisPlayerLeft, SessionData::Game);
GetCallback().SignalNetServerPlayerLeft(tmpPlayerData->GetName());
} }
GetLobbyThread().CloseSessionDelayed(session); GetLobbyThread().CloseSessionDelayed(session);
GetLobbyThread().NotifyPlayerLeftGame(GetId(), session.playerData->GetUniqueId());
} }
void void
+24
View File
@@ -93,6 +93,30 @@ ServerLobbyThread::CloseSessionDelayed(SessionWrapper session)
m_closeSessionList.push_back(closeSessionData); m_closeSessionList.push_back(closeSessionData);
} }
void
ServerLobbyThread::NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId)
{
// Send notification to players in lobby.
boost::shared_ptr<NetPacket> packet(new NetPacketGameListPlayerJoined);
NetPacketGameListPlayerJoined::Data packetData;
packetData.gameId = gameId;
packetData.playerId = playerId;
static_cast<NetPacketGameListPlayerJoined *>(packet.get())->SetData(packetData);
m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established);
}
void
ServerLobbyThread::NotifyPlayerLeftGame(unsigned gameId, unsigned playerId)
{
// Send notification to players in lobby.
boost::shared_ptr<NetPacket> packet(new NetPacketGameListPlayerLeft);
NetPacketGameListPlayerLeft::Data packetData;
packetData.gameId = gameId;
packetData.playerId = playerId;
static_cast<NetPacketGameListPlayerLeft *>(packet.get())->SetData(packetData);
m_sessionManager.SendToAllSessions(GetSender(), packet, SessionData::Established);
}
void void
ServerLobbyThread::RemoveGame(unsigned id) ServerLobbyThread::RemoveGame(unsigned id)
{ {
-3
View File
@@ -30,9 +30,6 @@ public:
virtual void SignalNetServerSuccess(int actionID) = 0; virtual void SignalNetServerSuccess(int actionID) = 0;
virtual void SignalNetServerError(int errorID, int osErrorID) = 0; virtual void SignalNetServerError(int errorID, int osErrorID) = 0;
virtual void SignalNetServerPlayerJoined(const std::string &playerName) = 0;
virtual void SignalNetServerPlayerLeft(const std::string &playerName) = 0;
}; };
#endif #endif
+2
View File
@@ -52,6 +52,8 @@ public:
void AddConnection(boost::shared_ptr<ConnectData> data); void AddConnection(boost::shared_ptr<ConnectData> data);
void CloseSessionDelayed(SessionWrapper session); void CloseSessionDelayed(SessionWrapper session);
void NotifyPlayerJoinedGame(unsigned gameId, unsigned playerId);
void NotifyPlayerLeftGame(unsigned gameId, unsigned playerId);
void RemoveGame(unsigned id); void RemoveGame(unsigned id);
+1
View File
@@ -90,6 +90,7 @@ int main( int argc, char **argv )
a.installTranslator(&translator); a.installTranslator(&translator);
qRegisterMetaType<unsigned>("unsigned");
qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>"); qRegisterMetaType<boost::shared_ptr<Game> >("boost::shared_ptr<Game>");
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
+8 -2
View File
@@ -276,9 +276,15 @@ bool Session::isNetworkServerRunning() const
return myNetServer != NULL; return myNetServer != NULL;
} }
GameInfo Session::getClientGameInfo(const string &game) GameInfo Session::getClientGameInfo(unsigned playerId)
{ {
assert(myNetClient); assert(myNetClient);
return myNetClient->GetGameInfo(game); return myNetClient->GetGameInfo(playerId);
}
PlayerInfo Session::getClientPlayerInfo(unsigned playerId)
{
assert(myNetClient);
return myNetClient->GetPlayerInfo(playerId);
} }
+2 -1
View File
@@ -68,7 +68,8 @@ public:
bool isNetworkClientRunning() const; // TODO hack bool isNetworkClientRunning() const; // TODO hack
bool isNetworkServerRunning() const; // TODO hack bool isNetworkServerRunning() const; // TODO hack
GameInfo getClientGameInfo(const std::string &game); GameInfo getClientGameInfo(unsigned gameId);
PlayerInfo getClientPlayerInfo(unsigned playerId);
private: private: