Module:SourcesQuery: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
Tag: Manual revert
mNo edit summary
 
(36 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local cargo = mw.ext.cargo
local cargo = mw.ext.cargo
local Utility = require('Module:Utility')


function p.Main( frame )
local function concatSourceStings(...)
local item = frame.args.item
local strings = {...}
local result = ''
local fullString = ''
local fullresult = ''
local recipe = ''
local drops = ''
local quests = ''
local shops = ''
local postoff = ''
local other = ''
recipe  = SmithingSources(item)
drops  = DropTableSources(item)
quests  = QuestSources(item)
shops  = VendorSources(item)
postoff = PostOfficeSources(item)
other  = CustomSources(item)
if (#recipe + #drops + #quests + #shops + #postoff + #other) == 0 then
for n=1, #strings do
result = 'Unobtainable'
if strings[n] ~= '' then
fullresult = 'Unobtainable'
if fullString ~= '' then fullString = fullString .. ', ' end
else
fullString = fullString .. strings[n]
if #recipe > 0 then
result = result .. recipe
fullresult = fullresult .. recipe
end
if #drops > 0 then
    if #result > 0 then
    result = result .. ', '
    fullresult = fullresult .. ', '
    end -- If there was other sources, then insert divider.
if #drops < 150 then
result = result .. drops
else
result = result .. "Many Drop Tables (Check Item Page)"
end
fullresult = fullresult .. drops
end
if #quests > 0 then
    if #result > 0 then
    result = result .. ', '
    fullresult = fullresult .. ', '  
    end -- If there was other sources, then insert divider.
if #quests < 150 then
result = result .. quests
else
result = result .. "Many Quest Rewards (Check Item Page)"
end
fullresult = fullresult .. quests
end
if #shops > 0 then
    if #result > 0 then
    result = result .. ', '
    fullresult = fullresult .. ', '  
    end -- If there was other sources, then insert divider.
result = result .. shops
fullresult = fullresult .. shops
end
if #postoff > 0 then
    if #result > 0 then
    result = result .. ', '  
    fullresult = fullresult .. ', '
    end -- If there was other sources, then insert divider.
result = result .. postoff
fullresult = fullresult .. postoff
end
if #other > 0 then
    if #result > 0 then
    result = result .. ', '
    fullresult = fullresult .. ', '
    end -- If there was other sources, then insert divider.
if #other < 200 then
result = result .. other
else
result = result .. "A lot of other sources (Check Item Page)"
end
fullresult = fullresult .. other
end
end
end
end
mw.ext.VariablesLua.vardefine('Sources', fullresult) -- Set LuaVar variable Sources so that we don't need to re-query on the same page.
return result
return fullString ~= '' and fullString or 'Unobtainable'
end
end


function SmithingSources(item)
local function SmithingSources(item)
local tables = 'AnvilCraft'
local tables = 'AnvilCraft'
     local fields = 'RecipeFrom'
     local fields = 'RecipeFrom'
    local temp = ''
    -- _pageName LIKE "{{{1|{{PAGENAME}}}}}"
     local args = {
     local args = {
         where = 'AnvilCraft.Item="'.. item ..'"',
         where = 'AnvilCraft.Item="'.. item ..'"',
Line 95: Line 25:
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
    local from = ''
    -- Unless there are two recipes for an item, this should only have one entry.
if 'Start' ~= results[1].RecipeFrom then
from = string.format(' (%s)', results[1].RecipeFrom)
end
    return string.format('[[Smithing]]%s', from)
end
 
local function NormalDropTableSources(item)
local fullList = {}
local tables = 'NormalDropTables'
    local fields = 'DropSource'
    local args = {
        where = 'NormalDropTables.DroppedItem="'.. item .. '"',
        groupBy = 'NormalDropTables.DropSource',
        orderBy = 'NormalDropTables.DropSource ASC'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
     for r = 1, #results do
     for r = 1, #results do
    if r > 1 then temp = temp .. ', ' end
         source = results[r].DropSource
    local from = ' '
        fullList[r] = string.format('[[%s]]', source)
         if 'Start' ~= results[r].RecipeFrom then
        from = ' (' .. results[r].RecipeFrom .. ')'
        end
        temp = temp .. '[[Smithing]]' .. from
     end
     end
      
      
     return temp
     return table.concat(fullList, ", ")
end
end


function DropTableSources(item)
local function EnemiesByRareTable(rareTableName, rarityID)
local tables = 'DropTables'
local results = {}
-- Find all Enemies that append the rare table denoted by rareTableName and return a list of their page links.
if rarityID == 'R' then
-- Query NormalDropTables for every source that 'drops' the table.
local tables = 'NormalDropTables'
    local fields = 'DropSource'
    local args = {
        where = 'DroppedItem IN ('.. rareTableName ..')',
        groupBy = 'DropSource',
    }
    local sources = cargo.query( tables, fields, args )
    if #sources == 0 then return '' end
    local source = ''
    for r = 1, #sources do
        results[r] = sources[r].DropSource
    end
    return table.concat(results, ", ")
elseif rarityID == 'MR' then
-- Query RareDropTables for every source that 'drops' the table. Then get every enemy in the NormalDropTables that drops that table.
local tables = 'RareDropTables'
    local fields = 'DropSourceEntity'
    local args = {
        where = 'DropItem='.. rareTableName,
        groupBy = 'DropSourceEntity',
    }
    local sources = cargo.query( tables, fields, args )
    if #sources == 0 then return '' end
    local source = ''
    for r = 1, #sources do
    source = sources[r].DropSourceEntity
        results[r] = string.format('"%s"', source)
    end
    -- Return the values generated by the Rare tables that call this Super Drop Table.
    return EnemiesByRareTable(table.concat(results, ", "), "R")
else
return "Enemy Lookup Error. Unknown Table Structure for table: " .. rareTableName
end
end
 
local function RareDropTableSources(item)
local fullList = {}
local tables = 'RareDropTables'
     local fields = 'DropSourceEntity'
     local fields = 'DropSourceEntity'
    local temp = ''
    -- DropItem="{{{1|{{PAGENAME}}}}}" AND NOT DropSourceEntity LIKE "%DropTable%"
     local args = {
     local args = {
         where = 'DropTables.DropItem="'.. item ..'" AND NOT DropTables.DropSourceEntity LIKE "%DropTable%"',
         where = 'RareDropTables.DropItem="'.. item ..'" AND NOT (DropSourceEntity LIKE "DropTable%" AND TableType="Mega")',
         groupBy = 'DropTables.DropSourceEntity',
         groupBy = 'RareDropTables.DropSourceEntity',
         orderBy = 'DropTables.DropSourceEntity ASC'
         orderBy = 'RareDropTables.DropSourceEntity ASC'
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
    local prettytext = ''
    local enemylist = ''
    local prefix = ''
     for r = 1, #results do
     for r = 1, #results do
    if r > 1 then temp = temp .. ', ' end
         source = results[r].DropSourceEntity
         local source = results[r].DropSourceEntity
         prefix = source:sub(1, 1) == 'S' and 'MR' or 'R' -- If first letter is S then MR, else R for prefix.
         temp = temp .. '[[' .. source .. ']]'
        -- Get Substring of source that is a digit followed by any letters,
        prettytext = prefix .. '-' .. source:sub(source:find("(%d+)(%a*)"))
        enemylist = EnemiesByRareTable('"'..source..'"', prefix)
        fullList[r] = string.format('[[%s|%s]] (%s)', source, prettytext, string.format(Utility.tooltipstruct, enemylist, "Sources"))
     end
     end
      
      
     return temp
     return table.concat(fullList, ", ")
end
end


function QuestSources(item)
local function QuestSources(item)
local fullList = {}
local tables = 'Quests'
local tables = 'Quests'
     local fields = '_pageName'
     local fields = '_pageName'
    local temp = ''
    -- Rewards HOLDS LIKE "%>{{{1|{{PAGENAME}}}}}<%"
     local args = {
     local args = {
         where = 'Quests.Rewards HOLDS LIKE "%>'.. item ..'<%"',
         where = 'Quests.Rewards HOLDS LIKE "%>'.. item ..'<%"',
Line 139: Line 131:
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
     for r = 1, #results do
     for r = 1, #results do
     if r > 1 then temp = temp .. ', ' end
     source = results[r]._pageName
        local source = results[r]._pageName
         fullList[r] = string.format('[[%s]]', source)
         temp = temp .. '[[' .. source .. ']]'
     end
     end
      
      
     return temp
     return table.concat(fullList, ", ")
end
end


function VendorSources(item)
local function VendorSources(item)
local fullList = {}
local tables = 'VendorItems'
local tables = 'VendorItems'
     local fields = 'Vendor'
     local fields = 'Vendor'
    local temp = ''
    -- Item="{{{1|{{PAGENAME}}}}}"
     local args = {
     local args = {
         where = 'VendorItems.Item="'.. item ..'"',
         where = 'VendorItems.Item="'.. item ..'"',
Line 159: Line 150:
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
    local vendor = ''
     for r = 1, #results do
     for r = 1, #results do
    if r > 1 then temp = temp .. ', ' end
         vendor = results[r].Vendor
         local vendor = results[r].Vendor
         fullList[r] = string.format('[[Vendors#%s|%s]] (Shop)', vendor, vendor)
         temp = temp .. '[[Vendors#' .. vendor .. '|' .. vendor .. ']] (Shop)'
     end
     end
      
      
     return temp
     return table.concat(fullList, ", ")
end
end


function PostOfficeSources(item)
local function PostOfficeSources(item)
local fullList = {}
local companies = {'Simple Shippin', 'Plan-it Express', 'Dudes Next Door', 'Down Undelivery', 'Alpine Suppliers', 'Cosmic Carrier'}
local companies = {'Simple Shippin', 'Plan-it Express', 'Dudes Next Door', 'Down Undelivery', 'Alpine Suppliers', 'Cosmic Carrier'}
local shortNames = {'SS', 'PE', 'DND', 'DU', 'AS', 'CC'} -- Handling it this way so we can control the order.
local shortNames = {'SS', 'PE', 'DND', 'DU', 'AS', 'CC'} -- Handling it this way so we can control the order.
Line 175: Line 167:
local tables = 'PORewards'
local tables = 'PORewards'
     local fields = 'Company'
     local fields = 'Company'
    local temp = ''
     local args = {
     local args = {
         where = 'PORewards.Item="'.. item ..'"',
         where = 'PORewards.Item="'.. item ..'"',
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
    local company = ''
     for r = 1, #results do
     for r = 1, #results do
         local company = results[r].Company
         company = results[r].Company
         for c = 1, #companies do
         for c = 1, #companies do
        if company == companies[c] then boolTracker[c] = 1 end
        if company == companies[c] then boolTracker[c] = 1 end
    end
    end
     end
     end
    temp = temp .. '[[Post Office#Companies & Rewards|Post Office]] ('
     local compTable = {}
     local comNum = 0
     for b = 1, #boolTracker do
     for b = 1, #boolTracker do
     if boolTracker[b] == 1 then  
     if boolTracker[b] == 1 then  
     comNum = comNum + 1
     compTable[#compTable+1] = string.format('<span class="simple-tooltip simple-tooltip-inline tooltipstered" style="color: #b847cb;" data-simple-tooltip="%s">[[Post Office#%s|%s]]</span>', companies[b], companies[b], shortNames[b])
    if comNum > 1 then temp = temp .. ', ' end
    temp = temp .. '<span class="simple-tooltip simple-tooltip-inline tooltipstered" style="color: #b847cb;" data-simple-tooltip="'
    .. companies[b] .. '">[[Post Office#' .. companies[b] .. '|' .. shortNames[b] .. ']]</span>'
     end
     end
     end
     end
    temp = temp .. ')'
      
      
     return temp
     return string.format('[[Post Office#Companies & Rewards|Post Office]] (%s)', table.concat(compTable, ", "))
end
end


function CustomSources(item)
local function CustomSources(item)
local tables = 'Sources'
local fullList = {}
local tables = 'ExtraSourceData'
     local fields = 'Link, SpecialText, EventRules'
     local fields = 'Link, SpecialText, EventRules'
    local temp = ''
    -- Item="{{{1|{{PAGENAME}}}}}"
     local args = {
     local args = {
         where = 'Sources.Item="'.. item ..'"',
         where = 'ExtraSourceData.Item="'.. item ..'"',
         orderBy = 'Sources.Source',
         orderBy = 'ExtraSourceData.Source',
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then return temp end -- if no results, return original string and continue.
     if #results == 0 then return end -- if no results, return original string and continue.
     local EventRulesCounter = 0
     local EventRulesCounter = 0
    local link = ''
    local sText = ''
    local tableSize = #fullList
     for r = 1, #results do
     for r = 1, #results do
     if r > 1 then temp = temp .. ', ' end
     link = results[r].Link
        local link = results[r].Link
         sText = results[r].SpecialText
         local sText = results[r].SpecialText
         EventRulesCounter = EventRulesCounter + results[r].EventRules
         EventRulesCounter = EventRulesCounter + results[r].EventRules
         temp = temp .. '[[' .. link .. ']]'
         if sText ~= 'None' then
         if sText ~= 'None' then temp = temp .. ' (' .. sText .. ')' end
        sText = string.format(' (%s)', sText)
         else
        sText = ''
        end
        fullList[tableSize+r] = string.format('[[%s]]%s', link, sText)
     end
     end
     if EventRulesCounter > 0 then temp = temp .. "<br/>''Additional Event drop.''" end
    -- If item is a special drop then append the following to the last entry so it isn't split by commas later.
     if EventRulesCounter > 0 then fullList[#fullList] = fullList[#fullList] .. "<br/>''Additional Event drop.''" end
   
    return table.concat(fullList, ", ")
end
 
local function RareDropChance(item)
local tables = 'RareDropTables'
    local fields = 'DropRate'
    local args = {
        where = 'RareDropTables.DropItem  = "'.. item ..'"',
        orderBy = 'RareDropTables.DropRate  DESC'
    }
    local results = cargo.query( tables, fields, args )
   
    return results
end
 
function p.PullSourcesFromList(frame)
local item = frame.args.item
local tables = 'ItemSources'
    local fields = 'SourcesList'
    local args = {
        where = 'ItemSources.Item="'.. item ..'"',
        orderBy = 'ItemSources.Item'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
   
    -- There should only ever be one option if it exists so return that.
    return results[1].SourcesList
      
      
    return temp
end
end


Line 231: Line 252:
local item = frame.args.item
local item = frame.args.item
local result = ''
local result = ''
local tables = 'DropTables'
local raretext = ''
     local fields = 'DropItem, DropRate'
local tables = 'NormalDropTables'
    -- DropItem="{{{1|{{PAGENAME}}}}}" AND NOT DropSourceEntity LIKE "%DropTable%"
     local fields = 'DropRate'
     local args = {
     local args = {
         where = 'DropTables.DropItem = "'.. item ..'" AND NOT DropSourceEntity LIKE "%DropTable%"',
         where = 'NormalDropTables.DroppedItem  = "'.. item ..'"',
         orderBy = 'DropTables.DropRate DESC'
         orderBy = 'NormalDropTables.DropRate DESC'
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
     if #results == 0 then result = 0 else result = '' .. results[1].DropRate end -- Gets the highest drop rate.
    -- If no results in the Normal tables, then query the rare tables.
    if #results == 0 then
    results = RareDropChance(item)
    raretext = '|raretip=y'
    end
     if #results == 0 then result = 0; raretext = '' else result = tostring(results[1].DropRate) end -- Gets the highest drop rate.
     mw.ext.VariablesLua.vardefine('Chance', result)
     mw.ext.VariablesLua.vardefine('Chance', result)
      
      
     return result
     return result .. raretext
end
 
function p.Main( frame )
local item = frame.args.item
local smith = SmithingSources(item) or ''
local drops = NormalDropTableSources(item) or ''
local rdrop = RareDropTableSources(item) or ''
local quest = QuestSources(item) or ''
local shops = VendorSources(item) or ''
local posts = PostOfficeSources(item) or ''
local custm = CustomSources(item) or ''
local result = concatSourceStings(smith, drops, rdrop, quest, shops, posts, custm)
return result
end
end


return p
return p

Latest revision as of 18:37, 28 March 2024

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

local p = {}
local cargo = mw.ext.cargo
local Utility = require('Module:Utility')

local function concatSourceStings(...)
	local strings = {...}
	local fullString = ''
	
	for n=1, #strings do
		if strings[n] ~= '' then
			if fullString ~= '' then fullString = fullString .. ', ' end
			fullString = fullString .. strings[n]
		end
	end
	
	return fullString ~= '' and fullString or 'Unobtainable'
end

local function SmithingSources(item)
	local tables = 'AnvilCraft'
    local fields = 'RecipeFrom'
    local args = {
        where = 'AnvilCraft.Item="'.. item ..'"',
        orderBy = 'AnvilCraft.Item'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local from = ''
    -- Unless there are two recipes for an item, this should only have one entry.
	if 'Start' ~= results[1].RecipeFrom then 
		from = string.format(' (%s)', results[1].RecipeFrom) 
	end
	
    return string.format('[[Smithing]]%s', from)
end

local function NormalDropTableSources(item)
	local fullList = {}
	local tables = 'NormalDropTables'
    local fields = 'DropSource'
    local args = {
        where = 'NormalDropTables.DroppedItem="'.. item .. '"',
        groupBy = 'NormalDropTables.DropSource',
        orderBy = 'NormalDropTables.DropSource ASC'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
    for r = 1, #results do
        source = results[r].DropSource
        fullList[r] = string.format('[[%s]]', source)
    end
    
    return table.concat(fullList, ", ")
end

local function EnemiesByRareTable(rareTableName, rarityID)
	local results = {}
	-- Find all Enemies that append the rare table denoted by rareTableName and return a list of their page links.
	if rarityID == 'R' then
		-- Query NormalDropTables for every source that 'drops' the table.
		local tables = 'NormalDropTables'
	    local fields = 'DropSource' 
	    local args = {
	        where = 'DroppedItem IN ('.. rareTableName ..')',
	        groupBy = 'DropSource',
	    }
	    local sources = cargo.query( tables, fields, args )
	    if #sources == 0 then return '' end 
	    local source = ''
	    for r = 1, #sources do
	        results[r] = sources[r].DropSource
    	end
    	return table.concat(results, ", ")
	elseif rarityID == 'MR' then
		-- Query RareDropTables for every source that 'drops' the table. Then get every enemy in the NormalDropTables that drops that table.
		local tables = 'RareDropTables'
	    local fields = 'DropSourceEntity' 
	    local args = {
	        where = 'DropItem='.. rareTableName,
	        groupBy = 'DropSourceEntity',
	    }
	    local sources = cargo.query( tables, fields, args )
	    if #sources == 0 then return '' end 
	    local source = ''
	    for r = 1, #sources do
	    	source = sources[r].DropSourceEntity
	        results[r] = string.format('"%s"', source)
    	end
    	-- Return the values generated by the Rare tables that call this Super Drop Table.
    	return EnemiesByRareTable(table.concat(results, ", "), "R") 
	else 
		return "Enemy Lookup Error. Unknown Table Structure for table: " .. rareTableName
	end
end

local function RareDropTableSources(item)
	local fullList = {}
	local tables = 'RareDropTables'
    local fields = 'DropSourceEntity'
    local args = {
        where = 'RareDropTables.DropItem="'.. item ..'" AND NOT (DropSourceEntity LIKE "DropTable%" AND TableType="Mega")',
        groupBy = 'RareDropTables.DropSourceEntity',
        orderBy = 'RareDropTables.DropSourceEntity ASC'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
    local prettytext = ''
    local enemylist = ''
    local prefix = ''
    for r = 1, #results do
        source = results[r].DropSourceEntity
        prefix = source:sub(1, 1) == 'S' and 'MR' or 'R' -- If first letter is S then MR, else R for prefix.
        -- Get Substring of source that is a digit followed by any letters,
        prettytext = prefix .. '-' .. source:sub(source:find("(%d+)(%a*)"))
        enemylist = EnemiesByRareTable('"'..source..'"', prefix)
        fullList[r] = string.format('[[%s|%s]] (%s)', source, prettytext, string.format(Utility.tooltipstruct, enemylist, "Sources"))
    end
    
    return table.concat(fullList, ", ")
end

local function QuestSources(item)
	local fullList = {}
	local tables = 'Quests'
    local fields = '_pageName'
    local args = {
        where = 'Quests.Rewards HOLDS LIKE "%>'.. item ..'<%"',
        groupBy = 'Quests._pageName',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local source = ''
    for r = 1, #results do
    	source = results[r]._pageName
        fullList[r] = string.format('[[%s]]', source)
    end
    
    return table.concat(fullList, ", ")
end

local function VendorSources(item)
	local fullList = {}
	local tables = 'VendorItems'
    local fields = 'Vendor'
    local args = {
        where = 'VendorItems.Item="'.. item ..'"',
        orderBy = 'VendorItems.Vendor',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local vendor = ''
    for r = 1, #results do
        vendor = results[r].Vendor
        fullList[r] = string.format('[[Vendors#%s|%s]] (Shop)', vendor, vendor)
    end
    
    return table.concat(fullList, ", ")
end

local function PostOfficeSources(item)
	local fullList = {}
	local companies = {'Simple Shippin', 'Plan-it Express', 'Dudes Next Door', 'Down Undelivery', 'Alpine Suppliers', 'Cosmic Carrier'}
	local shortNames = {'SS', 'PE', 'DND', 'DU', 'AS', 'CC'} -- Handling it this way so we can control the order.
	local boolTracker = {0, 0, 0, 0, 0, 0} --Used to track which Company offers the same item as a reward.
	local tables = 'PORewards'
    local fields = 'Company'
    local args = {
        where = 'PORewards.Item="'.. item ..'"',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local company = ''
    for r = 1, #results do
        company = results[r].Company
        for c = 1, #companies do
	        if company == companies[c] then boolTracker[c] = 1 end
	    end
    end
    local compTable = {}
    for b = 1, #boolTracker do
    	if boolTracker[b] == 1 then 
    		compTable[#compTable+1] = string.format('<span class="simple-tooltip simple-tooltip-inline tooltipstered" style="color: #b847cb;" data-simple-tooltip="%s">[[Post Office#%s|%s]]</span>', companies[b], companies[b], shortNames[b])
    	end
    end
    
    return string.format('[[Post Office#Companies & Rewards|Post Office]] (%s)', table.concat(compTable, ", "))
end

local function CustomSources(item)
	local fullList = {}
	local tables = 'ExtraSourceData'
    local fields = 'Link, SpecialText, EventRules'
    local args = {
        where = 'ExtraSourceData.Item="'.. item ..'"',
        orderBy = 'ExtraSourceData.Source',
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    local EventRulesCounter = 0
    local link = ''
    local sText = ''
    local tableSize = #fullList
    for r = 1, #results do
    	link = results[r].Link
        sText = results[r].SpecialText
        EventRulesCounter = EventRulesCounter + results[r].EventRules
        if sText ~= 'None' then
        	sText = string.format(' (%s)', sText)
        else
        	sText = ''
        end
        fullList[tableSize+r] = string.format('[[%s]]%s', link, sText)
    end
    -- If item is a special drop then append the following to the last entry so it isn't split by commas later.
    if EventRulesCounter > 0 then fullList[#fullList] = fullList[#fullList] .. "<br/>''Additional Event drop.''" end
    
    return table.concat(fullList, ", ")
end

local function RareDropChance(item)
	local tables = 'RareDropTables'
    local fields = 'DropRate'
    local args = {
        where = 'RareDropTables.DropItem  = "'.. item ..'"',
        orderBy = 'RareDropTables.DropRate  DESC'
    }
    local results = cargo.query( tables, fields, args )
    
    return results
end

function p.PullSourcesFromList(frame)
	local item = frame.args.item
	local tables = 'ItemSources'
    local fields = 'SourcesList'
    local args = {
        where = 'ItemSources.Item="'.. item ..'"',
        orderBy = 'ItemSources.Item'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then return end -- if no results, return original string and continue.
    
    -- There should only ever be one option if it exists so return that.
    return results[1].SourcesList
    
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 raretext = ''
	local tables = 'NormalDropTables'
    local fields = 'DropRate'
    local args = {
        where = 'NormalDropTables.DroppedItem  = "'.. item ..'"',
        orderBy = 'NormalDropTables.DropRate DESC'
    }
    local results = cargo.query( tables, fields, args )
    -- If no results in the Normal tables, then query the rare tables.
    if #results == 0 then 
    	results = RareDropChance(item)
    	raretext = '|raretip=y'
    end
    if #results == 0 then result = 0; raretext = '' else result = tostring(results[1].DropRate) end -- Gets the highest drop rate.
    mw.ext.VariablesLua.vardefine('Chance', result)
    
    return result .. raretext
end

function p.Main( frame )
	local item = frame.args.item
	local smith = SmithingSources(item) or ''
	local drops = NormalDropTableSources(item) or ''
	local rdrop = RareDropTableSources(item) or ''
	local quest = QuestSources(item) or ''
	local shops = VendorSources(item) or ''
	local posts = PostOfficeSources(item) or ''
	local custm = CustomSources(item) or ''
	
	local result = concatSourceStings(smith, drops, rdrop, quest, shops, posts, custm)
	
	return result
end

return p