aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Time/UpdateTime.cpp
diff options
context:
space:
mode:
authorxinef1 <w.szyszko2@gmail.com>2017-03-02 02:19:25 +0100
committerShauren <shauren.trinity@gmail.com>2019-08-17 20:04:14 +0200
commit60663d1374beef3103f4787152654034fa4a8897 (patch)
tree38e07d44442ad903a9729536942e8e253a072274 /src/server/game/Time/UpdateTime.cpp
parent98180ecdc179386270e93b80c0db8344b659557f (diff)
Ensure that all actions are compared to fixed point in time (ie. world update start) (#18910)
- Actions will not be dependent on processing moment - Increased GameObjects cooldown resolution to milliseconds, fixes arming time of traps to be exactly one second and not something from range (1000, 1999) - Created GameTime namespace and UpdateTime class and moved there some code out of world (cherrypicked from 7567cafec84080d26ea513242a1f540a823b8f9d)
Diffstat (limited to 'src/server/game/Time/UpdateTime.cpp')
-rw-r--r--src/server/game/Time/UpdateTime.cpp135
1 files changed, 135 insertions, 0 deletions
diff --git a/src/server/game/Time/UpdateTime.cpp b/src/server/game/Time/UpdateTime.cpp
new file mode 100644
index 00000000000..1cc9fd1c507
--- /dev/null
+++ b/src/server/game/Time/UpdateTime.cpp
@@ -0,0 +1,135 @@
+/*
+ * Copyright (C) 2008-2017 TrinityCore <http://www.trinitycore.org/>
+
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "UpdateTime.h"
+#include "Timer.h"
+#include "Config.h"
+#include "Log.h"
+
+// create instance
+WorldUpdateTime sWorldUpdateTime;
+
+UpdateTime::UpdateTime()
+{
+ _averageUpdateTime = 0;
+ _totalUpdateTime = 0;
+ _updateTimeTableIndex = 0;
+ _maxUpdateTime = 0;
+ _maxUpdateTimeOfLastTable = 0;
+ _maxUpdateTimeOfCurrentTable = 0;
+
+ _updateTimeDataTable = { };
+}
+
+uint32 UpdateTime::GetAverageUpdateTime() const
+{
+ return _averageUpdateTime;
+}
+
+uint32 UpdateTime::GetTimeWeightedAverageUpdateTime() const
+{
+ uint32 sum = 0, weightsum = 0;
+ for (uint32 diff : _updateTimeDataTable)
+ {
+ sum += diff * diff;
+ weightsum += diff;
+ }
+ return sum / weightsum;
+}
+
+uint32 UpdateTime::GetMaxUpdateTime() const
+{
+ return _maxUpdateTime;
+}
+
+uint32 UpdateTime::GetMaxUpdateTimeOfCurrentTable() const
+{
+ return std::max(_maxUpdateTimeOfCurrentTable, _maxUpdateTimeOfLastTable);
+}
+
+uint32 UpdateTime::GetLastUpdateTime() const
+{
+ return _updateTimeDataTable[_updateTimeTableIndex != 0 ? _updateTimeTableIndex - 1 : _updateTimeDataTable.size() - 1];
+}
+
+void UpdateTime::UpdateWithDiff(uint32 diff)
+{
+ _totalUpdateTime = _totalUpdateTime - _updateTimeDataTable[_updateTimeTableIndex] + diff;
+ _updateTimeDataTable[_updateTimeTableIndex] = diff;
+
+ if (diff > _maxUpdateTime)
+ _maxUpdateTime = diff;
+
+ if (diff > _maxUpdateTimeOfCurrentTable)
+ _maxUpdateTimeOfCurrentTable = diff;
+
+ if (++_updateTimeTableIndex >= _updateTimeDataTable.size())
+ {
+ _updateTimeTableIndex = 0;
+ _maxUpdateTimeOfLastTable = _maxUpdateTimeOfCurrentTable;
+ _maxUpdateTimeOfCurrentTable = 0;
+ }
+
+ if (_updateTimeDataTable[_updateTimeDataTable.size() - 1])
+ _averageUpdateTime = _totalUpdateTime / _updateTimeDataTable.size();
+ else if (_updateTimeTableIndex)
+ _averageUpdateTime = _totalUpdateTime / _updateTimeTableIndex;
+}
+
+void UpdateTime::RecordUpdateTimeReset()
+{
+ _recordedTime = getMSTime();
+}
+
+void UpdateTime::_RecordUpdateTimeDuration(std::string const& text, uint32 minUpdateTime)
+{
+ uint32 thisTime = getMSTime();
+ uint32 diff = getMSTimeDiff(_recordedTime, thisTime);
+
+ if (diff > minUpdateTime)
+ TC_LOG_INFO("misc", "Recored Update Time of %s: %u.", text.c_str(), diff);
+
+ _recordedTime = thisTime;
+}
+
+void WorldUpdateTime::LoadFromConfig()
+{
+ _recordUpdateTimeInverval = sConfigMgr->GetIntDefault("RecordUpdateTimeDiffInterval", 60000);
+ _recordUpdateTimeMin = sConfigMgr->GetIntDefault("MinRecordUpdateTimeDiff", 100);
+}
+
+void WorldUpdateTime::SetRecordUpdateTimeInterval(uint32 t)
+{
+ _recordUpdateTimeInverval = t;
+}
+
+void WorldUpdateTime::RecordUpdateTime(uint32 gameTimeMs, uint32 diff, uint32 sessionCount)
+{
+ if (_recordUpdateTimeInverval > 0 && diff > _recordUpdateTimeMin)
+ {
+ if (getMSTimeDiff(_lastRecordTime, gameTimeMs) > _recordUpdateTimeInverval)
+ {
+ TC_LOG_DEBUG("misc", "Update time diff: %u. Players online: %u.", GetAverageUpdateTime(), sessionCount);
+ _lastRecordTime = gameTimeMs;
+ }
+ }
+}
+
+void WorldUpdateTime::RecordUpdateTimeDuration(std::string const& text)
+{
+ _RecordUpdateTimeDuration(text, _recordUpdateTimeMin);
+}