Module:Sandbox/AB-me/Racing table: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(16 intermediate revisions by the same user not shown)
Line 1:
local p = {}
local getArgs = require('Module:Arguments').getArgs
 
local colors = { first = { "#ffffbf", nil }, second = { "#dfdfdf", nil }, third = { "#ffdf9f", nil }, points = { "#dfffdf", nil }, nonPoints = { "#cfcfff", nil }, ret = { "#efcfff", nil }, dnq = { "#ffcfcf", nil }, dsq = { "#000000", "#ffffff" }, dns = { "#ffffff", nil } }
 
function p.main(frame)
Line 10 ⟶ 8:
 
function p._main(args)
local eventseventsArg = mw.text.split(args['events'], "',"')
local participantsparticipantsArg = mw.text.split(args['participants'], "',"')
local pointPositionsArg = mw.text.split(args['points'], ',') or {}
local notesArg = mw.text.split(args['notes'], ',') or {}
local events = {} -- contains event information (code, name)
local pointPositionsArg = mw.text.split(args['points'], ",")
local participants = {} -- contains participant information (code, name, results, resultNotes, points, pointsTotal)
--local pointFastestLap = tonumber(args['pointsFL'])
local pointPositions = {} -- contains the distribution of points based on position
--local pointPole = tonumber(args['pointsPole'])
local notes = {} -- contains information about distribution of points not based on position (code, adj, num)
local pointPositions = {}
local stack = {}
 
for i,v in ipairs(pointPositionsArg) do pointPositions[i] = tonumber(v) end
for i,v in ipairs(eventsArg) do events[i] = {code = v, name = args['event_' .. v]} end
for i,v in ipairs(participantsArg) do participants[i] = {code = v, name = args['participant_' .. v], results = {}, resultNotes = {}, points = {}, pointsTotal = nil} end
local p = {}
for i,v in ipairs(pointPositionsArg) do pointPositions[i] = tonumber(v) or 0 end
local pCodes = {}
for i,v in ipairs(notesArg) do
local adj = true
for i,v in ipairs(participants) do
local num = 0
p[i] = args['participant_' .. v] or v
local arg = args['notesPoints_' .. v]
pCodes[v] = i
if (arg ~= null) then
local startsWith = mw.ustring.sub(arg, 1, 1)
if (startsWith ~= '+' and startsWith ~= '-') then
adj = false
end
num = tonumber(arg)
end
notes[i] = {code = v, adj = adj, num = tonumber(args['notesPoints_' .. v] or 0)}
end
-- Point handling and calculations
local e = {}
for iEvents,vEvents in ipairs(events) do
local r = {}
if (args['result_' .. vEvents['code']] ~= nil) then
local resultSplit = mw.text.split(args['result_' .. vEvents['code']], ',') or nil
local t = {}
for i,v in ipairs(events) do
e[i] = args['event_' .. v] or v
r[i] = {}
if (args['result_' .. v] ~= nil) then
local res = mw.text.split(args['result_' .. v], ",")
local res = {}
for j, w in ipairs(res) do
for i,v in ipairs(resultSplit) do res[v] = i end
r[i][pCodes[w]] = j
end
for iParticipants,vParticipants in ipairs(participants) do
vParticipants['results'][iEvents] = res[vParticipants['code']]
if (vParticipants['results'][iEvents] ~= nil) then
vParticipants['points'][iEvents] = pointPositions[res[vParticipants['code']]] or 0
else
vParticipants['points'][iEvents] = nil
end
end
end
local spec = {}
for iNotes,vNotes in ipairs(notes) do
if (args['notes_' .. vEvents['code'] .. '_' .. vNotes['code']]) then
local notesSplit = mw.text.split(args['notes_' .. vEvents['code'] .. '_' .. vNotes['code']], ',')
for i,v in ipairs(notesSplit) do
if (spec[v] == nil) then spec[v] = {} end
table.insert(spec[v], vNotes)
end
end
end
 
for iParticipants,vParticipants in ipairs(participants) do
vParticipants['resultNotes'][iEvents] = spec[vParticipants['code']]
end
 
end
--Summation of points
table.insert(t, '{| class="wikitable" style="font-size: 85%; vertical-align:top; text-align:center""\n')
for iParticipants,vParticipants in ipairs(participants) do
table.insert(t, '!style="vertical-align:middle;"|<abbr title="Position">Pos</abbr>\n')
local tot = nil
table.insert(t, '!style="vertical-align:middle;"|Driver\n')
for i,v in pairs(vParticipants['points']) do
if (v ~= nil) then
tot = (tot or 0) + v
end
end
vParticipants['pointsTotal'] = tot
end
--Writing table
--local stack = {}
table.insert(stack, '{| class="wikitable" style="font-size: 85%; vertical-align:top; text-align:center""\n')
for i,v in ipairs(e) do
table.insert(tstack, '!'style="vertical-align:middle;"|<abbr .. v .. 'title="Position">Pos</abbr>\n')
table.insert(stack, '!style="vertical-align:middle;"|'.. (args['text_participant'] or 'Participant') .. '\n')
for i,v in ipairs(events) do
table.insert(stack, '!' .. (v['name'] or v['code'] or i) .. '\n')
end
table.insert(tstack, '!style="vertical-align:middle"|Points\n')
for i,v in ipairs(pparticipants) do
table.insert(tstack, '|-\n')
table.insert(t, '|' .. i .. '\n')
table.insert(tstack, '|style="text-align:left;"|' .. v .. '\n!')
if (v['pointsTotal'] ~= nil) then
table.insert(stack, i)
else
table.insert(stack, '&ndash;')
end
table.insert(stack, '\n')
 
table.insert(stack, '|style="text-align:left;"|' .. (v['name'] or v['code']) .. '\n')
for j,w in ipairs(events) do
local pts = 0
table.insert(stack, '|')
table.insert(stack, v['results'][j])
for j,w in ipairs(r) do
 
pts = pts + (pointPositions[r[j][i]] or 0)
if (v['resultNotes'] ~= nil and v['resultNotes'][j] ~= nil) then
local firstNote = true
if (r[j] ~= nil and r[j][i] ~= nil) then
for k,x in ipairs(v['resultNotes'][j]) do
table.insert(t, '|' .. r[j][i] .. '\n')
if (firstNote and v['results'][j] == nil) then
else
table.insert(stack, x['code'])
local color = getColor(r[j][i])
else
if (color ~= nil) then
table.insert(tstack, '|<span style="margin:0 0.1em 0 0.1em;"><sup>' .. x['code'] .. '</sup></span>')
end
if (color[1] ~= nil) then
firstNote = false
table.insert(t,'background-color:' .. color[1])
end
end
end
if (color[2] ~= nil) then
 
table.insert(t,'color:' .. color[2])
table.insert(stack, '\n')
end
table.insert(t, '"|')
else
table.insert(t, '|\n')
end
end
end
table.insert(tstack, '|!' .. pts(v['pointsTotal'] or '&ndash;') .. '\n')
end
table.insert(stack, '|}')
return table.insertconcat(t, '|}'stack)
return table.concat(t)
end
 
function p.getColor(pos, inPoints)
if (pos == 1) then return colors["first"]
elseif (pos == 2) then return colors["second"]
elseif (pos == 3) then return colors["third"]
elseif (pos <= inPoints) then return colors["points"]
elseif (pos == nil) then return nil
else return colors["nonPoints"] end
end