Module:Store locations list: Difference between revisions

From Discord Dungeons Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
 
(37 intermediate revisions by the same user not shown)
Line 1: Line 1:
--------------------------
-- Module for [[Template:Store_locations_list]]
--
--
------------------------
-- <pre>
-- <pre>
local p = {}
local p = {}
Line 6: Line 12:
local yesNo = require("Module:Yesno")
local yesNo = require("Module:Yesno")
local purge = require("Module:Purge")._purge
local purge = require("Module:Purge")._purge
local inspect = require("Module:Inspect")


local p2pIcon = '[[File:Member icon.png|frameless|link=Pay-to-play]]'
--local p2pIcon = '[[File:Member icon.png|frameless|link=Pay-to-play]]'
local f2pIcon = '[[File:Free-to-play icon.png|frameless|link=Free-to-play]]'
--local f2pIcon = '[[File:Free-to-play icon.png|frameless|link=Free-to-play]]'
 
local p2pIcon = 'Yes'
local f2pIcon = 'No'


function p.main(frame)
function p.main(frame)
Line 14: Line 24:
end
end


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(args)
function p._main(args)
Line 36: Line 34:
     -- Get parsed smw data
     -- Get parsed smw data
     local data = p.getData(item, limit)
     local data = p.getData(item, limit)
mw.log("data", dump(data), '\n\n\n')


     -- Create the header of the output
     -- Create the header of the output
Line 47: Line 43:
             :tag('th'):wikitext('Seller'):done()
             :tag('th'):wikitext('Seller'):done()
             :tag('th'):wikitext('Location'):done()
             :tag('th'):wikitext('Location'):done()
             :tag('th'):wikitext('Price<br/>sold at'):attr('data-sort-type', 'number'):done()
             :tag('th'):wikitext('Buyable for'):attr('data-sort-type', 'number'):done()
             :tag('th'):wikitext('Price<br/>bought at'):attr('data-sort-type', 'number'):done()
             :tag('th'):wikitext('Sellable for'):attr('data-sort-type', 'number'):done()
             :tag('th'):wikitext('Members?'):done()
             :tag('th'):wikitext('Members?'):done()
         :done()
         :done()
local function addShopRow(shop, location)
restbl:tag('tr')
:tag('td'):wikitext(shop.seller):done()
:tag('td'):wikitext(location):done()
:tag('td')
:attr({['data-sort-value']=shop.buySortValue})
:wikitext(shop.buyvalue)
:done()
:tag('td')
:attr({['data-sort-value']=shop.sellSortValue})
:wikitext(shop.sellvalue)
:done()
:tag('td')
:attr({['data-sort-value']=shop.membersSortValue})
:wikitext(shop.members):done()
:done()
end


     -- Create the rows for the output table
     -- Create the rows for the output table
     for _, shop in ipairs(data) do
     for _, shop in ipairs(data) do  
mw.log("shop", dump(shop), '\n\n\n')
 
     for i, location in ipairs(shop.location) do
     for i, location in ipairs(shop.location) do
        restbl:tag('tr')
        addShopRow(shop, location)
            :tag('td'):wikitext(shop.seller):done()
end
            :tag('td'):wikitext(location):done()
            :tag('td')
if #shop.location == 0 then
                :attr({['data-sort-value']=shop.sellSortValue})
addShopRow(shop, 'N/A')
                :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
     end
     end
mw.log("restbl", tostring(restbl))


     return headerText .. tostring(restbl)
     return headerText .. tostring(restbl)
Line 88: Line 90:
         '?Sold by.Is members only = Is members only',
         '?Sold by.Is members only = Is members only',
         '?Sold by.NPC location = NPC location',
         '?Sold by.NPC location = NPC location',
'?Sold item Members',
         '?Sold by.Has subobject.NPC location=NPC location subobj',
         '?Sold by.Has subobject.NPC location=NPC location subobj',
         '?Sold item image',
         '?Sold item image',
Line 96: Line 99:
         '?Store currency #',
         '?Store currency #',
         '?Store notes',
         '?Store notes',
        '?Members #',
'?Is members only #',
         offset = 0,
         offset = 0,
         limit = limit or 500,
         limit = limit or 500,
Line 110: Line 115:


     table.sort(smwdata, function(a, b) return b['Store sell price'] < a['Store sell price'] end)
     table.sort(smwdata, function(a, b) return b['Store sell price'] < a['Store sell price'] end)
mw.log("smwdata", dump(smwdata))


     -- Iterate through each shop instance of item
     -- Iterate through each shop instance of item
Line 120: Line 123:
             table.insert(members, item['Store Is members only'])
             table.insert(members, item['Store Is members only'])
         elseif type(item['Is members only']) == 'boolean' then
         elseif type(item['Is members only']) == 'boolean' then
            table.insert(members, item['Is members only'])
elseif type(item['Is members only']) == 'string' then
             table.insert(members, item['Is members only'])
             table.insert(members, item['Is members only'])
         end
         end
Line 139: Line 144:
         item['Location'] = location
         item['Location'] = location
         -- item['Region'] = region
         -- item['Region'] = region
       
 
         if #members == 1 then
         if #members == 1 then
             item['Members'] = members[1]
             item['Members'] = members[1]
         else
         else
Line 146: Line 152:
         end
         end


mw.log("First item", dump(item), '\n\n\n')


         -- Loop over smw return items
         -- Loop over smw return items
Line 153: Line 158:
         -- Process the item and add it to the final data table
         -- Process the item and add it to the final data table
         local dataline = p.processData(item, editbtn)
         local dataline = p.processData(item, editbtn)
mw.log("dataline", dump(dataline), '\n\n\n')


         table.insert(data, dataline)
         table.insert(data, dataline)
Line 182: Line 184:
      
      
     local currency = item['Store currency']
     local currency = item['Store currency']
   
    mw.log("item", dump(item))


     local sellvalue = item['Store sell price'] or ''
     local sellvalue = item['Store sell price'] or ''
     local sellSortValue = 0
     local sellSortValue = 0
mw.log("sellvalue 1", sellvalue)
mw.log("sellvalue", sellvalue)
mw.log("sell is 1e10", tonumber(sellvalue) == 1e10)


 
     if not(sellvalue =='N/A') and not(tonumber(sellvalue) == 1e10) then
     if not(sellvalue == 1e10) then
         sellvalue = tonumber(sellvalue)
         sellvalue = tonumber(sellvalue)
mw.log("Sell value 2", sellvalue)


         if sellvalue then
         if sellvalue then
             sellSortValue = sellvalue
             sellSortValue = sellvalue
             sellvalue = commas(sellvalue)
             sellvalue = commas(sellvalue)
mw.log("sellvalue", sellvalue)
             local currencyImg =  ''
             local currencyImg =  ''
             if(params.has_content(currencyImg)) then
 
                currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
sellvalue = string.format('%s [[%s]]', sellvalue, currency)
                sellvalue = currencyImg .. sellvalue
 
             end
             -- 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
             sellvalue = editbtn    -- If sellvalue can't be converted to a number it will default to the edit button
Line 211: Line 212:
         sellvalue = 'N/A'
         sellvalue = 'N/A'
     end
     end
mw.log("sellvalue 2", sellvalue)
      
      
     local buyvalue = item['Store buy price'] or ''
     local buyvalue = item['Store buy price'] or ''
     local buySortValue = 0
     local buySortValue = 0
     if not(buyvalue == 'N/A') then
 
mw.log("buyvalue", buyvalue)
mw.log("buy is 1e10", tonumber(buyvalue) == 1e10)
 
 
     if not(buyvalue == 'N/A') and not(tonumber(buyvalue) == 1e10) then
         buyvalue = tonumber(buyvalue)
         buyvalue = tonumber(buyvalue)
mw.log("buyvalue 2", buyvalue)
         if buyvalue then
         if buyvalue then
             buySortValue = buyvalue
             buySortValue = buyvalue
             buyvalue = commas(buyvalue)
             buyvalue = commas(buyvalue)
             local currencyImg = currencyImage(currency, buySortValue) or ''
 
             if(params.has_content(currencyImg)) then
buyvalue = string.format('%s [[%s]]', buyvalue, currency)
                currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
 
                buyvalue = currencyImg .. buyvalue
 
             end
             -- local currencyImg = ''
        else
             -- if(params.has_content(currencyImg)) then
            buyvalue = editbtn -- If sellvalue can't be converted to a number it will default to the edit button
            --    currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
        end
            --    buyvalue = currencyImg .. buyvalue
    end
             -- end
else
buyvalue = editbtn -- If buyvalue can't be converted to a number it will default to the edit button
end
else
buySortValue = buyvalue
buyvalue = 'N/A'
end
      
      
     -- members only?
     -- members only?
     local members = item['Members']
     local members = item['Members']
     if type(members) == 'table' then
     if type(members) == 'table' then
         -- contains yes and/or no
         -- contains yes and/or no

Latest revision as of 13:00, 4 April 2022

Documentation for this module may be created at Module:Store locations list/doc

--------------------------
-- Module for [[Template:Store_locations_list]]
--
--
------------------------

-- <pre>
local p = {}

local commas = require("Module:Addcommas")._add
local params = require('Module:Paramtest')
local yesNo = require("Module:Yesno")
local purge = require("Module:Purge")._purge
local inspect = require("Module:Inspect")

--local p2pIcon = '[[File:Member icon.png|frameless|link=Pay-to-play]]'
--local f2pIcon = '[[File:Free-to-play icon.png|frameless|link=Free-to-play]]'

local p2pIcon = 'Yes'
local f2pIcon = 'No'

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


function p._main(args)
    local item, limit = params.defaults{
                            {args[1], mw.title.getCurrentTitle().text},
                            {args.Limit, 500}
                        }
    mw.log(string.format('Searching for shops that sell: %s', item))

    -- Get parsed smw data
    local data = p.getData(item, limit)

    -- 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('Buyable for'):attr('data-sort-type', 'number'):done()
            :tag('th'):wikitext('Sellable for'):attr('data-sort-type', 'number'):done()
            :tag('th'):wikitext('Members?'):done()
        :done()

	local function addShopRow(shop, location)
		restbl:tag('tr')
			:tag('td'):wikitext(shop.seller):done()
			:tag('td'):wikitext(location):done()
			:tag('td')
				:attr({['data-sort-value']=shop.buySortValue})
				:wikitext(shop.buyvalue)
			:done()
			:tag('td')
				:attr({['data-sort-value']=shop.sellSortValue})
				:wikitext(shop.sellvalue)
			:done()
			:tag('td')
				:attr({['data-sort-value']=shop.membersSortValue})
				:wikitext(shop.members):done()
		:done()
	end

    -- Create the rows for the output table
    for _, shop in ipairs(data) do    	
    	for i, location in ipairs(shop.location) do
	        addShopRow(shop, location)
		end
		
		if #shop.location == 0 then
			addShopRow(shop, 'N/A')
		end
    end

    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 item Members',
        '?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',
        '?Members #',
		'?Is members only #',
        offset = 0,
        limit = limit or 500,
    }
    local t1 = os.clock()
    local smwdata = mw.smw.ask(q)
    local t2 = os.clock()
    local data = {}

    if smwdata == nil then
        error('The item "' .. itemName .. '" is not sold in any shop, please check for typos[[Category:Empty store lists]]', 0)
    end
    mw.log(string.format('SMW (store locations list): entries: %d, time elapsed: %.3f ms.', #smwdata, (t2 - t1) * 1000))

    table.sort(smwdata, function(a, b) return b['Store sell price'] < a['Store sell price'] end)

    -- Iterate through each shop instance of item
    for _, item in ipairs(smwdata) do
        local members, location, region = {}, {}, {}

        if type(item['Store Is members only']) == 'boolean' then
            table.insert(members, item['Store Is members only'])
        elseif type(item['Is members only']) == 'boolean' then
            table.insert(members, item['Is members only'])
		elseif type(item['Is members only']) == 'string' then
            table.insert(members, item['Is members only'])
        end
        
        if type(item['Store location subobj']) == 'table' then
            for _, v in ipairs(item['Store location subobj']) do
                table.insert(location, v)
            end
        elseif type(item['Store location']) == 'string' then
            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

        item['Location'] = location
        -- item['Region'] = region

        if #members == 1 then
			
            item['Members'] = members[1]
        else
            item['Members'] = members
        end


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

        table.insert(data, dataline)
    end

    return data
end

function p.processData(item, editbtn)
    local seller = item['Sold by'] or ""
    local namenotes = item['Store notes']
    if namenotes then
        seller = seller .. ' ' .. namenotes
    end
    
    local location = item['Location']
    --local region = item['Region']

    if type(location) == 'table' and #location == 0 then
        location = {editbtn}
    end

    if type(region) == 'table' and #region == 0 then
        region = {editbtn}
    end
    
    local currency = item['Store currency']

    local sellvalue = item['Store sell price'] or ''
    local sellSortValue = 0
	
	mw.log("sellvalue", sellvalue)
	mw.log("sell is 1e10", tonumber(sellvalue) == 1e10)

    if not(sellvalue =='N/A') and not(tonumber(sellvalue) == 1e10) then
        sellvalue = tonumber(sellvalue)

        if sellvalue then
            sellSortValue = sellvalue
            sellvalue = commas(sellvalue)
            local currencyImg =  ''

			sellvalue = string.format('%s [[%s]]', sellvalue, currency)

            -- if(params.has_content(currencyImg)) then
            --     currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
            --     sellvalue = currencyImg .. sellvalue
            -- end
        else
            sellvalue = editbtn    -- If sellvalue can't be converted to a number it will default to the edit button
        end
    else
        sellSortValue = sellvalue
        sellvalue = 'N/A'
    end

	mw.log("sellvalue 2", sellvalue)

    
    local buyvalue = item['Store buy price'] or ''
    local buySortValue = 0

	mw.log("buyvalue", buyvalue)
	mw.log("buy is 1e10", tonumber(buyvalue) == 1e10)
	


    if not(buyvalue == 'N/A') and not(tonumber(buyvalue) == 1e10) then
        buyvalue = tonumber(buyvalue)
		mw.log("buyvalue 2", buyvalue)

        if buyvalue then
            buySortValue = buyvalue
            buyvalue = commas(buyvalue)

			buyvalue = string.format('%s [[%s]]', buyvalue, currency)


            -- local currencyImg = ''
            -- if(params.has_content(currencyImg)) then
            --     currencyImg = string.format('[[File:%s|link=%s]] ', currencyImg, currency)
            --     buyvalue = currencyImg .. buyvalue
            -- end
		else
			buyvalue = editbtn -- If buyvalue can't be converted to a number it will default to the edit button
		end
	else
		buySortValue = buyvalue
		buyvalue = 'N/A'
	end
    
    -- members only?
    local members = item['Members']

    if type(members) == 'table' then
        -- contains yes and/or no
        local hasYes, hasNo = false, false
        for _, value in ipairs(members) do
            if yesNo(value) then
                hasYes = true
            elseif not(yesNo(value)) then
                hasNo = true
            end
        end
        if hasYes and hasNo then
            members = f2pIcon.."/"..p2pIcon
        elseif hasYes then
            members = p2pIcon
        elseif hasNo then
            members = f2pIcon
        else
            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

    return {
        seller = seller,
        location = location,
        region = region,
        sellvalue = sellvalue,
        sellSortValue = sellSortValue,
        buyvalue = buyvalue,
        buySortValue = buySortValue,
        members = members
    }
end

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


return p