Module:MoneyMakingGuide: Difference between revisions

Fix styling issues with lists
(Add error display for invalid skill exp values)
(Fix styling issues with lists)
 
(35 intermediate revisions by the same user not shown)
Line 6: Line 6:
local itemdb = require('Module:Items')
local itemdb = require('Module:Items')
local icons = require('Module:Icons')
local icons = require('Module:Icons')
local StringBuilder = require('Module:StringBuilder')


-- Constants
-- Constants
Line 20: Line 21:
}
}


--- Formats a wikicode string to be bold and red
--- Possible fix to allow wikicode (especially tables) to be passed to the 'explanation'
local function formatError(errorMessage)
--- parameter, and get parsed by the wiki parser on return.
local eror = mw.html.create('span')
-- Not used currently, because it might not be necessary at all.
:wikitext("'''")
local function processMarkup(str)
:css('color', 'red')
--return frame:preprocess(str)
:wikitext(errorMessage)
end
:wikitext("'''")
:done()
return tostring(error)
end


--- Formats a given string to TitleCase.
--- Formats a given string to TitleCase.
-- @param name (string) String to format.
-- @param name (string) String to format.
-- @return (string) Formatted string, or the input if it's not a string.
-- @return (string) Formatted string, or the input if it's not a string.
local function formatName(name)
local function formatItemName(name)
-- Special case to correctly format GP name.
if shared.compareString(name, 'Gold Pieces', true) or shared.compareString(name, 'GP', true) then
return 'Gold Pieces'
end
-- Got to solve too many edge case swith this (like of and IV)
-- Better just make the user use the correct name instead.
--if type(name) == 'string' then
-- return shared.titleCase(name)
--end
return name
end
 
local function formatSkillName(name)
if type(name) == 'string' then
if type(name) == 'string' then
return shared.titleCase(name)
return shared.titleCase(name)
Line 41: Line 52:
return name
return name
end
local function getErrorDiv(message)
return mw.html.create('div')
:css('color', 'red')
:wikitext(message)
:done()
end
local function getItemIcon(iconName)
if iconName == 'Gold Pieces' then
return icons.GP()
end
return icons.Icon({iconName, type='item', notext=true})
end
end


local function getSkillExpIcon(skillName, expValue)
local function getSkillExpIcon(skillName, expValue)
if expValue == nil then error("expValue is nil") end
if expValue == nil then error("expValue is nil") end
local icon = icons.Icon({skillName, type='skill', notext=true})
expValue = num.formatnum(expValue)
expValue = num.formatnum(expValue)
return icons.Icon({
return icon .. ' ' .. expValue
skillName,
end
expValue,
 
type='skill',
local function parseProfitMargin(minProfit, maxProfit)
nolink=true})
local min = tonumber(minProfit)
local max = tonumber(maxProfit)
if max == nil then
error("maxProfit is not a valid number.")
end
local sb = StringBuilder:new()
if min then
sb  :append(icons.GP(num.round2(min / 1000)))
:append('k/hr Minimum ~ ')
end
sb  :append(icons.GP(num.round2(max / 1000)))
:append('k/hr Maximum')
return sb:toString()
end
end


Line 70: Line 115:
end
end
local pName = formatName(args[numPrefix])
local pName = formatItemName(args[numPrefix])
local pAmount = args[numPrefix .. AmountSuffix]
local pAmount = args[numPrefix .. AmountSuffix]
local pValue = args[numPrefix .. ValueSuffix]
local pValue = args[numPrefix .. ValueSuffix]
Line 116: Line 161:
end
end
local pSkill = formatName(args[skillPrefix])
local pSkill = formatSkillName(args[skillPrefix])
local pExp = args[skillPrefix .. AmountSuffix]
local pExp = tonumber(args[skillPrefix .. AmountSuffix])


if paramtest.has_content(pExp) then
pExp = tonumber(pExp)
end
table.insert(skills, {
table.insert(skills, {
prmNumber = i,
prmNumber = i,
Line 129: Line 170:
end
end
-- Return nil if there is no experience earned.
-- Lets the parent table know to enter 'None' instead
if #skills == 0 then
        return nil
    end
return skills
return skills
end
end
Line 135: Line 181:
-- @param items (table) A table containing items
-- @param items (table) A table containing items
-- @return (string) The HTML representation of the item table section.
-- @return (string) The HTML representation of the item table section.
local function buildItemTable(items)
local function buildItemTable(itemSet)
if itemSet == nil or itemSet.Items == nil or #itemSet.Items == 0 then
return nil
end
 
-- Create table with header for items
local tbl = mw.html.create('table')
tbl:addClass('wikitable sortable text-align-center')
:attr('style', 'font-size: 1em; width: calc(100% + 2px); margin: -1px')
:tag('tr')
:tag('th')
:addClass('unsortable')
:tag('th'):wikitext('Item')
:tag('th'):wikitext('Quantity')
:tag('th'):wikitext('Value')
:done()
-- Create individual rows for items
for _, i in pairs(itemSet.Items) do
local row = mw.html.create('tr')
local imgCell = mw.html.create('td')
:wikitext(getItemIcon(i.name))
:css('height', '28px')
:done()
local itemCell = mw.html.create('td')
:css('text-align','left')
:wikitext(string.format('[[%s]]', i.name))
:done()
local qtyCell = mw.html.create('td')
if i.amount == nil then
qtyCell:node(getErrorDiv("Unable to parse quantity for item: " .. i.name))
else
local qty = num.autoround(i.amount)
qtyCell:wikitext(num.formatnum(qty))
end
qtyCell:done()
local valCell = mw.html.create('td')
if i.value == nil then
valCell:node(getErrorDiv("Unable to get value for item: " .. i.name))
else
local tot = i.value * (i.amount or 0)
valCell
:css('text-align','right')
:wikitext(icons.GP(num.round2(tot)))
:attr("data-sort-value", tot)
end
valCell:done()
-- Add all cells to row, and row to table.
row:node(imgCell):node(itemCell):node(qtyCell):node(valCell):done()
tbl:node(row)
end
return tbl:done()
end
end


Line 145: Line 248:
for _, skillLine in pairs(skills) do
for _, skillLine in pairs(skills) do
local hasInvalidExp = false
local hasInvalidExp = false
local span = mw.html.create('div')
local span = nil
if skillLine.exp == nil or skillLine.exp <= 0 then
if skillLine.exp == nil or skillLine.exp <= 0 then
Line 151: Line 254:
end
end
local skillIcon = getSkillExpIcon(skillLine.name, skillLine.exp or 0)
local skillExp = num.round2(skillLine.exp or 0)
local skillIcon = getSkillExpIcon(skillLine.name, skillExp)


if hasInvalidExp then
if hasInvalidExp then
span:css('color', 'red')
span = getErrorDiv("Skill in parameter " .. skillLine.prmNumber .. " has an invalid experience value.")
:wikitext('Skill in parameter ' .. skillLine.prmNumber .. " has an invalid experience value.")
else
else
span:wikitext(skillIcon)
span = mw.html.create('div')
:wikitext(skillIcon)
:done()
end
end


Line 166: Line 271:
end
end


local function buildMMGTable(args)
function p._buildMMGTable(args)
if args['guideName'] == nil then
error("Money Making Guide must have a valid guideName parameter.")
end
-- Parse arguments.
-- Parse arguments.
local pSkills = parseExp(args)
local pSkills = parseExp(args)
Line 172: Line 281:
local pOutputs = parseItemInOut(args, 'output')
local pOutputs = parseItemInOut(args, 'output')
local dlcIcons = p.getDLCIcons(args['dlc'], ' ')
local dlcIcons = p.getDLCIcons(args['dlc'], ' ')
local minProfit = args['minimumProfit']
local maxProfit = pOutputs.TotalValue - pInputs.TotalValue


local tbl = mw.html.create()
local tbl = mw.html.create()
Line 181: Line 292:
         :tag("td")
         :tag("td")
             :attr("colspan", 2)
             :attr("colspan", 2)
            :css('font-weight', 'bold')
             :wikitext(table.concat(dlcIcons) .. ' ')
             :wikitext(table.concat(dlcIcons) .. ' ')
             :wikitext(args['guideName'])
             :wikitext(args['guideName'] or '{{{guideName}}}')
    :tag("tr")
        :tag("td")
            :attr("colspan", 2)
            :wikitext(parseProfitMargin(minProfit, maxProfit))
     :tag("tr")
     :tag("tr")
         :tag("th")
         :tag("th")
             :attr("colspan", 2)
             :attr("colspan", 2)
             :wikitext("Requirements")
             :wikitext("Requirements")
     :tag("tr")
     :tag('tr')
         :tag("th")
         :tag('th')
             :wikitext("Skills")
             :wikitext('Skills')
         :tag("th")
         :tag('th')
             :wikitext("Recommended")
             :wikitext('Other')
        :done()
     :tag("tr")
     :tag("tr")
         :tag("td")
         :tag("td")
        :wikitext(paramtest.default_to(args['skills'], 'None'))  
        :addClass('mmg-no-list')
        :newline()
        :wikitext(paramtest.default_to(args['skills'], 'None'))  
     :tag("td")
     :tag("td")
        :attr("rowspan", 3)
            :addClass('mmg-no-list')
        :wikitext(paramtest.default_to(args['other'], 'None'))  
        :newline()
     :tag("tr")
        :wikitext(paramtest.default_to(args['other'], 'None'))  
         :tag("th")
     :tag('tr')
             :wikitext("Items")
         :tag('th')
     :tag("tr")
             :wikitext('Items')
         :tag("td")
        :tag('th')
            :wikitext('Recommended')
     :tag('tr')
         :tag('td')
        :addClass('mmg-no-list')
        :newline()
             :wikitext(paramtest.default_to(args['items'], 'None'))  
             :wikitext(paramtest.default_to(args['items'], 'None'))  
        :tag('td')
            :addClass('mmg-no-list')
        :newline()
            :wikitext(paramtest.default_to(args['recommended'], 'None'))
     :tag("tr")
     :tag("tr")
         :tag("th")
         :tag("th")
Line 216: Line 344:
         :tag("td")
         :tag("td")
         if args['profit'] then
         if args['profit'] then
         html:wikitext(icons.GP(args['profit']))
         html:wikitext(icons.GP(num.round2(args['profit'])))
         else
         else
         html:wikitext(icons.GP(pOutputs.TotalValue - pInputs.TotalValue))
         html:wikitext(icons.GP(num.round2(maxProfit)))
         end
         end
         html = html
         html = html
Line 234: Line 362:
         :tag("th")
         :tag("th")
             :wikitext("Inputs")
             :wikitext("Inputs")
        :done()
            if pInputs.TotalValue ~= 0 then
            html:wikitext(" (" .. icons.GP(num.round2(pInputs.TotalValue)) .. ")")
            end
            html = html
         :tag("th")
         :tag("th")
             :wikitext("Outputs")
             :wikitext("Outputs")
            if pOutputs.TotalValue ~= 0 then
            html:wikitext(" (" .. icons.GP(num.round2(pOutputs.TotalValue)) .. ")")
            end
            html = html
     :tag("tr")
     :tag("tr")
         :tag("td")
         :tag("td")
        local inputsTable = buildItemTable(pInputs)
        if inputsTable ~= nil then
        html:css('padding', '0')
        :css('vertical-align', 'top')
        :node(inputsTable)
        else
        html:wikitext("None")
        end
        html = html
         :tag("td")
         :tag("td")
        local outputsTable = buildItemTable(pOutputs)
        if outputsTable ~= nil then
        html:css('padding', '0')
        :css('vertical-align', 'top')
        :node(outputsTable)
        else
        html:wikitext("None")
        end
        html = html
:done()
:done()
return tbl:done()
:newline()
end


return tostring(tbl)
function p.buildMMGTable(frame)
local args = frame:getParent().args
return p._buildMMGTable(args)
end
end


Line 261: Line 422:


function p.main(frame)
function p.main(frame)
local args = frame:getParent().args
error("Call a specific function rather than main.")
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
end


function p.test()
function p.test()
local args = {
local args = {
guideName = "",
guideName       ='Mining Pure Crystal',
interval = "",
category        ='Non-combat',
skills = "",
dlc              ='aod, toth',
items = "",
skills          =[[
other = "",
*{{SkillReq|Mining|85}}
skillExp1 = "Maagic",
*{{SkillReq|Herblore|53}}]],
skillExp1amount = "-1000",
items            =[=[
skillExp2 = "Mining",
*{{ItemIcon|Mining Gloves}}
skillExp2amount = "420",
*{{ItemIcon|Perfect Swing Potion IV}}]=],
input1 = "nature rune",
other            =[=[
input1amount = "5",
*{{ItemIcon|Pure Crystal|notext=true}}{{Icon|Mastery|notext=true}} Level 99
input1value = "69",
*{{UpgradeIcon|Dragon Pickaxe}}
input2 = "Fire Rune",
*[[Mining#Mastery Pool Checkpoints|95% Mining Mastery Pool Checkpoint]]]=],
input2amount = "20",
recommended      =[=[
output1 = "gp",
[[Money_Making/Mining_Pure_Crystal#Improves_GP_Rate|Bonusses that improve profit]]]=],
output1amount = "1050",
category = "",
dlc = "aod, toth",
intensity = "",
explanation = ""
}
}
mw.log(p._main(args))
local t = p._buildMMGTable(args)
mw.log(t)
end
end
-- function p.test()
-- local args = {
-- guideName = "",
-- interval = "",
-- skills = "",
-- items = "",
-- other = "",
-- recommended = "",
-- skillExp1 = "Maagic",
-- 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._buildMMGTable(args))
-- end


return p
return p
1,010

edits