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

Module:Game List

猛汉♂百科,万男皆可猛的百科全书!转载请标注来源页面的网页链接,并声明引自猛汉百科。内容不可商用。
跳到导航 跳到搜索
Template-info.svg 模块文档  [创建] [刷新]
local p = {}
local getArgs = require('Module:Arguments').getArgs
local data = require('Module:Game List/Data')
local template_zero = mw.getCurrentFrame():expandTemplate{ title = "0" }

function split(text)
	return mw.text.split(string.gsub(text, '<br%s*/?>', '/'), '[/、]')
end

function parse_season(text)
	if string.find(text, "^[1-3]月") then
		return "1-3月"
	elseif string.find(text, "^[4-6]月") then
		return "4-6月"
	elseif string.find(text, "^[7-9]月") then
		return "7-9月"
	elseif string.find(text, "^1[0-2]月") then
		return "10-12月"
	else
		return "未定"
	end
end

function p._row(frame, args)
	-- 标题、链接
	local title = args['title'] or args[1] or "无标题"
	local link = args['link'] or title
	
	-- 开发商、发行商
	local developers = split(args['developer'] or args[2] or "")
	for i, v in ipairs(developers) do
		developers[i] = data.publishers[string.lower(v)] or v
	end
	local publishers = split(args['publisher'] or args[3] or "")
	for i, v in ipairs(publishers) do
		publishers[i] = data.publishers[string.lower(v)] or v
	end
	
	-- 发售日期
	local release_date = string.gsub(args['release_date'] or args[4] or "未定", template_zero, "")
	local y, m, d, formated_date
	y, m, d = mw.ustring.match(release_date, '(%d+)[年%-%./](%d+)[月%-%./](%d+)[日]?')
	if y then
		formated_date = mw.ustring.gsub(string.format('%04d年%02d月%02d日', y, m, d), '([年月])0', '%1{{0}}')
	else
		y, m = mw.ustring.match(release_date, '(%d+)[年%-%./](%d+)[月%-%./]')
		d = 32
		if y then
			formated_date = mw.ustring.gsub(string.format('%04d年%02d月', y, m), '([年])0', '%1{{0}}')
		else
			y, m = mw.ustring.match(release_date, '(%d+)[年%-%./]?([春夏秋冬内內初末底]?)')
			if y then
				formated_date = string.format('%04d年%s', y, m)
				m, d = 13, string.find('春夏秋冬内內初末底', m)
			else
				y, m, d = 9999, 13, 32
				formated_date = release_date
			end
		end
	end
	local date_wikitext = string.format('data-sort-value="%04d-%02d-%02d | %s', y, m, d, formated_date)
	
	-- 中文情况
	local chinese = args['chinese'] or args[5] or ""
	local chinese_wikitext = ""
	local chinese_simplified = string.find(chinese, '简') or string.find(chinese, '簡')
	local chinese_traditional = string.find(chinese, '繁')
	local chinese_available = string.find(chinese, '有')
	local chinese_not_available = string.find(chinese, '无')
	if chinese_available then
		chinese_wikitext = 'colspan="2" data-sort-value="-0.5" | 有中文'
	elseif chinese_not_available or chinese_simplified or chinese_traditional then
		chinese_wikitext = (chinese_simplified and 'data-sort-value="-1" | 简体中文' or 'data-sort-value="0" {{N/A|无}}') .. '\n| ' .. (chinese_traditional and 'data-sort-value="-1" | 繁體中文' or 'data-sort-value="0" {{N/A|无}}')
	elseif chinese == "" then
		chinese_wikitext = 'colspan="2" data-sort-value="-0.2" {{N/A|未公布}}'
	else
		chinese_wikitext = 'colspan="2" data-sort-value="0" | ' .. chinese
	end
	
	-- 备注
	local note = args['note'] or args[6] or ""
	local note_name = args['note_name'] or ""
	local note_wikitext = " {{#var_final:gl-note}}"
	if note ~= "" then
		note_wikitext = '\n| {{#tag:ref|' .. note .. '|group=注' .. (note_name ~= "" and '|name=' .. note_name or "") .. '}}'
		note_wikitext = note_wikitext .. '{{#vardefine:gl-note|{{!}}{{!}}}}{{#vardefine:gl-note-head|!! rowspan="2" {{!}} 备注}}'
	end
	
	-- 输出
	local wikitext = table.concat({
		"|-",
		"{{#ifexist:" .. link .. "|[[" .. link .. "|" .. title .. "]]|" .. title .."}}",
		table.concat(developers, "<br>"),
		table.concat(publishers, "<br>"),
		date_wikitext,
		chinese_wikitext
	}, "\n| ")
	wikitext = wikitext .. note_wikitext
	return frame:preprocess(wikitext)
end

function p.row(frame)
	return p._row(frame, getArgs(frame, {wrappers = "Template:Game List/Row"}))
end

function p._navbox(frame, args)
	local raw_wikitext = mw.title.new(args['page']):getContent()
	if raw_wikitext ~= "" then
		local last_season = ""
		local group = 0
		local navbox_args = args
		navbox_args['page'] = nil
		for content in string.gmatch(raw_wikitext, '{{%s*Game[ _]List/Row%s*\|%s*(.-)%s*}}%s*\n') do
			content = content .. "|"
			content = string.gsub(content, '{{0}}', '')
			local title = string.match(content, '^%s*(.-)%s*\|') or '无标题'
			local link = string.match(content, '\|%s*link%s*=%s*(.-)%s*\|') or title
			local date = string.match(content, '\|%s*%d+年(.-)%s*\|') or '未定'
			if string.find("春夏秋冬", date) then
				date = date .. "季"
			elseif string.find("内內初末底", date) then
				date = "年" .. date
			end
			local season = parse_season(date)
			if season ~= last_season then
				group = group + 1
				navbox_args['group' .. group] = season
				navbox_args['list' .. group] = ''
				last_season = season
			else
				navbox_args['list' .. group] = navbox_args['list' .. group] .. " • "
			end
			navbox_args['list' .. group] = navbox_args['list' .. group] .. "[[" .. ((link == title) and title or (link .. "|" .. title)) .. "]](" .. date .. ")"
		end
		return frame:expandTemplate({ title = 'Navbox', args = navbox_args})
	else
		return ""
	end
end

function p.navbox(frame)
	return p._navbox(frame, getArgs(frame))
end

return p