Module:SourcesQuery: Difference between revisions

From IdleOn MMO Wiki
mNo edit summary
mNo edit summary
Line 68: Line 68:
function DropTableSources(item)
function DropTableSources(item)
local fullList = {}
local fullList = {}
local tables = 'DropTables'
local tables = 'NormalDropTables'
     local fields = 'DropSourceEntity'
     local fields = 'DropSource'
     local args = {
     local args = {
         where = 'DropTables.DropItem="'.. item ..'" AND NOT DropTables.DropSourceEntity LIKE "%DropTable%"',
         where = 'NormalDropTables.DroppedItem="'.. item .. '"',
         groupBy = 'DropTables.DropSourceEntity',
         groupBy = 'NormalDropTables.DropSource',
         orderBy = 'DropTables.DropSourceEntity ASC'
         orderBy = 'NormalDropTables.DropSource ASC'
     }
     }
     local results = cargo.query( tables, fields, args )
     local results = cargo.query( tables, fields, args )
Line 79: Line 79:
     local source = ''
     local source = ''
     for r = 1, #results do
     for r = 1, #results do
         source = results[r].DropSourceEntity
         source = results[r].DropSource
         fullList[r] = string.format('[[%s]]', source)
         fullList[r] = string.format('[[%s]]', source)
     end
     end

Revision as of 02:09, 24 March 2024

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

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

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

function p.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



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

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

function DropTableSources(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

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

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

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

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

-- 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 = 'DropItem, DropRate'
    -- DropItem="{{{1|{{PAGENAME}}}}}" AND NOT DropSourceEntity LIKE "%DropTable%"
    local args = {
        where = 'DropTables.DropItem = "'.. item ..'" AND NOT DropSourceEntity LIKE "%DropTable%"',
        orderBy = 'DropTables.DropRate DESC'
    }
    local results = cargo.query( tables, fields, args )
    if #results == 0 then result = 0 else result = '' .. results[1].DropRate end -- Gets the highest drop rate.
    mw.ext.VariablesLua.vardefine('Chance', result)
    
    return result
end

return p