Module:Sandbox/User:Californ1a/PriceChecker: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
(Created page with "local p = {} -- Utility function to clean up number strings by removing commas local function cleanNumberString(value) if type(value) == "string" then return value:gsub(",", "") end return value end -- Non-dynamic module, no inputs function p.main() -- Returns only directly needed parameters for the row, -- other parameters are determined by subqueries of chained pages local query = { '-Sold item::~*', '?=Name',...")
 
mNo edit summary
 
Line 3: Line 3:
-- Utility function to clean up number strings by removing commas
-- Utility function to clean up number strings by removing commas
local function cleanNumberString(value)
local function cleanNumberString(value)
if type(value) == "string" then
if type(value) == 'string' then
if value == 'N/A' then
return value:gsub(",", "")
return -1
end
return value:gsub(',', '')
end
end
return value
return value
Line 33: Line 36:
local infoboxValue = item.InfoboxValue
local infoboxValue = item.InfoboxValue
if infoboxValue == nil then
if string.find(infoboxValue or '', '%S') then
infoboxValue = 0
infoboxValue = tonumber(cleanNumberString(infoboxValue))
else
elseif type(infoboxValue)=='string' then
infoboxValue = tonumber(cleanNumberString(item.InfoboxValue))
infoboxValue = -2
end
end
local hasMismatch = false
local hasMatch = false
for _, shopValue in ipairs(shopValues) do
for _, shopValue in ipairs(shopValues) do
if shopValue ~= "N/A" then
-- Compare numeric values
-- Compare numeric values
local cleanShopValue = cleanNumberString(shopValue)
local cleanShopValue = cleanNumberString(shopValue)
local numericShopValue = tonumber(cleanShopValue)
local numericShopValue = tonumber(cleanShopValue)
if numericShopValue == infoboxValue then
if numericShopValue and numericShopValue ~= infoboxValue then
hasMatch = true
hasMismatch = true
end
break
end
end
end
end
-- Add the item to the output if any shopValue mismatched InfoboxValue
-- Add the item to the output if any shopValue mismatched InfoboxValue
if hasMismatch then
if not hasMatch then
table.insert(out, item)
table.insert(out, item)
end
end

Latest revision as of 06:35, 5 December 2024

Documentation for this module may be created at Module:Sandbox/User:Californ1a/PriceChecker/doc

local p = {}

-- Utility function to clean up number strings by removing commas
local function cleanNumberString(value)
    if type(value) == 'string' then
    	if value == 'N/A' then
    		return -1
    	end
        return value:gsub(',', '')
    end
    return value
end

-- Non-dynamic module, no inputs
function p.main()
    -- Returns only directly needed parameters for the row,
    -- other parameters are determined by subqueries of chained pages
    local query = {
        '[[-Sold item::~*]]',
        '?=Name',
        '?-Sold item.Shop sell price=ShopValue',
        '?Value=InfoboxValue',
        'limit = 10000',
        'order = asc'
    }

    local results = mw.smw.ask(query)

    local out = {}
    for _, item in ipairs(results) do
        local shopValues = item.ShopValue -- Could be an array or a single value
        if shopValues ~= nil then
	        if type(shopValues) == "string" then
	            shopValues = { shopValues } -- Normalize to a table for consistency
	        end
	
			local infoboxValue = item.InfoboxValue
			if string.find(infoboxValue or '', '%S') then
				infoboxValue = tonumber(cleanNumberString(infoboxValue))
			else
				infoboxValue = -2
			end
	        local hasMatch = false
	        
	        for _, shopValue in ipairs(shopValues) do
                -- Compare numeric values
                local cleanShopValue = cleanNumberString(shopValue)
                local numericShopValue = tonumber(cleanShopValue)
                if numericShopValue == infoboxValue then
                	hasMatch = true
                end
	        end
	
	        -- Add the item to the output if any shopValue mismatched InfoboxValue
	        if not hasMatch then
	            table.insert(out, item)
	        end
	    end
    end

    -- Format the output to display each item's details
    local output = {}
    for _, item in ipairs(out) do
        table.insert(output, mw.text.jsonEncode(item)) -- Convert each item to a JSON string for display
    end

    return table.concat(output, '<br>') -- Concatenate the JSON representations with a line break
end

return p