mirror of
https://github.com/DarkflameUniverse/DarkflameServer.git
synced 2025-01-09 14:27:10 +00:00
fix: update player container on shutdown (#1704)
* update activity log on shutdown * fix online notification * update container on shutdown
This commit is contained in:
parent
ff4546c027
commit
3ecbd1013b
@ -29,6 +29,7 @@ void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) {
|
|||||||
auto& player = Game::playerContainer.GetPlayerDataMutable(playerID);
|
auto& player = Game::playerContainer.GetPlayerDataMutable(playerID);
|
||||||
if (!player) return;
|
if (!player) return;
|
||||||
|
|
||||||
|
if (player.friends.empty()) {
|
||||||
auto friendsList = Database::Get()->GetFriendsList(playerID);
|
auto friendsList = Database::Get()->GetFriendsList(playerID);
|
||||||
for (const auto& friendData : friendsList) {
|
for (const auto& friendData : friendsList) {
|
||||||
FriendData fd;
|
FriendData fd;
|
||||||
@ -57,6 +58,7 @@ void ChatPacketHandler::HandleFriendlistRequest(Packet* packet) {
|
|||||||
|
|
||||||
player.friends.push_back(fd);
|
player.friends.push_back(fd);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Now, we need to send the friendlist to the server they came from:
|
//Now, we need to send the friendlist to the server they came from:
|
||||||
CBITSTREAM;
|
CBITSTREAM;
|
||||||
|
@ -122,6 +122,8 @@ int main(int argc, char** argv) {
|
|||||||
uint32_t framesSinceMasterDisconnect = 0;
|
uint32_t framesSinceMasterDisconnect = 0;
|
||||||
uint32_t framesSinceLastSQLPing = 0;
|
uint32_t framesSinceLastSQLPing = 0;
|
||||||
|
|
||||||
|
auto lastTime = std::chrono::high_resolution_clock::now();
|
||||||
|
|
||||||
Game::logger->Flush(); // once immediately before main loop
|
Game::logger->Flush(); // once immediately before main loop
|
||||||
while (!Game::ShouldShutdown()) {
|
while (!Game::ShouldShutdown()) {
|
||||||
//Check if we're still connected to master:
|
//Check if we're still connected to master:
|
||||||
@ -132,7 +134,11 @@ int main(int argc, char** argv) {
|
|||||||
break; //Exit our loop, shut down.
|
break; //Exit our loop, shut down.
|
||||||
} else framesSinceMasterDisconnect = 0;
|
} else framesSinceMasterDisconnect = 0;
|
||||||
|
|
||||||
//In world we'd update our other systems here.
|
const auto currentTime = std::chrono::high_resolution_clock::now();
|
||||||
|
const float deltaTime = std::chrono::duration<float>(currentTime - lastTime).count();
|
||||||
|
lastTime = currentTime;
|
||||||
|
|
||||||
|
Game::playerContainer.Update(deltaTime);
|
||||||
|
|
||||||
//Check for packets here:
|
//Check for packets here:
|
||||||
Game::server->ReceiveFromMaster(); //ReceiveFromMaster also handles the master packets if needed.
|
Game::server->ReceiveFromMaster(); //ReceiveFromMaster also handles the master packets if needed.
|
||||||
@ -168,7 +174,7 @@ int main(int argc, char** argv) {
|
|||||||
t += std::chrono::milliseconds(chatFrameDelta); //Chat can run at a lower "fps"
|
t += std::chrono::milliseconds(chatFrameDelta); //Chat can run at a lower "fps"
|
||||||
std::this_thread::sleep_until(t);
|
std::this_thread::sleep_until(t);
|
||||||
}
|
}
|
||||||
|
Game::playerContainer.Shutdown();
|
||||||
//Delete our objects here:
|
//Delete our objects here:
|
||||||
Database::Destroy("ChatServer");
|
Database::Destroy("ChatServer");
|
||||||
delete Game::server;
|
delete Game::server;
|
||||||
@ -287,7 +293,7 @@ void HandlePacket(Packet* packet) {
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MessageType::Chat::UNEXPECTED_DISCONNECT:
|
case MessageType::Chat::UNEXPECTED_DISCONNECT:
|
||||||
Game::playerContainer.RemovePlayer(packet);
|
Game::playerContainer.ScheduleRemovePlayer(packet);
|
||||||
break;
|
break;
|
||||||
case MessageType::Chat::WHO:
|
case MessageType::Chat::WHO:
|
||||||
ChatPacketHandler::HandleWho(packet);
|
ChatPacketHandler::HandleWho(packet);
|
||||||
|
@ -57,13 +57,32 @@ void PlayerContainer::InsertPlayer(Packet* packet) {
|
|||||||
LOG("Added user: %s (%llu), zone: %i", data.playerName.c_str(), data.playerID, data.zoneID.GetMapID());
|
LOG("Added user: %s (%llu), zone: %i", data.playerName.c_str(), data.playerID, data.zoneID.GetMapID());
|
||||||
|
|
||||||
Database::Get()->UpdateActivityLog(data.playerID, eActivityType::PlayerLoggedIn, data.zoneID.GetMapID());
|
Database::Get()->UpdateActivityLog(data.playerID, eActivityType::PlayerLoggedIn, data.zoneID.GetMapID());
|
||||||
|
m_PlayersToRemove.erase(playerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerContainer::RemovePlayer(Packet* packet) {
|
void PlayerContainer::ScheduleRemovePlayer(Packet* packet) {
|
||||||
CINSTREAM_SKIP_HEADER;
|
CINSTREAM_SKIP_HEADER;
|
||||||
LWOOBJID playerID;
|
LWOOBJID playerID{ LWOOBJID_EMPTY };
|
||||||
inStream.Read(playerID);
|
inStream.Read(playerID);
|
||||||
|
constexpr float updatePlayerOnLogoutTime = 20.0f;
|
||||||
|
if (playerID != LWOOBJID_EMPTY) m_PlayersToRemove.insert_or_assign(playerID, updatePlayerOnLogoutTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerContainer::Update(const float deltaTime) {
|
||||||
|
for (auto it = m_PlayersToRemove.begin(); it != m_PlayersToRemove.end();) {
|
||||||
|
auto& [id, time] = *it;
|
||||||
|
time -= deltaTime;
|
||||||
|
|
||||||
|
if (time <= 0.0f) {
|
||||||
|
RemovePlayer(id);
|
||||||
|
it = m_PlayersToRemove.erase(it);
|
||||||
|
} else {
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PlayerContainer::RemovePlayer(const LWOOBJID playerID) {
|
||||||
//Before they get kicked, we need to also send a message to their friends saying that they disconnected.
|
//Before they get kicked, we need to also send a message to their friends saying that they disconnected.
|
||||||
const auto& player = GetPlayerData(playerID);
|
const auto& player = GetPlayerData(playerID);
|
||||||
|
|
||||||
@ -417,3 +436,13 @@ const PlayerData& PlayerContainer::GetPlayerData(const LWOOBJID& playerID) {
|
|||||||
const PlayerData& PlayerContainer::GetPlayerData(const std::string& playerName) {
|
const PlayerData& PlayerContainer::GetPlayerData(const std::string& playerName) {
|
||||||
return GetPlayerDataMutable(playerName);
|
return GetPlayerDataMutable(playerName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PlayerContainer::Shutdown() {
|
||||||
|
m_Players.erase(LWOOBJID_EMPTY);
|
||||||
|
while (!m_Players.empty()) {
|
||||||
|
const auto& [id, playerData] = *m_Players.begin();
|
||||||
|
Database::Get()->UpdateActivityLog(id, eActivityType::PlayerLoggedOut, playerData.zoneID.GetMapID());
|
||||||
|
m_Players.erase(m_Players.begin());
|
||||||
|
}
|
||||||
|
for (auto* team : mTeams) if (team) delete team;
|
||||||
|
}
|
||||||
|
@ -62,10 +62,12 @@ class PlayerContainer {
|
|||||||
public:
|
public:
|
||||||
void Initialize();
|
void Initialize();
|
||||||
void InsertPlayer(Packet* packet);
|
void InsertPlayer(Packet* packet);
|
||||||
void RemovePlayer(Packet* packet);
|
void ScheduleRemovePlayer(Packet* packet);
|
||||||
|
void RemovePlayer(const LWOOBJID playerID);
|
||||||
void MuteUpdate(Packet* packet);
|
void MuteUpdate(Packet* packet);
|
||||||
void CreateTeamServer(Packet* packet);
|
void CreateTeamServer(Packet* packet);
|
||||||
void BroadcastMuteUpdate(LWOOBJID player, time_t time);
|
void BroadcastMuteUpdate(LWOOBJID player, time_t time);
|
||||||
|
void Shutdown();
|
||||||
|
|
||||||
const PlayerData& GetPlayerData(const LWOOBJID& playerID);
|
const PlayerData& GetPlayerData(const LWOOBJID& playerID);
|
||||||
const PlayerData& GetPlayerData(const std::string& playerName);
|
const PlayerData& GetPlayerData(const std::string& playerName);
|
||||||
@ -89,11 +91,15 @@ public:
|
|||||||
uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; }
|
uint32_t GetMaxNumberOfBestFriends() { return m_MaxNumberOfBestFriends; }
|
||||||
uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; }
|
uint32_t GetMaxNumberOfFriends() { return m_MaxNumberOfFriends; }
|
||||||
|
|
||||||
|
void Update(const float deltaTime);
|
||||||
|
bool PlayerBeingRemoved(const LWOOBJID playerID) { return m_PlayersToRemove.contains(playerID); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
LWOOBJID m_TeamIDCounter = 0;
|
LWOOBJID m_TeamIDCounter = 0;
|
||||||
std::map<LWOOBJID, PlayerData> m_Players;
|
std::map<LWOOBJID, PlayerData> m_Players;
|
||||||
std::vector<TeamData*> mTeams;
|
std::vector<TeamData*> mTeams;
|
||||||
std::unordered_map<LWOOBJID, std::u16string> m_Names;
|
std::unordered_map<LWOOBJID, std::u16string> m_Names;
|
||||||
|
std::map<LWOOBJID, float> m_PlayersToRemove;
|
||||||
uint32_t m_MaxNumberOfBestFriends = 5;
|
uint32_t m_MaxNumberOfBestFriends = 5;
|
||||||
uint32_t m_MaxNumberOfFriends = 50;
|
uint32_t m_MaxNumberOfFriends = 50;
|
||||||
uint32_t m_PlayerCount = 0;
|
uint32_t m_PlayerCount = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user