Module:SourcesQuery

From IdleOn MMO Wiki
Revision as of 15:23, 12 January 2023 by Kiokurashi (talk | contribs)

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

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

function p.Main( frame )
	local item = frame.args.item
	local result = ''
	result = SmithingSources(item, result)
	result = DropTableSources(item, result)
	result = QuestSources(item, result)
	result = VendorSources(item, result)
	result = CustomSources(item, result)
	if #result == 0 then result = 'Unobtainable' end
	mw.ext.VariablesLua.vardefine('Sources', result) -- Set LuaVar variable Sources so that we don't need to re-query on the same page.
	
	return result .. ' '
end

function SmithingSources(item, result)
	local tables = 'AnvilCraft'
    local fields = 'RecipeFrom'
    local temp = result
    -- _pageName LIKE "{{{1|{{PAGENAME}}}}}"
    local args = {
        where = 'AnvilCraft._pageName="'.. item ..'"',
        orderBy = 'AnvilCraft._pageName'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return temp end -- if no results, return original string and continue.
    for r = 1, #results do
    	if r > 1 then temp = temp .. ', ' end
    	local from = ' '
        if 'Start' ~= results[r].RecipeFrom then
        	from = ' (' .. results[r].RecipeFrom .. ')'
        end
        temp = temp .. '[[Smithing]]' .. from
    end
    
    return temp
end

function DropTableSources(item, result)
	local tables = 'DropTables'
    local fields = 'DropSourceEntity'
    local temp = result
    -- DropItem="{{{1|{{PAGENAME}}}}}" AND NOT DropSourceEntity LIKE "%DropTable%"
    local args = {
        where = 'DropTables.DropItem="'.. item ..'" AND NOT DropTables.DropSourceEntity LIKE "%DropTable%"',
        groupBy = 'DropTables.DropSourceEntity',
        orderBy = 'DropTables.DropSourceEntity ASC'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return temp end -- if no results, return original string and continue.
    if #temp > 0 then temp = temp .. ', ' end -- If there was other sources, then insert divider.
    for r = 1, #results do
    	if r > 1 then temp = temp .. ', ' end
        local source = results[r].DropSourceEntity
        temp = temp .. '[[' .. source .. ']]'
    end
    
    return temp
end

function QuestSources(item, result)
	local tables = 'Quests'
    local fields = '_pageName'
    local temp = result
    -- Rewards HOLDS LIKE "%>{{{1|{{PAGENAME}}}}}<%"
    local args = {
        where = 'Quests.Rewards HOLDS LIKE "%>'.. item ..'<%"',
        groupBy = 'Quests._pageName',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return temp end -- if no results, return original string and continue.
    if #temp > 0 then temp = temp .. ', ' end -- If there was other sources, then insert divider.
    for r = 1, #results do
    	if r > 1 then temp = temp .. ', ' end
        local source = results[r]._pageName
        temp = temp .. '[[' .. source .. ']]'
    end
    
    return temp
end

function VendorSources(item, result)
	local tables = 'VendorItems'
    local fields = 'Vendor'
    local temp = result
    -- Item="{{{1|{{PAGENAME}}}}}"
    local args = {
        where = 'VendorItems.Item="'.. item ..'"',
        orderBy = 'VendorItems.Vendor',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return temp end -- if no results, return original string and continue.
    if #temp > 0 then temp = temp .. ', ' end -- If there was other sources, then insert divider.
    for r = 1, #results do
    	if r > 1 then temp = temp .. ', ' end
        local vendor = results[r].Vendor
        temp = temp .. '[[Vendors#' .. vendor .. '|' .. vendor .. ']]'
    end
    
    return temp
end

function CustomSources(item, result)
	local tables = 'Sources'
    local fields = 'Link, SpecialText, EventRules'
    local temp = result
    -- Item="{{{1|{{PAGENAME}}}}}"
    local args = {
        where = 'Sources.Item="'.. item ..'"',
        orderBy = 'Sources.Source',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return temp end -- if no results, return original string and continue.
    if #temp > 0 then temp = temp .. ', ' end -- If there was other sources, then insert divider.
    local EventRulesCounter = 0
    for r = 1, #results do
    	if r > 1 then temp = temp .. ', ' end
        local link = results[r].Link
        local sText = results[r].SpecialText
        EventRulesCounter = EventRulesCounter + results[r].EventRules
        temp = temp .. '[[' .. link .. ']]'
        if sText ~= 'None' then temp = temp .. ' (' .. sText .. ')' end
    end
    if EventRulesCounter > 0 then temp = temp .. "<br/>''" .. item .. " can be obtained from any enemy during its event.''" end
    
    return temp
end

-- This one is just to get the Drop Chance value for an Item.
function p.DropChance(frame)
	local item = frame.args.item
	local result = ''
	local tables = 'DropTables'
    local fields = 'DropRate'
    -- DropItem="{{{1|{{PAGENAME}}}}}" AND NOT DropSourceEntity LIKE "%DropTable%"
    local args = {
        where = 'DropTables.DropItem LIKE "'.. item ..'"',
        groupBy = 'DropTables.DropItem',
        orderBy = 'DropTables.DropRate DESC' -- For any item with multiple drop sources, get the highest one.
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then result = 0 else result = results[1].DropRate end
    mw.ext.VariablesLua.vardefine('Chance', result)
    
    return result
end

return p