Module:Grand Slam Track

p = {}

local P_POINTINTIME = 'P585'
local P_PARTICIPANTOF = 'P1344'
local P_SPORTSDISCIPLINE = 'P2416'
local P_COMPETITIONCLASS = 'P2094'
local P_RANK = 'P1352'
local P_RACETIME = 'P2781'
local P_ENDCAUSE = 'P1534'
local P_PRIZEMONEY = 'P2121'

local Q_SHORTSPRINTS = 'Q133805007'
local Q_LONGSPRINTS = 'Q133805012'
local Q_SHORTHURDLES = 'Q133805023'
local Q_LONGHURDLES = 'Q133805093'
local Q_SHORTDISTANCE = 'Q133805111'
local Q_LONGDISTANCE = 'Q133805125'

local Q_DNS = 'Q1210382'
local Q_DNF = 'Q1210380'
local Q_DQ = 'Q1229261'

local slams = {'Q133804597', 'Q134348075', 'Q134350808'} -- in chronological order

local function toOrdinal(n)
  n = math.floor(n)
  local lastTwo = n % 100
  if lastTwo >= 11 and lastTwo <= 13 then
    return tostring(n) .. "th"
  end
  local last = n % 10
  if last == 1 then
    return n .. "st"
  elseif last == 2 then
    return n .. "nd"
  elseif last == 3 then
    return n .. "rd"
  else
    return n .. "th"
  end
end

function addCommas(i)
  return tostring(i):reverse():gsub("%d%d%d", "%1,"):reverse():gsub("^,", "")
end

p.gst = function(frame)
  local qid = frame.args[1]
  local ent = mw.wikibase.getEntity(qid)
  local participantOfClaims = ent['claims'][P_PARTICIPANTOF] or {}
  local output = ''
  output = output .. '|-\n'
  output = output .. '!scope="col" | Slam !!scope="col" | Race group !!scope="col" | Event !!scope="col" | <abbr title="Place">Pl.</abbr> !!scope="col" | Time !!scole="col" | Prize money\n'
  
  local slamSet = {}
  for _, slamQid in ipairs(slams) do
    slamSet[slamQid] = true
  end

  for i, pc in ipairs(participantOfClaims) do
    local mainsnak = pc.mainsnak

    -- mw.logObject(mw.wikibase.renderSnak(pc['mainsnak']))
    if mainsnak.snaktype == 'value' and slamSet[mainsnak.datavalue.value.id] and pc['qualifiers'] then
      local qs = pc['qualifiers']
      output = output .. '|-\n'
      output = output .. '| '

      -- Slam
      output = output .. 'rowspan=2| '
      output = output .. '[[' .. mw.wikibase.renderSnak(pc['mainsnak']) .. ']]'
      output = output .. ' || '

      -- Race group
      output = output .. 'rowspan=2| '
      if qs[P_COMPETITIONCLASS] then
        local cc = mw.wikibase.renderSnak(qs[P_COMPETITIONCLASS][1])
        output = output .. cc:sub(1, 1):upper() .. cc:sub(2)
      end
      output = output .. '\n'

      -- Place dupe fix
      if qs[P_RACETIME] and qs[P_RACETIME][2] and qs[P_RANK] and qs[P_RANK][1] and not qs[P_RANK][2] and not qs[P_ENDCAUSE] then
        qs[P_RANK][2] = qs[P_RANK][1]
      end
      -- End cause dupe fix
      if qs[P_RACETIME] and qs[P_RACETIME][1] and qs[P_RACETIME][1]['snaktype'] == 'novalue' and not qs[P_RACETIME][2] then
        qs[P_RACETIME][2] = qs[P_RACETIME][1]
      end
      for j = 1,2 do
        -- Event
        output = output .. '| '
        if qs[P_SPORTSDISCIPLINE] and qs[P_SPORTSDISCIPLINE][j] then
          output = output .. mw.wikibase.renderSnak(qs[P_SPORTSDISCIPLINE][j]):gsub("metres", "m")
        end
        output = output .. ' || '

        -- Place
        if qs[P_RANK] and qs[P_RANK][j] then
          output = output .. toOrdinal(tonumber(mw.wikibase.renderSnak(qs[P_RANK][j])))
        end
        output = output .. ' || '
            
        -- Time
        if qs[P_RACETIME] and qs[P_RACETIME][j] then
          local timeStr = mw.wikibase.renderSnak(qs[P_RACETIME][j]):gsub(" second", "")
          if timeStr == 'no value' and qs[P_ENDCAUSE] then
            local endCauseIdx = 1
            if j == 2 and qs[P_ENDCAUSE][2] then endCauseIdx = 2 end
            local endCause = qs[P_ENDCAUSE][endCauseIdx]['datavalue']['value']['id']
            if endCause == Q_DNS then timeStr = 'DNS' end
            if endCause == Q_DNF then timeStr = 'DNF' end
            if endCause == Q_DQ then timeStr = 'DQ' end
          end
          local seconds = tonumber(timeStr)
          if seconds and seconds >= 60 then
            local minutes = math.floor(seconds / 60)
            local remainingSeconds = seconds % 60
            -- Format with leading zero for seconds if needed
            output = output .. string.format("%d:%05.2f", minutes, remainingSeconds)
          else
            output = output .. timeStr
          end
        end
        
        -- Prize money
        if j == 1 and qs[P_PRIZEMONEY] then
          output = output .. ' ||rowspan=2| '
          output = output .. 'US$' .. addCommas(qs[P_PRIZEMONEY][1]['datavalue']['value']['amount']:sub(2))
        end
        output = output .. '\n|-\n'
      end
    end
  end
  output = output .. '|}'
  return output
end
return p

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.