From a2ab15f816f146ea253a569edb09958f816fcc20 Mon Sep 17 00:00:00 2001 From: TracentEden <92269743+TracentEden@users.noreply.github.com> Date: Fri, 31 May 2024 04:31:51 +0300 Subject: [PATCH] Add zone uptime command Co-Authored-By: Frankie-hz --- scripts/commands/uptime.lua | 50 +++++++++++++++++++++++++++++++++++++ src/map/lua/lua_zone.cpp | 11 ++++++++ src/map/lua/lua_zone.h | 1 + 3 files changed, 62 insertions(+) create mode 100644 scripts/commands/uptime.lua diff --git a/scripts/commands/uptime.lua b/scripts/commands/uptime.lua new file mode 100644 index 00000000000..aabdf8f5d7f --- /dev/null +++ b/scripts/commands/uptime.lua @@ -0,0 +1,50 @@ +----------------------------------- +-- func: uptime +-- desc: prints zone uptime +----------------------------------- +local commandObj = {} + +commandObj.cmdprops = +{ + permission = 1, + parameters = '' +} + +local function formatSeconds(seconds) + local days = math.floor(seconds / 86400) + seconds = seconds % 86400 -- Use modulo to get remaining seconds after days + local hours = math.floor(seconds / 3600) + seconds = seconds % 3600 -- Use modulo again for remaining seconds after hours + local minutes = math.floor(seconds / 60) + seconds = seconds % 60 -- And again for remaining seconds + + local parts = {} + + if days > 0 then + table.insert(parts, string.format('%d day%s', days, days > 1 and 's' or '')) + end + + if hours > 0 then + table.insert(parts, string.format('%d hour%s', hours, hours > 1 and 's' or '')) + end + + if minutes > 0 then + table.insert(parts, string.format('%d minute%s', minutes, minutes > 1 and 's' or '')) + end + + if seconds > 0 or #parts == 0 then + table.insert(parts, string.format('%d second%s', seconds, seconds > 1 and 's' or '')) + end + + return table.concat(parts, ' ') +end + +commandObj.onTrigger = function(player) + local zone = player:getZone() + if zone then + local uptime = zone:getUptime() + player:printToPlayer('The zone has been up for ' .. formatSeconds(uptime), xi.msg.channel.SYSTEM_3) + end +end + +return commandObj diff --git a/src/map/lua/lua_zone.cpp b/src/map/lua/lua_zone.cpp index d8a083838fc..15a6ec9fbaa 100644 --- a/src/map/lua/lua_zone.cpp +++ b/src/map/lua/lua_zone.cpp @@ -181,6 +181,16 @@ WEATHER CLuaZone::getWeather() return m_pLuaZone->GetWeather(); } +uint32 CLuaZone::getUptime() +{ + time_point currentTime = std::chrono::system_clock::now(); // Gets the current time + time_point zoneStartTime = get_server_start_time(); // Gets the start time of the zone group (cluster) + + long long uptime = std::chrono::duration_cast(currentTime - zoneStartTime).count(); + // returns the zone up time in seconds + return static_cast(uptime); +} + void CLuaZone::reloadNavmesh() { if (m_pLuaZone->m_navMesh) @@ -317,6 +327,7 @@ void CLuaZone::Register() SOL_REGISTER("getBattlefieldByInitiator", CLuaZone::getBattlefieldByInitiator); SOL_REGISTER("battlefieldsFull", CLuaZone::battlefieldsFull); SOL_REGISTER("getWeather", CLuaZone::getWeather); + SOL_REGISTER("getUptime", CLuaZone::getUptime); SOL_REGISTER("reloadNavmesh", CLuaZone::reloadNavmesh); SOL_REGISTER("isNavigablePoint", CLuaZone::isNavigablePoint); SOL_REGISTER("insertDynamicEntity", CLuaZone::insertDynamicEntity); diff --git a/src/map/lua/lua_zone.h b/src/map/lua/lua_zone.h index ca14e212f47..c682e5fb90f 100644 --- a/src/map/lua/lua_zone.h +++ b/src/map/lua/lua_zone.h @@ -56,6 +56,7 @@ class CLuaZone auto getBattlefieldByInitiator(uint32 charID) -> std::optional; bool battlefieldsFull(int battlefieldId); WEATHER getWeather(); + uint32 getUptime(); void reloadNavmesh(); bool isNavigablePoint(const sol::table& position); auto insertDynamicEntity(sol::table table) -> std::optional;