Module:Item

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

To edit the documentation or categories for this module, click here.


local util_vars = require("Module:VarsUtil")

local p = require('Module:EntityAbstract'):finalExtends()
local h = {}

p.objectType = 'Item'
p.imagelength = 'key'
p.defaultlength = 'key'
p.defaultLink = 'link'
p.imageDisplayLength = 'display'
p.filePattern = 'ItemSquare%s.png'

function p:init(str, opts)
	if not opts then opts = {} end
	self:super('init', str, 'Item')
	if self.unknown then
		self.vars = {
			link = str,
			key = str,
			display = str,
		}
	end
	if self.vars and self.vars.hasPatchSpecificHandling then
		self:setCurrentVars(opts.patch)
	end
end

function p:name(opts)
	if not opts then opts = {} end
	return opts.display or self:get(opts.len or self.defaultlength)
end

-- deal with patch-specific handling in init
function p:setCurrentVars(patchString)
	local versionHistory = self.vars
	-- if we don't provide a patch string then just return
	-- the vars of the last thing in version history
	if not patchString then
		self.vars = self:getMostRecentVersionVars()
		return
	end
	
	season, patch = patchString:match('(%d+)%.(%d+)')
	season = tonumber(season)
	patch = tonumber(patch)
	if not season or not patch then
		error('Invalid patch for item patch lookup')
	end
	self.vars = h.getCurrentVarsFromVerHistory(season, patch, versionHistory)
end

function p:getMostRecentVersionVars()
	-- we don't have # because we used loadData and we only shallow cloned
	local ret
	for _, v in ipairs(self.vars) do
		ret = v.vars
	end
	return ret
end

function h.getCurrentVarsFromVerHistory(season, patch, versionHistory)
	for _, version in ipairs(versionHistory) do
		if h.isCurrentVersion(season, patch, version) then
			return version.vars
		end
	end
end

function h.isCurrentVersion(season, patch, version)
	-- strict inequality because both of the boundaries in the json are inclusive
	
	-- if we're before the version in question
	local versionStart = version.versionStart
	if versionStart.season > season then return false end
	if versionStart.season == season and versionStart.patch > patch then return false end
		
	-- if we're after the version in question
	local versionEnd = version.versionEnd
	if versionEnd and versionEnd.season < season then return false end
	if versionEnd and versionEnd.season == season and versionEnd.patch < patch then return false end
	
	-- otherwise we're good
	return true
end

return p