Module:VOCALOID殿堂曲/Random Song
来自Vocawiki
更多操作
此模块的文档可以在Module:VOCALOID殿堂曲/Random Song/doc创建
local module = {}
local getArgs = require('Module:Arguments').getArgs
local colorBlock = require('Module:ColorBlock')
local var = require('Module:Variables')
function getRandomNumber(max)
local max = tonumber(max)
local ran = math.random(1, max) + tonumber(os.time())
return ran % max
end
function random_count(count, max, content_table)
local elected_indexes = {}
local result = {}
if count > max then count = max end
while #elected_indexes < count do
local index = getRandomNumber(max) + 1
local already_selected = false
for _, v in ipairs(elected_indexes) do
if v == index then
already_selected = true
break
end
end
if not already_selected then
elected_indexes[#elected_indexes + 1] = index
end
end
for _, index in ipairs(elected_indexes) do
result[#result + 1] = content_table[index]
end
return table.concat(result, "\n")
end
function module.temple(args, frame)
local titleName = args['page'] or ''
local title = mw.title.new(titleName)
if not title.exists then
return error("指定页面不存在")
end
local content_before = title:getContent()
local _, total_count_ts = string.gsub(content_before, "[Tt]emple[ _]Song", "")
local _, total_count_sh = string.gsub(content_before, "[Ss]ong[ _]Honor", "") -- 合并后移除
local total_count = total_count_ts + total_count_sh;
local content_table = {}
local pattern_ts = "{{[Tt]emple[ _]Song.-|%s*image.-}}\n"
local pattern_sh = "{{[Ss]ong[ _]Honor.-|%s*image.-}}\n" -- 合并后移除
for i = 1, total_count_ts, 1 do
local card = string.match(content_before, pattern_ts)
content_before = string.gsub(content_before, pattern_ts, "", 1)
content_table[i] = card
end
-- 合并后移除
for i = 1, total_count_sh, 1 do
local card = string.match(content_before, pattern_sh)
content_before = string.gsub(content_before, pattern_sh, "", 1)
content_table[i] = card
end
local count = tonumber(args['count']) or 6
local result = random_count(count, total_count, content_table)
-- 收集颜色信息,与[[Module:ColorBlock]]耦合
local colorList = colorBlock.content2ColorList(result)
local colors = var.getPlain("temple-colors")..table.concat(colorList)
colorList = colorBlock.content2ColorList(colors, true)
var.set("temple-colors", table.concat(colorList))
var.set("temple-colors-text", colorBlock.colorList2Text(colorList, frame))
--local content = frame:preprocess(content_before)
return frame:preprocess(result)
end
function module.templemain(frame)
local args = getArgs(frame)
return module.temple(args, frame)
end
return module