Module:Sports table/WDL: Difference between revisions

Content deleted Content added
add option for ppg and pts
fix inconsistent use of ranking_style=='wpct' or ranking_style=='%w' which was causing strange results when either was uses
 
(19 intermediate revisions by 3 users not shown)
Line 28:
local loss_first = Args['loss_before_draw'] or Args['loss_before_tie'] or false
-- What to rank the teams by
local ranking_style = string.lower(Args['ranking_style'] or 'pts')
local show_points, show_wperc, show_perc, show_ppg = false, false, false, false
ranking_style = string.lower(ranking_style)
if ranking_style=='w' or ranking_style=='win' or ranking_style=='wins' or ranking_style=='none' then
-- Based on wins
elseif ranking_style=='wperc' or ranking_style=='win percentage' or ranking_style=='wpct' or ranking_style=='%w' then
show_wperc = true
elseif ranking_style=='perc' or ranking_style=='percentage' or ranking_style=='%' then
-- Based on percentage
Line 90 ⟶ 91:
fa_letter = 'R'
fa_word_sing = 'Run'
fa_word_plur = 'Runs'
elseif for_against_style=='l' or for_against_style=='leg' or for_against_style=='legs' then
fa_letter = 'L'
fa_word_sing = 'Leg'
fa_word_plur = 'Legs'
elseif for_against_style=='none' then
hide_for_against = true
Line 103 ⟶ 108:
 
-- Whether to use goal percentage instead
local do_percentage = yesno(Args['use_goal_percentage'] or Args['use_against_percentage'] or Args['use_point_percentage'] or 'no')
local do_point_for_avg = yesno(Args['use_goals_per_match'] or Args['use_points_per_match'] or 'no')
Line 192 ⟶ 197:
if show_perc then
tt = p_sub.colhead(tt,36,'<abbr title="Win percentage">PCT</abbr>') -- Win percentage col
elseif show_wperc then
tt = p_sub.colhead(tt,36,'<abbr title="Win percentage">WPCT</abbr>') -- Win percentage col
end
if show_ppg then
Line 204 ⟶ 211:
table.insert(tt.tab_text,results_header_txt)
end
 
return tt
end
 
-- Numeric alignment
local function num_align(num, digits)
if tonumber(digits) and tonumber(num) then
local n = tonumber(num)
digits = tonumber(digits)
if matchesdigits == 01 then
if n >= 0 and n < 10 then
return '<span style="visibility:hidden;color:transparent;">0</span>' .. num
end
elseif digits == 2 then
if n >= 0 and n < 10 then
return '<span style="visibility:hidden;color:transparent;">00</span>' .. num
elseif lossesn >= 010 orand drawsn >< 0100 then
return '<span style="visibility:hidden;color:transparent;">0</span>' .. num
end
end
end
return num
end
 
Line 214 ⟶ 241:
local mm = require('Module:Math')
local yesno = require('Module:Yesno')
 
-- Get custom/default options for in table
local show_win = yesno(Args['show_win'] or 'yes')
Line 223 ⟶ 250:
local loss_points = tonumber(Args['losspoints']) or (show_draw and 0 or 1)
local rw_points = tonumber(Args['rwpoints']) or 1
local digits = Args['digitpad']
local total_row_name = 'SPORTS_TABLE_TOTAL'
-- Add to tallies if enabled
if yesno(Args['show_totals'] or 'no') and (team_code_ii ~= total_row_name) then
for k,v in ipairs({'win', 'draw', 'loss', 'gf', 'ga', 'adjust_points', 'startpoints'}) do
Args[v .. '_' .. total_row_name] =
(tonumber(Args[v .. '_' .. total_row_name]) or 0)
+ (tonumber(Args[v .. '_' .. team_code_ii]) or 0)
end
end
 
-- Order of draws and losses --
local loss_first = Args['loss_before_draw'] or Args['loss_before_tie'] or false
Line 239 ⟶ 277:
local s_pts = (tonumber(Args['adjust_points_'..team_code_ii]) or 0) + (tonumber(Args['startpoints_'..team_code_ii]) or 0)
local hth_local = yesno(Args['show_hth'] or 'yes') and Args['hth_'..team_code_ii] or nil
local ranking_style = string.lower(Args['ranking_style'] or 'pts')
local pct_style = string.lower(Args['pct_style'] or 'ratio')
 
-- Then calculate some values
local matches = wins + draws + losses
Line 253 ⟶ 294:
end
local win_perc = ''
--Some sports use draw as well
local ppg = ''
local win_perc = mm._precision_format((2*wins + draws) / (2*matches), 3)
if tonumber(matches) == nil or matches == 0 then
local ppg = mm._precision_format(points / matches, 2)
if matches == 0 then
-- Escape for zero matches
win_perc = '&mdash;'
ppg = '&mdash;'
else
elseif losses > 0 or draws > 0 then
--Some sports use draw as well
-- Drop the leading zero (from the string)
win_perc = string.sub(win_perc,2,string.len(win_perc)pct_style == 'ratio') and
mm._precision_format((2*wins + draws + s_pts) / (2*matches), 3) or
mm._precision_format(100*(2*wins + draws + s_pts) / (2*matches), 2)
-- Allow win_perc to be altered due to point deductions/additions
local ppg = mm._precision_format(points / matches, 2)
if (losses > 0 or draws > 0) and pct_style == 'ratio' then
-- Drop the leading zero (from the string)
win_perc = string.sub(win_perc,2,string.len(win_perc))
end
end
if ranking_style=='wperc' or ranking_style=='win percentage' or ranking_style=='wpct' or ranking_style=='%w' then
if matches == 0 then
win_perc = '&mdash;'
else
local win_perc = mm._precision_format((2100 * wins + draws) / (2*matches), 32)
end
end
 
Line 290 ⟶ 345:
else
gcomp = mm._precision_format(100 * gfor / gaig , 1)
end
elseif yesno(Args['use_against_percentage'] or 'no') then
-- Now it is the percentage
if (gfor == '?') or (gaig == '?') or gfor == 0 then
gcomp = '&mdash;'
else
gcomp = mm._precision_format(100 * gaig / gfor , 1)
end
elseif yesno(Args['use_point_percentage'] or 'no') then
Line 313 ⟶ 375:
-- Formatting with signs
if gcomp>0 then
gcomp='+'..num_align(gcomp,digits)
elseif gcomp < 0 then
gcomp='&minus;'..-num_align(gcomp,digits)
end
end
Line 337 ⟶ 399:
if GB_num >0 then
-- Indicates trailing GB_team
GB_text = num_align(GB_num,digits)
elseif GB_num == 0 then
-- Equal, do nothing to get dash
else
-- Ahead of GB team, should be noted by plus (instead of minus that comes from formula)
GB_text = '+'..num_align(-GB_num,digits)
end
end
Line 355 ⟶ 417:
-- What to rank the teams by
local rank_points, rank_perc, rank_wperc, rank_ppg = false, false, false, false
local ranking_style = Args['ranking_style'] or 'pts'
local rank_points, rank_perc, rank_ppg = false, false, false
local win_fw, win_string
ranking_style = string.lower(ranking_style)
if ranking_style=='w' or ranking_style=='win' or ranking_style=='wins' then
-- Based on wins
Line 366 ⟶ 427:
-- Based on percentage
rank_perc = true
win_fw=ii_fw
win_string = ''
elseif ranking_style=='wperc' or ranking_style=='win percentage' or ranking_style=='wpct' or ranking_style=='%w' then
-- Based on percentage
rank_wperc = true
win_fw=ii_fw
win_string = ''
Line 397 ⟶ 463:
local show_away_goals = yesno(Args['show_away_goals'] or 'no')
local show_bonus_points = yesno(Args['show_bonus_points'] or 'no')
local pts_perc_first = yesno((rank_points and Args['pts_first']) or ((rank_perc or rank_wperc) and Args['perc_first']) or 'no')
-- Row building
if (tonumber(Args['adjust_points_'..team_code_ii]) and tonumber(Args['startpoints_'..team_code_ii])) then
Line 406 ⟶ 472:
-- Add &minus; for negative point totals
if points<0 then
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | &minus;'..num_align(-points,digits)..hth_string..'\n')
else
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | '..num_align(points,digits)..hth_string..'\n')
end
end
if rank_perc then
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | '..win_perc..hth_string..'\n')
elseif rank_wperc then
table.insert(t,'| style="font-weight: bold;text-align:right;'..bg_col..'" | '..win_perc..hth_string..'\n')
end
if rank_ppg then
Line 432 ⟶ 500:
end
if show_win then
table.insert(t,'| style="'..win_fw..bg_col..'" |'..num_align(wins,digits)..win_string..'\n') -- Won
end
if loss_first then
if show_loss then
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(losses,digits)..'\n') -- Lost
end
if show_draw then
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(draws,digits)..'\n') -- Drawn
end
else
if show_draw then
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(draws,digits)..'\n') -- Drawn
end
if show_loss then
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(losses,digits)..'\n') -- Lost
end
end
Line 458 ⟶ 526:
if not hide_for_against then
if not yesno(Args['hide_for_against_columns'] or 'no') then
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(gfor,digits)..'\n') -- GF
table.insert(t,'| style="'..ii_fw..bg_col..'" |'..num_align(gaig,digits)..'\n') -- GA
end
if gcomp then
Line 472 ⟶ 540:
if show_bonus_points then
if s_pts < 0 then
table.insert(t,'| style="'..bg_col..'" | &minus;'..num_align(-s_pts,digits)..'\n')
else
table.insert(t,'| style="'..bg_col..'" | '..num_align(s_pts,digits)..'\n')
end
end
Line 481 ⟶ 549:
-- Add &minus; for negative point totals
if points<0 then
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | &minus;'..num_align(-points,digits)..hth_string..'\n')
else
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | '..num_align(points,digits)..hth_string..'\n')
end
end
if rank_perc then
table.insert(t,'| style="font-weight: bold;'..bg_col..'" | '..win_perc..hth_string..'\n')
elseif rank_wperc then
table.insert(t,'| style="font-weight: bold;text-align:right;'..bg_col..'" | '..win_perc..hth_string..'\n')
end
if rank_ppg then
Line 506 ⟶ 576:
-- ------------------------------------------------------------
local status_code, status_called = {}, {}
status_code = { A='Advance to a further round', C='ChampionChampions', D='Disqualified',
E='Eliminated', G='GuestGuests', H='HostHosts', O='Play-off winnerwinners', P='Promoted', Q='Qualified tofor the phase indicated',
R='Relegated', T='Qualified, but not yet tofor the particular phase indicated'}
local status_letters = (Args['status_order'] or '') .. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'