打开主菜单

Ballance Wiki β

模块:Standings

Onlyforyou讨论 | 贡献2022年11月14日 (一) 23:03的版本 (创建页面,内容为“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 -- compare…”)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

此模块的文档可以在模块: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

-- compare score, including HS and SR
function score_less(a, b)
	if string.find(a, ':') and string.find(b, ':') then
		return a > b
	elseif not string.find(a, ':') and not string.find(b, ':') then
		return 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
	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_sorted_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_sorted_records(), '常规HS', '01')
	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_sorted_records(), args[1], args[2])
-- end

return p