置顶公告:【置顶】关于临时开启评论区所有功能的公告(2022.10.22) | 【置顶】关于本站Widget恢复使用的公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:TodayBirthday

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [查看] [编辑] [历史] [刷新]

这是一个用于显示某个主题中在今天以及近期过生日的角色的模块,其封装模板为{{TodayBirthday}}。

调用方法

使用{{#invoke:TodayBirthday|main|数据模块名|角色称呼|最近生日天数|设置当前时间}}即可调用模块,各参数说明如下:

  • 数据模块名:存放在Module:TodayBirthday/data/下的数据模块名称,不需要写前缀。
  • 角色称呼:输出语句中对角色的称呼,可省略,默认为“角色”。
  • 最近生日天数:设置显示最近多少天内过生日的角色,设为0为不显示,默认为0。
  • 设置当前时间:给定计算生日的“当天”时间,测试用,在封装模板中不提供此参数,默认为今天。

示例

  • 调用{{#invoke:TodayBirthday|main|赛马娘|赛马娘|7}}将显示:

7天内没有赛马娘过生日。

  • 调用{{#invoke:TodayBirthday|main|赛马娘|赛马娘|7|2021-5-1}}将显示:

无声铃鹿狂怒乐章生日快乐!7天内过生日的赛马娘有:特别周(5月2日)骏川手纲(5月2日)成田白仁(5月3日)谷野美酒(5月4日)目白多伯(5月6日)稻荷一(5月7日)待兼诗歌剧(5月7日)森林宝穴(5月7日)樱花桂冠(5月8日)

使用更多数据

模块通过变量扩展和{{array}}输出了更多可用的数据,可以用于更多定制功能(以下所有示例均为上面{{#invoke:TodayBirthday|main|赛马娘|赛马娘|7|2021-5-1}}的输出结果):

  • {{#var:tbirth_today_raw}}(不带描述信息的今天过生日的角色列表):无声铃鹿狂怒乐章
  • {{#var:tbirth_recent_raw}}(不带描述信息的最近过生日的角色列表):特别周(5月2日)骏川手纲(5月2日)成田白仁(5月3日)谷野美酒(5月4日)目白多伯(5月6日)稻荷一(5月7日)待兼诗歌剧(5月7日)森林宝穴(5月7日)樱花桂冠(5月8日)
  • {{#var:tbirth_count}}(今天和最近过生日的角色总数):11
  • {{#var:tbirth_count_today}}(今天过生日人数):2
  • {{#var:tbirth_count_recent}}(最近过生日人数):9


  • {{array|get|tbirth_today|1}}(今天过生日角色数组):无声铃鹿
  • {{array|get|tbirth_recent|1}}(最近过生日角色数组):特别周

使用方法

使用以下按钮创建一个主题的角色数据模块:

以下为目前已创建的角色数据模块,请注意不要重复创建(点击展开):

数据填写说明

使用以上方式创建新模块后会有如下代码:

local p = {}

p.data={
	{
        "",--填写显示名称
        "",--填写内部链接
        1,1--填写生日,形式:月,日
    },
    {--以同样格式添加下一个数据
        "",
        "",
        1,1
    },
}

return p

需要填写的是使用--注释的三行(即5-7行),说明如下:

  • 在第一行填写角色显示名称,支持html,但建议将第一行的字符串改为用单引号'包裹,或者将双引号"使用反斜杠\转义,写为\";并且如果这么做第二行必须要填写内部链接。
  • 显示图片:受wiki内链生成机制限制,图片不能以[[File:]]的形式插入,请使用<img />语法插入图片
  • 第二行填写内部链接,若与显示名称相同则可省略,不需要写[[]]
  • 第三行填写生日,以月,日的形式。注意分隔符为半角逗号,

填写示例:

    {
        "特别周",
        "",
        5,2
    },

使用html的填写示例:

    {
        '<span style="color:#EE6ECB">特别周</span>',
        "特别周",
        5,2
    },

然后在下一行填写新的角色数据即可。

local p = {}

local array = require("Module:Var-array") --调用array

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]:指定当前日期

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

    --通过变量扩展输出信息
    frame:callParserFunction("#vardefine:tbirth_today_raw",today_out)
    frame:callParserFunction("#vardefine:tbirth_recent_raw",recent_out)
    frame:callParserFunction("#vardefine:tbirth_count",count_recent+count_today)
    frame:callParserFunction("#vardefine:tbirth_count_today",count_today)
    frame:callParserFunction("#vardefine:tbirth_count_recent",count_recent)
    
    --通过array输出信息
    array.new("tbirth_today",today)
    array.new("tbirth_recent",recent_aout)

    --处理输出
    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
    return out
end


return p