temp fix and small refactor

This commit is contained in:
CPunch 2020-09-17 21:41:09 -05:00
parent 22e3e9e4de
commit 31ef03610d
3 changed files with 9 additions and 11 deletions

View File

@ -34,9 +34,7 @@ void CNShardServer::handlePacket(CNSocket* sock, CNPacketData* data) {
} }
void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) { void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) {
auto cachedPlayers = PlayerManager::players; for (auto& pair : PlayerManager::players) {
for (auto& pair : cachedPlayers) {
if (pair.second.lastHeartbeat != 0 && currTime - pair.second.lastHeartbeat > settings::TIMEOUT) { if (pair.second.lastHeartbeat != 0 && currTime - pair.second.lastHeartbeat > settings::TIMEOUT) {
// if the client hasn't responded in 60 seconds, its a dead connection so throw it out // if the client hasn't responded in 60 seconds, its a dead connection so throw it out
pair.first->kill(); pair.first->kill();
@ -49,9 +47,8 @@ void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) {
} }
void CNShardServer::periodicSaveTimer(CNServer* serv, time_t currTime) { void CNShardServer::periodicSaveTimer(CNServer* serv, time_t currTime) {
auto cachedPlayers = PlayerManager::players; std::cout << "players size: " << PlayerManager::players.size() << std::endl;
for (auto& pair : PlayerManager::players) {
for (auto& pair : cachedPlayers) {
Database::updatePlayer(pair.second.plr); Database::updatePlayer(pair.second.plr);
} }
} }
@ -65,13 +62,13 @@ void CNShardServer::killConnection(CNSocket* cns) {
if (PlayerManager::players.find(cns) == PlayerManager::players.end()) if (PlayerManager::players.find(cns) == PlayerManager::players.end())
return; return;
int64_t key = PlayerManager::getPlayer(cns)->SerialKey;
// save player to DB // save player to DB
Database::updatePlayer(PlayerManager::players[cns].plr); Database::updatePlayer(PlayerManager::players[cns].plr);
// remove from CNSharedData
int64_t key = PlayerManager::getPlayer(cns)->SerialKey;
PlayerManager::removePlayer(cns); PlayerManager::removePlayer(cns);
// remove from CNSharedData
CNSharedData::erasePlayer(key); CNSharedData::erasePlayer(key);
} }

View File

@ -43,8 +43,6 @@ void ChunkManager::addPlayer(int posX, int posY, CNSocket* sock) {
Chunk* chunk = chunks[pos]; Chunk* chunk = chunks[pos];
chunk->players.insert(sock); chunk->players.insert(sock);
// TODO: update view for all players in surrounding chunks
} }
std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) { std::pair<int, int> ChunkManager::grabChunk(int posX, int posY) {

View File

@ -96,6 +96,9 @@ void PlayerManager::removePlayerFromChunks(std::vector<Chunk*> chunks, CNSocket*
exitPlayer.iID = players[otherSock].plr->iID; exitPlayer.iID = players[otherSock].plr->iID;
sock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT)); sock->sendPacket((void*)&exitPlayer, P_FE2CL_PC_EXIT, sizeof(sP_FE2CL_PC_EXIT));
} }
// temp-fix for weird edgecase
chunk->players.erase(sock);
} }
} }