<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AShop_calculator</id>
	<title>Module:Shop calculator - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.runerealm.org/index.php?action=history&amp;feed=atom&amp;title=Module%3AShop_calculator"/>
	<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:Shop_calculator&amp;action=history"/>
	<updated>2026-04-25T22:25:32Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.1</generator>
	<entry>
		<id>https://wiki.runerealm.org/index.php?title=Module:Shop_calculator&amp;diff=35114&amp;oldid=prev</id>
		<title>Alex: Created page with &quot;local p = {}  local paramTest = require(&#039;Module:Paramtest&#039;) local yesNo = require(&#039;Module:Yesno&#039;)  function truncate(num) 	local whole, decimal = math.modf(num + 0.000001) -- addition is to prevent floating point rounding issues 	return whole end  function sell(basePrice, shopBuysAt, delta, baseStock, currentStock, quantitySold, itemValue) 	local total = 0 	 	local upperBound = truncate(itemValue + (shopBuysAt * itemValue)) -- Item value + base price shop buys at 	local...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.runerealm.org/index.php?title=Module:Shop_calculator&amp;diff=35114&amp;oldid=prev"/>
		<updated>2024-10-16T23:13:01Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;local p = {}  local paramTest = require(&amp;#039;Module:Paramtest&amp;#039;) local yesNo = require(&amp;#039;Module:Yesno&amp;#039;)  function truncate(num) 	local whole, decimal = math.modf(num + 0.000001) -- addition is to prevent floating point rounding issues 	return whole end  function sell(basePrice, shopBuysAt, delta, baseStock, currentStock, quantitySold, itemValue) 	local total = 0 	 	local upperBound = truncate(itemValue + (shopBuysAt * itemValue)) -- Item value + base price shop buys at 	local...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;local p = {}&lt;br /&gt;
&lt;br /&gt;
local paramTest = require(&amp;#039;Module:Paramtest&amp;#039;)&lt;br /&gt;
local yesNo = require(&amp;#039;Module:Yesno&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
function truncate(num)&lt;br /&gt;
	local whole, decimal = math.modf(num + 0.000001) -- addition is to prevent floating point rounding issues&lt;br /&gt;
	return whole&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function sell(basePrice, shopBuysAt, delta, baseStock, currentStock, quantitySold, itemValue)&lt;br /&gt;
	local total = 0&lt;br /&gt;
	&lt;br /&gt;
	local upperBound = truncate(itemValue + (shopBuysAt * itemValue)) -- Item value + base price shop buys at&lt;br /&gt;
	local lowerBound = truncate(itemValue * .1)&lt;br /&gt;
	&lt;br /&gt;
	local function understockSellPrice(stockDelta)&lt;br /&gt;
		return truncate(itemValue * (shopBuysAt + (delta * stockDelta)))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function overstockSellPrice(stockDelta)&lt;br /&gt;
		return truncate(itemValue * (shopBuysAt - (delta * stockDelta)))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function applyBounds(num)&lt;br /&gt;
		if(num &amp;gt; upperBound) then&lt;br /&gt;
			return upperbound&lt;br /&gt;
		elseif(num &amp;lt;= lowerBound) then&lt;br /&gt;
			return lowerBound&lt;br /&gt;
		else&lt;br /&gt;
			return num&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for i = quantitySold, 1, -1 do&lt;br /&gt;
		local profit = 0&lt;br /&gt;
		if(currentStock &amp;lt; baseStock) then&lt;br /&gt;
			profit = applyBounds(understockSellPrice(baseStock - currentStock))&lt;br /&gt;
		elseif(currentStock == baseStock) then&lt;br /&gt;
			profit = applyBounds(basePrice)&lt;br /&gt;
		elseif(currentStock &amp;gt; baseStock) then&lt;br /&gt;
			profit = applyBounds(overstockSellPrice(currentStock - baseStock))&lt;br /&gt;
		end&lt;br /&gt;
		total = total + profit&lt;br /&gt;
		currentStock = currentStock + 1&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	return total, &amp;#039;&amp;#039;&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function buy(basePrice, shopSellsAt, delta, baseStock, currentStock, quantityBought, itemValue)&lt;br /&gt;
	local total = 0&lt;br /&gt;
	local warningString = &amp;#039;&amp;#039;&lt;br /&gt;
	&lt;br /&gt;
	local upperBound = truncate(itemValue * (shopSellsAt + 5)) -- +500% base price&lt;br /&gt;
	local lowerBound = truncate(itemValue * (shopSellsAt - 1)) -- -100% base price&lt;br /&gt;
	local lowererBound = truncate(itemValue * .1)&lt;br /&gt;
	local minimumBound = 1&lt;br /&gt;
	&lt;br /&gt;
	if(currentStock == 0) then&lt;br /&gt;
		return 0, &amp;#039;You cannot purchase from 0 stock.&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	if(quantityBought &amp;gt; currentStock) then&lt;br /&gt;
		quantityBought = currentStock&lt;br /&gt;
		warningString = &amp;#039;You can only purchase &amp;#039; .. currentStock .. &amp;#039; items. Purchasing &amp;#039; .. quantityBought .. &amp;#039; items instead.&amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function overstockBuyPrice(stockDelta)&lt;br /&gt;
		return truncate(itemValue / (stockDelta * delta + shopSellsAt))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function understockBuyPrice(stockDelta)&lt;br /&gt;
		return truncate(itemValue * (stockDelta * delta + shopSellsAt))&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	local function applyBounds(num)&lt;br /&gt;
		if(num &amp;gt; upperBound) then&lt;br /&gt;
			return upperBound&lt;br /&gt;
		elseif(num &amp;lt; minimumBound) then&lt;br /&gt;
			return minimumBound&lt;br /&gt;
		elseif(num &amp;lt; lowererBound) then&lt;br /&gt;
			return lowererBound&lt;br /&gt;
		elseif(num &amp;lt; lowerBound) then&lt;br /&gt;
			return lowerBound&lt;br /&gt;
		else&lt;br /&gt;
			return num&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	for i = quantityBought, 1, -1 do&lt;br /&gt;
		local cost = 0&lt;br /&gt;
		if(currentStock &amp;gt; baseStock) then&lt;br /&gt;
			cost = applyBounds(overstockBuyPrice(currentStock - baseStock))&lt;br /&gt;
		elseif(currentStock == baseStock) then&lt;br /&gt;
			cost = applyBounds(basePrice)&lt;br /&gt;
		elseif(currentStock &amp;lt; baseStock) then&lt;br /&gt;
			cost = applyBounds(understockBuyPrice(baseStock - currentStock))&lt;br /&gt;
		end&lt;br /&gt;
		total = total + cost&lt;br /&gt;
		currentStock = currentStock - 1&lt;br /&gt;
	end&lt;br /&gt;
	&lt;br /&gt;
	return total, warningString&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
function p._main(args)&lt;br /&gt;
	local buying = (args.action == &amp;#039;Buying&amp;#039;)&lt;br /&gt;
	local basePrice = buying and paramTest.default_to(tonumber(args.priceShopSellsAt), 1) or paramTest.default_to(tonumber(args.priceShopBuysAt), 0)&lt;br /&gt;
	local exchangeAt = buying and paramTest.default_to(tonumber(args.shopSellsAtValue), 1) or paramTest.default_to(tonumber(args.shopBuysAtValue), 0)&lt;br /&gt;
	local delta = paramTest.default_to(tonumber(args.delta), 0)&lt;br /&gt;
	local baseStock = paramTest.default_to(tonumber(args.baseStock), 1)&lt;br /&gt;
	local currentStock = paramTest.default_to(tonumber(args.currentStock), 1)&lt;br /&gt;
	local quantityExchanged = buying and paramTest.default_to(tonumber(args.buyQuantity), 1) or paramTest.default_to(tonumber(args.sellQuantity), 1)&lt;br /&gt;
&lt;br /&gt;
	exchangeAt = exchangeAt / 100&lt;br /&gt;
	delta = delta / 100&lt;br /&gt;
	&lt;br /&gt;
	local itemValue = truncate(basePrice / exchangeAt)&lt;br /&gt;
&lt;br /&gt;
	local totalCoins = 0&lt;br /&gt;
	local warning = &amp;#039;&amp;#039;&lt;br /&gt;
	&lt;br /&gt;
	if(buying) then&lt;br /&gt;
		totalCoins, warning = buy(basePrice, exchangeAt, delta, baseStock, currentStock, quantityExchanged, itemValue)&lt;br /&gt;
	else -- selling&lt;br /&gt;
		totalCoins, warning = sell(basePrice, exchangeAt, delta, baseStock, currentStock, quantityExchanged, itemValue)&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	return warning .. &amp;#039;\n&amp;#039; .. tostring(totalCoins)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
--[[ DEBUG example: Lowes Sells at: 100.0% • Buys at: 55.0% • Change per: 1.0% Stock: 600 Sold at: 80 Bought at: 44&lt;br /&gt;
p._main({ action = &amp;#039;Buying&amp;#039;, priceShopSellsAt = 80, shopSellsAtValue = 100, delta = 1, baseStock = 600, currentStock = 601, buyQuantity = 50 }) -- result 4920&lt;br /&gt;
p._main({ action = &amp;#039;Buying&amp;#039;, priceShopSellsAt = 50, shopSellsAtValue = 100, delta = 3, baseStock = 10, currentStock = 5, buyQuantity = 1 }) -- result 57&lt;br /&gt;
p._main({ action = &amp;#039;Selling&amp;#039;, priceShopBuysAt = 80, shopBuysAtValue = 100, delta = 1, baseStock = 600, currentStock = 600, sellQuantity = 50 }) -- result should be 3000&lt;br /&gt;
p._main({ action = &amp;#039;Selling&amp;#039;, priceShopBuysAt = 44, shopBuysAtValue = 55, delta = 1, baseStock = 600, currentStock = 600, sellQuantity = 50 }) -- result is 1210&lt;br /&gt;
p._main({ action = &amp;#039;Selling&amp;#039;, priceShopBuysAt = 36, shopBuysAtValue = 60, delta = 2, baseStock = 0, currentStock = 300, sellQuantity = 1 }) -- result is 6&lt;br /&gt;
--]]&lt;br /&gt;
&lt;br /&gt;
function p.main(frame)&lt;br /&gt;
	local args = frame.args&lt;br /&gt;
	--mw.logObject(args)&lt;br /&gt;
	return p._main(args)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>Alex</name></author>
	</entry>
</feed>