Skip to content

Commit

Permalink
feat: add Lua function to format remaining time
Browse files Browse the repository at this point in the history
  • Loading branch information
omarcopires committed Dec 18, 2024
1 parent 05ff8be commit 2f42728
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 40 deletions.
32 changes: 32 additions & 0 deletions data/libs/functions/game.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion data/libs/systems/vip.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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<uint32_t>(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);
Expand Down
1 change: 0 additions & 1 deletion src/lua/functions/core/game/global_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t> &list, uint32_t &rows);
Expand Down
29 changes: 0 additions & 29 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1925,35 +1925,6 @@ std::vector<std::string> 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<int>(std::floor(timeRemaining / 86400));

std::stringstream output;
if (days > 1) {
output << days << " days";
return output.str();
}

const auto hours = static_cast<int>(std::floor((timeRemaining % 86400) / 3600));
const auto minutes = static_cast<int>(std::floor((timeRemaining % 3600) / 60));
const auto seconds = static_cast<int>(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;
Expand Down
1 change: 0 additions & 1 deletion src/utils/tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ uint8_t forgeBonus(int32_t number);

std::string formatPrice(std::string price, bool space /* = false*/);
std::vector<std::string> split(const std::string &str, char delimiter = ',');
std::string getFormattedTimeRemaining(uint32_t time);

unsigned int getNumberOfCores();

Expand Down

0 comments on commit 2f42728

Please sign in to comment.