Deleting players on logout

This commit is contained in:
Kayne Ruse
2013-06-24 23:34:48 +10:00
parent 6d1ba24404
commit 50bef9736c
3 changed files with 44 additions and 5 deletions
+28 -3
View File
@@ -39,6 +39,8 @@ InWorld::InWorld() {
//debugging //debugging
Packet p; Packet p;
p.meta.type = Packet::Type::PLAYER_NEW; 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.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
p.playerInfo.position = {0, 50}; p.playerInfo.position = {0, 50};
@@ -163,6 +165,7 @@ int InWorld::HandlePacket(Packet p) {
case Packet::Type::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = Packet::Type::PONG; p.meta.type = Packet::Type::PONG;
p.meta.clientIndex = infoMgr->GetClientIndex();
netUtil->Send(&p.meta.address, &p, sizeof(Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet));
break; break;
case Packet::Type::PONG: case Packet::Type::PONG:
@@ -202,15 +205,24 @@ int InWorld::HandlePacket(Packet p) {
} }
void InWorld::Disconnect() { void InWorld::Disconnect() {
//disconnect
Packet p; 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.type = Packet::Type::DISCONNECT;
p.meta.clientIndex = infoMgr->GetClientIndex(); p.meta.clientIndex = infoMgr->GetClientIndex();
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet)); netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
netUtil->Unbind(GAME_CHANNEL); netUtil->Unbind(GAME_CHANNEL);
//reset the client //reset the client
infoMgr->ResetClientIndex(); infoMgr->ResetClientIndex();
infoMgr->ResetPlayerIndex();
} }
void InWorld::ExitGame() { void InWorld::ExitGame() {
@@ -228,6 +240,10 @@ void InWorld::HandleDisconnection(Packet& disconnect) {
} }
void InWorld::AddPlayer(Packet& p) { void InWorld::AddPlayer(Packet& p) {
if (playerCharacters.find(p.playerInfo.index) != playerCharacters.end()) {
throw(runtime_error("Duplicate players detected"));
}
//sprite //sprite
playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48); 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].SetPosition(p.playerInfo.position);
playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
//is it this player?
if (p.meta.clientIndex == infoMgr->GetClientIndex()) {
infoMgr->SetPlayerIndex(p.playerInfo.index);
}
//debugging //debugging
cout << "New player, index " << p.playerInfo.index << endl; cout << "New player, index " << p.playerInfo.index << endl;
} }
void InWorld::RemovePlayer(Packet& p) { 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) { void InWorld::UpdatePlayer(Packet& p) {
+6
View File
@@ -27,8 +27,14 @@ public:
int SetClientIndex(int i) { return clientIndex = i; } int SetClientIndex(int i) { return clientIndex = i; }
int GetClientIndex() { return clientIndex; } int GetClientIndex() { return clientIndex; }
void ResetClientIndex() { clientIndex = -1; } 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: private:
int clientIndex = -1; int clientIndex = -1;
int playerIndex = -1;
}; };
#endif #endif
+10 -2
View File
@@ -292,9 +292,17 @@ void ServerApplication::AddPlayer(Packet& p) {
} }
void ServerApplication::RemovePlayer(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) { 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;
} }