Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:TrackInfobox: Difference between revisions

From Trackmania Wiki
Created page with "-- Module:TrackInfobox local p = {} local function safe(s) if not s then return '' end return mw.html.escape(tostring(s)) end function p.formatWR(frame) -- args: time_ms, player, fallback local time_ms = frame.args[1] or '' local player = frame.args[2] or '' local fallback = frame.args[3] or '' if time_ms == '' or time_ms == nil then if fallback ~= '' then return fallback end return 'Unknown' end local ms = tonumber(time_ms) if not ms then..."
 
Fixed format
 
(One intermediate revision by the same user not shown)
Line 2: Line 2:
local p = {}
local p = {}


local function safe(s)
local function escape_html(s)
   if not s then return '' end
   if s == nil then return '' end
   return mw.html.escape(tostring(s))
   s = tostring(s)
  s = s:gsub("&", "&")
  s = s:gsub("<", "&lt;")
  s = s:gsub(">", "&gt;")
  s = s:gsub('"', "&quot;")
  s = s:gsub("'", "&#39;")
  return s
end
end


function p.formatWR(frame)
function p.formatWR(frame)
  -- args: time_ms, player, fallback
   local time_ms = frame.args[1] or ''
   local time_ms = frame.args[1] or ''
   local player = frame.args[2] or ''
   local player = frame.args[2] or ''
Line 24: Line 29:
   end
   end


   local total_seconds = ms / 1000
  -- centiseconds (100 Hz -> 10 ms per tick)
   local mins = math.floor(total_seconds / 60)
  local cs = math.floor(ms / 10)
   local secs = total_seconds - mins * 60
   local total_seconds = math.floor(cs / 100)
   local time_str = string.format("%d:%06.2f", mins, secs)
  local centi = cs % 100
 
   local hours = math.floor(total_seconds / 3600)
  local rem = total_seconds % 3600
  local minutes = math.floor(rem / 60)
   local seconds = rem % 60
 
   local time_str
  if hours > 0 then
    -- h:mm:ss.cc (hours not zero-padded, minutes and seconds are 2 digits)
    time_str = string.format("%d:%02d:%02d.%02d", hours, minutes, seconds, centi)
  elseif minutes > 0 then
    -- mm:ss.cc (minutes zero-padded)
    time_str = string.format("%02d:%02d.%02d", minutes, seconds, centi)
  else
    -- ss.cc (no leading zero on seconds)
    time_str = string.format("%d.%02d", seconds, centi)
  end


   if player ~= '' then
   if player ~= '' then
     return time_str .. ' by ' .. safe(player)
     return time_str .. ' by ' .. escape_html(player)
   else
   else
     return time_str
     return time_str

Latest revision as of 13:19, 14 August 2025

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

-- Module:TrackInfobox
local p = {}

local function escape_html(s)
  if s == nil then return '' end
  s = tostring(s)
  s = s:gsub("&", "&amp;")
  s = s:gsub("<", "&lt;")
  s = s:gsub(">", "&gt;")
  s = s:gsub('"', "&quot;")
  s = s:gsub("'", "&#39;")
  return s
end

function p.formatWR(frame)
  local time_ms = frame.args[1] or ''
  local player = frame.args[2] or ''
  local fallback = frame.args[3] or ''

  if time_ms == '' or time_ms == nil then
    if fallback ~= '' then return fallback end
    return 'Unknown'
  end

  local ms = tonumber(time_ms)
  if not ms then
    if fallback ~= '' then return fallback end
    return 'Unknown'
  end

  -- centiseconds (100 Hz -> 10 ms per tick)
  local cs = math.floor(ms / 10)
  local total_seconds = math.floor(cs / 100)
  local centi = cs % 100

  local hours = math.floor(total_seconds / 3600)
  local rem = total_seconds % 3600
  local minutes = math.floor(rem / 60)
  local seconds = rem % 60

  local time_str
  if hours > 0 then
    -- h:mm:ss.cc (hours not zero-padded, minutes and seconds are 2 digits)
    time_str = string.format("%d:%02d:%02d.%02d", hours, minutes, seconds, centi)
  elseif minutes > 0 then
    -- mm:ss.cc (minutes zero-padded)
    time_str = string.format("%02d:%02d.%02d", minutes, seconds, centi)
  else
    -- ss.cc (no leading zero on seconds)
    time_str = string.format("%d.%02d", seconds, centi)
  end

  if player ~= '' then
    return time_str .. ' by ' .. escape_html(player)
  else
    return time_str
  end
end

return p