Module:Infobox/Weapon

From VALORANT Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HtmlUtil')
local util_infobox = require('Module:InfoboxUtil')
local util_map = require("Module:MapUtil")
local util_news = require("Module:NewsUtil")
local util_table = require('Module:TableUtil')
local util_text = require("Module:TextUtil")
local util_title = require("Module:TitleUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require('Module:i18nUtil')

local lang = mw.getLanguage('en')
local Country = require('Module:Country').main

local CREDS_ICON = '[[File:Creds Icon.png|8px|alt=Creds|baseline]]'

local Infobox = require('Module:Infobox'):extends()

local DAMAGE_ARGS = { 'RangeMin', 'RangeMax', 'DamageBody', 'DamageHead', 'DamageLegs' }
local DAMAGE_COLUMNS = { 'DamageRange', 'DamageBody', 'DamageHead', 'DamageLegs' }

Infobox.type = 'Weapon'

Infobox.LAYOUT = {
	--tabs = 'WeaponTabsHeader',
	sections = { 'General Information', 'Primary Fire', 'Alternate Fire',  'Developer', 'Damage' },
	contents = {
		{ 'WeaponType', 'Cost', 'Ammo', 'WallPenetration', 'ADS', 'Zoom', 'Surpressor' },
		{ 'FireMode1', 'FireRate1' },
		{ 'FireMode2', 'FireRate2', 'Spread' },
		{ 'KeyDdragon', 'KeyInt' },
		{ 'Damage', Damage = 'wide', },
	},
}

local h = {}
local p = {}

function p.main(frame)
	local args = util_args.merge()
	return Infobox(args):run()
end

function Infobox:castArgs(args)
	args.damage = util_args.splitArgsArray(args.damage, DAMAGE_ARGS)
end

function Infobox:validateArgs(args)
end

function Infobox:getProcessed(args)
	local processed = self:super('getProcessed', args)
	local tbl = {
		
	}
	return util_table.merge(processed, tbl)
end

function Infobox:getDisplay(args, processed)
	local display = self:super('getDisplay', args, processed)
	local tbl = {
		
		title = args.name,
		image = args.image or ('Weapon %s Model.png'):format(args.name),
		imagesize = not (args.image and not args.imagesize) and (args.imagesize or '300px'),
		
		WeaponType = args.type,
		Cost = h.getCostDisplay(args.cost),
		Ammo = ('%s / %s %s'):format(args.rounds, (args.totalrounds - args.rounds), args.pallets and ('(%s pallets per round)'):format(args.pallets) or ''),
		WallPenetration = args.penetration,
		ADS = args.ads,
		Zoom = args.zoom,
		Surpressor = args.surpressor,

		FireMode1 = args.mode1,
		FireRate1 = ('%s rounds per second'):format(args.rate1),

		FireMode2 = args.mode2,
		FireRate2 = args.rate2 and ('%s rounds per second'):format(args.rate2),
		Spread = args.spread,

		Damage = h.getDamage(args.damage),

		KeyDdragon = args.key_ddragon,
		KeyInt = args.key_int,
		
	}
	return Infobox.mergeDisplay(display, tbl)
end

function h.getCostDisplay(cost)
	if cost == '0' then
		return i18n.print('free')
	end
	return ("%s %s"):format(CREDS_ICON, cost)
end

function h.getDamage(damage)
	if not damage then return end
	local output = mw.html.create()
	local tbl = output:tag('table')
		:addClass('infobox-weapon-damage')
	util_map.rowsInPlace(damage, h.formatOneDamageRow)
	util_html.printHeaderFromI18n(tbl, DAMAGE_COLUMNS)
	util_html.printRowsByList(tbl, damage, DAMAGE_COLUMNS)
	return tostring(output)
end

function h.formatOneDamageRow(row)
	row.DamageRange = (i18n.print('damageRange', row.RangeMin, row.RangeMax))
end

function Infobox:getCargo(args, processed)
	local cargo = self:super('getCargo', args, processed)
	if util_args.castAsBool(args.nocargo) then return cargo end
	local tbl = {
		{
			_table = 'Weapons',
			Name = args.name,
			-- Title = args.title,
			-- Class = args.class,
			-- Origin = args.origin and m_country.name(args.origin),
			-- Quote = args.quote,
			-- ReleaseDate = args.releasedate,
			-- Creds = args.creds,
			-- Attributes = util_table.concatFromArgs(args,'attribute',','),
			-- Resource = args.resource,
			-- RealName = args.realname,
			-- KeyDdragon = args.key_ddragon,
			-- KeyInteger = args.key_int,
		},
	}
	-- tbl[#tbl+1] = h.getNewsLine(args, processed)
	util_table.mergeArrays(tbl, h.getDamageCargo(args.damage))
	return util_table.mergeArrays(cargo, tbl)
end

function h.getDamageCargo(damage)
	return util_map.rowsInPlace(damage, h.formatOneCargoDamageRow)
end

function h.formatOneCargoDamageRow(row)
	row._table = 'WeaponDamages'
	row.Body = row.DamageBody
	row.Legs = row.DamageLegs
	row.Head = row.DamageHead
	row.Weapon = mw.title.getCurrentTitle().text
end

function h.getNewsLine(args, processed)
	if util_args.castAsBool(args.notreleased) then return end
	util_news.setId()
	args.date = lang:formatDate('Y-m-d', args.releasedate)
	args.Sentence = i18n.default(
		'NewsSentence',
		util_text.intLinkOrText(args.name)
	)
	local tbl = {
		Subject = 'Riot',
		SubjectLink = 'Portal:Weapons',
		SubjectType = 'Tournament',
		Region = 'International',
	}
	util_table.mergeDontOverwrite(tbl, util_news.getNewsCargoFieldsFromArgs(args))
	return tbl
end

function Infobox:getCategories(args, processed)
	local categories = self:super('getCategories', args, processed)
	local tbl = {
		'Weapons'
	}
	return util_table.mergeArrays(categories, tbl)
end

function Infobox:getVariables(args, processed)
	local variables = self:super('getVariables', args, processed)
	local tbl = {
	}
	return util_table.merge(variables, tbl)
end

return p