Module:SCValue: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 5: Line 5:
local Items = require('Module:Items')
local Items = require('Module:Items')
local Common = require('Module:Common')
local Common = require('Module:Common')
local Number = require('Module:Number')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug')
local Debug = require('Module:Debug')


Line 64: Line 66:


local function getMostProfitablePurchases(amount)
local function getMostProfitablePurchases(amount)
amount = tonumber(amount) or 1
amount = tonumber(amount)
amount = math.max(amount, 1)
amount = math.max(amount, 1)
Line 85: Line 87:
table.sort(data, function(a, b) return a.GPPerSC > b.GPPerSC end)
table.sort(data, function(a, b) return a.GPPerSC > b.GPPerSC end)


-- Keep most profitable entries only
-- Keep top x most profitable entries only
local entries = {}
if amount ~= nil then
for i = 1, math.min(#data, amount) do
local entries = {}
table.insert(entries, data[i])
for i = 1, math.min(#data, amount) do table.insert(entries, data[i]) end
return entries
end
end
return entries
return data
end
end


Line 99: Line 102:
function p._getSCItemProfits(amount)
function p._getSCItemProfits(amount)
local purchases = getMostProfitablePurchases(amount)
local purchases = getMostProfitablePurchases(amount)
Debug.log(purchases)
 
local html = mw.html.create('table')
:addClass('wikitable sortable stickyHeader')
html:tag('tr')
:tag('th'):attr('colspan', 2)
  :wikitext('Purchase')
:tag('th'):wikitext('Cost')
:tag('th'):wikitext('Requirements')
:tag('th'):wikitext('GP Value')
:tag('th'):wikitext('GP per SC')
for _, v in pairs(purchases) do
local purchase =  v.Purchase
local name = Common.getPurchaseName(purchase)
local expansionIcon = Shop._getPurchaseExpansionIcon(purchase)
local costs = Shop.getCostString(purchase.cost, false)
html:tag('tr')
:tag('td'):addClass('table-img')
  :wikitext(Common.getPurchaseIcon({purchase, notext=true, size='50'}))
:tag('td'):attr('data-sort-value', name)
  :wikitext(expansionIcon)
  :wikitext(Common.getPurchaseIcon({purchase, noicon=true}))
:tag('td'):attr('data-sort-value', v.SCCost)
  :css('text-align', 'right')
  :wikitext(costs)
:tag('td'):wikitext(Common.getRequirementString(purchase.purchaseRequirements, 'None'))
:tag('td'):attr('data-sort-value', v.GPValue)
  :css('text-align', 'right')
  :wikitext(Icons.GP(v.GPValue))
:tag('td'):attr('data-sort-value', v.GPPerSC)
  :css('text-align', 'right')
  :wikitext(Icons.GP(Number.autoround(v.GPPerSC)))
end
return tostring(html)
end
end


Line 112: Line 151:


function p.test()
function p.test()
p._getSCItemProfits(1)
return p._getSCItemProfits(1)
Debug.log(getPurchase('Mystic Lantern'))
 
--mw.log(p.getSCValue(item))
--mw.log(p.getSCValue(item))
end
end


return p
return p

Revision as of 18:55, 6 May 2024

This module can be used to get information regarding SC Slayer Coins related shop purchases. For instance, it calculates for much effective profit there can be gained or lost by buying specific items with SC and selling them for GP.


local p = {}

local Shared = require('Module:Shared')
local Shop = require('Module:Shop')
local Items = require('Module:Items')
local Common = require('Module:Common')
local Number = require('Module:Number')
local Icons = require('Module:Icons')
local Debug = require('Module:Debug')

local function getPurchase(itemName)
	local func = 
		function(purchase, name)
			return name == itemName
		end

	local purchList = Shop.getCategoryPurchases(func, 'Slayer')
	if purchList ~= nil and not Shared.tableIsEmpty(purchList) then
		return purchList[1]
	end
end

local function getPurchaseValue(purchase)
	if purchase == nil or purchase.cost == nil or purchase.contains == nil 
	then return 0 end
	
	local costs = purchase.cost
	local sales = purchase.contains
	
	local function getItemValues(items)
		local gpVal = 0
		if items ~= nil then
			for _, item in pairs(items) do
				gpVal = gpVal + Items.getItemValueByID(item['id']) * item['quantity']
			end
		end	
		return gpVal
	end
	
	-- Get total costs (including gp/item costs)
	local gpCost = 0
	if costs.gp ~= nil then	gpCost = costs.gp['cost'] or 0 end
	gpCost = gpCost + getItemValues(costs.items)

	-- Get gross profit from sale
	local gpProfit = getItemValues(sales.items)

	-- Calculate actual profit
	return gpProfit - gpCost
end

-- Returns all SC items that can be sold for GP, and can be bought in bulk.
local function getSellablePurchases()
	-- We need something to sell!
	-- We also want to buy/sell massive quantities for profit!
	local func = 
		function(purchase, name)
			local items = purchase.contains.items
			return purchase.allowQuantityPurchase and 
				items ~= nil and 
				not Shared.tableIsEmpty(items)
		end
		
	return Shop.getCategoryPurchases(func, 'Slayer')
end

local function getMostProfitablePurchases(amount)
	amount = tonumber(amount)
	amount = math.max(amount, 1)
	
	local data = {}
	-- Gather value data about SC Shop Items
	for _, v in pairs(getSellablePurchases()) do
		local gpValue = getPurchaseValue(v)
		local scCost = (v.cost.slayerCoins or {}).cost or 0
		local gpPersc = (scCost ~= 0) and (gpValue / scCost) or (gpValue == 0 and 0 or 0)
		
		table.insert(data, {
			Purchase = v,
			SCCost = scCost,
			GPValue = gpValue,
			GPPerSC = gpPersc,
		})
	end
	
	-- Sort by most profitable to least profitable
	table.sort(data, function(a, b) return a.GPPerSC > b.GPPerSC end)

	-- Keep top x most profitable entries only
	if amount ~= nil then
		local entries = {}
		for i = 1, math.min(#data, amount) do table.insert(entries, data[i]) end
		return entries
	end
	
	return data
end

function p.getSCItemProfits(frame)
end

function p._getSCItemProfits(amount)
	local purchases = getMostProfitablePurchases(amount)

	local html = mw.html.create('table')
		:addClass('wikitable sortable stickyHeader')
	
	html:tag('tr')
			:tag('th'):attr('colspan', 2)
					  :wikitext('Purchase')
			:tag('th'):wikitext('Cost')
			:tag('th'):wikitext('Requirements')
			:tag('th'):wikitext('GP Value')
			:tag('th'):wikitext('GP per SC')
			
	for _, v in pairs(purchases) do
		local purchase =  v.Purchase
		local name = Common.getPurchaseName(purchase)
		local expansionIcon = Shop._getPurchaseExpansionIcon(purchase)
		local costs = Shop.getCostString(purchase.cost, false)
		
		html:tag('tr')
				:tag('td'):addClass('table-img')
						  :wikitext(Common.getPurchaseIcon({purchase, notext=true, size='50'}))
				:tag('td'):attr('data-sort-value', name)
						  :wikitext(expansionIcon)
						  :wikitext(Common.getPurchaseIcon({purchase, noicon=true}))
				:tag('td'):attr('data-sort-value', v.SCCost)
						  :css('text-align', 'right')
						  :wikitext(costs)
				:tag('td'):wikitext(Common.getRequirementString(purchase.purchaseRequirements, 'None'))
				:tag('td'):attr('data-sort-value', v.GPValue)
						  :css('text-align', 'right')
						  :wikitext(Icons.GP(v.GPValue))
				:tag('td'):attr('data-sort-value', v.GPPerSC)
						  :css('text-align', 'right')
						  :wikitext(Icons.GP(Number.autoround(v.GPPerSC)))
	end
	
	return tostring(html)
end

function p.getSCValue(item)
	local purchase = getPurchase(item)
	if purchase == nil then
		return Shared.printError("No Slayer Shop item exists with the name: " .. item)
	end
	
	return getPurchaseValue(purchase)
end

function p.test()
	return p._getSCItemProfits(1)

	--mw.log(p.getSCValue(item))
end

return p