יחידה:בית בספורט
ניתן ליצור תיעוד על היחידה הזאת בדף יחידה:בית בספורט/תיעוד
--[[Automatically calculate the teams score.
]]
function calcTeamScores(field, teamScore)
if field == 'כדורגל' then
return 3*teamScore['wins']+1*teamScore['draws']
elseif field == 'כדורסל' or field == 'כדורסל 3x3' then
return 2 * teamScore['wins'] + teamScore['losses']
end
end
function parseData(data, pFrame)
local gamesData = {}
local sumScores = {}
local numGames = 0
for line in mw.text.gsplit( data, "\n" ) do
line = mw.ustring.gsub( line, '%s+', ' ' )
line = mw.text.trim( line )
local playerA, scoreA, scoreB, playerB = mw.ustring.match( line, '([^0-9]+) (\-?[0-9]+)%s*[–-]%s*(\-?[0-9]+) ([^0-9]+)' )
if playerA then
for _, p in pairs({playerA, playerB}) do
if sumScores[p]==nil then
sumScores[p] = {
['wins']= 0,
['losses']=0 ,
['draws']= 0 ,
['points'] = 0,
['games'] = 0,
['goalsFor'] = 0,
['goalsAgainst'] = 0
}
end
end
sumScores[playerA]['games']=sumScores[playerA]['games']+1
sumScores[playerB]['games']=sumScores[playerB]['games']+1
if (scoreA+0)==(scoreB+0) then
sumScores[playerA]['draws'] = sumScores[playerA]['draws']+ 1
sumScores[playerB]['draws'] = sumScores[playerB]['draws']+ 1
elseif (scoreA+0)>(scoreB+0) then
sumScores[playerA]['wins'] = sumScores[playerA]['wins']+ 1
sumScores[playerB]['losses'] = sumScores[playerB]['losses']+ 1
else
sumScores[playerB]['wins'] = sumScores[playerB]['wins']+ 1
sumScores[playerA]['losses'] = sumScores[playerA]['losses']+ 1
end
sumScores[playerA]['goalsFor'] = sumScores[playerA]['goalsFor'] + scoreA
sumScores[playerB]['goalsFor'] = sumScores[playerB]['goalsFor'] + scoreB
sumScores[playerA]['goalsAgainst'] = sumScores[playerA]['goalsAgainst'] + scoreB
sumScores[playerB]['goalsAgainst'] = sumScores[playerB]['goalsAgainst'] + scoreA
numGames = numGames+1
table.insert(gamesData, {
['playerA'] = playerA,
['playerB'] = playerB,
['flagA'] = pFrame.args['דגל '..playerA] or playerA,
['flagB'] = pFrame.args['דגל '..playerB] or playerB,
['scoreA'] = scoreA,
['scoreB'] = scoreB
})
end
end
return gamesData, sumScores, numGames
end
function renderScore(scoreA, scoreB, bold)
local style = 'direction:ltr;display:inline-block;'
if bold then
style = style .. 'font-weight:bold;'
end
return '<span style="'..style..'">' .. scoreA ..'</span> – <span style="'..style..'">' .. scoreB .. '</span>'
end
function buildGamesTable(gamesData, floatStyle, linkTemplate)
local frame = mw.getCurrentFrame()
local gamesTable = '<table style="'..floatStyle..'margin: 1em 1.5em;">'
for _, game in ipairs(gamesData) do
local flagA = playerFlag(game.flagA, game.playerA)
local flagB = playerFlag(game.flagB, game.playerB)
local nicePlayerA = playerLink( linkTemplate, game.playerA)
local nicePlayerB = playerLink( linkTemplate, game.playerB)
gamesTable = gamesTable .. '<tr><td>'..flagA.. nicePlayerA .. '</td><td>'..renderScore( game.scoreA, game.scoreB, true) .. '</span></td><td align="left">' ..nicePlayerB ..' '.. flagB..'</td></tr>'
end
gamesTable = gamesTable .. '</table>'
return gamesTable
end
function playerFlag(flagArg, playerArg)
-- for countryA/countryB skip the flag unless explicitly specified
if mw.ustring.find(playerArg, '/') and not flagArg then
flagArg = 'ריק'
end
local frame = mw.getCurrentFrame()
local flagPlayer = mw.ustring.format('[[File:%s|22px|border|%s|link=]] ', frame:expandTemplate{ title = 'דגל/'.. (flagArg or playerArg )}, playerArg)
return flagPlayer
end
function playerLink(linkTemplate, player)
-- for countryA/countryB pattern - avoid linking
if mw.ustring.find( player, '/') then return player end
return mw.ustring.gsub( linkTemplate, '\{\{\{1\}\}\}', player)
end
function buildSummaryTable(teamScore, sumScores, teamsAttr, tableStyle, showDraws, titleGoals, sumKeys,linkTemplate, gamesData)
local sumTxt
local frame = mw.getCurrentFrame()
local pFrame = frame:getParent()
local showMatrix = gamesData ~= nil
sumTxt = '<table class="wikitable" border="1" style="text-align:center;'..tableStyle..'">'
sumTxt = sumTxt..'<tr><th rowspan="2" style="width:200px;">נבחרת</th>'
if teamScore then
sumTxt = sumTxt..'<th rowspan="2">נקודות</th>'
end
sumTxt = sumTxt..'<th rowspan="2">משחקים</th><th rowspan="2">ניצחונות</th>'..((showDraws and '<th rowspan="2">תיקו</th>') or '')..'<th rowspan="2">הפסדים</th><th colspan="3">'..titleGoals..'</th>'
if showMatrix then
sumTxt = sumTxt .. '<td rowspan="'..(#sumKeys+2)..'" style="background:#ffffff;"></td>'
for _, k in pairs(sumKeys) do
local flagPlayer = playerFlag(pFrame.args['דגל '..k], k)
sumTxt = sumTxt .. '<td rowspan="2">'..flagPlayer..'</td>'
end
end
sumTxt = sumTxt .. '</tr>'
sumTxt = sumTxt..'<tr><th>זכות</th><th>חובה</th><th>הפרש</th></tr>'
for _, k in pairs(sumKeys) do
local v=sumScores[k]
local nicePlayer = playerLink( linkTemplate, k)
local background = ''
if teamsAttr.winners[k] then
background = '#cfc'
elseif teamsAttr.playoffPlayers[k] then
background = '#ffebad'
elseif teamsAttr.relegatPlayers[k] then
background = '#ffcccc'
end
if background then background = 'background: ' .. background .. ';' end
sumTxt = sumTxt ..'<tr>'
local flagPlayer = playerFlag(pFrame.args['דגל '..k], k)
sumTxt = sumTxt ..'<td style="text-align:right;'..background .. '">'..flagPlayer ..nicePlayer ..'</td>'
if teamScore then
sumTxt = sumTxt..'<td style="font-weight:bold;direction:ltr;'..background..'">'..v['points'] ..'</td>'
end
local cells
if showDraws then
cells = {v['games'], v['wins'], v['draws'], v['losses'] }
else
cells = {v['games'], v['wins'], v['losses'] }
end
for _, c in ipairs(cells) do
if background ~= '' then
sumTxt = sumTxt ..mw.ustring.format('<td style="%s">%s</td>', background, c)
else
sumTxt = sumTxt ..mw.ustring.format('<td>%s</td>', c)
end
end
cells = {v['goalsFor'], v['goalsAgainst'], v['goalsFor']-v['goalsAgainst'] }
for _, c in ipairs(cells) do
sumTxt = sumTxt ..mw.ustring.format('<td style="direction:ltr;%s">%s</td>', background, c)
end
if showMatrix then
for _, playerA in pairs(sumKeys) do
if playerA==k then
sumTxt = sumTxt .. '<td>—</td>'
else
local gameScoreFound = false
for _,game in ipairs(gamesData) do
if game.playerA==k and game.playerB==playerA then
sumTxt = sumTxt .. '<td>'..renderScore(game.scoreA, game.scoreB)..'</td>'
gameScoreFound = true
end
end
if gameScoreFound == false then
sumTxt = sumTxt .. '<td>-</td>'
end
end
end
end
sumTxt = sumTxt .. '</tr>'
end
sumTxt = sumTxt.. '</table>'
return sumTxt
end
function render(frame)
local pFrame = frame:getParent()
local data = pFrame.args[1] or ''
local sumTxt, gamesTable
local titleGoals = 'שערים'
local teamScore = pFrame.args['נקודות'] or nil
local winnerTeams = pFrame.args['עולות'] or ''
local playoffTeams = pFrame.args['פלייאוף'] or ''
local relegatTeams = pFrame.args['יורדות'] or ''
local hideSummary = pFrame.args['סיכום'] and pFrame.args['סיכום']=='לא'
local hideGames = pFrame.args['תוצאות'] and pFrame.args['תוצאות']=='לא'
local gamesMatrix = pFrame.args['תוצאות'] and pFrame.args['תוצאות']=='מטריצה'
if gamesMatrix then
hideGames = true
end
local floatStyle = 'float: right;'
if (hideSummary or hideGames) then
floatStyle = ''
end
local showDraws = true
if pFrame.args['ענף'] == 'כדורסל' or pFrame.args['ענף'] == 'כדורסל 3x3' then
titleGoals = 'סלים'
showDraws = false
elseif pFrame.args['ענף'] == 'בייסבול' then
titleGoals = 'ריצות'
showDraws = false
end
local sportType = pFrame.args['ענף'] or 'כדורגל'
local suffix = pFrame.args['נשים'] and ' נשים' or ''
local linkTemplate = '[[נבחרת {{{1}}} ב'..sportType..suffix..'|{{{1}}}]]'
if pFrame.args['קישור'] then
linkTemplate = pFrame.args['קישור']
end
data = mw.ustring.gsub( data , '', '' )
local gamesData, sumScores, numGames = parseData(data, pFrame)
if hideGames then
gamesTable = ''
else
gamesTable= buildGamesTable(gamesData, floatStyle, linkTemplate)
end
if (hideSummary) then
sumTxt=''
else
local sumKeys = {}
for k, v in pairs(sumScores) do sumKeys[#sumKeys+1] = k end
local lineHeight = ''
local numPlayers = #sumKeys
local function sortTeams(a,b)
if sumScores[a]['points']==sumScores[b]['points'] then
-- sort according to specified index (manual)
if sumScores[a]['pointsI']~=nil then
return sumScores[a]['pointsI']<sumScores[b]['pointsI']
end
-- in soccer, sort by goal difference
if sportType == 'כדורגל' then
local aDiff = sumScores[a]['goalsFor']-sumScores[a]['goalsAgainst']
local bDiff = sumScores[b]['goalsFor']-sumScores[b]['goalsAgainst']
return aDiff>bDiff
end
-- TODO: add support for more fields
end
return sumScores[a]['points']>sumScores[b]['points']
end
if teamScore == 'אוטומטי' then
-- automaticlly calculate points
for player, v in pairs(sumScores) do
v['points'] = calcTeamScores(sportType, v)
end
table.sort( sumKeys, sortTeams)
elseif teamScore then
local scoreIndx = 1
for player, score in mw.ustring.gmatch( teamScore, '(%a[%a\' /]+)%s+(\-?%d+)' ) do
if sumScores[player]==nil then
sumScores[player] = {
['wins']= 0,
['losses']=0 ,
['draws']= 0 ,
['points'] = 0,
['games'] = 0,
['goalsFor'] = 0,
['goalsAgainst'] = 0
}
sumKeys[#sumKeys+1] = player
numPlayers = numPlayers+1
end
sumScores[player]['points'] = score+0 -- cast to int
sumScores[player]['pointsI'] = scoreIndx
scoreIndx = scoreIndx + 1
end
table.sort( sumKeys, sortTeams)
end
if not hideGames then
-- base line height is 22, but may vary between 22 to 60 based on numGames and numPlayers to fit the games table
lineHeight = 22*math.ceil(numGames/(numPlayers+2))
lineHeight = math.max(lineHeight, 22)
lineHeight = math.min(lineHeight, 60)
lineHeight = 'line-height:'..lineHeight..'px;'
end
local function keyTable(str)
local players = {}
for player in mw.ustring.gmatch( str, '(%a[%a\' /]+)' ) do
players[player]=1
end
return players
end
local teamsAttr = {winners=keyTable(winnerTeams), playoffPlayers = keyTable(playoffTeams), relegatPlayers = keyTable(relegatTeams)}
local tableStyle = floatStyle .. lineHeight
if gamesMatrix then
sumTxt = buildSummaryTable(teamScore, sumScores, teamsAttr, tableStyle, showDraws, titleGoals, sumKeys, linkTemplate, gamesData)
else
sumTxt = buildSummaryTable(teamScore, sumScores, teamsAttr, tableStyle, showDraws, titleGoals, sumKeys, linkTemplate)
end
end
if (hideSummary or hideGames) then
return sumTxt .. gamesTable
end
return sumTxt .. gamesTable ..'<br style="clear:both;">'
end
return {
['חשב'] = render
}