From 03083e8ec0df22abc56400985ae0237624946eb1 Mon Sep 17 00:00:00 2001 From: lotodore Date: Tue, 11 Oct 2011 19:18:11 +0000 Subject: [PATCH] Rejoin works. --- src/engine/game.h | 5 ++ src/net/common/clientstate.cpp | 78 ++++++++++++++++-------------- src/net/common/servergame.cpp | 2 +- src/net/common/servergamestate.cpp | 16 +++++- 4 files changed, 63 insertions(+), 38 deletions(-) diff --git a/src/engine/game.h b/src/engine/game.h index 26ed3917..cdc0762b 100755 --- a/src/engine/game.h +++ b/src/engine/game.h @@ -103,6 +103,11 @@ public: return dealerPosition; } + void replaceDealer(unsigned oldDealer, unsigned newDealer) { + if (dealerPosition == oldDealer) + dealerPosition = newDealer; + } + boost::shared_ptr getPlayerByUniqueId(unsigned id); boost::shared_ptr getPlayerByName(const std::string &name); boost::shared_ptr getCurrentPlayer(); diff --git a/src/net/common/clientstate.cpp b/src/net/common/clientstate.cpp index a8891ddb..3e011772 100644 --- a/src/net/common/clientstate.cpp +++ b/src/net/common/clientstate.cpp @@ -682,6 +682,36 @@ AbstractClientStateReceiving::HandlePacket(boost::shared_ptr clien // Set new game admin and signal to GUI. client->SetNewGameAdmin(netChanged->newAdminPlayerId); + } else if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_gamePlayerJoined) { + // Another player joined the network game. + GamePlayerJoined_t *netPlayerJoined = &netGamePlayer->gamePlayerNotification.choice.gamePlayerJoined; + + boost::shared_ptr playerData; + PlayerInfo info; + if (client->GetCachedPlayerInfo(netPlayerJoined->playerId, info)) { + playerData.reset( + new PlayerData(netPlayerJoined->playerId, 0, info.ptype, + info.isGuest ? PLAYER_RIGHTS_GUEST : PLAYER_RIGHTS_NORMAL, netPlayerJoined->isGameAdmin)); + playerData->SetName(info.playerName); + if (info.hasAvatar) { + string avatarFile; + if (client->GetAvatarManager().GetAvatarFileName(info.avatar, avatarFile)) + playerData->SetAvatarFile(client->GetQtToolsInterface().stringToUtf8(avatarFile)); + else + client->RetrieveAvatarIfNeeded(netPlayerJoined->playerId, info); + } + } else { + ostringstream name; + name << "#" << netPlayerJoined->playerId; + + // Request player info. + client->RequestPlayerInfo(netPlayerJoined->playerId, true); + // Use temporary data until the PlayerInfo request is completed. + playerData.reset( + new PlayerData(netPlayerJoined->playerId, 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL, netPlayerJoined->isGameAdmin)); + playerData->SetName(name.str()); + } + client->AddPlayerData(playerData); } } else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_timeoutWarningMessage) { TimeoutWarningMessage_t *tmpTimeout = &tmpPacket->GetMsg()->choice.timeoutWarningMessage; @@ -1347,40 +1377,6 @@ ClientStateWaitGame::InternalHandlePacket(boost::shared_ptr client if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_startEventMessage) { client->GetCallback().SignalNetClientGameInfo(MSG_NET_GAME_CLIENT_SYNCSTART); client->SetState(ClientStateSynchronizeStart::Instance()); - } else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_gamePlayerMessage) { - GamePlayerMessage_t *netGamePlayer = &tmpPacket->GetMsg()->choice.gamePlayerMessage; -// unsigned gameId = netGamePlayer->gameId; - if (netGamePlayer->gamePlayerNotification.present == gamePlayerNotification_PR_gamePlayerJoined) { - // Another player joined the network game. - GamePlayerJoined_t *netPlayerJoined = &netGamePlayer->gamePlayerNotification.choice.gamePlayerJoined; - - boost::shared_ptr playerData; - PlayerInfo info; - if (client->GetCachedPlayerInfo(netPlayerJoined->playerId, info)) { - playerData.reset( - new PlayerData(netPlayerJoined->playerId, 0, info.ptype, - info.isGuest ? PLAYER_RIGHTS_GUEST : PLAYER_RIGHTS_NORMAL, netPlayerJoined->isGameAdmin)); - playerData->SetName(info.playerName); - if (info.hasAvatar) { - string avatarFile; - if (client->GetAvatarManager().GetAvatarFileName(info.avatar, avatarFile)) - playerData->SetAvatarFile(client->GetQtToolsInterface().stringToUtf8(avatarFile)); - else - client->RetrieveAvatarIfNeeded(netPlayerJoined->playerId, info); - } - } else { - ostringstream name; - name << "#" << netPlayerJoined->playerId; - - // Request player info. - client->RequestPlayerInfo(netPlayerJoined->playerId, true); - // Use temporary data until the PlayerInfo request is completed. - playerData.reset( - new PlayerData(netPlayerJoined->playerId, 0, PLAYER_TYPE_HUMAN, PLAYER_RIGHTS_NORMAL, netPlayerJoined->isGameAdmin)); - playerData->SetName(name.str()); - } - client->AddPlayerData(playerData); - } } else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_inviteNotifyMessage) { InviteNotifyMessage_t *netInvNotify = &tmpPacket->GetMsg()->choice.inviteNotifyMessage; client->GetCallback().SignalPlayerGameInvitation( @@ -1635,7 +1631,7 @@ ClientStateWaitHand::InternalHandlePacket(boost::shared_ptr client client->SetState(ClientStateRunHand::Instance()); } else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_endOfGameMessage) { boost::shared_ptr curGame = client->GetGame(); - if (curGame.get()) { + if (curGame) { EndOfGameMessage_t *netEndOfGame = &tmpPacket->GetMsg()->choice.endOfGameMessage; boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(netEndOfGame->winnerPlayerId); @@ -1670,6 +1666,18 @@ ClientStateWaitHand::InternalHandlePacket(boost::shared_ptr client tmpPlayer->setLastMoneyWon(r->moneyWon); client->GetCallback().SignalNetClientPostRiverShowCards(r->playerId); + } else if (tmpPacket->GetMsg()->present == PokerTHMessage_PR_playerIdChangedMessage) { + boost::shared_ptr curGame = client->GetGame(); + if (curGame) { + // Perform Id change. + PlayerIdChangedMessage_t *idChanged = &tmpPacket->GetMsg()->choice.playerIdChangedMessage; + boost::shared_ptr tmpPlayer = curGame->getPlayerByUniqueId(idChanged->oldPlayerId); + if (!tmpPlayer) + throw ClientException(__FILE__, __LINE__, ERR_NET_UNKNOWN_PLAYER_ID, 0); + tmpPlayer->setMyUniqueID(idChanged->newPlayerId); + // Also update the dealer, if necessary. + curGame->replaceDealer(idChanged->oldPlayerId, idChanged->newPlayerId); + } } } diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index da68e987..1d819f8b 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -812,7 +812,7 @@ ServerGame::RemoveDisconnectedPlayers() if ((tmpPlayer->getMyType() == PLAYER_TYPE_HUMAN && !GetSessionManager().IsPlayerConnected(tmpPlayer->getMyUniqueID())) || (tmpPlayer->getMyType() == PLAYER_TYPE_COMPUTER && !IsComputerPlayerActive(tmpPlayer->getMyUniqueID()))) { // Setting player cash to 0 will deactivate the player. - tmpPlayer->setMyCash(0); + //tmpPlayer->setMyCash(0); tmpPlayer->setIsConnected(false); } ++i; diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index e27d0117..0322d7af 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -1146,9 +1146,22 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) // Set new player id. boost::shared_ptr rejoinPlayer = curGame.getPlayerByName(session->GetPlayerData()->GetName()); if (rejoinPlayer) { + // Notify other clients about id change. + boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + packet->GetMsg()->present = PokerTHMessage_PR_playerIdChangedMessage; + PlayerIdChangedMessage_t *netIdChanged = &packet->GetMsg()->choice.playerIdChangedMessage; + netIdChanged->oldPlayerId = rejoinPlayer->getMyUniqueID(); + netIdChanged->newPlayerId = session->GetPlayerData()->GetUniqueId(); + server->SendToAllButOnePlayers(packet, session->GetId(), SessionData::Game); + + // Change the Id in the poker engine. rejoinPlayer->setMyUniqueID(session->GetPlayerData()->GetUniqueId()); rejoinPlayer->setIsConnected(true); - boost::shared_ptr packet(new NetPacket(NetPacket::Alloc)); + // Also update the dealer, if necessary. + curGame.replaceDealer(rejoinPlayer->getMyUniqueID(), session->GetPlayerData()->GetUniqueId()); + + // Send game start notification to rejoining client. + packet.reset(new NetPacket(NetPacket::Alloc)); packet->GetMsg()->present = PokerTHMessage_PR_gameStartMessage; GameStartMessage_t *netGameStart = &packet->GetMsg()->choice.gameStartMessage; netGameStart->gameId = server->GetId(); @@ -1156,7 +1169,6 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) netGameStart->gameStartMode.present = gameStartMode_PR_gameStartModeRejoin; GameStartModeRejoin_t *netStartModeRejoin = &netGameStart->gameStartMode.choice.gameStartModeRejoin; - // Send player data to client. netStartModeRejoin->handNum = curGame.getCurrentHandID(); PlayerListIterator player_i = curGame.getSeatsList()->begin(); PlayerListIterator player_end = curGame.getSeatsList()->end();