Module:StoreLine: Difference between revisions

From Discord Dungeons Wiki
Jump to navigation Jump to search
(Undo revision 29644 by Mackan (talk))
Tag: Undo
(Undo revision 29643 by Mackan (talk))
Tag: Undo
Line 1: Line 1:
-- <pre>
local p = {}
local p = {}
 
local lang = mw.language.getContentLanguage()
local commas = require("Module:Addcommas")._add
local var = mw.ext.VariablesLua
local params = require('Module:Paramtest')
local params = require('Module:Paramtest')
local yesNo = require("Module:Yesno")
local yesno = require('Module:Yesno')
local purge = require("Module:Purge")._purge
local commas = require("Module:Addcommas")
 
local p2pIcon = '[[File:Member icon.png|frameless|link=Pay-to-play]]'
local f2pIcon = '[[File:Free-to-play icon.png|frameless|link=Free-to-play]]'


function p.main(frame)
local ptitle = mw.title.getCurrentTitle()
    return p._main(frame:getParent().args)
local ns = ptitle.nsText
end
local title = ptitle.fullText
local pgTitle = ptitle.text


function dump(o)
function dump(o)
Line 27: Line 24:
  end
  end


function p._main(args)
function p.main(frame)
     local item, limit = params.defaults{
     local args = frame:getParent().args
                            {args[1], mw.title.getCurrentTitle().text},
     -- Params and defaults
                            {args.Limit, 500}
     local name, buyvalue, sellvalue, currency, level = params.defaults{
                        }
         {args.name or args.Name, ''},
    mw.log(string.format('Searching for shops that sell: %s', item))
        {args.buy or args.Buy, ''},
 
         {args.sell or args.Sell, ''},
     -- Get parsed smw data
{args.currency or args.Currency, 'Gold'},
     local data = p.getData(item, limit)
{args.level or args.Level, '?'},
 
mw.log("data", dump(data), '\n\n\n')
 
    -- Create the header of the output
    local headerText = "<div class=\"seealso\">This list was created dynamically. For help, see [[Template:Store locations list/FAQ|the FAQ]]. "
         .. "To force an update of this list, click " .. purge('res', 'here', 'span') .. ".</div>\n"
    local restbl = mw.html.create('table')
    restbl:addClass('wikitable sortable align-center-3 align-center-4 align-center-5 align-center-6 align-center-7')
        :tag('tr')
            :tag('th'):wikitext('Seller'):done()
            :tag('th'):wikitext('Location'):done()
            :tag('th'):wikitext('Price<br/>sold at'):attr('data-sort-type', 'number'):done()
            :tag('th'):wikitext('Price<br/>bought at'):attr('data-sort-type', 'number'):done()
            :tag('th'):wikitext('Members?'):done()
         :done()
 
    -- Create the rows for the output table
    for _, shop in ipairs(data) do
mw.log("shop", dump(shop), '\n\n\n')
 
    for i, location in ipairs(shop.location) do
        restbl:tag('tr')
            :tag('td'):wikitext(shop.seller):done()
            :tag('td'):wikitext(location):done()
            :tag('td')
                :attr({['data-sort-value']=shop.sellSortValue})
                :wikitext(shop.sellvalue)
            :done()
            :tag('td')
                :attr({['data-sort-value']=shop.buySortValue})
                :wikitext(shop.buyvalue)
            :done()
            :tag('td'):wikitext(shop.members):done()
        :done()
end
    end
 
mw.log("restbl", tostring(restbl))
 
    return headerText .. tostring(restbl)
end
 
function p.getData(itemName, limit)
    -- Query smw
    local q = {
        '[[Sold item::'..itemName..']]',
        '?Sold by',
        '?Sold by.Store Is members only = Store Is members only',
        '?Sold by.Store location = Store location',
        '?Sold by.Has subobject.Store location=Store location subobj',
        '?Sold by.Is members only = Is members only',
        '?Sold by.NPC location = NPC location',
        '?Sold by.Has subobject.NPC location=NPC location subobj',
        '?Sold item image',
        '?Sold item',
        '?Sold item text',
        '?Store sell price',
        '?Store buy price',
        '?Store currency',
        '?Store notes',
        offset = 0,
        limit = limit or 500,
     }
     }
     local t1 = os.clock()
     local itemvalue = ''
     local smwdata = mw.smw.ask(q)
     local smwname = params.default_to(args.smwname,name)
     local t2 = os.clock()
     local displayname = params.default_to(args.displayname or args.DisplayName,name)
     local data = {}
     local image = 'File:' .. params.default_to(args.image or args.Image, name .. '.png')


     if smwdata == nil then
    -- Lookup SMW value
         error('The item "' .. itemName .. '" is not sold in any shop, please check for typos[[Category:Empty store lists]]', 0)
     if itemvalue == ''  then
         itemvalue = getSMWInfo(smwname) or ''
     end
     end
     mw.log(string.format('SMW (store locations list): entries: %d, time elapsed: %.3f ms.', #smwdata, (t2 - t1) * 1000))
      
 
    -- Check precached Module:DropsLine/itemData - gets GE alch so inaccurate
     table.sort(smwdata, function(a, b) return b['Store sell price'] < a['Store sell price'] end)
    -- if itemvalue == '' then
 
    --    local droppeditem_data = mw.loadData('Module:DropsLine/itemData')
     -- Iterate through each shop instance of item
    --    local cached_dropdata = droppeditem_data[name]
     for _, item in ipairs(smwdata) do
    --    if type(cached_dropdata) == 'table' and type(cached_dropdata[2]) == 'number' then
        local members, location, region = {}, {}, {}
    --        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)
   
    buyvalue = commas._strip(buyvalue)
    sellvalue = commas._strip(sellvalue)
    levelvalue = commas._strip(level)
    local buy_smw = tonumber(buyvalue)
     local sell_smw = tonumber(sellvalue) or 1e10
     local level_smw = tonumber(levelvalue) or 1e10
    local buyCurrency = ''
    local sellCurrency = ''


        if type(item['Store Is members only']) == 'boolean' then
    if buyvalue ~= 'N/A' then
            table.insert(members, item['Store Is members only'])
         if buyvalue == '' and itemvalue ~= '' then
         elseif type(item['Is members only']) == 'boolean' then
             buyvalue = math.floor(math.max(itemvalue*buymultiplier/1000,itemvalue*0.1))
             table.insert(members, item['Is members only'])
            buy_smw = buyvalue
         end
         end
          
         buyvalue = showCurrencyAmount(currency, buyvalue)
        if type(item['Store location subobj']) == 'table' then
    end
            for _, v in ipairs(item['Store location subobj']) do
    if sellvalue ~= 'N/A' then
                table.insert(location, v)
         if sellvalue == '' and itemvalue ~= '' then
            end
             sellvalue = math.floor(math.max(itemvalue*sellmultiplier/1000,1))
        elseif type(item['Store location']) == 'string' then
             sell_smw = sellvalue
            table.insert(location, item['Store location'])
         elseif type(item['NPC location subobj']) == 'table' then
             for _, v in ipairs(item['NPC location subobj']) do
                table.insert(location, v)
             end
        elseif type(item['NPC location']) == 'string' then
            table.insert(location, item['NPC location'])
         end
         end
        sellvalue = showCurrencyAmount(currency, sellvalue)


         item['Location'] = location
mw.log("sellvalue", sellvalue)
        -- item['Region'] = region
mw.log("sell_smw", sell_smw)
       
    end
         if #members == 1 then
   
             item['Members'] = members[1]
    local ret = mw.html.create('tr'):css('text-align','center')
    if not hideimage then
         ret:tag('td'):wikitext(mw.ustring.format('[[%s|link=%s]]', 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 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
         else
             item['Members'] = members
             ret:tag('td')
                :attr('data-sort-value', sell_smw)
                :wikitext(sellvalue)
            :done()
         end
         end
mw.log(dump(item), '\n\n\n')
        -- Loop over smw return items
        -- item['Store location'] = 'x'
        -- item['Store Is members only'] = 'Yes'
        -- Process the item and add it to the final data table
        local dataline = p.processData(item, editbtn)
mw.log("dataline", dump(dataline), '\n\n\n')
        table.insert(data, dataline)
     end
     end
 
     if not hidebuy then
     return data
        if buyvalue=='N/A' then
end
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
 
        else
function p.processData(item, editbtn)
            ret:tag('td')
    local seller = item['Sold by'] or ""
                    :attr('data-sort-value', buy_smw or 0)
    local namenotes = item['Store notes']
                    :wikitext(buyvalue)
    if namenotes then
              :done()
        seller = seller .. ' ' .. namenotes
         end
    end
   
    local location = item['Location']
    --local region = item['Region']
 
    if type(location) == 'table' and #location == 0 then
         location = {editbtn}
     end
     end


    if type(region) == 'table' and #region == 0 then
if not hidelevel then
         region = {editbtn}
         if levelvalue=='N/A' then
    end
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
   
    local currency = item['Store currency']
 
    local sellvalue = item['Store sell price'] or ''
    local sellSortValue = 0
mw.log("sellvalue 1", sellvalue)
 
 
    if not(sellvalue == 1e10) then
        sellvalue = tonumber(sellvalue)
        if sellvalue then
            sellSortValue = sellvalue
            sellvalue = commas(sellvalue)
mw.log("sellvalue", sellvalue)
            local currencyImg =  ''
            if(params.has_content(currencyImg)) then
                currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
                sellvalue = currencyImg .. sellvalue
            end
         else
         else
             sellvalue = editbtn    -- If sellvalue can't be converted to a number it will default to the edit button
             ret:tag('td')
                    :attr('data-sort-value', level_smw or 0)
                    :wikitext(levelvalue)
              :done()
         end
         end
    else
        sellSortValue = sellvalue
        sellvalue = 'N/A'
     end
     end
   
 
     local buyvalue = item['Store buy price'] or ''
     local i = 1
    local buySortValue = 0
    while args['column' .. i] do
    if not(buyvalue == 'N/A') then
        if args['column' .. i]=='N/A' then
        buyvalue = tonumber(buyvalue)
            ret:tag('td'):attr('data-sort-value', 0):attr('class','table-na'):wikitext('<small>N/A</small>'):done()
        if buyvalue then
            buySortValue = buyvalue
            buyvalue = commas(buyvalue)
            local currencyImg = currencyImage(currency, buySortValue) or ''
            if(params.has_content(currencyImg)) then
                currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
                buyvalue = currencyImg .. buyvalue
            end
         else
         else
            buyvalue = editbtn -- If sellvalue can't be converted to a number it will default to the edit button
        ret:tag('td'):wikitext(args['column' .. i]):done()
         end
         end
        i = i+1
     end
     end
       
      
      
    -- members only?
     local onMain = ns == '' or ns == '(Main)'
     local members = item['Members']
    if onMain and useSmw then
    if type(members) == 'table' then
         local smw = {}
         -- contains yes and/or no
         local smw_sub = {}
         local hasYes, hasNo = false, false
         local source = pgTitle
         for _, value in ipairs(members) do
        smw['Sells item'] = name
            if yesNo(value) then
         smw_sub['Sold by'] = source
                hasYes = true
         smw_sub['Sold item image'] = image
            elseif not(yesNo(value)) then
         smw_sub['Sold item'] = name
                hasNo = true
         smw_sub['Sold item text'] = displayname
            end
         smw_sub['Store sell price'] = sell_smw --type = number for sorting purposes
         end
        smw_sub['Store buy price'] = buy_smw or 'N/A'
         if hasYes and hasNo then
        smw_sub['Store currency'] = currency
            members = f2pIcon.."/"..p2pIcon
         smw_sub['Store notes'] = namenotes
         elseif hasYes then
        mw.smw.subobject(smw_sub) -- add item subobject to page
            members = p2pIcon
          
         elseif hasNo then
        mw.log("smw", dump(smw_sub))
            members = f2pIcon
          
         else
        mw.smw.set(smw) -- add data to page
            members = editbtn -- Unsupported type for yesNo, default to editbtn
         end
    elseif yesNo(members) then
         members = p2pIcon
    elseif not(yesNo(members)) then
         members = f2pIcon
    else -- Unsupported type for yesNo, default to editbtn
        members = editbtn
     end
     end
    return tostring(ret)
end


     return {
local smwData = nil
         seller = seller,
function getSMWInfo(item)
         location = location,
     if smwData ~= nil then
        region = region,
        return smwData
        sellvalue = sellvalue,
    end
         sellSortValue = sellSortValue,
    local smw = mw.smw.ask({
        buyvalue = buyvalue,
         '[['..item..']]',
         buySortValue = buySortValue,
         '?Value'
        members = members
    })
     }
    if smw and smw[1] then
         smwData = smw[1]['Value']
    else
         smwData = ''
    end
     return smwData
end
end


function editbutton(title)
function showCurrencyAmount(currency, amount)
    local link = string.gsub(mw.title.getCurrentTitle():fullUrl("action=edit"), mw.title.getCurrentTitle().fullText, title)
return string.format('%s [[%s]]', amount, currency)
     link = string.gsub(link, ' ', '_')
 
     link = string.format("<span class='plainlinks'>[%s '''?''' (edit)]</span>", link)
 
     return link
     -- 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
end


return p
return p

Revision as of 13:55, 3 April 2022

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

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 = params.defaults{
        {args.name or args.Name, ''},
        {args.buy or args.Buy, ''},
        {args.sell or args.Sell, ''},
		{args.currency or args.Currency, 'Gold'},
		{args.level or args.Level, '?'},
    }
    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)
    
    buyvalue = commas._strip(buyvalue)
    sellvalue = commas._strip(sellvalue)
    levelvalue = commas._strip(level)
    local buy_smw = tonumber(buyvalue)
    local sell_smw = tonumber(sellvalue) or 1e10
    local level_smw = tonumber(levelvalue) or 1e10
    local buyCurrency = ''
    local sellCurrency = ''

    if buyvalue ~= 'N/A' then
        if buyvalue == '' and itemvalue ~= '' then
            buyvalue = math.floor(math.max(itemvalue*buymultiplier/1000,itemvalue*0.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)

		mw.log("sellvalue", sellvalue)
		mw.log("sell_smw", sell_smw)
    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]]', 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 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)
                :wikitext(sellvalue)
            :done()
        end
    end
    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 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

    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 --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
        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)
	return string.format('%s [[%s]]', amount, currency)


    -- 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