Skip to content

Commit

Permalink
Merge pull request #5860 from TracentEden2/add_zone_uptime_command
Browse files Browse the repository at this point in the history
[core, lua] Add zone uptime command
  • Loading branch information
claywar authored May 31, 2024
2 parents 59e3d24 + a2ab15f commit 1312db2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
50 changes: 50 additions & 0 deletions scripts/commands/uptime.lua
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions src/map/lua/lua_zone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::seconds>(currentTime - zoneStartTime).count();
// returns the zone up time in seconds
return static_cast<uint32>(uptime);
}

void CLuaZone::reloadNavmesh()
{
if (m_pLuaZone->m_navMesh)
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/map/lua/lua_zone.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class CLuaZone
auto getBattlefieldByInitiator(uint32 charID) -> std::optional<CLuaBattlefield>;
bool battlefieldsFull(int battlefieldId);
WEATHER getWeather();
uint32 getUptime();
void reloadNavmesh();
bool isNavigablePoint(const sol::table& position);
auto insertDynamicEntity(sol::table table) -> std::optional<CLuaBaseEntity>;
Expand Down

0 comments on commit 1312db2

Please sign in to comment.