Module:Barrows item repairs calculator

From Old School RuneScape Wiki
Jump to: navigation, search
Module documentation
This documentation is transcluded from Template:No documentation/doc. [edit] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Barrows item repairs calculator/doc. [edit]
Module:Barrows item repairs calculator requires Module:Coins.
Module:Barrows item repairs calculator requires Module:Paramtest.

local Paramtest = require( 'Module:Paramtest' )
local Coins = require( 'Module:Coins' )

local p = {}

local helmetCost = 60000
local bodyCost = 90000
local legsCost = 80000
local weaponCost = 100000

function p.invoke_main( frame )
	local results = p.main( frame:getParent().args )
	
	return p.resultsTable(results)
end

function p.main( args )
	local smithingLevel, spicyStew, smithingCape, dwarvenStoutm, helmetState, helmetPercent, bodyState, bodyPercent, legsState, legsPercent, weaponState, weaponPercent = Paramtest.defaults{
		{ args.smithingLevel, 1 }, --- 1 - 99
		{ args.spicyStew, 0 }, --- 0 - 5 in case of using a spicy stew
		{ args.smithingCape, 0 }, --- Smithing cape acts as regular Dwarven stout and Smithing cape since boosts cannot stack
		{ args.dwarvenStoutm, 0 }, --- Either 0 or 2, since Dwarven stout (m) adds a +2 boost
		{ args.helmetState, -1 }, --- The percent that shows when you use Check on a barrows item ( % until next degradation )
		{ args.helmetPercent, 1 }, --- 100,75,50,25 are regular states, 0 is fully degraded and -1 is to not take into account
		{ args.bodyState, -1 },
		{ args.bodyPercent, 1 },
		{ args.legsState, -1 },
		{ args.legsPercent, 1 },
		{ args.weaponState, -1 },
		{ args.weaponPercent, 1 }
	}
	
	smithingLevel = tonumber( smithingLevel )
	spicyStew = tonumber( spicyStew )
	smithingCape = tonumber( smithingCape )
	dwarvenStoutm = tonumber( dwarvenStoutm )
	helmetState = tonumber( helmetState )
	helmetPercent = tonumber( helmetPercent )
	--- In case of reverting back to having each item it's own individual inputs
	--bodyState = tonumber( bodyState )
	--bodyPercent = tonumber( bodyPercent )
	--legsState = tonumber( legsState )
	--legsPercent = tonumber( legsPercent )
	--weaponState = tonumber( weaponState )
	--weaponPercent = tonumber( weaponPercent )
	bodyState = tonumber( helmetState )
	bodyPercent = tonumber( helmetPercent )
	legsState = tonumber( helmetState )
	legsPercent = tonumber( helmetPercent )
	weaponState = tonumber( helmetState )
	weaponPercent = tonumber( helmetPercent )
	
	-- Get the regular costs of each item
	local regularCosts = p.getRegularItemCosts( {
		helmetState = helmetState,
		helmetPercent = helmetPercent,
		bodyState = bodyState,
		bodyPercent = bodyPercent,
		legsState = legsState,
		legsPercent = legsPercent,
		weaponState = weaponState,
		weaponPercent = weaponPercent
	} )

	local smithingCosts = p.getSmithingItemCosts(regularCosts, smithingLevel+spicyStew+smithingCape+dwarvenStoutm)
	---mw.log("Start cost (regular): ".. regularCosts.helmet.startCost)
	---mw.log("End cost (regular): ".. regularCosts.helmet.endCost)
	---mw.log("Start cost (smithing): ".. smithingCosts.helmet.startCost)
	---mw.log("End cost (smithing): ".. smithingCosts.helmet.endCost)
	return {regularCosts = regularCosts, smithingCosts = smithingCosts}
end

function p.getRegularItemCosts( args )
	local helmetState, helmetPercent, bodyState, bodyPercent, legsState, legsPercent, weaponState, weaponPercent = Paramtest.defaults{
		{ args.helmetState, -1 },
		{ args.helmetPercent, 1 },
		{ args.bodyState, -1 },
		{ args.bodyPercent, 1 },
		{ args.legsState, -1 },
		{ args.legsPercent, 1 },
		{ args.weaponState, -1 },
		{ args.weaponPercent, 1 }
	}
	
	return { 
		helmet = p.getRegularSingleCost(helmetState, helmetPercent, helmetCost),
		body = p.getRegularSingleCost(bodyState, bodyPercent, bodyCost),
		legs = p.getRegularSingleCost(legsState, legsPercent, legsCost),
		weapon = p.getRegularSingleCost(weaponState, weaponPercent, weaponCost)
	}
end

function p.getRegularSingleCost( itemState, itemPercent, itemFullyDegradedCost )
	local itemStartCost = 0
	local itemEndCost = 0
	local itemCostIncrements = itemFullyDegradedCost/1000
	if itemState ~= -1 and itemState ~= 0 then
		if itemPercent ~= 100 then
			if itemPercent % 2 == 0 then
				-- For percent that are even numbers
				itemStartCost = (itemFullyDegradedCost * ((100-itemState)/100))+((itemCostIncrements*2)*math.floor((100-itemPercent)/2))+((itemCostIncrements*3)*(math.floor((100-itemPercent)/2)))
				itemEndCost = itemStartCost+(itemCostIncrements*2)
			else
				-- For percent that are odd numbers
				itemStartCost = (itemFullyDegradedCost * ((100-itemState)/100))+((itemCostIncrements*2)*math.floor((100-itemPercent)/2))+((itemCostIncrements*3)*(math.floor((100-itemPercent)/2)+1))
				itemEndCost = itemStartCost+itemCostIncrements
			end
		else
			-- When item is at 100% in any degraded state, determine it manually
			itemStartCost = itemFullyDegradedCost * ((100-itemState)/100)
			itemEndCost = itemStartCost+(itemCostIncrements*2)
		end
	elseif itemState == 0 then
		-- If state = 0 then we assume it is fully degraded
		itemStartCost = itemFullyDegradedCost
		itemEndCost = 0
	else
		itemStartCost = 0
		itemEndCost = 0
	end
	return {
		startCost = itemStartCost,
		endCost = itemEndCost
	}
end


function p.getSmithingItemCosts( regularCosts, smithingTotalLevel )
	return { 
		helmet = p.getSmithingSingleCost(regularCosts.helmet, smithingTotalLevel),
		body = p.getSmithingSingleCost(regularCosts.body, smithingTotalLevel),
		legs = p.getSmithingSingleCost(regularCosts.legs, smithingTotalLevel),
		weapon = p.getSmithingSingleCost(regularCosts.weapon, smithingTotalLevel)
	}
end

function p.getSmithingSingleCost( itemRegularCosts, smithingTotalLevel )
	-- Get the smithing discount
	local smithingDiscount = (1-(smithingTotalLevel/200))
	return {
		-- Round up weirdly enough
		startCost = math.ceil(itemRegularCosts.startCost*smithingDiscount),
		endCost = math.ceil(itemRegularCosts.endCost*smithingDiscount)
	}
end

function p.resultsTable(results)
	local resultsDiv = mw.html.create( 'div' )
	
	local helmetStartAmountSaved = results.regularCosts.helmet.startCost - results.smithingCosts.helmet.startCost
	local helmetEndAmountSaved = results.regularCosts.helmet.endCost - results.smithingCosts.helmet.endCost
	
	local bodyStartAmountSaved = results.regularCosts.body.startCost - results.smithingCosts.body.startCost
	local bodyEndAmountSaved = results.regularCosts.body.endCost - results.smithingCosts.body.endCost
	
	local legsStartAmountSaved = results.regularCosts.legs.startCost - results.smithingCosts.legs.startCost
	local legsEndAmountSaved = results.regularCosts.legs.endCost - results.smithingCosts.legs.endCost
	
	local weaponStartAmountSaved = results.regularCosts.weapon.startCost - results.smithingCosts.weapon.startCost
	local weaponEndAmountSaved = results.regularCosts.weapon.endCost - results.smithingCosts.weapon.endCost
	
	local resultsTable = mw.html.create('table')
	
	local header = mw.html.create('tr')
	header:tag( 'th' )
		:wikitext( '<small>Costs to repair<br/>barrows equipment</small>' )
	:done()
	:tag( 'th' )
		:wikitext( 'NPC' )
	:done()
	:tag( 'th' )
		:wikitext( 'POH Armour Stand' )
	:done()
	:tag( 'th' )
		:wikitext( 'Amount Saved' )
	:done()
	
	local helmet
	local body
	local legs
	local weapon
	
	--Helmet row
	if results.regularCosts.helmet.startCost ~= 0 then
		if results.regularCosts.helmet.endCost ~= 0 then
		helmet = mw.html.create('tr')
		helmet:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Helmet' )
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.regularCosts.helmet.startCost) .. " - " .. Coins._amount(results.regularCosts.helmet.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.smithingCosts.helmet.startCost) .. " - " .. Coins._amount(results.smithingCosts.helmet.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(helmetStartAmountSaved) .. " - " .. Coins._amount(helmetEndAmountSaved))
			:done()
		:done()
		elseif results.regularCosts.helmet.endCost == 0 then
			helmet = mw.html.create('tr')
			helmet:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Helmet' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.helmet.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.helmet.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(helmetStartAmountSaved))
				:done()
			:done()
		end
	elseif results.regularCosts.helmet.startCost == 0 and results.regularCosts.helmet.endCost ~= 0 then
		helmet = mw.html.create('tr')
		helmet:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Helmet' )
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.regularCosts.helmet.startCost) .. " - " .. Coins._amount(results.regularCosts.helmet.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.smithingCosts.helmet.startCost) .. " - " .. Coins._amount(results.smithingCosts.helmet.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(helmetStartAmountSaved) .. " - " .. Coins._amount(helmetEndAmountSaved))
			:done()
		:done()
	end
	-- Body row
	if results.regularCosts.body.startCost ~= 0 then
		if results.regularCosts.body.endCost ~= 0 then
			body = mw.html.create('tr')
			body:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Body' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.body.startCost) .. " - " .. Coins._amount(results.regularCosts.body.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.body.startCost) .. " - " .. Coins._amount(results.smithingCosts.body.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(bodyStartAmountSaved) .. " - " .. Coins._amount(bodyEndAmountSaved))
				:done()
			:done()
		elseif results.regularCosts.body.endCost == 0 then
			body = mw.html.create('tr')
			body:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Body' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.body.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.body.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(bodyStartAmountSaved))
				:done()
			:done()
		end
	elseif results.regularCosts.body.startCost == 0 and results.regularCosts.body.endCost ~= 0 then
		body = mw.html.create('tr')
		body:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Body' )
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.regularCosts.body.startCost) .. " - " .. Coins._amount(results.regularCosts.body.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.smithingCosts.body.startCost) .. " - " .. Coins._amount(results.smithingCosts.body.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(bodyStartAmountSaved) .. " - " .. Coins._amount(bodyEndAmountSaved))
			:done()
		:done()
	end
	
	-- Legs row
	if results.regularCosts.legs.startCost ~= 0 then
		if results.regularCosts.legs.endCost ~= 0 then
			legs = mw.html.create('tr')
			legs:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Legs' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.legs.startCost) .. " - " .. Coins._amount(results.regularCosts.legs.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.legs.startCost) .. " - " .. Coins._amount(results.smithingCosts.legs.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(legsStartAmountSaved) .. " - " .. Coins._amount(legsEndAmountSaved))
				:done()
			:done()
		elseif results.regularCosts.legs.endCost == 0 then
			legs = mw.html.create('tr')
			legs:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Legs' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.legs.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.legs.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(legsStartAmountSaved))
				:done()
			:done()
		end
	elseif results.regularCosts.body.startCost == 0 and results.regularCosts.body.endCost ~= 0 then
		legs = mw.html.create('tr')
		legs:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Legs' )
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.regularCosts.legs.startCost) .. " - " .. Coins._amount(results.regularCosts.legs.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.smithingCosts.legs.startCost) .. " - " .. Coins._amount(results.smithingCosts.legs.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(legsStartAmountSaved) .. " - " .. Coins._amount(legsEndAmountSaved))
			:done()
		:done()
	end
	
	-- Weapon row
	if results.regularCosts.weapon.startCost ~= 0 then
		if results.regularCosts.weapon.endCost ~= 0 then
			weapon = mw.html.create('tr')
			weapon:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Weapon' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.weapon.startCost) .. " - " .. Coins._amount(results.regularCosts.weapon.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.weapon.startCost) .. " - " .. Coins._amount(results.smithingCosts.weapon.endCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(weaponStartAmountSaved) .. " - " .. Coins._amount(weaponEndAmountSaved))
				:done()
			:done()
		elseif results.regularCosts.weapon.endCost == 0 then
			weapon = mw.html.create('tr')
			weapon:tag( 'tr' )
				:tag( 'th' )
					:wikitext( 'Weapon' )
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.regularCosts.weapon.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(results.smithingCosts.weapon.startCost))
				:done()
				:tag( 'td' )
					:wikitext( Coins._amount(weaponStartAmountSaved))
				:done()
			:done()
		end
	elseif results.regularCosts.body.startCost == 0 and results.regularCosts.body.endCost ~= 0 then
		weapon = mw.html.create('tr')
		weapon:tag( 'tr' )
			:tag( 'th' )
				:wikitext( 'Weapon' )
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.regularCosts.weapon.startCost) .. " - " .. Coins._amount(results.regularCosts.weapon.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(results.smithingCosts.weapon.startCost) .. " - " .. Coins._amount(results.smithingCosts.weapon.endCost))
			:done()
			:tag( 'td' )
				:wikitext( Coins._amount(weaponStartAmountSaved) .. " - " .. Coins._amount(weaponEndAmountSaved))
			:done()
		:done()
	end
	
	local regularStartTotal = results.regularCosts.helmet.startCost+results.regularCosts.body.startCost+results.regularCosts.legs.startCost+results.regularCosts.weapon.startCost
	local smithingStartTotal = results.smithingCosts.helmet.startCost+results.smithingCosts.body.startCost+results.smithingCosts.legs.startCost+results.smithingCosts.weapon.startCost
	local amountStartSavedTotal = helmetStartAmountSaved+bodyStartAmountSaved+legsStartAmountSaved+weaponStartAmountSaved
	local total = mw.html.create('tr')
	
	if results.regularCosts.helmet.endCost ~=0 then
		
		local regularEndTotal = results.regularCosts.helmet.endCost+results.regularCosts.body.endCost+results.regularCosts.legs.endCost+results.regularCosts.weapon.endCost
		local smithingEndTotal = results.smithingCosts.helmet.endCost+results.smithingCosts.body.endCost+results.smithingCosts.legs.endCost+results.smithingCosts.weapon.endCost
		local amountEndSavedTotal = helmetEndAmountSaved+bodyEndAmountSaved+legsEndAmountSaved+weaponEndAmountSaved
		
		total:tag( 'tr' )
			:tag( 'th' )
				:tag( 'b' )
					:wikitext( 'Total' )
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(regularStartTotal) .. " - " .. Coins._amount(regularEndTotal))
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(smithingStartTotal) .. " - " .. Coins._amount(smithingEndTotal))
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(amountStartSavedTotal) .. " - " .. Coins._amount(amountEndSavedTotal) )
				:done()
			:done()
		:done()
	else 
		total:tag( 'tr' )
			:tag( 'th' )
				:tag( 'b' )
					:wikitext( 'Total' )
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(regularStartTotal))
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(smithingStartTotal))
				:done()
			:done()
			:tag( 'td' )
				:tag( 'b' )
					:wikitext( Coins._amount(amountStartSavedTotal) )
				:done()
			:done()
		:done()
	end
	resultsTable:addClass( 'wikitable' ):addClass( 'align-center-1' )
		:node(header)
		:node(helmet)
		:node(body)
		:node(legs)
		:node(weapon)
		:node(total)
	:done()
	resultsDiv:node(resultsTable)
	
	return resultsDiv
end

return p