Module:Item

From The Remnant 2 Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:Item/doc

local core = require('Module:Game/core')
--local m_cargo = require('Module:CargoLib')
local m_types = require('Module:TypesLib')

local cfg = mw.loadData('Module:Item/config')
local i18n = cfg.i18n

-- --------------------------------------------------
-- Builder functions
-- --------------------------------------------------
local b = {}

function b.build_header(t_args)
	local header_container = mw.html.create('div'):addClass('infobox-header box-lines'):attr('style', '--bg:#0a0a0a;--lines:#111')
	header_container
			:tag('div'):addClass('infobox-name'):wikitext(t_args.name):done()
			:tag('div'):addClass('infobox-class'):wikitext(cfg.i18n.classes[t_args.class_id] or t_args.class):done()
			:done()
	return header_container
end

function b.build_mainstats(t_args)
	local mainstats_container = mw.html.create('ul'):addClass('infobox-mainstats box-lines'):attr('style', '--bg:#1d1d1d;--lines:#242424')
	local mainstat_params = { 'damage', 'rps', 'magazine', 'armor', 'weight' }
	
	local i = 0
	for _, key in ipairs(mainstat_params) do
		if (t_args[key] ~= nil) then
			mainstats_container:tag('li')
					:tag('span'):addClass('infobox-mainstats-label'):wikitext(i18n.labels[key]):done()
					:tag('span'):addClass('infobox-mainstats-value'):wikitext(t_args[key]):done()
					:done()
					
			i = i + 1
		end
	end
	
	if i > 0 then
		return mainstats_container
	end
end

function b.build_resistances(t_args)
	local resistances_container = mw.html.create('ul'):addClass('infobox-resistances box-lines'):attr('style', '--bg:#2c2c2c;--lines:#2f2f2f')
	local resistance_params = { 'bleed_res', 'fire_res', 'shock_res', 'corrosive_res', 'blight_res' }
	
	local i = 0
	for _, key in ipairs(resistance_params) do
		if (t_args[key] ~= nil) then
			resistances_container:tag('li')
				:tag('span'):wikitext(string.format('[[File:%s|link=|alt=%s|32px]]', core.icons.resistances[key], i18n.tooltips[key])):done()
				:tag('span'):addClass('infobox-resistances-value'):wikitext(t_args[key]):done()
				:done()
				
			i = i + 1
		end
	end
	
	if i > 0 then
		return resistances_container
	end
end

function b.build_substats(t_args)
	local substats_container = mw.html.create('ul'):tag('ul'):addClass('infobox-substats box-lines'):attr('style', '--bg:#2a2a2a;--lines:#2c2c2c')
	local substat_params = { 'accuracy', 'ideal', 'falloff', 'ammo', 'crit', 'weakspot', 'stagger' }
	
	local i = 0
	for _, key in ipairs(substat_params) do
		if (t_args[key] ~= nil) then
			local li = substats_container:tag('li')
			
			local tooltip = i18n.tooltips[key]
			if (tooltip ~= nil) then
				li:tag('abbr'):addClass('infobox-substats-label'):attr('title', tooltip):wikitext(i18n.labels[key]):done()
			else
				li:tag('span'):addClass('infobox-substats-label'):wikitext(i18n.labels[key]):done()
			end
			
			if (key == 'accuracy') then
				li:tag('span'):addClass('infobox-substats-bar')
					:tag('span'):addClass('infobox-substats-accuracy box-lines'):attr('style', string.format('width:%s%%;--bg:#c9c9c9;--lines:#dadada', t_args[key])):done()
					:done()
			else
				local value = t_args[key]
				if key == 'ideal' or key == 'falloff' then
					value = value .. i18n.units.meters
				elseif key == 'weakspot' then
					if tonumber(value) >= 0 then
						value = '+' .. value .. '%'
					else
						value = value .. '%'
					end
				elseif key == 'crit' or key == 'stagger' then
					value = value .. '%'
				end
				
				li
					:tag('hr'):done()
					:tag('span'):addClass('infobox-substats-value'):wikitext(value):done()
			end
			i = i + 1
		end
	end
	
	if i > 0 then
		return substats_container
	end
end

function b.build_mod(t_args)
	return mw.html.create('div'):addClass('infobox-attachment'):attr('style', '--color:#204053bb')
		:wikitext(string.format('[[File:%s.png|link=%s|64px]]', t_args.mod, t_args.mod))
		:tag('div')
			:tag('div'):addClass('infobox-attachment-name'):wikitext(t_args.mod):done()
			:tag('div'):addClass('infobox-attachment-description'):wikitext(t_args.mod_description):done() 
			:done()
		:done()
end

-- --------------------------------------------------
-- Subroutines
-- --------------------------------------------------
local s = {}

function s.parse_args(t_args)
	t_args.test = m_types.string_to_bool(t_args.test)
	t_args.ignore_default_cats = m_types.string_to_bool(t_args.ignore_default_cats)
	
	if t_args.test == false then
		local formatted_class = t_args.class_id or string.lower(string.gsub(t_args.class, "%s+", "_"))
		if (t_args.cats ~= nil) then
			t_args.cats = mw.text.split(t_args.cats, ',')
		else
			t_args.cats = {}
		end

		if t_args.ignore_default_cats == false then
			for _, cat in ipairs(cfg.i18n.cats[formatted_class] or {}) do
				table.insert(t_args.cats, 1, cat)
			end
		end
	end
	
	return t_args
end

function s.make_infobox(t_args)
	local container = mw.html.create('span')
	container:addClass('infobox')
	
	-- Header
	container:node(b.build_header(t_args)):done()
	
	-- Image
	container:tag('div'):addClass('infobox-image diamond-lines'):wikitext(t_args.image or string.format('[[File:%s.png|%s|x100px]]', t_args.name, t_args.name)):done()
	
	-- Stat tables
	container
		:node(b.build_mainstats(t_args))
		:node(b.build_resistances(t_args))
		:node(b.build_substats(t_args))
		:done()
	
	-- Description
	if (t_args.description ~= nil and string.len(t_args.description) > 0) then
		container:tag('div'):addClass('infobox-description'):wikitext(t_args.description):done()
	end
	
	-- Attachments
	if (t_args.mod ~= nil and string.len(t_args.mod) > 0) then
		container:node(b.build_mod(t_args)):done()
	end
	
	-- End gap
	container:tag('div'):addClass('infobox-end box-lines'):attr('style', '--bg:#0a0a0a;--lines:#111'):done()
	
	return container
end

function s.add_categories(t_args)
	local cats = {}
	for _, cat in ipairs(t_args.cats) do
		table.insert(cats, string.format('[[Category:%s]]', cat))
	end
	
	return table.concat(cats)
end

-- Store item into cargo tables
function s.store_item(t_args)
	local tables = cfg.tables[t_args.class_id] or cfg.tables.default
	
	for _, table_name in ipairs(tables) do
		local fields = cfg[table_name .. '_table'] or {}
		local store_data = { _table = table_name }
	
		for _, item in ipairs(fields) do
			store_data[item.field] = t_args[item.field]
		end

		if t_args.test == false then
			m_cargo.store(store_data)
		elseif t_args.test == true then
			mw.logObject(store_data)
		end
	end
end

-- --------------------------------------------------
-- Exported functions
-- --------------------------------------------------
local p = {}

function p.main(frame)
	local t_args = frame:getParent().args
    return p._main(t_args)
end

function p._main(t_args)
	t_args = s.parse_args(t_args)
	local ret = tostring(s.make_infobox(t_args))
	
	if t_args.test == false then
		ret = ret .. s.add_categories(t_args)
	end
	
	return ret
end

return p