From 2f4272812c789ccb971a3e9b2944873fecdb6839 Mon Sep 17 00:00:00 2001 From: Marco Date: Wed, 18 Dec 2024 14:50:42 -0300 Subject: [PATCH] feat: add Lua function to format remaining time --- data/libs/functions/game.lua | 32 +++++++++++++++++++ data/libs/systems/vip.lua | 2 +- .../functions/core/game/global_functions.cpp | 8 ----- .../functions/core/game/global_functions.hpp | 1 - src/utils/tools.cpp | 29 ----------------- src/utils/tools.hpp | 1 - 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/data/libs/functions/game.lua b/data/libs/functions/game.lua index ae13f50038a..ad1dc6a9228 100644 --- a/data/libs/functions/game.lua +++ b/data/libs/functions/game.lua @@ -182,3 +182,35 @@ function Game.getPlayerAccountId(name) end return 0 end + +function Game.getFormattedTimeRemaining(time) + local timeNow = os.time() + local timeRemaining = time - timeNow + local days = math.floor(timeRemaining / 86400) + local output = "" + + if days > 1 then + return days .. " days" + end + + local hours = math.floor((timeRemaining % 86400) / 3600) + local minutes = math.floor((timeRemaining % 3600) / 60) + local seconds = timeRemaining % 60 + + if hours == 0 and minutes == 0 and seconds > 0 then + output = "less than 1 minute" + else + if hours > 0 then + output = output .. hours .. " hour" .. (hours ~= 1 and "s" or "") + end + + if minutes > 0 then + if hours > 0 then + output = output .. " and " + end + + output = output .. minutes .. " minute" .. (minutes ~= 1 and "s" or "") + end + end + return output +end diff --git a/data/libs/systems/vip.lua b/data/libs/systems/vip.lua index 49ef7bcf8cc..55219ad3320 100644 --- a/data/libs/systems/vip.lua +++ b/data/libs/systems/vip.lua @@ -57,5 +57,5 @@ function Player.sendVipStatus(self) return true end - self:sendTextMessage(MESSAGE_LOGIN, string.format("You have %s of VIP time remaining.", getFormattedTimeRemaining(playerVipTime))) + self:sendTextMessage(MESSAGE_LOGIN, string.format("You have %s of VIP time remaining.", Game.getFormattedTimeRemaining(playerVipTime))) end diff --git a/src/lua/functions/core/game/global_functions.cpp b/src/lua/functions/core/game/global_functions.cpp index 6eb21910701..c6b141ab714 100644 --- a/src/lua/functions/core/game/global_functions.cpp +++ b/src/lua/functions/core/game/global_functions.cpp @@ -63,7 +63,6 @@ void GlobalFunctions::init(lua_State* L) { Lua::registerGlobalMethod(L, "rawgetmetatable", GlobalFunctions::luaRawGetMetatable); Lua::registerGlobalMethod(L, "createTable", GlobalFunctions::luaCreateTable); Lua::registerGlobalMethod(L, "systemTime", GlobalFunctions::luaSystemTime); - Lua::registerGlobalMethod(L, "getFormattedTimeRemaining", GlobalFunctions::luaGetFormattedTimeRemaining); Lua::registerGlobalMethod(L, "reportError", GlobalFunctions::luaReportError); } @@ -877,13 +876,6 @@ int GlobalFunctions::luaSystemTime(lua_State* L) { return 1; } -int GlobalFunctions::luaGetFormattedTimeRemaining(lua_State* L) { - // getFormattedTimeRemaining(time) - const time_t time = Lua::getNumber(L, 1); - lua_pushstring(L, getFormattedTimeRemaining(time).c_str()); - return 1; -} - int GlobalFunctions::luaReportError(lua_State* L) { // reportError(errorDescription) const auto errorDescription = Lua::getString(L, 1); diff --git a/src/lua/functions/core/game/global_functions.hpp b/src/lua/functions/core/game/global_functions.hpp index 7ed0c177c88..858bb2af004 100644 --- a/src/lua/functions/core/game/global_functions.hpp +++ b/src/lua/functions/core/game/global_functions.hpp @@ -46,7 +46,6 @@ class GlobalFunctions { static int luaRawGetMetatable(lua_State* L); static int luaCreateTable(lua_State* L); static int luaSystemTime(lua_State* L); - static int luaGetFormattedTimeRemaining(lua_State* L); static int luaReportError(lua_State* L); static bool getArea(lua_State* L, std::list &list, uint32_t &rows); diff --git a/src/utils/tools.cpp b/src/utils/tools.cpp index 57c9c873282..ffc54aa0e74 100644 --- a/src/utils/tools.cpp +++ b/src/utils/tools.cpp @@ -1925,35 +1925,6 @@ std::vector split(const std::string &str, char delimiter /* = ','*/ return tokens; } -std::string getFormattedTimeRemaining(uint32_t time) { - const time_t timeRemaining = time - getTimeNow(); - - const int days = static_cast(std::floor(timeRemaining / 86400)); - - std::stringstream output; - if (days > 1) { - output << days << " days"; - return output.str(); - } - - const auto hours = static_cast(std::floor((timeRemaining % 86400) / 3600)); - const auto minutes = static_cast(std::floor((timeRemaining % 3600) / 60)); - const auto seconds = static_cast(timeRemaining % 60); - - if (hours == 0 && minutes == 0 && seconds > 0) { - output << " less than 1 minute"; - } else { - if (hours > 0) { - output << hours << " hour" << (hours != 1 ? "s" : ""); - } - if (minutes > 0) { - output << (hours > 0 ? " and " : "") << minutes << " minute" << (minutes != 1 ? "s" : ""); - } - } - - return output.str(); -} - unsigned int getNumberOfCores() { static auto cores = std::thread::hardware_concurrency(); return cores; diff --git a/src/utils/tools.hpp b/src/utils/tools.hpp index 64fd2e8c1e6..b376552927b 100644 --- a/src/utils/tools.hpp +++ b/src/utils/tools.hpp @@ -173,7 +173,6 @@ uint8_t forgeBonus(int32_t number); std::string formatPrice(std::string price, bool space /* = false*/); std::vector split(const std::string &str, char delimiter = ','); -std::string getFormattedTimeRemaining(uint32_t time); unsigned int getNumberOfCores();