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

@@ -417,9 +417,7 @@ bool AchievementCriteriaData::Meets(uint32 criteria_id, Player const* source, Un
return source->GetMapId() == map_id.mapId;
case ACHIEVEMENT_CRITERIA_DATA_TYPE_NTH_BIRTHDAY:
{
time_t birthday_start = time_t(sWorld->getIntConfig(CONFIG_BIRTHDAY_TIME));
tm birthday_tm;
localtime_r(&birthday_start, &birthday_tm);
tm birthday_tm = Acore::Time::TimeBreakdown(sWorld->getIntConfig(CONFIG_BIRTHDAY_TIME));
// exactly N birthday
birthday_tm.tm_year += birthday_login.nth_birthday;

View File

@@ -15,6 +15,7 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Player.h"
#include "AccountMgr.h"
#include "AchievementMgr.h"
#include "ArenaSpectator.h"
@@ -31,11 +32,11 @@
#include "CharacterCache.h"
#include "CharacterDatabaseCleaner.h"
#include "Chat.h"
#include "Config.h"
#include "CombatLogPackets.h"
#include "Common.h"
#include "ConditionMgr.h"
#include "Config.h"
#include "CreatureAI.h"
#include "CombatLogPackets.h"
#include "DatabaseEnv.h"
#include "DisableMgr.h"
#include "Formulas.h"
@@ -50,8 +51,8 @@
#include "GuildMgr.h"
#include "InstanceSaveMgr.h"
#include "InstanceScript.h"
#include "Language.h"
#include "LFGMgr.h"
#include "Language.h"
#include "Log.h"
#include "LootItemStorage.h"
#include "MapMgr.h"
@@ -62,11 +63,10 @@
#include "OutdoorPvPMgr.h"
#include "Pet.h"
#include "PetitionMgr.h"
#include "Player.h"
#include "QuestDef.h"
#include "QueryHolder.h"
#include "ReputationMgr.h"
#include "QuestDef.h"
#include "Realm.h"
#include "ReputationMgr.h"
#include "ScriptMgr.h"
#include "SocialMgr.h"
#include "Spell.h"
@@ -15172,11 +15172,7 @@ void Player::_LoadBrewOfTheMonth(PreparedQueryResult result)
lastEventId = fields[0].GetUInt32();
}
time_t curtime = time(nullptr);
tm localTime;
localtime_r(&curtime, &localTime);
uint16 month = uint16(localTime.tm_mon);
uint16 month = static_cast<uint16>(Acore::Time::GetMonth());
uint16 eventId = month;
if (eventId < 9)
eventId += 3;

View File

@@ -21,8 +21,8 @@
#include "ArenaTeam.h"
#include "Battleground.h"
#include "CharacterCache.h"
#include "DatabaseEnvFwd.h"
#include "DBCStores.h"
#include "DatabaseEnvFwd.h"
#include "GroupReference.h"
#include "InstanceSaveMgr.h"
#include "Item.h"

View File

@@ -15,9 +15,9 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Tokenize.h"
#include "StringConvert.h"
#include "Player.h"
#include "StringConvert.h"
#include "Tokenize.h"
/*********************************************************/
/*** PLAYER SETTINGS SYSTEM ***/

View File

@@ -40,9 +40,9 @@
#include "InstanceScript.h"
#include "Log.h"
#include "MapMgr.h"
#include "MovementGenerator.h"
#include "MoveSpline.h"
#include "MoveSplineInit.h"
#include "MovementGenerator.h"
#include "ObjectAccessor.h"
#include "ObjectMgr.h"
#include "Opcodes.h"

View File

@@ -1849,7 +1849,7 @@ void GameEventMgr::SetHolidayEventTime(GameEventData& event)
tm timeInfo;
if (singleDate)
{
localtime_r(&curTime, &timeInfo);
timeInfo = Acore::Time::TimeBreakdown(curTime);
timeInfo.tm_year -= 1; // First try last year (event active through New Year)
}
else
@@ -1873,8 +1873,7 @@ void GameEventMgr::SetHolidayEventTime(GameEventData& event)
}
else if (singleDate)
{
tm tmCopy;
localtime_r(&curTime, &tmCopy);
tm tmCopy = Acore::Time::TimeBreakdown(curTime);
int year = tmCopy.tm_year; // This year
tmCopy = timeInfo;
tmCopy.tm_year = year;

View File

@@ -20,8 +20,8 @@
#include "Item.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Optional.h"
#include "Player.h"
#include "World.h"
#include "WorldPacket.h"
#include <set>

View File

@@ -16,8 +16,8 @@
*/
#include "BankPackets.h"
#include "Item.h"
#include "DBCStores.h"
#include "Item.h"
#include "Log.h"
#include "Opcodes.h"
#include "Player.h"

View File

@@ -15,8 +15,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScriptMgr.h"
#include "Chat.h"
#include "ScriptMgr.h"
Acore::ChatCommands::ChatCommandTable ScriptMgr::GetChatCommands()
{

View File

@@ -15,9 +15,9 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Player.h"
#include "ScriptMgr.h"
#include "ScriptMgrMacros.h"
#include "Player.h"
void ScriptMgr::OnGlobalItemDelFromDB(CharacterDatabaseTransaction trans, ObjectGuid::LowType itemGuid)
{

View File

@@ -15,8 +15,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ScriptMgr.h"
#include "OutdoorPvPMgr.h"
#include "ScriptMgr.h"
OutdoorPvP* ScriptMgr::CreateOutdoorPvP(OutdoorPvPData const* data)
{

View File

@@ -16,11 +16,11 @@
*/
#include "ScriptMgr.h"
#include "InstanceScript.h"
#include "ScriptSystem.h"
#include "SmartAI.h"
#include "SpellMgr.h"
#include "UnitAI.h"
#include "SmartAI.h"
#include "ScriptSystem.h"
#include "InstanceScript.h"
namespace
{

View File

@@ -18,13 +18,13 @@
#ifndef AllPackets_h__
#define AllPackets_h__
#include "CombatLogPackets.h"
#include "ChatPackets.h"
#include "CharacterPackets.h"
#include "MiscPackets.h"
#include "WorldStatePackets.h"
#include "TotemPackets.h"
#include "BankPackets.h"
#include "CharacterPackets.h"
#include "ChatPackets.h"
#include "CombatLogPackets.h"
#include "GuildPackets.h"
#include "MiscPackets.h"
#include "TotemPackets.h"
#include "WorldStatePackets.h"
#endif // AllPackets_h__

View File

@@ -18,8 +18,8 @@
#ifndef BankPackets_h__
#define BankPackets_h__
#include "Packet.h"
#include "ObjectGuid.h"
#include "Packet.h"
namespace WorldPackets
{

View File

@@ -18,8 +18,8 @@
#ifndef ChatPackets_h__
#define ChatPackets_h__
#include "Packet.h"
#include "ObjectGuid.h"
#include "Packet.h"
namespace WorldPackets
{

View File

@@ -17,12 +17,12 @@
#ifndef GuildPackets_h__
#define GuildPackets_h__
#include "Packet.h"
#include "Guild.h"
#include "ObjectGuid.h"
#include "Packet.h"
#include "PacketUtilities.h"
#include <boost/container/static_vector.hpp>
#include <array>
#include <boost/container/static_vector.hpp>
namespace WorldPackets
{

View File

@@ -18,8 +18,8 @@
#ifndef TotemPackets_h__
#define TotemPackets_h__
#include "Packet.h"
#include "ObjectGuid.h"
#include "Packet.h"
namespace WorldPackets
{

View File

@@ -17,10 +17,10 @@
#include "Opcodes.h"
#include "Log.h"
#include "Packets/AllPackets.h"
#include "WorldSession.h"
#include <iomanip>
#include <sstream>
#include "Packets/AllPackets.h"
template<class PacketClass, void(WorldSession::* HandlerFunction)(PacketClass&)>
class PacketHandler : public ClientOpcodeHandler

View File

@@ -20,12 +20,12 @@
*/
#include "Weather.h"
#include "MiscPackets.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "Util.h"
#include "World.h"
#include "WorldPacket.h"
#include "MiscPackets.h"
/// Create the Weather object
Weather::Weather(uint32 zone, WeatherData const* weatherChances)
@@ -89,10 +89,7 @@ bool Weather::ReGenerate()
//78 days between January 1st and March 20nd; 365/4=91 days by season
// season source http://aa.usno.navy.mil/data/docs/EarthSeasons.html
time_t gtime = sWorld->GetGameTime();
struct tm ltime;
localtime_r(&gtime, &ltime);
uint32 season = ((ltime.tm_yday - 78 + 365) / 91) % 4;
uint32 season = ((Acore::Time::GetDayInYear() - 78 + 365) / 91) % 4;
static char const* seasonName[WEATHER_SEASONS] = { "spring", "summer", "fall", "winter" };
LOG_DEBUG("weather", "Generating a change in %s weather for zone %u.", seasonName[season], m_zone);

View File

@@ -21,12 +21,12 @@
#include "WeatherMgr.h"
#include "Log.h"
#include "MiscPackets.h"
#include "ObjectMgr.h"
#include "Player.h"
#include "Weather.h"
#include "WorldPacket.h"
#include "WorldSession.h"
#include "MiscPackets.h"
#include <memory>
namespace WeatherMgr

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