Module:Loops

From Legends of Idleon Wiki

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

local loops = {}

local function explode(list, delimiter)
    if delimiter == nil then
		delimiter = "%s"
	end
	t = {}
	for str in string.gmatch(list, "([^" .. delimiter .. "]+)") do
		table.insert(t, str)
	end
	return t
end

local function numFormater(arg)
	if arg == "Lots" then
		return n
	end
	local n = tonumber(arg)
    if n >= 10^27 then
        return string.format("%.2fAE", n / 10^27)
    elseif n >= 10^24 then
        return string.format("%.2fAD", n / 10^24)
    elseif n >= 10^21 then
        return string.format("%.2fAC", n / 10^21)
    elseif n >= 10^18 then
        return string.format("%.2fAB", n / 10^18)
    elseif n >= 10^15 then
        return string.format("%.2fAA", n / 10^15)
    elseif n >= 10^12 then
        return string.format("%.2fT", n / 10^12)
    elseif n >= 10^9 then
        return string.format("%.2fB", n / 10^9)
    elseif n >= 10^6 then
        return string.format("%.2fM", n / 10^6)
    elseif n >= 10^4 then
        return string.format("%.1fK", n / 10^3)
    else
        return tostring(n)
    end
end

function loops.coinDisplay(frame)
	local n = frame.args.coins
	local ret = ""
	local i = 18
	local coins = {"Copper", "Silver", "Gold", "Platinum", "Dementia", "Void", "Lustre", "Starfire", "Dreadlo", "Godshard"}
	
	repeat
		local len = n:len()
		if len > i then
			local num = tonumber(n:sub(1, len - i))
			if num > 0 then
				ret = ret .. "[[File:" .. coins[(i+2)/2] .. " Coin.png|link=]] " .. num .. " "
			end
			n = n:sub(-i)
		end
		i = i - 2
	until(i < 0)
	
	return ret
end

function loops.bossAttacks(frame)
	local frameArgs = frame.args
	local attackList = explode(frameArgs.attackList,",")
	local damageList = explode(frameArgs.damageList,",")
	
	local ret = ""
	for i = 1, #attackList do
		ret = ret .. "<dt>" .. attackList[i]
		if(damageList[i])
    	then
			ret = ret .. "</dt><dd><span class='simple-tooltip simple-tooltip-inline tooltipstered' data-simple-tooltip='&lt;span class=&apos;text-only&apos;&gt;"..damageList[i] .. "&lt;/span&gt;'>" .. numFormater(damageList[i]) .. "</span></dd>"
		else
			ret = ret .. "</dt><dd>N.A.</dd>"
		end
	end

	return ret
end

function loops.parseLoop(frame)
	local ret = ""
	local frameArgs = frame.args
	local delimiter = frameArgs.delimiter
	local list1, list2, list3, list4, returnDelim
	if(frameArgs.list1) then list1 = explode(frameArgs.list1, delimiter) end
	if(frameArgs.list2) then list2 = explode(frameArgs.list2, delimiter) end
	if(frameArgs.list3) then list3 = explode(frameArgs.list3, delimiter) end
	if(frameArgs.list4) then list4 = explode(frameArgs.list4, delimiter) end
	if(frameArgs.returnDelimiter) then returnDelim = frameArgs.returnDelimiter end
	local replaceString = frameArgs.replaceString
	
	for i = 1, #list1 do
		if(i > 1 and returnDelim) then ret = ret .. returnDelim end
		local tStr = replaceString
		if(list1) then tStr = tStr:gsub("%(1%)", list1[i]) end
		if(list2) then tStr = tStr:gsub("%(2%)", list2[i]) end
		if(list3) then tStr = tStr:gsub("%(3%)", list3[i]) end
		if(list4) then tStr = tStr:gsub("%(4%)", list4[i]) end
		
		ret = ret .. tStr
	end
	
	return ret
end

VariablesLua = mw.ext.VariablesLua
function loops.numberNabber(frame)
	local ret = ""
	local frameArgs = frame.args
	local searchText = frameArgs.searchText
	local temp = frameArgs.list
	-- Remove image size and tooltip data
	local list = explode(temp:gsub("%d+px","px"):gsub('data\-simple\-tooltip.+\>"\>', ''), ",")
	local n
	
	for i = 1, #list do
		if(list[i]:find(searchText, 1, true)) then
			-- These are for testing and should be disabled when not in use.
			--local varValue = list[i]:sub(1, 2)
			--VariablesLua.vardefine( "nabberTest", varValue )
			if (list[i]:sub(1, 2):match("%d")) then
				n = tonumber(list[i]:match('%d[%d.,]*'))
			elseif(list[i]:find(": ", 1, true)) then 
				n = tonumber(list[i]:match('.*%f[%d.](%d*%.?%d+)'))
			end
			if (n >= 10^4) then
				ret = "<div class='tooltip'>" .. numFormater(n) .. "<span>" .. n .. "</span></div>"
			else
				ret = n
			end
			if(list[i]:match("[Cc]raft")) then ret = ret .. " (Crafted)"	end
			if(list[i]:match("[Bb]uy")) then ret = ret .. " (Bought)"	end
			return ret
		end
	end
	
	return ret
end

function loops.oneVariableTemplateParser(frame)
	local ret = ""
	local frameArgs = frame.args
	local delimiter = frameArgs.delimiter
	local list = explode(frameArgs.list, delimiter)
	local cols
	local template = frameArgs.template
	if(frameArgs.colCount) then cols = frameArgs.colCount else cols = 3 end
	if(frameArgs.divClass) then ret = '<div class="' .. frameArgs.divClass .. '">' else ret = '<div class="DetailboxGrid">' end
	for i = 1, #list do
		ret = ret .. frame:expandTemplate{title = template, args = { list[i] } };
	end
	ret = ret .. '</div>'
	return ret
end

return loops