「Module:MemberStats」の版間の差分
表示
編集の要約なし |
編集の要約なし |
||
| 1行目: | 1行目: | ||
local p = {} | local p = {} | ||
-- | -- メンバーの誕生日を一時的に蓄積するテーブル | ||
local function | local birthDates = {} | ||
local frame | |||
-- 表の各行(MemberRow)から誕生日を受け取って蓄積する関数 | |||
function p.addMember(frame) | |||
local birth = frame.args[1] or frame.args['生年月日順'] | |||
if birth and tonumber(birth) then | |||
table.insert(birthDates, tonumber(birth)) | |||
end | |||
return "" -- 画面には何も表示しない | |||
end | end | ||
function p. | -- 蓄積されたデータから統計を出すメイン関数 | ||
local | function p.getStats(frame) | ||
local mode = frame.args[1] -- "count" か "average" | |||
local totalAge = 0 | local totalAge = 0 | ||
local count = | local count = #birthDates | ||
local today = | |||
local todayStr = frame:preprocess("{{CURRENTYEAR}}{{CURRENTMONTH}}{{CURRENTDAY}}") | |||
local today = tonumber(todayStr) | |||
if | if count == 0 then return "0" end | ||
for _, | for _, birth in ipairs(birthDates) do | ||
local | local age = math.floor((today - birth) / 10000) | ||
totalAge = totalAge + age | |||
end | end | ||
if | if mode == "count" then | ||
return count | |||
elseif mode == "average" then | |||
return string.format("%.2f", totalAge / count) | |||
end | end | ||
end | end | ||
return p | return p | ||
2026年2月20日 (金) 11:05時点における版
local p = {}
-- メンバーの誕生日を一時的に蓄積するテーブル local birthDates = {}
-- 表の各行(MemberRow)から誕生日を受け取って蓄積する関数 function p.addMember(frame)
local birth = frame.args[1] or frame.args['生年月日順']
if birth and tonumber(birth) then
table.insert(birthDates, tonumber(birth))
end
return "" -- 画面には何も表示しない
end
-- 蓄積されたデータから統計を出すメイン関数 function p.getStats(frame)
local mode = frame.args[1] -- "count" か "average"
local totalAge = 0
local count = #birthDates
local todayStr = frame:preprocess("2026041")
local today = tonumber(todayStr)
if count == 0 then return "0" end
for _, birth in ipairs(birthDates) do
local age = math.floor((today - birth) / 10000)
totalAge = totalAge + age
end
if mode == "count" then
return count
elseif mode == "average" then
return string.format("%.2f", totalAge / count)
end
end
return p