Module:Infobox/Skill

From Liquipedia Commons Wiki
Module documentation[view] [edit] [history] [purge]

Creates the infobox for a skill.

Usage

Implement a Module on your wiki that calls run on this module, and then override the customizable sections as needed. Example:

local Class = require('Module:Class')
local Skill = require('Module:Infobox/Skill')
local Injector = require('Module:Infobox/Widget/Injector')
local Cell = require('Module:Infobox/Widget/Cell')
local CleanRace = require('Module:CleanRace2')
local Hotkeys = require('Module:Hotkey')
local String = require('Module:StringUtils')
local PageLink = require('Module:Page')

local Ability = Class.new()

local CustomInjector = Class.new(Injector)

local _args

function Ability.run(frame)
	local ability = Skill(frame)
	ability.createWidgetInjector = Ability.createWidgetInjector
	ability.getCategories = Ability.getCategories
	_args = ability.args
	return ability:createInfobox(frame)
end

function CustomInjector:addCustomCells(widgets)
	table.insert(widgets, Cell{
		name = 'Research Hotkey',
		content = {Ability:getResearchHotkey()}
	})

	return widgets
end

function Ability:createWidgetInjector()
	return CustomInjector()
end

function CustomInjector:parse(id, widgets)
	if id == 'hotkey' then
		return {
			Cell{
				name = '[[Hotkeys per Race|Hotkey]]',
				content = {Ability:getHotkeys()}

			}
		}
	elseif id == 'cooldown' then
		return {
			Cell{
				name = PageLink.makeInternalLink({onlyIfExists = true},'Cooldown') or 'Cooldown',
				content = {_args.cooldown}

			}
		}
	end

	return widgets
end

function Ability:getResearchHotkey()
	local display
	if not String.isEmpty(_args.from) then
		display = Hotkeys.hotkey(_args.rhotkey)
	end

	return display
end

function Ability:getCategories()
	local categories = { 'Abilities' }
	local race = string.lower(_args.race or '')
	race = CleanRace[race]
	if race then
		table.insert(categories, race .. ' Abilities')
	end

	return categories
end

function Ability:getHotkeys()
	local display
	if not String.isEmpty(_args.hotkey) then
		if not String.isEmpty(_args.hotkey2) then
			display = Hotkeys.hotkey(_args.hotkey, _args.hotkey2, 'slash')
		else
			display = Hotkeys.hotkey(_args.hotkey)
		end
	end

	return display
end

return Ability

Parameters

[edit]
|game=
The specifier for the game infobox, e.g. wol for Wings of Liberty
|informationType=
Specify the Display in the "... Information" below the Image. Examples: Spell, Ability, ...
|name=
The name of the skill/spell/ability/...
|image=
Image to be displayed in the infobox
|imageSize=
Size (in px) the image should be displayed in
Default: 600
|caption=
Caption for the image
|range=
Range of the skill
|radius=
Radius the skill takes affect in
|cooldown=
Caption for the image
|duration=
Duration of the skill
|cooldown=
Cooldown of the skill
|hotkey=
Hotkey to use the skill
|hotkey2=
Second hotkey in case a combination is needed
|cost=
Cost for the usage f the skill (e.g. in Mana/Energy/Minerals/...)
|footnotes=
footnotes

---
-- @Liquipedia
-- page=Module:Infobox/Skill
--
-- Please see https://github.com/Liquipedia/Lua-Modules to contribute
--

local Lua = require('Module:Lua')

local Class = Lua.import('Module:Class')
local Json = Lua.import('Module:Json')
local Hotkey = Lua.import('Module:Hotkey')
local Namespace = Lua.import('Module:Namespace')
local String = Lua.import('Module:StringUtils')
local Table = Lua.import('Module:Table')
local Variables = Lua.import('Module:Variables')

local BasicInfobox = Lua.import('Module:Infobox/Basic')

local Widgets = Lua.import('Module:Widget/All')
local Cell = Widgets.Cell
local Header = Widgets.Header
local Title = Widgets.Title
local Center = Widgets.Center
local Customizable = Widgets.Customizable

---@class SkillInfobox: BasicInfobox
---@operator call(Frame): SkillInfobox
local Skill = Class.new(BasicInfobox)

---@param frame Frame
---@return Widget
function Skill.run(frame)
	local skill = Skill(frame)
	return skill:createInfobox()
end

---@return Widget
function Skill:createInfobox()
	local args = self.args

	if String.isEmpty(args.informationType) then
		error('You need to specify an informationType, e.g. "Spell", "Ability, ...')
	end

	local widgets = {
		Header{
			name = self:nameDisplay(args),
			image = args.image,
			imageDark = args.imagedark or args.imagedarkmode,
			size = args.imagesize,
		},
		Center{children = {args.caption}},
		Title{children = args.informationType .. ' Information'},
		Customizable{
			id = 'caster',
			children = {
				Cell{name = 'Caster(s)', children = self:getAllArgsForBase(args, 'caster', {makeLink = true})},
			}
		},
		Customizable{
			id = 'cost',
			children = {
				Cell{name = 'Cost', children = {args.cost}},
			}
		},
		Customizable{
			id = 'hotkey',
			children = {
				Cell{name = 'Hotkey', children = {self:_getHotkeys(args)}},
			}
		},
		Cell{name = 'Range', children = {args.range}},
		Cell{name = 'Radius', children = {args.radius}},
		Customizable{
			id = 'cooldown',
			children = {
				Cell{name = 'Cooldown', children = {args.cooldown}},
			}
		},
		Customizable{
			id = 'duration',
			children = {
				Cell{name = 'Duration', children = {args.duration}},
			}
		},
		Customizable{id = 'custom', children = {}},
		Center{children = {args.footnotes}},
	}

	if Namespace.isMain() then
		local categories = self:getCategories(args)
		self:categories(unpack(categories))
		self:_setLpdbData(args)
	end

	return self:build(widgets, 'Skill')
end

---@param args table
---@return string?
function Skill:nameDisplay(args)
	return args.name
end

--- Allows for overriding this functionality
---@param args table
---@return string[]
function Skill:getCategories(args)
	return {}
end

---@param args table
function Skill:_setLpdbData(args)
	local skillIndex = (tonumber(Variables.varDefault('skill_index')) or 0) + 1
	Variables.varDefine('skill_index', skillIndex)

	local lpdbData = {
		objectName = 'skill_' .. skillIndex .. '_' .. self.name,
		name = args.name,
		type = args.informationType,
		image = args.image,
		imagedark = args.imagedark,
		extradata = {},
	}
	lpdbData = self:addToLpdb(lpdbData, args)
	local objectName = Table.extract(lpdbData, 'objectName')

	mw.ext.LiquipediaDB.lpdb_datapoint(objectName, Json.stringifySubTables(lpdbData))
end

---@param lpdbData table
---@param args table
---@return table
function Skill:addToLpdb(lpdbData, args)
	return lpdbData
end

---@param args table
---@return string?
function Skill:_getHotkeys(args)
	local display
	if not String.isEmpty(args.hotkey) then
		if not String.isEmpty(args.hotkey2) then
			display = Hotkey.hotkey2{hotkey1 = args.hotkey, hotkey2 = args.hotkey2, seperator = 'slash'}
		else
			display = Hotkey.hotkey{hotkey = args.hotkey}
		end
	end

	return display
end

return Skill