Adding feature to leave a game as spectator.

This commit is contained in:
lotodore
2013-08-11 13:25:26 +02:00
parent be0f363af4
commit 7b305b36e2
4 changed files with 28 additions and 16 deletions
+25 -13
View File
@@ -837,7 +837,7 @@ ServerGame::ResetComputerPlayerList()
while (i != end) { while (i != end) {
GetLobbyThread().RemoveComputerPlayer(*i); GetLobbyThread().RemoveComputerPlayer(*i);
RemovePlayerData(*i, NTF_NET_REMOVED_ON_REQUEST); RemovePlayerData(*i, NTF_NET_REMOVED_ON_REQUEST, false);
++i; ++i;
} }
@@ -853,13 +853,13 @@ ServerGame::RemoveSession(boost::shared_ptr<SessionData> session, int reason)
if (GetSessionManager().RemoveSession(session->GetId())) { if (GetSessionManager().RemoveSession(session->GetId())) {
boost::shared_ptr<PlayerData> tmpPlayerData = session->GetPlayerData(); boost::shared_ptr<PlayerData> tmpPlayerData = session->GetPlayerData();
if (tmpPlayerData && !tmpPlayerData->GetName().empty()) { if (tmpPlayerData && !tmpPlayerData->GetName().empty()) {
RemovePlayerData(tmpPlayerData, reason); RemovePlayerData(tmpPlayerData, reason, session->GetState() == SessionData::Spectating || session->GetState() == SessionData::SpectatorWaiting);
} }
} }
} }
void void
ServerGame::RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason) ServerGame::RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason, bool spectateOnly)
{ {
if (player->IsGameAdmin()) { if (player->IsGameAdmin()) {
// Find new admin for the game // Find new admin for the game
@@ -886,25 +886,37 @@ ServerGame::RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason)
// Send "Player Left" to clients. // Send "Player Left" to clients.
boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacket); boost::shared_ptr<NetPacket> thisPlayerLeft(new NetPacket);
thisPlayerLeft->GetMsg()->set_messagetype(PokerTHMessage::Type_GamePlayerLeftMessage); GamePlayerLeftMessage::GamePlayerLeftReason netReason = GamePlayerLeftMessage::leftError;
GamePlayerLeftMessage *netPlayerLeft = thisPlayerLeft->GetMsg()->mutable_gameplayerleftmessage();
netPlayerLeft->set_gameid(GetId());
netPlayerLeft->set_playerid(player->GetUniqueId());
switch (reason) { switch (reason) {
case NTF_NET_REMOVED_ON_REQUEST : case NTF_NET_REMOVED_ON_REQUEST :
netPlayerLeft->set_gameplayerleftreason(GamePlayerLeftMessage::leftOnRequest); netReason = GamePlayerLeftMessage::leftOnRequest;
break; break;
case NTF_NET_REMOVED_KICKED : case NTF_NET_REMOVED_KICKED :
netPlayerLeft->set_gameplayerleftreason(GamePlayerLeftMessage::leftKicked); netReason = GamePlayerLeftMessage::leftKicked;
break;
default :
netPlayerLeft->set_gameplayerleftreason(GamePlayerLeftMessage::leftError);
break; break;
} }
if (spectateOnly) {
thisPlayerLeft->GetMsg()->set_messagetype(PokerTHMessage::Type_GameSpectatorLeftMessage);
GameSpectatorLeftMessage *netPlayerLeft = thisPlayerLeft->GetMsg()->mutable_gamespectatorleftmessage();
netPlayerLeft->set_gameid(GetId());
netPlayerLeft->set_playerid(player->GetUniqueId());
netPlayerLeft->set_gamespectatorleftreason(netReason);
} else {
thisPlayerLeft->GetMsg()->set_messagetype(PokerTHMessage::Type_GamePlayerLeftMessage);
GamePlayerLeftMessage *netPlayerLeft = thisPlayerLeft->GetMsg()->mutable_gameplayerleftmessage();
netPlayerLeft->set_gameid(GetId());
netPlayerLeft->set_playerid(player->GetUniqueId());
netPlayerLeft->set_gameplayerleftreason(netReason);
}
GetSessionManager().SendToAllSessions(GetLobbyThread().GetSender(), thisPlayerLeft, SessionData::Game); GetSessionManager().SendToAllSessions(GetLobbyThread().GetSender(), thisPlayerLeft, SessionData::Game);
GetState().NotifySessionRemoved(shared_from_this()); GetState().NotifySessionRemoved(shared_from_this());
GetLobbyThread().NotifyPlayerLeftGame(GetId(), player->GetUniqueId()); if (spectateOnly) {
GetLobbyThread().NotifySpectatorLeftGame(GetId(), player->GetUniqueId());
} else {
GetLobbyThread().NotifyPlayerLeftGame(GetId(), player->GetUniqueId());
}
} }
void void
+1 -1
View File
@@ -874,7 +874,7 @@ AbstractServerGameStateRunning::HandleNewSpectator(boost::shared_ptr<ServerGame>
{ {
if (session && session->GetPlayerData()) { if (session && session->GetPlayerData()) {
AcceptNewSession(server, session, true); AcceptNewSession(server, session, true);
session->SetState(SessionData::Waiting); session->SetState(SessionData::SpectatorWaiting);
server->AddNewSpectator(session->GetPlayerData()->GetUniqueId()); server->AddNewSpectator(session->GetPlayerData()->GetUniqueId());
} }
} }
+1 -1
View File
@@ -159,7 +159,7 @@ protected:
void ResetComputerPlayerList(); void ResetComputerPlayerList();
void RemoveSession(boost::shared_ptr<SessionData> session, int reason); void RemoveSession(boost::shared_ptr<SessionData> session, int reason);
void RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason); void RemovePlayerData(boost::shared_ptr<PlayerData> player, int reason, bool spectateOnly);
void SessionError(boost::shared_ptr<SessionData> session, int errorCode); void SessionError(boost::shared_ptr<SessionData> session, int errorCode);
void MoveSessionToLobby(boost::shared_ptr<SessionData> session, int reason); void MoveSessionToLobby(boost::shared_ptr<SessionData> session, int reason);
+1 -1
View File
@@ -58,7 +58,7 @@ class ServerGame;
class SessionData : public boost::enable_shared_from_this<SessionData> class SessionData : public boost::enable_shared_from_this<SessionData>
{ {
public: public:
enum State { Init = 1, ReceivingAvatar = 2, Established = 4, Game = 8, Spectating = 16, Waiting = 32, Closed = 64 }; enum State { Init = 1, ReceivingAvatar = 2, Established = 4, Game = 8, Spectating = 16, GameWaiting = 32, SpectatorWaiting = 64, Closed = 128 };
SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService); SessionData(boost::shared_ptr<boost::asio::ip::tcp::socket> sock, SessionId id, SessionDataCallback &cb, boost::asio::io_service &ioService);
~SessionData(); ~SessionData();