跳转到内容

模組:CCHPaward

本页使用了标题或全文手工转换
维基百科,自由的百科全书

require("strict")

local getArgs = require("Module:Arguments").getArgs

local RECORD_MODULE = "Module:CCHPaward/list"

local function load_record()
	return mw.loadData(RECORD_MODULE)
end

local function fetch_username(user)
	if user == nil or user == "" then
		return mw.title.getCurrentTitle().baseText
	end
	return user
end

local function number(value)
	return tonumber(value) or 0
end

local function user_scores(record, username)
	local data = record[username] or {}
	return {
		article = number(data.article),
		image = number(data.image),
		review = number(data.review),
		project = number(data.project),
	}
end

-- X1条目分、X2图片分、X3评审分、X4站务分分别沿用1、0.2、0.4、1权重。
local function calculate_scores(article, image, review, project)
	local total_score = number(article) + number(image) * 0.2 + number(review) * 0.4 + number(project)
	local level = math.floor(total_score / 10)
	return level, total_score
end

local p = {}

function p.level(frame)
	local args = getArgs(frame)
	local username = fetch_username(args.user)
	local scores = user_scores(load_record(), username)
	local level = calculate_scores(scores.article, scores.image, scores.review, scores.project)
	return tostring(level)
end

function p.total_score(frame)
	local args = getArgs(frame)
	local username = fetch_username(args.user)
	local scores = user_scores(load_record(), username)
	local _, total_score = calculate_scores(scores.article, scores.image, scores.review, scores.project)
	local rounded_score = math.floor(total_score * 10 + 0.5) / 10
	return tostring(rounded_score)
end

function p.project_score(frame)
	local args = getArgs(frame)
	local username = fetch_username(args.user)
	return tostring(user_scores(load_record(), username).project)
end

function p._ranking()
	local record = load_record()
	local user_list = {}
	for username in pairs(record) do
		local scores = user_scores(record, username)
		local level, total_score = calculate_scores(scores.article, scores.image, scores.review, scores.project)
		table.insert(user_list, {
			username = username,
			level = level,
			total_score = total_score,
			article = scores.article,
			image = scores.image,
			review = scores.review,
			project = scores.project,
		})
	end
	table.sort(user_list, function(a, b)
		if a.total_score == b.total_score then
			return a.username < b.username
		end
		return a.total_score > b.total_score
	end)

	local lines = {
		'{| class="wikitable sortable"',
		'! 排名 !! 用户名 !! 等级 !! 总分 !! 条目分 !! 图片分 !! 评审分 !! 站务分',
	}
	for i, user in ipairs(user_list) do
		table.insert(lines, "|-")
		table.insert(lines, string.format(
			"| %d || [[User:%s|%s]] || %d级 || %.1f || %.1f || %.1f || %.1f || %.1f",
			i,
			user.username,
			user.username,
			user.level,
			user.total_score,
			user.article,
			user.image,
			user.review,
			user.project
		))
	end
	table.insert(lines, "|}")
	return table.concat(lines, "\n")
end

function p.ranking()
	return p._ranking()
end

function p.count_above_level(frame)
	local args = getArgs(frame)
	local level_threshold = tonumber(args[1]) or 0
	local record = load_record()
	local count = 0
	for username in pairs(record) do
		local scores = user_scores(record, username)
		local level = calculate_scores(scores.article, scores.image, scores.review, scores.project)
		if level >= level_threshold then
			count = count + 1
		end
	end
	return tostring(count)
end

return p