Jump to content

Module:ItemSearch

From Conan Exiles Wiki
Revision as of 02:11, 8 January 2026 by imported>Testerle
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local util_args = require("Module:ArgsUtil")
local util_cache = require("Module:CacheUtil")
local util_vars = require("Module:VarsUtil")
local cache = require('mw.ext.LuaCache')
local PREFIX = 'ITEMLINK_09'
local lang = mw.getLanguage('en')

local p = {}
local h = {}
local cargo = mw.ext.cargo

function p.Main(frame)
	local parent = frame:getParent();
	
	local itemType = frame.args['type']
	
	local name = frame.args['name']
	local level = frame.args['level']
	local armorTypes = frame.args['armorTypes']
	local bonus = frame.args['bonus']
	local slots = frame.args['slots']
	local effects = frame.args['effects']
	local sources = frame.args['sources']
	local craftable = frame.args['craftable']
	
	local searchLimit = frame.args['limit'] or 300
	
	local text = ''
	
	local where = 'type = "' .. itemType .. '"' .. h.prepareQuery(name, level, armorTypes, bonus, slots, effects, sources, craftable)
	
	-- local having = ''
	-- if level ~= nil and level ~= "" then
	-- 	having = 'MIN(Recipes.level) <= ' .. level
	-- end
	
	return frame:expandTemplate{ title = 'ArmorSearchResult', args = { ['where'] = where, ['limit'] = searchLimit } }
end

function h.prepareQuery(name, level, armorTypes, bonus, slots, effects, sources, craftable) 
	local result = ''
	
	if name ~= nil and name ~= "" then
		result = result .. ' AND Item.uniqueName LIKE "%' .. name .. '%"'
	end
	
	if level ~= nil and level ~= "" then
		result = result .. ' AND Item.minLevelRequirement <= ' .. level
	end
	
	if armorTypes ~= nil and armorTypes ~= "" then
		local armorTypeResult = ''
		for key in armorTypes:gmatch('[^,%s]+') do
			if armorTypeResult ~= '' then
				armorTypeResult = armorTypeResult .. " OR "
			end
			armorTypeResult = armorTypeResult .. 'Item.armorType = "' .. key .. '"'
		end
		
		result = result .. ' AND (' .. armorTypeResult .. ')'
	end
	
	if bonus ~= nil and bonus ~= "" then
		for key in bonus:gmatch('[^,%s]+') do
			result = result .. ' AND Item.bonus LIKE "%' .. key .. '%"'
		end
	end
	
	if slots ~= nil and slots ~= "" then
		local slotsResult = ''
		for key in slots:gmatch('[^,%s]+') do
			if slotsResult ~= '' then
				slotsResult = slotsResult .. " OR "
			end
			slotsResult = slotsResult .. 'Item.equipslot = "' .. key .. '"'
		end
		
		result = result .. ' AND (' .. slotsResult .. ')'
	end
	
	if effects ~= nil and effects ~= "" then
		for key in effects:gmatch('[^,%s]+') do
			result = result .. ' AND Item.effect LIKE "%' .. key .. '%"'
		end
	end
	
	if sources ~= nil and sources ~= "" then
		local sourcesResult = ''
		for key in sources:gmatch('[^,%s]+') do
			if sourcesResult ~= '' then
				sourcesResult = sourcesResult .. " OR "
			end

			-- possible keys: 'basegame', 'dlc', 'bazaar', 'battlepass', 'twitchdrop'
			if key == 'basegame' then
				sourcesResult = sourcesResult .. 'Item.dlcPackage IS NULL'
			elseif key == 'dlc' then
				sourcesResult = sourcesResult .. 'Item.dlcPackage IN("The Imperial East Pack", "Jewel of the West Pack", "The Savage Frontier Pack", "Seekers of the Dawn Pack", "Treasures of Turan Pack", "The Riddle of Steel", "Blood and Sand Pack", "Debaucheries of Derketo Pack", "Riders of Hyboria Pack", "Architects of Argos Pack", "Isle of Siptah", "People of the Dragon Pack", "Physical pre-order", "Digital pre-order", "Isle of Siptah Pre-Order")'
			elseif key == 'bazaar' then
				sourcesResult = sourcesResult .. 'Item.dlcPackage = "Black Lotus Bazaar"'
			elseif key == 'battlepass' then
				sourcesResult = sourcesResult .. 'Item.dlcPackage LIKE "Battle Pass%"'
			elseif key == 'twitchdrop' then
				sourcesResult = sourcesResult .. 'Item.dlcPackage = "Twitch Drop"'
			end
		end
		
		result = result .. ' AND (' .. sourcesResult .. ')'
	end
	
	if craftable ~= nil and craftable ~= "both" then
		result = result .. ' AND Item.isCraftable = "' .. craftable .. '"'
	end
	
	return result
end

function p.updateCache(frame)
	-- this method returns nothing & just exists to query & update the LuaCache object
	-- it's called from the infobox so that when the page is saved, the cache will be updated
	local args = util_args.merge()
	local str = args[1]
	if not str then return end
	local key = h.getKey(str)
	h.getFromCargoAndUpdateCache(str, key)
end

-- the following code extracts the result from either:
-- (a) a LuaVariables result stored earlier on the page; or
-- (b) a LuaCache object of the lowercase name; or finally
-- (c) an actual Cargo query to retrieve the information from the page
-- we avoid doing a Cargo query at all costs because Cargo queries are extremely expensive

-- this was not premature optimization; the wiki was actually experiencing errors
-- prior to this optimization being added
-- this code was adapted from Module:Team on Leaguepedia
-- contact River for questions

function h.getKey(str)
	return PREFIX .. lang:lc(str)
end

function h.getFromVariablesOrCache(str, key)
	return util_vars.getObject(key) or h.getFromCacheOrCargo(str, key)
end

function h.getFromCacheOrCargo(str, key)
	local result = cache.get(key) or h.getFromCargoAndUpdateCache(str, key)
	util_vars.setObject(key, result)
	return result
end

function h.getFromCargoAndUpdateCache(str, key)
	local tables = 'Item'
	local fields = '_pageName,image,epicBorder,imageOverlay,dlcPackage,imageBackground'
	local query = {
		where = 'uniqueName = "' .. str .. '" ',
		limit = 1
	}
	local result = cargo.query( tables, fields, query )[1]
	cache.set(key, result)
	return result
end

-- end luacache wrapper

function h.switchString(str1, str2)
	if str2 == nil then return str1 end
	if str2 == '' then return str1 end
	return str2
end

return p