From 1bf12328ee65760cf704e9db6db0ab5f7c9da4af Mon Sep 17 00:00:00 2001 From: lotodore Date: Sun, 11 Dec 2011 19:37:51 +0000 Subject: [PATCH] Remove players from game (without possibility to rejoin) if they did not act within a certain time. Feature is still broken, because the timer is canceled on disconnect. --- src/engine/game.cpp | 2 +- src/engine/local_engine/localplayer.cpp | 10 ++--- src/engine/local_engine/localplayer.h | 6 +-- src/engine/network_engine/clientplayer.cpp | 10 ++--- src/engine/network_engine/clientplayer.h | 6 +-- src/engine/playerinterface.h | 4 +- src/net/common/servergame.cpp | 45 +++++++++++++--------- src/net/common/servergamestate.cpp | 18 ++++++--- src/net/common/serverlobbythread.cpp | 12 +++++- src/net/servergame.h | 3 +- 10 files changed, 72 insertions(+), 44 deletions(-) diff --git a/src/engine/game.cpp b/src/engine/game.cpp index 61579434..2f04568b 100755 --- a/src/engine/game.cpp +++ b/src/engine/game.cpp @@ -96,7 +96,7 @@ Game::Game(GuiInterface* gui, boost::shared_ptr factory, // create player objects boost::shared_ptr tmpPlayer = myFactory->createPlayer(i, uniqueId, type, myName, myAvatarFile, myStartCash, startQuantityPlayers > i, 0); - tmpPlayer->setIsConnected(true); + tmpPlayer->setIsSessionActive(true); tmpPlayer->setMyGuid(myGuid); // fill player lists diff --git a/src/engine/local_engine/localplayer.cpp b/src/engine/local_engine/localplayer.cpp index edde803f..fae3e8c7 100755 --- a/src/engine/local_engine/localplayer.cpp +++ b/src/engine/local_engine/localplayer.cpp @@ -849,7 +849,7 @@ LocalPlayer::LocalPlayer(ConfigFile *c, int id, unsigned uniqueId, PlayerType ty : PlayerInterface(), myConfig(c), currentHand(0), myID(id), myUniqueID(uniqueId), myType(type), myName(name), myAvatar(avatar), myDude(0), myDude4(0), myCardsValueInt(0), myOdds(-1.0), logHoleCardsDone(false), myCash(sC), mySet(0), myLastRelativeSet(0), myAction(PLAYER_ACTION_NONE), myButton(mB), myActiveStatus(aS), myStayOnTableStatus(1), myTurn(0), myCardsFlip(0), myRoundStartCash(0), lastMoneyWon(0), - sBluff(0), sBluffStatus(false), m_actionTimeoutCounter(0), m_isConnected(false), m_isKicked(false) + sBluff(0), sBluffStatus(false), m_actionTimeoutCounter(0), m_isSessionActive(false), m_isKicked(false) { // !!!!!!!!!!!!!!!!!!!!!!!! testing !!!!!!!!!!!!!!!!!!!!!!!! @@ -5094,14 +5094,14 @@ void LocalPlayer::riverEngine3() } -void LocalPlayer::setIsConnected(bool connected) +void LocalPlayer::setIsSessionActive(bool active) { - m_isConnected = connected; + m_isSessionActive = active; } -bool LocalPlayer::isConnected() const +bool LocalPlayer::isSessionActive() const { - return m_isConnected; + return m_isSessionActive; } void LocalPlayer::setIsKicked(bool kicked) diff --git a/src/engine/local_engine/localplayer.h b/src/engine/local_engine/localplayer.h index 0a70d71c..94512fb3 100755 --- a/src/engine/local_engine/localplayer.h +++ b/src/engine/local_engine/localplayer.h @@ -280,8 +280,8 @@ public: void evaluation(int, int); - void setIsConnected(bool connected); - bool isConnected() const; + void setIsSessionActive(bool active); + bool isSessionActive() const; void setIsKicked(bool kicked); bool isKicked() const; @@ -334,7 +334,7 @@ private: bool sBluffStatus; unsigned m_actionTimeoutCounter; - bool m_isConnected; + bool m_isSessionActive; bool m_isKicked; }; diff --git a/src/engine/network_engine/clientplayer.cpp b/src/engine/network_engine/clientplayer.cpp index 93b938b1..f4c0e330 100644 --- a/src/engine/network_engine/clientplayer.cpp +++ b/src/engine/network_engine/clientplayer.cpp @@ -26,7 +26,7 @@ ClientPlayer::ClientPlayer(ConfigFile *c, int id, unsigned uniqueId, PlayerType : PlayerInterface(), myConfig(c), currentHand(0), myID(id), myUniqueID(uniqueId), myType(type), myName(name), myAvatar(avatar), myDude(0), myDude4(0), myCardsValueInt(0), myOdds(-1.0), logHoleCardsDone(false), myCash(sC), mySet(0), myLastRelativeSet(0), myAction(PLAYER_ACTION_NONE), myButton(mB), myActiveStatus(aS), myStayOnTableStatus(true), myTurn(false), myCardsFlip(false), myRoundStartCash(0), - lastMoneyWon(0), sBluff(0), sBluffStatus(false), m_isConnected(false), m_isKicked(false) + lastMoneyWon(0), sBluff(0), sBluffStatus(false), m_isSessionActive(false), m_isKicked(false) { myBestHandPosition[0] = myBestHandPosition[1] = myBestHandPosition[2] = myBestHandPosition[3] = myBestHandPosition[4] = 0; myNiveau[0] = myNiveau[1] = myNiveau[2] = 0; @@ -536,17 +536,17 @@ ClientPlayer::riverEngine3() void -ClientPlayer::setIsConnected(bool connected) +ClientPlayer::setIsSessionActive(bool active) { boost::recursive_mutex::scoped_lock lock(m_syncMutex); - m_isConnected = connected; + m_isSessionActive = active; } bool -ClientPlayer::isConnected() const +ClientPlayer::isSessionActive() const { boost::recursive_mutex::scoped_lock lock(m_syncMutex); - return m_isConnected; + return m_isSessionActive; } void diff --git a/src/engine/network_engine/clientplayer.h b/src/engine/network_engine/clientplayer.h index 47ae5a8b..2789391b 100644 --- a/src/engine/network_engine/clientplayer.h +++ b/src/engine/network_engine/clientplayer.h @@ -133,8 +133,8 @@ public: void evaluation(int, int); - void setIsConnected(bool connected); - bool isConnected() const; + void setIsSessionActive(bool active); + bool isSessionActive() const; void setIsKicked(bool kicked); bool isKicked() const; @@ -183,7 +183,7 @@ private: int sBluff; bool sBluffStatus; - bool m_isConnected; + bool m_isSessionActive; bool m_isKicked; }; diff --git a/src/engine/playerinterface.h b/src/engine/playerinterface.h index 37b147b4..a7d818a9 100644 --- a/src/engine/playerinterface.h +++ b/src/engine/playerinterface.h @@ -118,8 +118,8 @@ public: virtual void turnEngine() =0; virtual void riverEngine() =0; - virtual void setIsConnected(bool connected) =0; - virtual bool isConnected() const=0; + virtual void setIsSessionActive(bool connected) =0; + virtual bool isSessionActive() const=0; virtual void setIsKicked(bool kicked) =0; virtual bool isKicked() const=0; diff --git a/src/net/common/servergame.cpp b/src/net/common/servergame.cpp index 611d8c7e..30966caa 100644 --- a/src/net/common/servergame.cpp +++ b/src/net/common/servergame.cpp @@ -97,15 +97,37 @@ ServerGame::AddSession(boost::shared_ptr session) void ServerGame::RemovePlayer(unsigned playerId, unsigned errorCode) { - if (errorCode == ERR_NET_PLAYER_KICKED) { - MarkPlayerAsKicked(playerId); - } boost::shared_ptr tmpSession = GetSessionManager().GetSessionByUniquePlayerId(playerId); // Only kick if the player was found. if (tmpSession) SessionError(tmpSession, errorCode); } +void +ServerGame::MarkPlayerAsInactive(unsigned playerId) +{ + if (m_game) { + boost::shared_ptr tmpPlayer(m_game->getPlayerByUniqueId(playerId)); + if (tmpPlayer) { + tmpPlayer->setIsSessionActive(false); + } + } +} + +void +ServerGame::MarkPlayerAsKicked(unsigned playerId) +{ + // Mark the player as kicked in the engine. + if (m_game) { + boost::shared_ptr tmpPlayer(m_game->getPlayerByUniqueId(playerId)); + if (tmpPlayer) { + // Player was kicked, so he is not allowed to rejoin. + tmpPlayer->setIsKicked(true); + tmpPlayer->setMyGuid(""); + } + } +} + void ServerGame::HandlePacket(boost::shared_ptr session, boost::shared_ptr packet) { @@ -375,20 +397,6 @@ ServerGame::InternalEndGame() m_game.reset(); } -void -ServerGame::MarkPlayerAsKicked(unsigned playerId) -{ - // Mark the player as kicked in the engine. - if (m_game) { - boost::shared_ptr tmpPlayer(m_game->getPlayerByUniqueId(playerId)); - if (tmpPlayer) { - // Player was kicked, so he is not allowed to rejoin. - tmpPlayer->setIsKicked(true); - tmpPlayer->setMyGuid(""); - } - } -} - void ServerGame::InternalKickPlayer(unsigned playerId) { @@ -828,8 +836,9 @@ ServerGame::RemoveDisconnectedPlayers() // The player should only be deactivated if rejoin is not possible. if (tmpPlayer->isKicked() || tmpPlayer->getMyGuid().empty()) { tmpPlayer->setMyCash(0); + tmpPlayer->setMyGuid(""); } - tmpPlayer->setIsConnected(false); + tmpPlayer->setIsSessionActive(false); } ++i; } diff --git a/src/net/common/servergamestate.cpp b/src/net/common/servergamestate.cpp index dc3f7b91..da770b17 100644 --- a/src/net/common/servergamestate.cpp +++ b/src/net/common/servergamestate.cpp @@ -222,8 +222,15 @@ AbstractServerGameStateReceiving::~AbstractServerGameStateReceiving() void AbstractServerGameStateReceiving::ProcessPacket(boost::shared_ptr server, boost::shared_ptr session, boost::shared_ptr packet) { - if (packet->IsClientActivity()) + if (packet->IsClientActivity()) { session->ResetActivityTimer(); + if (server->IsRunning()) { + boost::shared_ptr tmpPlayer(server->GetGame().getPlayerByUniqueId(session->GetPlayerData()->GetUniqueId())); + if (tmpPlayer) { + tmpPlayer->setIsSessionActive(true); + } + } + } if (packet->GetMsg()->present == PokerTHMessage_PR_playerInfoRequestMessage) { // Delegate to Lobby. server->GetLobbyThread().HandleGameRetrievePlayerInfo(session, packet->GetMsg()->choice.playerInfoRequestMessage); @@ -940,7 +947,8 @@ ServerGameStateHand::EngineLoop(boost::shared_ptr server) &ServerGameStateHand::TimerComputerAction, this, boost::asio::placeholders::error, server)); } // If the player we are waiting for left, continue without him. - else if (!server->GetSessionManager().IsPlayerConnected(curPlayer->getMyUniqueID())) { + else if (!server->GetSessionManager().IsPlayerConnected(curPlayer->getMyUniqueID()) + || !curPlayer->isSessionActive()) { PerformPlayerAction(*server, curPlayer, PLAYER_ACTION_FOLD, 0); server->GetStateTimer1().expires_from_now( @@ -1138,10 +1146,10 @@ ServerGameStateHand::StartNewHand(boost::shared_ptr server) // Send cards to all players. while (i != end) { - // also send to inactive players, but not to disconnected players. + // Also send to inactive players. boost::shared_ptr tmpPlayer = *i; boost::shared_ptr tmpSession = server->GetSessionManager().GetSessionByUniquePlayerId(tmpPlayer->getMyUniqueID()); - if (tmpPlayer->isConnected() && tmpSession) { + if (tmpSession) { int cards[2]; bool errorFlag = false; tmpPlayer->getMyCards(cards); @@ -1274,7 +1282,7 @@ ServerGameStateHand::PerformRejoin(boost::shared_ptr server, boost:: // Change the Id in the poker engine. rejoinPlayer->setMyUniqueID(session->GetPlayerData()->GetUniqueId()); rejoinPlayer->setMyGuid(session->GetPlayerData()->GetGuid()); - rejoinPlayer->setIsConnected(true); + rejoinPlayer->setIsSessionActive(true); // Send game start notification to rejoining client. packet.reset(new NetPacket(NetPacket::Alloc)); diff --git a/src/net/common/serverlobbythread.cpp b/src/net/common/serverlobbythread.cpp index 8964beb6..28788686 100644 --- a/src/net/common/serverlobbythread.cpp +++ b/src/net/common/serverlobbythread.cpp @@ -65,7 +65,7 @@ #define SERVER_INIT_SESSION_TIMEOUT_SEC 60 #define SERVER_TIMEOUT_WARNING_REMAINING_SEC 60 -#define SERVER_SESSION_ACTIVITY_TIMEOUT_SEC 1800 // 30 min, MUST be > SERVER_TIMEOUT_WARNING_REMAINING_SEC +#define SERVER_SESSION_ACTIVITY_TIMEOUT_SEC 180/*1800*/ // 30 min, MUST be > SERVER_TIMEOUT_WARNING_REMAINING_SEC #define SERVER_SESSION_FORCED_TIMEOUT_SEC 86400 // 1 day, should be quite large. #define SERVER_ADDRESS_LOCALHOST_STR_V4 "127.0.0.1" @@ -1802,12 +1802,22 @@ ServerLobbyThread::SessionTimeoutWarning(boost::shared_ptr session, netWarning->timeoutReason = timeoutReason_timeoutNoDataReceived; netWarning->remainingSeconds = remainingSec; GetSender().Send(session, packet); + + if (session->GetGame() && session->GetPlayerData()) { + session->GetGame()->MarkPlayerAsInactive(session->GetPlayerData()->GetUniqueId()); + } } void ServerLobbyThread::SessionError(boost::shared_ptr session, int errorCode) { if (session) { + if (errorCode == ERR_NET_PLAYER_KICKED || errorCode == ERR_NET_SESSION_TIMED_OUT) { + if (session->GetGame() && session->GetPlayerData()) { + session->GetGame()->MarkPlayerAsKicked(session->GetPlayerData()->GetUniqueId()); + } + } + SendError(session, errorCode); CloseSession(session); } diff --git a/src/net/servergame.h b/src/net/servergame.h index 49075f04..d5f97d37 100644 --- a/src/net/servergame.h +++ b/src/net/servergame.h @@ -54,6 +54,8 @@ public: void AddSession(boost::shared_ptr session); void RemovePlayer(unsigned playerId, unsigned errorCode); + void MarkPlayerAsInactive(unsigned playerId); + void MarkPlayerAsKicked(unsigned playerId); void HandlePacket(boost::shared_ptr session, boost::shared_ptr packet); @@ -115,7 +117,6 @@ protected: void RemoveAutoLeavePlayers(); void InternalEndGame(); - void MarkPlayerAsKicked(unsigned playerId); void InternalKickPlayer(unsigned playerId); void InternalAskVoteKick(boost::shared_ptr byWhom, unsigned playerIdWho, unsigned timeoutSec); void InternalDenyAskVoteKick(boost::shared_ptr byWhom, unsigned playerIdWho, DenyKickPlayerReason reason);