Module:Utils

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

local p = {}
local category = mw.loadJsonData("Module:Category.json")
local data = mw.loadJsonData("Module:Data.json")
local objectID = mw.loadJsonData("Module:ObjectID.json")
local condition = mw.loadJsonData("Module:Condition.json")

function p.concat(t, separator)
	local str = ""
	separator = separator and separator or ", "
	local length = -1 - #separator
	
	if t ~= nil then
		for k, v in ipairs(t) do
			str = str .. v .. separator
		end
		str = str:sub(1, length)
	end
	
	return str
end

function p.split(inputstr, separator)
	if separator == nil then
		separator = "%s"
	end
	local t = {}
		for str in string.gmatch(inputstr, "([^"..separator.."]+)") do
		table.insert(t, str)
	end
	return t
end

function p.length(t)
	local str = 0
	
	if t ~= nil then
		for k, v in pairs(t) do
			str = str + 1
		end
	end
	
	return str
end

function p.find(t, property, value)
	if t ~= nil then
		for k, v in pairs(t) do
			if v[property] == value then
				return t[k]
			end
		end
	end
	
	return nil
end

function p.merge(t1, t2)
	local t = {}
	if t1 == nil and t2 == nil then
		return t
	elseif t1 == nil then
		return t2
	elseif t2 == nil then
		return t1
	else
		for k, v in ipairs(t1) do
			table.insert(t, v)
		end
		for k, v in ipairs(t2) do
			table.insert(t, v)
		end
		return t
	end
end

function p.getCategory(name)
	for k, v in pairs(category) do
		for k2, v2 in pairs(v) do
			for k3, v3 in pairs(v2) do
				if v3 == name then
					return {k, k2}
				end
			end
		end
	end
end

function p.getDamage(obj, level)
	local str = ""
	if obj.equip and obj.equip.damage then
		local currentLevel = level and level or obj.level
		local type = (obj.equip.magic and 'magic' or 'physical') .. ' damage ' .. (obj.equip.range and '(range)' or '(melee)')
		
		str = p.damageRange(obj.equip.damage[currentLevel]) .. ' ' .. type
	elseif obj.trap and obj.trap.damage then
		str = p.damageRange(obj.trap.damage) .. ' damage'
	elseif obj.pet and obj.pet.damage then
		local currentLevel = level and level or 1
		local type = obj.pet.type == 0 and ' melee' or ' range'
		str = p.damageRange(obj.pet.damage[currentLevel]) .. type .. ' damage'
	end
	
	return str
end

function p.getEffects(obj, level)
	local str = ""
	local currentLevel = level and level or obj.level
	
	if obj.pet and obj.conditions then
		if obj.pet.type == 2 then
			str = '<span style="color:#7d725d">Buffs owner with:</span><br>'
		end
		return str .. p.concat(obj.conditions, '<br>')
	end
	if obj.equip and obj.equip.extraInventory then
		str = '+' .. obj.equip.extraInventory .. ' inventory slots<br>'
	end
	if obj.equip and obj.equip.projectile then
		local name = objectID[obj.equip.projectile]
		local proObj = data[name]
		
		if proObj.projectile then
			str = str .. (proObj.projectile.pierce and 'Projectiles pierces through enemies<br>' or '')
			str = str .. (proObj.projectile.bounce and 'Projectiles bounce on impact<br>' or '')
		end

		if proObj.explosive then
			if proObj.explosive.damage then
				str = str .. p.damageRange(proObj.explosive.damage[currentLevel]) .. ' explosive damage<br>'
			end
			str = str .. (proObj.explosive.mining and proObj.explosive.mining[currentLevel] .. ' mining damage<br>' or '')
		end
	end
	if obj.explosive then
		if obj.explosive.damage then
			str = str .. p.damageRange(obj.explosive.damage[currentLevel]) .. ' explosive damage<br>'
		end
		str = str .. (obj.explosive.mining and obj.explosive.mining[currentLevel] .. ' mining damage<br>' or '')
	end
	if obj.equip and obj.equip.conditions then
		for k, v in ipairs(obj.equip.conditions) do
			str = str .. p.getCondition(v.id, v.value[currentLevel]) .. '<br>'
		end
	end
	if obj.equip and obj.equip.secondary then
		if obj.equip.minionDamage then
			str = str .. '<span style="color:rgb(183 151 53)">' .. obj.equip.secondary .. '</span><br>'
			str = str .. '<span style="color:rgb(197 178 119)">' .. p.damageRange(obj.equip.minion[currentLevel]) .. ' ' .. obj.equip.minionDamage .. ' damage</span><br>'
			str = str .. '<span style="color:rgb(197 178 119)">Minion lifespan: 60 sec</span><br>'
		else
			str = str .. '<span style="color:rgb(183 151 53)">Secondary use: ' .. obj.equip.secondary .. '</span><br>'
		end
	end
	if obj.mechanic then
		str = str .. '<span style="color:rgb(183 151 53)">Off-hand use: ' .. obj.mechanic .. '</span><br>'
	end
	if obj.use then
		str = str .. obj.use ..'<br>'
	end
	if obj.conditionsWhenConsumed then
		local once = true
		for k, v in ipairs(obj.conditionsWhenConsumed) do
			if not string.find(v, "(only once)") then
				str = str .. (once and '<span style="color:#7d725d">When eaten:</span><br>' or '')
				str = str .. v .. '<br>'
				once = false
			end
		end
		once = true
		for k, v in ipairs(obj.conditionsWhenConsumed) do
			if string.find(v, "(only once)") then
				str = str .. (once and '<span style="color:#7d725d">Permanent when eaten:</span><br>' or '')
				str = str .. v .. '<br>'
				once = false
			end
		end
	end
	if obj.trap and obj.trap.conditions then
		for k, v in ipairs(obj.trap.conditions) do
			str = str .. p.getCondition(v.id, v.value) .. '<br>'
		end
	end
	str = str:sub(1, -5)
	
	return str
end

function p.getSetEffects(obj, frame)
	local str = ""
	local set = ""
	
	if obj.set then
		for k, v in ipairs(obj.set.items) do
			set = set .. frame:expandTemplate{title = 'Icon', args = {objectID[v]}} .. '<br>'
		end
		set = set:sub(1, -5)
		str = str .. p.concat(obj.set.effects, '<br>') .. '<br>' .. set
	end
	
	return str
end

function p.damageRange(damage)
	local range = math.floor(damage * 0.1)
	return (damage - range) .. "-" .. (damage + range)
end

function p.getCondition(id, value)
	local con = condition[id]
	local permanent = ""
	if con.showDecimal then
		value = value / 10
	end
	if con.skipSign then
		value = math.abs(value)
	elseif value >= 0 then
		value = '+' .. value
	end
	if con.isPermanent and not con.isAdditiveWithSelf then
		permanent = " (only once)"
	end
	return string.gsub(con.condition, "{0}", value) .. permanent
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.