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

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
mNo edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Utility function to clean up number strings by removing commas
-- non dynamic module, no inputs
local function cleanNumberString(value)
if type(value) == "string" then
return value:gsub(",", "")
end
return value
end

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


local results = mw.smw.ask(query)
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 out = {}
local infoboxValue = item.InfoboxValue
if infoboxValue == nil then
for _,item in ipairs(results) do
infoboxValue = 0
if not(tonumber(item.ShopValue)==tonumber(item.InfoboxValue)) then
elseif type(infoboxValue)=='string' then
table.insert(out,item.Name)
infoboxValue = tonumber(cleanNumberString(item.InfoboxValue))
end
end
end
local hasMismatch = false
return table.concat(out, '<br>')
for _, shopValue in ipairs(shopValues) do
if shopValue ~= "N/A" then
-- Compare numeric values
local cleanShopValue = cleanNumberString(shopValue)
local numericShopValue = tonumber(cleanShopValue)
if numericShopValue and numericShopValue ~= infoboxValue then
hasMismatch = true
break
end
end
end
-- Add the item to the output if any shopValue mismatched InfoboxValue
if hasMismatch 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
end



Revision as of 11:03, 4 December 2024

Module documentation
This documentation is transcluded from Module:Sandbox/User:Alsang/PriceChecker/doc. [edit] [history] [purge]
This module does not have any documentation. Please consider adding documentation at Module:Sandbox/User:Alsang/PriceChecker/doc. [edit]
Module:Sandbox/User:Alsang/PriceChecker's function main is invoked by Template:Sandbox/User:Alsang/PriceChecker.

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',
        '?-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 infoboxValue == nil then
				infoboxValue = 0
			elseif type(infoboxValue)=='string' then
				infoboxValue = tonumber(cleanNumberString(item.InfoboxValue))
			end
	        local hasMismatch = false
	        
	        for _, shopValue in ipairs(shopValues) do
	            if shopValue ~= "N/A" then
	                -- Compare numeric values
	                local cleanShopValue = cleanNumberString(shopValue)
	                local numericShopValue = tonumber(cleanShopValue)
	                if numericShopValue and numericShopValue ~= infoboxValue then
	                    hasMismatch = true
	                    break
	                end
	            end
	        end
	
	        -- Add the item to the output if any shopValue mismatched InfoboxValue
	        if hasMismatch 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