“模块:Standings”的版本间的差异
Onlyforyou(讨论 | 贡献) |
Onlyforyou(讨论 | 贡献) |
||
第7行: | 第7行: | ||
local video_col = 5 | local video_col = 5 | ||
local date_col = 6 | local date_col = 6 | ||
+ | |||
+ | -- .0 => .4999, .5 => .9999 | ||
+ | function expand_from_old_SR_time(score) | ||
+ | if score:len() == 6 then | ||
+ | if score:sub(-1) == '0' then | ||
+ | score = score:sub(1, -2) .. '4999' | ||
+ | elseif score:sub(-1) == '5' then | ||
+ | score = score:sub(1, -2) .. '9999' | ||
+ | end | ||
+ | end | ||
+ | return score | ||
+ | end | ||
+ | |||
+ | function score_less_SR(a, b) | ||
+ | a = expand_from_old_SR_time(a) | ||
+ | b = expand_from_old_SR_time(b) | ||
+ | return a > b | ||
+ | end | ||
+ | |||
+ | function score_less_HS(a, b) | ||
+ | return a < b | ||
+ | end | ||
+ | |||
+ | function remove_asterisk(score) | ||
+ | if score:sub(-1) == '*' then | ||
+ | score = score:sub(1, -2) | ||
+ | end | ||
+ | return score | ||
+ | end | ||
-- compare score, including HS and SR | -- compare score, including HS and SR | ||
function score_less(a, b) | function score_less(a, b) | ||
+ | a = remove_asterisk(a) | ||
+ | b = remove_asterisk(b) | ||
if string.find(a, ':') and string.find(b, ':') then | if string.find(a, ':') and string.find(b, ':') then | ||
− | return a | + | return score_less_SR(a, b) |
elseif not string.find(a, ':') and not string.find(b, ':') then | elseif not string.find(a, ':') and not string.find(b, ':') then | ||
− | return a | + | return score_less_HS(a, b) |
end | end | ||
end | end | ||
第79行: | 第110行: | ||
local record = records[i] | local record = records[i] | ||
if record[mode_col] == mode and record[level_col] == level then | if record[mode_col] == mode and record[level_col] == level then | ||
− | if highest == nil or | + | if highest == nil or score_less(highest, record[score_col]) then |
table.insert(records_new, record) | table.insert(records_new, record) | ||
highest = record[score_col] | highest = record[score_col] | ||
第92行: | 第123行: | ||
end | end | ||
− | function | + | function get_records() |
local content = mw.title.new('Ballance个人纪录列表'):getContent() | local content = mw.title.new('Ballance个人纪录列表'):getContent() | ||
local records = {} | local records = {} | ||
第113行: | 第144行: | ||
function p.standings(frame) | function p.standings(frame) | ||
local args = frame.args[1] and frame.args or frame:getParent().args; | local args = frame.args[1] and frame.args or frame:getParent().args; | ||
− | local records = select_standing( | + | local records = select_standing(get_records(), args[1], args[2]) |
local record_last = nil | local record_last = nil | ||
local ranking = 0 | local ranking = 0 | ||
第142行: | 第173行: | ||
function p.historical_WRs(frame) | function p.historical_WRs(frame) | ||
local args = frame.args[1] and frame.args or frame:getParent().args; | local args = frame.args[1] and frame.args or frame:getParent().args; | ||
− | local records = select_WR( | + | local records = select_WR(get_records(), args[1], args[2]) |
local date_until = 0 | local date_until = 0 | ||
local scire_until = 0 | local scire_until = 0 |
2022年11月18日 (五) 23:45的版本
此模块的文档可以在模块:Standings/doc创建
local p = {} --p stands for package local mode_col = 1 local level_col = 2 local score_col = 3 local name_col = 4 local video_col = 5 local date_col = 6 -- .0 => .4999, .5 => .9999 function expand_from_old_SR_time(score) if score:len() == 6 then if score:sub(-1) == '0' then score = score:sub(1, -2) .. '4999' elseif score:sub(-1) == '5' then score = score:sub(1, -2) .. '9999' end end return score end function score_less_SR(a, b) a = expand_from_old_SR_time(a) b = expand_from_old_SR_time(b) return a > b end function score_less_HS(a, b) return a < b end function remove_asterisk(score) if score:sub(-1) == '*' then score = score:sub(1, -2) end return score end -- compare score, including HS and SR function score_less(a, b) a = remove_asterisk(a) b = remove_asterisk(b) if string.find(a, ':') and string.find(b, ':') then return score_less_SR(a, b) elseif not string.find(a, ':') and not string.find(b, ':') then return score_less_HS(a, b) end end -- sort by mode, level, score and date function standing_less(a, b) if a[mode_col] ~= b[mode_col] then return a[mode_col] < b[mode_col] elseif a[level_col] ~= b[level_col] then return a[level_col] < b[level_col] elseif a[score_col] ~= b[score_col] then return not score_less(a[score_col], b[score_col]) else return a[date_col] < b[date_col] end end -- sort by mode, level, name and score function unique_less(a, b) if a[mode_col] ~= b[mode_col] then return a[mode_col] < b[mode_col] elseif a[level_col] ~= b[level_col] then return a[level_col] < b[level_col] elseif a[name_col] ~= b[name_col] then return a[name_col] < b[name_col] else return not score_less(a[score_col], b[score_col]) end end function unique(records) local records_new = {} local record_last = nil table.sort(records, unique_less) for key, record in ipairs(records) do if record_last == nil then table.insert(records_new, record) elseif record[mode_col] ~= record_last[mode_col] or record[level_col] ~= record_last[level_col] or record[name_col] ~= record_last[name_col] then table.insert(records_new, record) end record_last = record end return records_new end function select_standing(records, mode, level) local records_new = {} for key, record in ipairs(records) do if record[mode_col] == mode and record[level_col] == level then table.insert(records_new, record) end end records_new = unique(records_new) table.sort(records_new, standing_less) return records_new end function select_WR(records, mode, level) local records_new = {} local highest = nil table.sort(records, standing_less) for i = #records, 1, -1 do local record = records[i] if record[mode_col] == mode and record[level_col] == level then if highest == nil or score_less(highest, record[score_col]) then table.insert(records_new, record) highest = record[score_col] end end end return records_new end function trim(s) return s:match'^()%s*$' and '' or s:match'^%s*(.*%S)' end function get_records() local content = mw.title.new('Ballance个人纪录列表'):getContent() local records = {} for line in content:gmatch('[^\r\n]+') do if string.sub(line, 1, string.len('| ')) == '| ' then line = line:sub(2) record = {} for element in line:gmatch("[^|]+") do if element ~= '' then element = trim(element) table.insert(record, element) end end table.insert(records, record) end end return records end function p.standings(frame) local args = frame.args[1] and frame.args or frame:getParent().args; local records = select_standing(get_records(), args[1], args[2]) local record_last = nil local ranking = 0 local ranking_hold = 0 local highest = nil local wikitable_str = '{| class="wikitable" style="display: inline-block; vertical-align: top"\n' wikitable_str = wikitable_str .. '|+ <h3> 关卡 ' .. args[2] .. '</h3>\n' wikitable_str = wikitable_str .. '! 名次 !! 分数 !! 纪录持有者 !! 视频链接\n' for key, record in ipairs(records) do ranking = ranking + 1 if highest == nil or highest ~= record[score_col] then ranking_hold = ranking end wikitable_str = wikitable_str .. '|-\n' wikitable_str = wikitable_str .. '|' .. ranking_hold wikitable_str = wikitable_str .. '||' .. record[score_col] wikitable_str = wikitable_str .. '||' .. record[name_col] wikitable_str = wikitable_str .. '||' .. record[video_col] wikitable_str = wikitable_str .. '\n' highest = record[score_col] end wikitable_str = wikitable_str .. '|}\n' return wikitable_str end function p.historical_WRs(frame) local args = frame.args[1] and frame.args or frame:getParent().args; local records = select_WR(get_records(), args[1], args[2]) local date_until = 0 local scire_until = 0 local name_until = 0 local wikitable_str = '{| class="wikitable" style="display: inline-block; vertical-align: top"\n' wikitable_str = wikitable_str .. '|+ <h3> 关卡 ' .. args[2] .. '</h3>\n' wikitable_str = wikitable_str .. '! 时间 !! 纪录分数 !! 纪录持有者 !! 视频链接\n' for key, record in ipairs(records) do wikitable_str = wikitable_str .. '|-\n' wikitable_str = wikitable_str .. '|' .. record[date_col] wikitable_str = wikitable_str .. '||' .. record[score_col] wikitable_str = wikitable_str .. '||' .. record[name_col] wikitable_str = wikitable_str .. '||' .. record[video_col] wikitable_str = wikitable_str .. '\n' end wikitable_str = wikitable_str .. '|}\n' mw.log(wikitable_str) return wikitable_str end return p