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
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<void*>(&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) {
+6
View File
@@ -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
+10 -2
View File
@@ -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;
}