Module:Sandbox/User:Alsang/PotionList

Revision as of 11:50, 27 November 2024 by Alsang (talk | contribs) (not saved progress in a while)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:Sandbox/User:Alsang/PotionList/doc

local p = {}

-- non dynamic module, no inputs
function p.main()
	
	-- returns almost every parameter needed for the row, except buy values for reagents
	local query = {
		'[[Category:Potions]]',
		'[[Uses facility::Standard Potion Station||Potent Potion Station]]',
		'[[Uses item.Uses item::Brown Kelp]]', -- restrict for testing
		'?Profession Level A = lvl',
		'? #- = name',
		'?Uses item.Uses item #- = reagents',
		'?Value = sell',
		'?Recipe XP = brewXP',
		'?Uses item.Recipe XP = prepXP',
		'?Recipe KP = brewKP',
		'?Uses item.Recipe KP = prepKP',
		'?Recipe duration = brewDuration',
		'?Uses item.Recipe duration = prepDuration',
		'sort = Profession Level A'
	}
	local results = mw.smw.ask(query)
	
	local results = p.calculateValues(results)

	--return p.displayTable(results)
	
	return '<pre>' ..mw.text.jsonEncode(results, mw.text.JSON_PRETTY).. '</pre>'
	
end

-- any values not directly returned by the query
function p.calculateValues(results)

    -- iterate through potions
    for i, item in ipairs(results) do
    	
    	--in case of single reagent potions, make table of 1 element
    	if type(item.reagents) ~= 'table' then
    		item.reagents = {item.reagents}
    	end
    	
    	-- iterate through reagents, adding buy price to running total (individuals not needed)
    	-- starting value 20 is for bottle
    	item.buy = 20
    	for j, reagent in ipairs(item.reagents) do
    			
    		local shopPriceQuery = '[[:+]][[Sold item::' .. reagent .. ']]|?Shop buy price|mainlabel=' .. reagent
            local shopPriceResult = mw.smw.ask(shopPriceQuery) or {}
            local shopPrice = 0

            if shopPriceResult[1] and shopPriceResult[1]["Shop buy price"] then
                shopPrice = tonumber(shopPriceResult[1]["Shop buy price"]) or 0
            end
    		
            item.buy = item.buy + shopPrice
    	end
    	
    	item.profit = item.sell - item.buy
    	item.XP = item.brewXP + item.prepXP
    	item.KP = item.brewKP + item.prepKP
    	item.profitPerXP = item.profit / item.XP
    	
    	-- 40 seconds to buy new supplies and travel and deposit and everything, per 12 potions
    	item.duration = item.prepDuration + item.brewDuration + 40/12
    	item.XPPerHour = item.XP / item.duration * 3600
    	item.KPPerHour = item.KP / item.duration * 3600 / 100
    	item.profitPerHour = item.profit / item.duration * 3600

	end

    return results
end

-- make the table
function p.displayTable(results)
	local currency = require('Module:Currency').parse
	
    local out = {}
    table.insert(out, '{| class="wikitable sortable"')
    
    local headerRow = {}
    table.insert(headerRow, '! Level')
    table.insert(headerRow, '!! Potion')
    table.insert(headerRow, '!! Reagents (plus bottle)')
    table.insert(headerRow, '!! colspan="10" | Buy Value')
    table.insert(headerRow, '!! colspan="10" | Sell Value')
    table.insert(headerRow, '!! colspan="10" | Profit Value')
    table.insert(headerRow, '!! XP')
    table.insert(headerRow, '!! KP')
    table.insert(headerRow, '!! colspan="10" | Profit/XP')
    table.insert(headerRow, '!! XP/hr')
    table.insert(headerRow, '!! KP/hr')
    table.insert(headerRow, '!! colspan="10" | Profit/hr')
	table.insert(out, table.concat(headerRow, ' '))
	table.insert(headerRow, '|-')
	
	
	
	
	
	table.insert(out, '|}')
	return table.concat(out, '\n')
end

return p