Module:Ticks: Difference between revisions

From HighSpell Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 6: Line 6:
     local seconds = amount * 0.6
     local seconds = amount * 0.6


     -- Format to one decimal place explicitly
     local timeDisplay
     local secondsFormatted = string.format("%.1f", seconds)
     local minutes = seconds / 60


     local timeDisplay = string.format('<abbr title="Time in seconds">%ss</abbr>', secondsFormatted)
     if seconds >= 120 then
        if minutes == math.floor(minutes) then
            timeDisplay = string.format('<abbr title="Time in minutes">%.0fm</abbr>', minutes)
        elseif (minutes * 10) % 5 == 0 then  -- divisible by 0.5
            timeDisplay = string.format('<abbr title="Time in minutes">%.1fm</abbr>', minutes)
        end
    end
 
    if not timeDisplay then
        -- Fall back to seconds with 1 decimal place, strip .0 if present
        local secondsFormatted = string.format("%.1f", seconds):gsub("%.0$", "")
        timeDisplay = string.format('<abbr title="Time in seconds">%ss</abbr>', secondsFormatted)
    end


     return string.format("%d [[Ticks]] <small>(%s)</small>", amount, timeDisplay)
     return string.format("%d [[Ticks]] <small>(%s)</small>", amount, timeDisplay)

Latest revision as of 21:38, 17 June 2025

Documentation for this module may be created at Module:Ticks/doc

local p = {}

function p.display(frame)
    local args = frame:getParent().args
    local amount = tonumber(args[1] or args["amount"]) or 0
    local seconds = amount * 0.6

    local timeDisplay
    local minutes = seconds / 60

    if seconds >= 120 then
        if minutes == math.floor(minutes) then
            timeDisplay = string.format('<abbr title="Time in minutes">%.0fm</abbr>', minutes)
        elseif (minutes * 10) % 5 == 0 then  -- divisible by 0.5
            timeDisplay = string.format('<abbr title="Time in minutes">%.1fm</abbr>', minutes)
        end
    end

    if not timeDisplay then
        -- Fall back to seconds with 1 decimal place, strip .0 if present
        local secondsFormatted = string.format("%.1f", seconds):gsub("%.0$", "")
        timeDisplay = string.format('<abbr title="Time in seconds">%ss</abbr>', secondsFormatted)
    end

    return string.format("%d [[Ticks]] <small>(%s)</small>", amount, timeDisplay)
end

return p