feat(Core/Common): add new helpers for time utility (#10207)

This commit is contained in:
Kargatum
2022-01-19 12:01:59 +07:00
committed by GitHub
parent b5ab409614
commit 259b9133f6
60 changed files with 732 additions and 341 deletions

View File

@@ -587,8 +587,6 @@ public:
[[nodiscard]] virtual uint32 GetCleaningFlags() const = 0;
virtual void SetCleaningFlags(uint32 flags) = 0;
virtual void ResetEventSeasonalQuests(uint16 event_id) = 0;
virtual time_t GetNextTimeWithDayAndHour(int8 dayOfWeek, int8 hour) = 0;
virtual time_t GetNextTimeWithMonthAndHour(int8 month, int8 hour) = 0;
[[nodiscard]] virtual std::string const& GetRealmName() const = 0;
virtual void SetRealmName(std::string name) = 0;
virtual void RemoveOldCorpses() = 0;

View File

@@ -75,6 +75,7 @@
#include "SkillExtraItems.h"
#include "SmartAI.h"
#include "SpellMgr.h"
#include "TaskScheduler.h"
#include "TicketMgr.h"
#include "Transport.h"
#include "TransportMgr.h"
@@ -89,7 +90,6 @@
#include "WhoListCacheMgr.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "TaskScheduler.h"
#include <boost/asio/ip/address.hpp>
#include <cmath>
@@ -3044,55 +3044,10 @@ void World::_UpdateRealmCharCount(PreparedQueryResult resultCharCount)
}
}
// int8 dayOfWeek: 0 (sunday) to 6 (saturday)
time_t World::GetNextTimeWithDayAndHour(int8 dayOfWeek, int8 hour)
{
if (hour < 0 || hour > 23)
hour = 0;
time_t curr = time(nullptr);
tm localTm;
localtime_r(&curr, &localTm);
localTm.tm_hour = hour;
localTm.tm_min = 0;
localTm.tm_sec = 0;
uint32 add;
if (dayOfWeek < 0 || dayOfWeek > 6)
dayOfWeek = (localTm.tm_wday + 1) % 7;
if (localTm.tm_wday >= dayOfWeek)
add = (7 - (localTm.tm_wday - dayOfWeek)) * DAY;
else
add = (dayOfWeek - localTm.tm_wday) * DAY;
return mktime(&localTm) + add;
}
// int8 month: 0 (january) to 11 (december)
time_t World::GetNextTimeWithMonthAndHour(int8 month, int8 hour)
{
if (hour < 0 || hour > 23)
hour = 0;
time_t curr = time(nullptr);
tm localTm;
localtime_r(&curr, &localTm);
localTm.tm_mday = 1;
localTm.tm_hour = hour;
localTm.tm_min = 0;
localTm.tm_sec = 0;
if (month < 0 || month > 11)
{
month = (localTm.tm_mon + 1) % 12;
if (month == 0)
localTm.tm_year += 1;
}
else if (localTm.tm_mon >= month)
localTm.tm_year += 1;
localTm.tm_mon = month;
return mktime(&localTm);
}
void World::InitWeeklyQuestResetTime()
{
time_t wstime = time_t(sWorld->getWorldState(WS_WEEKLY_QUEST_RESET_TIME));
m_NextWeeklyQuestReset = wstime ? wstime : GetNextTimeWithDayAndHour(4, 6);
m_NextWeeklyQuestReset = wstime ? wstime : Acore::Time::GetNextTimeWithDayAndHour(4, 6);
if (!wstime)
sWorld->setWorldState(WS_WEEKLY_QUEST_RESET_TIME, uint64(m_NextWeeklyQuestReset));
}
@@ -3100,7 +3055,7 @@ void World::InitWeeklyQuestResetTime()
void World::InitDailyQuestResetTime()
{
time_t wstime = time_t(sWorld->getWorldState(WS_DAILY_QUEST_RESET_TIME));
m_NextDailyQuestReset = wstime ? wstime : GetNextTimeWithDayAndHour(-1, 6);
m_NextDailyQuestReset = wstime ? wstime : Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
if (!wstime)
sWorld->setWorldState(WS_DAILY_QUEST_RESET_TIME, uint64(m_NextDailyQuestReset));
}
@@ -3108,7 +3063,7 @@ void World::InitDailyQuestResetTime()
void World::InitMonthlyQuestResetTime()
{
time_t wstime = time_t(sWorld->getWorldState(WS_MONTHLY_QUEST_RESET_TIME));
m_NextMonthlyQuestReset = wstime ? wstime : GetNextTimeWithMonthAndHour(-1, 6);
m_NextMonthlyQuestReset = wstime ? wstime : Acore::Time::GetNextTimeWithMonthAndHour(-1, 6);
if (!wstime)
sWorld->setWorldState(WS_MONTHLY_QUEST_RESET_TIME, uint64(m_NextMonthlyQuestReset));
}
@@ -3116,7 +3071,7 @@ void World::InitMonthlyQuestResetTime()
void World::InitRandomBGResetTime()
{
time_t wstime = time_t(sWorld->getWorldState(WS_BG_DAILY_RESET_TIME));
m_NextRandomBGReset = wstime ? wstime : GetNextTimeWithDayAndHour(-1, 6);
m_NextRandomBGReset = wstime ? wstime : Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
if (!wstime)
sWorld->setWorldState(WS_BG_DAILY_RESET_TIME, uint64(m_NextRandomBGReset));
}
@@ -3125,7 +3080,7 @@ void World::InitCalendarOldEventsDeletionTime()
{
time_t now = time(nullptr);
time_t currentDeletionTime = getWorldState(WS_DAILY_CALENDAR_DELETION_OLD_EVENTS_TIME);
time_t nextDeletionTime = currentDeletionTime ? currentDeletionTime : GetNextTimeWithDayAndHour(-1, getIntConfig(CONFIG_CALENDAR_DELETE_OLD_EVENTS_HOUR));
time_t nextDeletionTime = currentDeletionTime ? currentDeletionTime : Acore::Time::GetNextTimeWithDayAndHour(-1, getIntConfig(CONFIG_CALENDAR_DELETE_OLD_EVENTS_HOUR));
// If the reset time saved in the worldstate is before now it means the server was offline when the reset was supposed to occur.
// In this case we set the reset time in the past and next world update will do the reset and schedule next one in the future.
@@ -3141,7 +3096,7 @@ void World::InitCalendarOldEventsDeletionTime()
void World::InitGuildResetTime()
{
time_t wstime = time_t(getWorldState(WS_GUILD_DAILY_RESET_TIME));
m_NextGuildReset = wstime ? wstime : GetNextTimeWithDayAndHour(-1, 6);
m_NextGuildReset = wstime ? wstime : Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
if (!wstime)
sWorld->setWorldState(WS_GUILD_DAILY_RESET_TIME, uint64(m_NextGuildReset));
}
@@ -3155,7 +3110,7 @@ void World::ResetDailyQuests()
if (itr->second->GetPlayer())
itr->second->GetPlayer()->ResetDailyQuestStatus();
m_NextDailyQuestReset = GetNextTimeWithDayAndHour(-1, 6);
m_NextDailyQuestReset = Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
sWorld->setWorldState(WS_DAILY_QUEST_RESET_TIME, uint64(m_NextDailyQuestReset));
// change available dailies
@@ -3190,7 +3145,7 @@ void World::ResetWeeklyQuests()
if (itr->second->GetPlayer())
itr->second->GetPlayer()->ResetWeeklyQuestStatus();
m_NextWeeklyQuestReset = GetNextTimeWithDayAndHour(4, 6);
m_NextWeeklyQuestReset = Acore::Time::GetNextTimeWithDayAndHour(4, 6);
sWorld->setWorldState(WS_WEEKLY_QUEST_RESET_TIME, uint64(m_NextWeeklyQuestReset));
// change available weeklies
@@ -3208,7 +3163,7 @@ void World::ResetMonthlyQuests()
if (itr->second->GetPlayer())
itr->second->GetPlayer()->ResetMonthlyQuestStatus();
m_NextMonthlyQuestReset = GetNextTimeWithMonthAndHour(-1, 6);
m_NextMonthlyQuestReset = Acore::Time::GetNextTimeWithMonthAndHour(-1, 6);
sWorld->setWorldState(WS_MONTHLY_QUEST_RESET_TIME, uint64(m_NextMonthlyQuestReset));
}
@@ -3234,7 +3189,7 @@ void World::ResetRandomBG()
if (itr->second->GetPlayer())
itr->second->GetPlayer()->SetRandomWinner(false);
m_NextRandomBGReset = GetNextTimeWithDayAndHour(-1, 6);
m_NextRandomBGReset = Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
sWorld->setWorldState(WS_BG_DAILY_RESET_TIME, uint64(m_NextRandomBGReset));
}
@@ -3251,7 +3206,7 @@ void World::ResetGuildCap()
{
LOG_INFO("server.worldserver", "Guild Daily Cap reset.");
m_NextGuildReset = GetNextTimeWithDayAndHour(-1, 6);
m_NextGuildReset = Acore::Time::GetNextTimeWithDayAndHour(-1, 6);
sWorld->setWorldState(WS_GUILD_DAILY_RESET_TIME, uint64(m_NextGuildReset));
sGuildMgr->ResetTimes();

View File

@@ -365,9 +365,6 @@ public:
void SetCleaningFlags(uint32 flags) override { m_CleaningFlags = flags; }
void ResetEventSeasonalQuests(uint16 event_id) override;
time_t GetNextTimeWithDayAndHour(int8 dayOfWeek, int8 hour) override; // pussywizard
time_t GetNextTimeWithMonthAndHour(int8 month, int8 hour) override; // pussywizard
[[nodiscard]] std::string const& GetRealmName() const override { return _realmName; } // pussywizard
void SetRealmName(std::string name) override { _realmName = name; } // pussywizard