这是一个用于显示某个主题中在今天以及近期过生日的角色的模块,其封装模板为{{TodayBirthday}}。
使用{{#invoke:TodayBirthday|main|数据模块名|角色称呼|最近生日天数|设置当前时间}}即可调用模块,各参数说明如下:
Module:TodayBirthday/data/下的数据模块名称,不需要写前缀。{{#invoke:TodayBirthday|main|赛马娘|赛马娘|7}}将显示:7天内没有赛马娘过生日。
{{#invoke:TodayBirthday|main|赛马娘|赛马娘|7|2021-5-1}}将显示:祝无声铃鹿、狂怒乐章生日快乐!7天内过生日的赛马娘有:特别周(5月2日)、爱如往昔(5月2日)、骏川手纲(5月2日)、金镇之光(5月3日)、速度象征(5月3日)、成田白仁(5月3日)、谷野美酒(5月4日)、目白多伯(5月6日)、待兼诗歌剧(5月7日)、稻荷一(5月7日)、森林宝穴(5月7日)、樱花桂冠(5月8日)。
使用以下按钮创建一个主题的角色数据模块:
使用以上方式创建新模块后会有如下代码:
local p = {}
p.data={
{
"", --填写显示名称
"", --填写内部链接
1,1 --填写生日,形式:月,日
},
{ --以同样格式添加下一个数据
"",
"",
1,1
},
}
return p
需要填写的是使用--注释的三行(即5-7行),说明如下:
'包裹,或者将双引号"使用反斜杠\转义,写为\";并且如果这么做第二行必须要填写内部链接。[[File:]]的形式插入,请使用<img />语法插入图片[[和]]。月,日的形式。注意分隔符为半角逗号,。填写示例:
{
"特别周",
"",
5,2
},
使用html的填写示例:
{
'<span style="color:#EE6ECB">特别周</span>',
"特别周",
5,2
},
然后在下一行填写新的角色数据即可。
local p = {}
local config = {
name = "角色",
recent = 0
}
local data = {}
--接近生日排序函数
function birth_comp(a, b)
return a[2] < b[2]
end
--frame.args[1]:数据模块名称
--frame.args[2]:角色称呼
--frame.args[3]:最近生日天数
--frame.args[4]:指定当前日期
--frame.args['count']:是否返回最近过生日人数
function p.main(frame)
--变量定义
local lang = mw.language.new("zh") --语言库
local today = {} --今天过生日
local recent = {} --最近过生日
local recent_aout = {}
local today_out = ""
local recent_out = ""
local year = os.date("%Y") --年份
local now = "" -- 当前时间
local count_today = 0 --今天过生日人数
local count_recent = 0 --最近过生日人数
-- 处理参数和默认值
if frame.args[1] then
local data_module = require("Module:TodayBirthday/data/" .. frame.args[1])
data = data_module.data
else
return '<span class="scribunto-error">请指定数据模块名!</span>'
end
if frame.args[2] then
config.name = frame.args[2]
end
if tonumber(frame.args[3]) then
config.recent = tonumber(frame.args[3])
end
if frame.args[4] then
now = lang:formatDate("U", frame.args[4], true)
year = lang:formatDate("Y", frame.args[4], true)
else
now = lang:formatDate("U", "", true)
end
--计算是否达到/接近生日
for k, v in ipairs(data) do
local birth = lang:formatDate("U", year .. "-" .. v[3] .. "-" .. v[4] .. "-8hour", true)
local birth_next = lang:formatDate("U", year .. "-" .. v[3] .. "-" .. v[4] .. "-8hour+1 year", true)
local link = ""
if v[2] == "" then
link = "[[" .. v[1] .. "]]"
else
link = "[[" .. v[2] .. "|" .. v[1] .. "]]"
end
if (now - birth >= 0 and now - birth < 86400) or (now - birth_next >= 0 and now - birth_next < 86400) then
table.insert(today, link)
count_today = count_today + 1
end
if config.recent > 0 then
if birth - now > 0 and birth - now <= 86400 * config.recent then
table.insert(recent, {link, birth - now, v[3], v[4]})
table.insert(recent_aout, link)
elseif birth_next - now > 0 and birth_next - now <= 86400 * config.recent then
table.insert(recent, {link, birth_next - now, v[3], v[4]})
table.insert(recent_aout, link)
end
end
end
--生成今天生日字符串
for k, v in ipairs(today) do
if today_out == "" then
today_out = v
else
today_out = today_out .. "、" .. v
end
end
--处理接近生日顺序
if config.recent > 0 then
table.sort(recent, birth_comp)
for k, v in ipairs(recent) do
if recent_out == "" then
recent_out = v[1] .. "<small>(" .. v[3] .. "月" .. v[4] .. "日)</small>"
else
recent_out = recent_out .. "、" .. v[1] .. "<small>(" .. v[3] .. "月" .. v[4] .. "日)</small>"
end
count_recent = count_recent + 1
end
end
--处理输出
local out = ""
if count_today == 0 then
out = ""
else
out = "祝" .. today_out .. "生日快乐!"
end
if config.recent > 0 then
if recent_out == "" then
out = out .. config.recent .. "天内没有" .. config.name .. "过生日。"
else
out = out .. config.recent .. "天内过生日的" .. config.name .. "有:" .. recent_out .. "。"
end
else
out = out .. "。"
end
if frame.args['count'] and frame.args['count'] ~= '' then
return count_recent+count_today -- 返回最近过生日人数
else
return out
end
end
return p