モジュール:MemberStats
表示
このモジュールについての説明文ページを モジュール:MemberStats/doc に作成できます
local p = {}
function p.getStats(frame)
local mode = frame.args[1]
local targetBlood = frame.args['blood']
local title = mw.title.getCurrentTitle()
local content = title:getContent()
if not content then return "" end
local todayYear = tonumber(frame:preprocess("{{CURRENTYEAR}}"))
local todayMonth = tonumber(frame:preprocess("{{CURRENTMONTH}}"))
local todayDay = tonumber(frame:preprocess("{{CURRENTDAY}}"))
local totalAge = 0
local count = 0
for row in content:gmatch("{{MemberRow%s*(.-)}}") do
local birthStr = row:match("生年月日順%s*=%s*(%d%d%d%d%d%d%d%d)")
-- 【修正ポイント】漢字や「?」なども含めて、次の「|」や「}」が来るまでを読み取るようにしました
local bloodType = row:match("血液型%s*=%s*([^%s|%}]+)")
if bloodType then
bloodType = bloodType:gsub("型", "") -- 「型」があれば取って比較しやすくする
end
if birthStr then
local matchBlood = true
if targetBlood and targetBlood ~= "" then
-- ターゲットから「型」を抜いて比較(AでもA型でも一致するように)
local cleanTarget = targetBlood:gsub("型", "")
if bloodType ~= cleanTarget then
matchBlood = false
end
end
if matchBlood then
local birth = tonumber(birthStr)
local bYear = math.floor(birth / 10000)
local bMonth = math.floor((birth % 10000) / 100)
local bDay = birth % 100
local age = todayYear - bYear
if todayMonth < bMonth or (todayMonth == bMonth and todayDay < bDay) then
age = age - 1
end
totalAge = totalAge + age
count = count + 1
end
end
end
if count == 0 then
if targetBlood and targetBlood ~= "" then
return "" -- 0名なら空を返す(#if用)
else
return "0"
end
end
if mode == "count" then
return tostring(count)
elseif mode == "average" then
local avg = totalAge / count
return string.format("%.2f", avg + 0.0000000001)
end
end
-- ========================================================================
-- 関数名: countSai
-- 用途: テンプレート(MemberRow)を使用していない「歴代ハロプロ研修生・エッグ」ページ専用
-- 内容: ページ内の「歳」という文字列をカウントして総人数(行数)を返す
-- ========================================================================
function p.countSai(frame)
local title = mw.title.getCurrentTitle()
local content = title:getContent()
if not content then return "0" end
-- ページ全体の「歳」の出現回数をカウント
local _, count = content:gsub("歳", "")
return tostring(count)
end
-- ここまで
return p