「Module:MemberStats」の版間の差分
表示
ページの作成:「local p = {} function p.averageAge(frame) local args = frame.args local totalAge = 0 local count = 0 -- 今日の日付を取得 (YYYYMMDD) local today = tonumber(os.date("%Y%m%d")) -- 引数から生年月日をスキャン for _, birthStr in ipairs(args) do local birth = tonumber(birthStr) if birth then -- 年齢計算ロジック: floor((今日 - 誕生日) / 10000) local age = math.fl…」 |
編集の要約なし |
||
| (同じ利用者による、間の2版が非表示) | |||
| 1行目: | 1行目: | ||
local p = {} | local p = {} | ||
function p. | function p.getStats(frame) | ||
local | local mode = frame.args[1] -- "count" か "average" | ||
-- 現在のページのソースコードを取得 | |||
local title = mw.title.getCurrentTitle() | |||
local content = title:getContent() | |||
if not content then return "ページ内容が取得できません" end | |||
-- 今日の日付 | |||
local todayStr = frame:preprocess("{{CURRENTYEAR}}{{CURRENTMONTH}}{{CURRENTDAY}}") | |||
local today = tonumber(todayStr) | |||
local totalAge = 0 | local totalAge = 0 | ||
local count = 0 | local count = 0 | ||
-- | -- ページ内から「生年月日順=8桁数字」をすべて抽出して計算 | ||
for birthStr in content:gmatch("生年月日順%s*=%s*(%d%d%d%d%d%d%d%d)") do | |||
local birth = tonumber(birthStr) | local birth = tonumber(birthStr) | ||
if birth then | if birth then | ||
local age = math.floor((today - birth) / 10000) | local age = math.floor((today - birth) / 10000) | ||
totalAge = totalAge + age | totalAge = totalAge + age | ||
| 19行目: | 25行目: | ||
end | end | ||
end | end | ||
if count == 0 then return "0" end | if count == 0 then return "0" end | ||
if mode == "count" then | |||
return count | |||
else | |||
return string.format("%.2f", totalAge / count) | |||
end | end | ||
end | end | ||
return p | return p | ||
2026年2月20日 (金) 11:13時点における最新版
local p = {}
function p.getStats(frame)
local mode = frame.args[1] -- "count" か "average" -- 現在のページのソースコードを取得 local title = mw.title.getCurrentTitle() local content = title:getContent() if not content then return "ページ内容が取得できません" end
-- 今日の日付
local todayStr = frame:preprocess("2026041")
local today = tonumber(todayStr)
local totalAge = 0 local count = 0
-- ページ内から「生年月日順=8桁数字」をすべて抽出して計算
for birthStr in content:gmatch("生年月日順%s*=%s*(%d%d%d%d%d%d%d%d)") do
local birth = tonumber(birthStr)
if birth then
local age = math.floor((today - birth) / 10000)
totalAge = totalAge + age
count = count + 1
end
end
if count == 0 then return "0" end
if mode == "count" then
return count
else
return string.format("%.2f", totalAge / count)
end
end
return p