Module:StoreLine: Difference between revisions

From Discord Dungeons Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 35: Line 35:
     local name, buyvalue, sellvalue, currency, level, members = params.defaults{
     local name, buyvalue, sellvalue, currency, level, members = params.defaults{
         {args.name or args.Name, ''},
         {args.name or args.Name, ''},
         {args.buy or args.Buy, ''},
         {args.buy or args.Buy, 'N/A'},
         {args.sell or args.Sell, ''},
         {args.sell or args.Sell, 'N/A'},
{args.currency or args.Currency, 'Gold'},
{args.currency or args.Currency, 'Gold'},
{args.level or args.Level, '?'},
{args.level or args.Level, '?'},

Latest revision as of 11:54, 4 April 2022

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

--------------------------
-- Module for [[Template:StoreLine]]
--
--
------------------------

local p = {}
local lang = mw.language.getContentLanguage()
local var = mw.ext.VariablesLua
local params = require('Module:Paramtest')
local yesno = require('Module:Yesno')
local commas = require("Module:Addcommas")

local ptitle = mw.title.getCurrentTitle()
local ns = ptitle.nsText
local title = ptitle.fullText
local pgTitle = ptitle.text

function dump(o)
	if type(o) == 'table' then
	   local s = '{ '
	   for k,v in pairs(o) do
		  if type(k) ~= 'number' then k = '"'..k..'"' end
		  s = s .. '['..k..'] = ' .. dump(v) .. ','
	   end
	   return s .. '} '
	else
	   return tostring(o)
	end
 end

function p.main(frame)
    local args = frame:getParent().args
    -- Params and defaults
    local name, buyvalue, sellvalue, currency, level, members = params.defaults{
        {args.name or args.Name, ''},
        {args.buy or args.Buy, 'N/A'},
        {args.sell or args.Sell, 'N/A'},
		{args.currency or args.Currency, 'Gold'},
		{args.level or args.Level, '?'},
		{args.members or args.Members, 'No'},
    }
    local itemvalue = ''
    local smwname = params.default_to(args.smwname,name)
    local displayname = params.default_to(args.displayname or args.DisplayName,name)
    local image = 'File:' .. params.default_to(args.image or args.Image, name .. '.png')

    -- Lookup SMW value
    --if itemvalue == ''  then
   --     itemvalue = getSMWInfo(smwname) or ''
    --end
    
    -- Check precached Module:DropsLine/itemData - gets GE alch so inaccurate
    -- if itemvalue == '' then
    --     local droppeditem_data = mw.loadData('Module:DropsLine/itemData')
    --     local cached_dropdata = droppeditem_data[name]
    --     if type(cached_dropdata) == 'table' and type(cached_dropdata[2]) == 'number' then
    --         itemvalue = cached_dropdata[2]/.6
    --     end
    -- end
    
    local buymultiplier = var.var('BuyMultiplier', 1)
    local sellmultiplier = var.var('SellMultiplier', 1)
    local currency = var.var('Currency', currency)
    local namenotes = var.var('NameNotes', '')
    local useSmw = yesno(var.var('smw','yes'), true)
    local hideimage = yesno(var.var('hideImage','no'), false)
    local hidesell = yesno(var.var('hideSell','no'), false)
    local hidebuy = yesno(var.var('hideBuy','no'), false)
    local hidelevel = yesno(var.var('hideLevel','no'), false)
	local hidemembers = yesno(var.var('hideMembers', 'no'), false)
    
    buyvalue = commas._strip(buyvalue)
    sellvalue = commas._strip(sellvalue)
    levelvalue = commas._strip(level)
    local buy_smw = tonumber(buyvalue) or 1e10
    local sell_smw = tonumber(sellvalue) or 1e10
    local level_smw = tonumber(levelvalue) or 1e10
	local members_smw = members
    local buyCurrency = ''
    local sellCurrency = ''

    if buyvalue ~= 'N/A' then
        if buyvalue == '' and itemvalue ~= '' then
            buyvalue = math.floor(math.max(itemvalue*buymultiplier/1000,1))
            buy_smw = buyvalue
        end
        buyvalue = showCurrencyAmount(currency, buyvalue)
    end
    if sellvalue ~= 'N/A' then
        if sellvalue == '' and itemvalue ~= '' then
            sellvalue = math.floor(math.max(itemvalue*sellmultiplier/1000,1))
            sell_smw = sellvalue
        end
        sellvalue = showCurrencyAmount(currency, sellvalue)
    end
    
    local ret = mw.html.create('tr'):css('text-align','center')
    if not hideimage then
        ret:tag('td'):wikitext(mw.ustring.format('[[%s|link=%s|32px]]', image, name))
    end
    -- no hideXXX parameter for the column that lists the actual items.
    ret:tag('td'):css('text-align','left'):wikitext(mw.ustring.format('[[%s|%s]]', name, displayname))
    if not hidebuy then
        if buyvalue=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        else
            ret:tag('td')
                    :attr('data-sort-value', buy_smw or 0)
                    :wikitext(buyvalue)
               :done()
        end
    end

	if not hidesell then
        if sellvalue == 'N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        elseif sell_smw == 0 then
            ret:tag('td')
                :attr('data-sort-value', 0)
                :wikitext('<small>Free</small>')
            :done()
        else
            ret:tag('td')
                :attr('data-sort-value', sell_smw or 0)
                :wikitext(sellvalue)
            :done()
        end
    end

	if not hidelevel then
        if levelvalue=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        else
            ret:tag('td')
                    :attr('data-sort-value', level_smw or 0)
                    :wikitext(levelvalue)
               :done()
        end
	end

	if not hidemembers then
		if members == 'N/A' then
			ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
		else
			 ret:tag('td')
                    :attr('data-sort-value', members_smw or 0)
                    :wikitext(members)
               :done()
		end
	end

    local i = 1
    while args['column' .. i] do
        if args['column' .. i]=='N/A' then
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        else
        ret:tag('td'):wikitext(args['column' .. i]):done()
        end
        i = i+1
    end
        
    
    local onMain = ns == '' or ns == '(Main)'
    if onMain and useSmw then
        local smw = {}
        local smw_sub = {}
        local source = pgTitle
        smw['Sells item'] = name
        smw_sub['Sold by'] = source
        smw_sub['Sold item image'] = image
        smw_sub['Sold item'] = name
        smw_sub['Sold item text'] = displayname
        smw_sub['Store sell price'] = sell_smw or 'N/A' --type = number for sorting purposes
        smw_sub['Store buy price'] = buy_smw or 'N/A'
        smw_sub['Store currency'] = currency
        smw_sub['Store notes'] = namenotes
		smw_sub['Is members only'] = members
        mw.smw.subobject(smw_sub) -- add item subobject to page
        
        mw.log("smw", dump(smw_sub))
        
        mw.smw.set(smw) -- add data to page
    end
    return tostring(ret)
end

local smwData = nil
function getSMWInfo(item)
    if smwData ~= nil then
        return smwData
    end
    local smw = mw.smw.ask({
        '[['..item..']]',
        '?Value'
    })
    if smw and smw[1] then
        smwData = smw[1]['Value']
    else
        smwData = ''
    end
    return smwData
end

function showCurrencyAmount(currency, amount)
	local formatted = string.format('%s [[%s]]', commas._add(amount), currency)
	
	
	return formatted


    -- local image = currency and currency_image(currency, amount) or ''
    -- if image ~= '' and currency and tonumber(amount) then
    --     return string.format('[[File:%s|link=%s]] %s', image, currency, commas._add(amount))
    -- else
    -- end
end

return p