From 50bef9736c7238f98291697fa187c089734c8a13 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 24 Jun 2013 23:34:48 +1000 Subject: [PATCH] Deleting players on logout --- client/in_world.cpp | 31 ++++++++++++++++++++++++++++--- client/information_manager.hpp | 6 ++++++ server/server_application.cpp | 12 ++++++++++-- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/client/in_world.cpp b/client/in_world.cpp index dc61d22..9992d7c 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -39,6 +39,8 @@ InWorld::InWorld() { //debugging Packet p; p.meta.type = Packet::Type::PLAYER_NEW; + p.meta.clientIndex = infoMgr->GetClientIndex(); + snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); p.playerInfo.position = {0, 50}; @@ -163,6 +165,7 @@ int InWorld::HandlePacket(Packet p) { case Packet::Type::PING: //quick pong p.meta.type = Packet::Type::PONG; + p.meta.clientIndex = infoMgr->GetClientIndex(); netUtil->Send(&p.meta.address, &p, sizeof(Packet)); break; case Packet::Type::PONG: @@ -202,15 +205,24 @@ int InWorld::HandlePacket(Packet p) { } void InWorld::Disconnect() { - //disconnect Packet p; + + //delete the player + p.meta.type = Packet::Type::PLAYER_DELETE; + p.meta.clientIndex = infoMgr->GetClientIndex(); + p.playerInfo.index = infoMgr->GetPlayerIndex(); + netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet)); + + //disconnect p.meta.type = Packet::Type::DISCONNECT; p.meta.clientIndex = infoMgr->GetClientIndex(); - netUtil->Send(GAME_CHANNEL, reinterpret_cast(&p), sizeof(Packet)); + netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet)); + netUtil->Unbind(GAME_CHANNEL); //reset the client infoMgr->ResetClientIndex(); + infoMgr->ResetPlayerIndex(); } void InWorld::ExitGame() { @@ -228,6 +240,10 @@ void InWorld::HandleDisconnection(Packet& disconnect) { } void InWorld::AddPlayer(Packet& p) { + if (playerCharacters.find(p.playerInfo.index) != playerCharacters.end()) { + throw(runtime_error("Duplicate players detected")); + } + //sprite playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48); @@ -235,12 +251,21 @@ void InWorld::AddPlayer(Packet& p) { playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion); + //is it this player? + if (p.meta.clientIndex == infoMgr->GetClientIndex()) { + infoMgr->SetPlayerIndex(p.playerInfo.index); + } + //debugging cout << "New player, index " << p.playerInfo.index << endl; } void InWorld::RemovePlayer(Packet& p) { - // + if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) { + throw(runtime_error("Player to delete not found")); + } + + playerCharacters.erase(p.playerInfo.index); } void InWorld::UpdatePlayer(Packet& p) { diff --git a/client/information_manager.hpp b/client/information_manager.hpp index 4974ae0..e36bf8b 100644 --- a/client/information_manager.hpp +++ b/client/information_manager.hpp @@ -27,8 +27,14 @@ public: int SetClientIndex(int i) { return clientIndex = i; } int GetClientIndex() { return clientIndex; } void ResetClientIndex() { clientIndex = -1; } + + //one player at a time + int SetPlayerIndex(int i) { return playerIndex = i; } + int GetPlayerIndex() { return playerIndex; } + void ResetPlayerIndex() { playerIndex = -1; } private: int clientIndex = -1; + int playerIndex = -1; }; #endif diff --git a/server/server_application.cpp b/server/server_application.cpp index a063d58..3663418 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -292,9 +292,17 @@ void ServerApplication::AddPlayer(Packet& p) { } void ServerApplication::RemovePlayer(Packet& p) { - //TODO remove a player + if (players.find(p.playerInfo.index) == players.end()) { + throw(runtime_error("Player to delete not found")); + } + + players.erase(p.playerInfo.index); } void ServerApplication::UpdatePlayer(Packet& p) { - //TODO update a player + if (players.find(p.playerInfo.index) == players.end()) { + throw(runtime_error("Player to update not found")); + } + players[p.playerInfo.index].position = p.playerInfo.position; + players[p.playerInfo.index].motion = p.playerInfo.motion; }