Module:MoneyMakingGuide: Difference between revisions

From Melvor Idle
(Add experience rewards first version)
mNo edit summary
Line 168: Line 168:
table.insert(layoutLines, span)
table.insert(layoutLines, span)
end
end
return layoutLines
end
end



Revision as of 00:41, 22 March 2024

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

local p = {}

local shared = require('Module:Shared')
local num = require('Module:Number')
local paramtest = require('Module:Shared/Paramtest')
local itemdb = require('Module:Items')
local icons = require('Module:Icons')

-- Constants
local MaxDynamicArgs = 20
local AmountSuffix = 'amount'
local ValueSuffix = 'value'
local SkillPrefix = 'skillExp'

-- Determines the order of Icons
local DLCParams = { 
	toth  = icons.TotH(), 
	aod = icons.AoD(), 
	ita = icons.ItA()
}

--- Formats a wikicode string to be bold and red
local function formatError(errorMessage)
	local eror = mw.html.create('span')
		:wikitext("'''")
		:css('color', 'red')
		:wikitext(errorMessage)
		:wikitext("'''")
		:done()
		
	return tostring(error)
end

--- Formats a given string to TitleCase.
-- @param name (string) String to format.
-- @return (string) Formatted string, or the input if it's not a string.
local function formatName(name)
	if type(name) == 'string' then
		return shared.titleCase(name)
	end
	
	return name
end

local function getSkillExpIcon(skillName, expValue)
	if expValue == nil then error("expValue is nil") end
	expValue = num.formatnum(expValue)
	
	return icons.Icon({
		skillName,
		expValue,
		type='skill',
		nolink=true})
end

--- Parses the input and output items from the provided arguments.
-- @param args (table) Passed args table from the caller.
-- @param prefix (string) The prefix that determines input or output item parsing.
-- @return (table) A table containing parsed item information.
local function parseItemInOut(args, prefix)
	local items = {}
	local totalValue = 0
	
	for i = 1, MaxDynamicArgs do
		local numPrefix = prefix .. i

		-- Stop parsing. Could cause problems if user input skips indexes.
		if paramtest.is_empty(args[numPrefix]) then
			break
		end
			
		local pName = formatName(args[numPrefix])
		local pAmount = args[numPrefix .. AmountSuffix]
		local pValue = args[numPrefix .. ValueSuffix]

		-- Values *should* always exit this function with a non nil value.
		if paramtest.has_content(pAmount) then
			pAmount = tonumber(pAmount)
		end
		
		if not paramtest.has_content(pValue) or tonumber(pValue) == nil then
			pValue = itemdb.getItemValue(pName)
		else
			pValue = tonumber(pValue)
		end
		
		-- Default to 0 to add to totals.
		local totVal = pValue or 0
		local totAmt = pAmount or 0
		totalValue = totalValue + (totVal * totAmt)

		table.insert(items, {
			prmNumber = i,
			name = pName, 
			amount = pAmount, 
			value = pValue})
	end
	
	return {
		TotalValue = totalValue, 
		Items = items 
	}
end

--- Parses the skill experience from the provided arguments.
-- @param args (table) Passed args table from the caller.
-- @return (table) A table containing parsed skill experience information.
local function parseExp(args)
	local skills = {}
	for i = 1, MaxDynamicArgs do
		local skillPrefix = 'skillExp' .. i
		
		-- Stop parsing. Could cause problems if user input skips indexes.
		if paramtest.is_empty(args[skillPrefix]) then
			break
		end
			
		local pSkill = formatName(args[skillPrefix])
		local pExp = args[skillPrefix .. AmountSuffix]

		if paramtest.has_content(pExp) then 
			pExp = tonumber(pExp)
		end
		
		table.insert(skills, {
			prmNumber = i,
			name = pSkill, 
			exp = pExp})
	end
	
	return skills
end

--- Builds the section of the mmg table that shows the input and output items.
-- @param items (table) A table containing items
-- @return (string) The HTML representation of the item table section.
local function buildItemTable(items)
end

--- Builds the section of the mmg table that shows the skill experience gained.
-- @param items (table) A table containing skills and experience values.
-- @return (string) The HTML representation of the item table section.
local function buildExpLayout(skills)
	local layoutLines = {}
	for _, skillLine in pairs(skills) do
		local hasInvalidExp = false
		local hasInvalidName = false
		local span = mw.html.create('div')
	
		if skillLine.exp == nil or skillLine.exp == 0 then
			hasInvalidExp = true
		end
		
		local skillIcon = getSkillExpIcon(skillLine.name, skillLine.exp or 0)
		
		if skillIcon == nil then
			hasInvalidName = true
		end
		
		if hasInvalidExp or hasInvalidName then
		else
			span:wikitext(skillIcon)
		end

		mw.log(skillIcon)
		mw.log(skillLine.name)

		table.insert(layoutLines, span)
	end
	
	return layoutLines
end

local function buildMMGTable(args)
	-- Parse arguments.
	local pSkills = parseExp(args)
	local pInputs = parseItemInOut(args, 'input')
	local pOutputs = parseItemInOut(args, 'output')
	local dlcIcons = p.getDLCIcons(args['dlc'], ' ')

	local tbl = mw.html.create()

	local html = tbl:tag("table")
    :addClass("wikitable")
    :attr('style', 'width: 100%; text-align: center;')
    :tag("tr")
        :tag("td")
            :attr("colspan", 2)
            :wikitext(table.concat(dlcIcons) .. ' ')
            :wikitext(args['guideName'])
    :tag("tr")
        :tag("th")
            :attr("colspan", 2)
            :wikitext("Requirements")
    :tag("tr")
        :tag("th")
            :wikitext("Skills")
        :tag("th")
            :wikitext("Recommended")
    :tag("tr")
        :tag("td")
        :wikitext(paramtest.default_to(args['skills'], 'None')) 
    :tag("td")
        :attr("rowspan", 3)
        :wikitext(paramtest.default_to(args['other'], 'None')) 
    :tag("tr")
        :tag("th")
            :wikitext("Items")
    :tag("tr")
        :tag("td")
            :wikitext(paramtest.default_to(args['items'], 'None')) 
    :tag("tr")
        :tag("th")
            :attr("colspan", 2)
            :wikitext("Results")
    :tag("tr")
        :tag("th")
            :wikitext("Profit")
        :tag("th")
            :wikitext("Experience gained")
    :tag("tr")
        :tag("td")
        	if args['profit'] then
        		html:wikitext(icons.GP(args['profit']))
        	else
        		html:wikitext(icons.GP(pOutputs.TotalValue - pInputs.TotalValue))
        	end
        	html = html
        :done()
        :tag("td")
        	if pSkills ~= nil then
        		for _, v in ipairs(buildExpLayout(pSkills)) do
        			html:node(v)
        		end
        	else
        		html:wikitext("None")
        	end
            html = html
    :tag("tr")
        :tag("th")
            :wikitext("Inputs")
        :done()
        :tag("th")
            :wikitext("Outputs")
    :tag("tr")
        :tag("td")
        :tag("td")

return tostring(tbl)
end

--- Returns the parsed result of the dlcParam.
-- @param dlcParam (string) A string separated by , listing the DLCs required for the MMG
-- @return (table) A table containing the items of provided DLCs
function p.getDLCIcons(dlcParam)
	if type(dlcParam) ~= 'string' then return {} end

	local input = shared.splitString(dlcParam, ',', true)
	local dlcs = {}
	for _, arg in pairs(input) do
		local argLower = arg:lower()
		table.insert(dlcs, DLCParams[argLower])
	end

	return dlcs
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

function p._main(args)
	if args['guideName'] == nil then
		error("Money Making Guide must have a valid guideName parameter.")
	end

	return buildMMGTable(args)
end

function p.test()
	local args = {
		guideName = "",
		interval = "",
		skills = "",
		items = "",
		other = "",
		skillExp1 = "Magic",
		skillExp1amount = "1000",
		skillExp2 = "Mining",
		skillExp2amount = "420",
		input1 = "nature rune",
		input1amount = "5",
		input1value = "69",
		input2 = "Fire Rune",
		input2amount = "20",
		output1 = "gp",
		output1amount = "1050",
		category = "",
		dlc = "aod, toth",
		intensity = "",
		explanation = ""
	}
	
	mw.log(p._main(args))
end

return p