From 35a21106987537d82125c530b54e695b449d42e2 Mon Sep 17 00:00:00 2001 From: dongresource Date: Fri, 2 Oct 2020 19:31:47 +0200 Subject: [PATCH] Save connected players to DB when gracefully terminating the server. --- src/CNShardServer.cpp | 10 ++++++++++ src/CNShardServer.hpp | 1 + src/TableData.cpp | 1 - 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/CNShardServer.cpp b/src/CNShardServer.cpp index 589a31e..037349b 100644 --- a/src/CNShardServer.cpp +++ b/src/CNShardServer.cpp @@ -47,9 +47,13 @@ void CNShardServer::keepAliveTimer(CNServer* serv, time_t currTime) { } void CNShardServer::periodicSaveTimer(CNServer* serv, time_t currTime) { + std::cout << "[INFO] Saving players to DB..." << std::endl; + for (auto& pair : PlayerManager::players) { Database::updatePlayer(pair.second.plr); } + + std::cout << "[INFO] Done." << std::endl; } void CNShardServer::newConnection(CNSocket* cns) { @@ -85,6 +89,12 @@ void CNShardServer::killConnection(CNSocket *cns) { _killConnection(cns); } +// flush the DB when terminating the server +void CNShardServer::kill() { + periodicSaveTimer(nullptr, 0); + CNServer::kill(); +} + void CNShardServer::onStep() { time_t currTime = getTime(); diff --git a/src/CNShardServer.hpp b/src/CNShardServer.hpp index 078b59c..c4a5e2d 100644 --- a/src/CNShardServer.hpp +++ b/src/CNShardServer.hpp @@ -25,5 +25,6 @@ public: void newConnection(CNSocket* cns); void killConnection(CNSocket* cns); + void kill(); void onStep(); }; diff --git a/src/TableData.cpp b/src/TableData.cpp index 94c6bbc..d84e71d 100644 --- a/src/TableData.cpp +++ b/src/TableData.cpp @@ -244,7 +244,6 @@ void TableData::loadPaths(int* nextId) { auto sliderPoint = _sliderPoint.value(); if (sliderPoint["stop"] && sliders % 2 == 0) { // check if this point in the circuit is a stop // spawn a slider - std::cout << "bus ID was " << *nextId << std::endl; BaseNPC* slider = new BaseNPC(sliderPoint["iX"], sliderPoint["iY"], sliderPoint["iZ"], 1, (*nextId)++, NPC_BUS); NPCManager::NPCs[slider->appearanceData.iNPC_ID] = slider; NPCManager::updateNPCPosition(slider->appearanceData.iNPC_ID, slider->appearanceData.iX, slider->appearanceData.iY, slider->appearanceData.iZ);