Module:Shops: Difference between revisions

From The Walkscape Walkthrough
Content deleted Content added
Today we learned: In MediaWiki, parameters are passed to Lua modules as tables, not as individual arguments...
Clean up debug code
Line 19: Line 19:
}
}


function p.getShopData(shopName)
function p.getShopData(frame)
local shopName = frame.args[1]
for _, shop in ipairs(shops) do
for _, shop in ipairs(shops) do
if shop.ShopName == shopName then
if shop.ShopName == shopName then
Line 32: Line 34:
local shopName = frame.args[1]
local shopName = frame.args[1]


local o = {}
for _, shop in ipairs(shops) do
for _, shop in ipairs(shops) do
table.insert(o, string.format("shop.ShopName: %s did not match input of %s", shop.ShopName, shopName))
if shop.ShopName == shopName then
if shop.ShopName == shopName then
return string.format("|-\n| [[%s]] || [[%s]] || [[%s]]", shop.ShopName, shop.Location, shop.Realm)
return string.format("|-\n| [[%s]] | [[%s]] | [[%s]]", shop.ShopName, shop.Location, shop.Realm)
end
end
end
end
return table.concat(o, "\n")
return string.format("Shop %s not found", shopName)
end
end


Line 49: Line 49:
local rows = {}
local rows = {}
for _, shop in ipairs(shops) do
for _, shop in ipairs(shops) do
table.insert(rows, string.format("|-\n| [[%s]] || [[%s]] || [[%s]]", shop.ShopName, shop.Location, shop.Realm))
table.insert(rows, string.format("|-\n| [[%s]] | [[%s]] | [[%s]]", shop.ShopName, shop.Location, shop.Realm))
end
end
return table.concat(rows, "\n")
return table.concat(rows, "\n")

Revision as of 01:01, 8 March 2024

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

local p = {}

local shops = {
    {ShopName = "Helpful Herbert's Hut",        Location = "DisenchantedForest", Realm = "Jarvonia"},
    {ShopName = "Temple of Azurazera",          Location = "Azurazera",          Realm = "Jarvonia"},
    {ShopName = "The Icy Goblet",               Location = "Kallaheim",          Realm = "Jarvonia"},
    {ShopName = "Kallaheim's Frosty Finds",     Location = "Kallaheim",          Realm = "Jarvonia"},
    {ShopName = "Jarvojoy",                     Location = "Frusenholm",         Realm = "Jarvonia"},
    {ShopName = "Northstar Mercantile",         Location = "Centaham",           Realm = "Jarvonia"},
    {ShopName = "Frosthook Emporium",           Location = "PortSkildar",        Realm = "Jarvonia"},
    {ShopName = "Woodcutter's Cabin",           Location = "NomadWoods",         Realm = "Jarvonia"},
    {ShopName = "Fort of Permafrost",           Location = "FortOfPermafrost",   Realm = "Jarvonia"},
    {ShopName = "Paws Are Us",                  Location = "Salsfirth",          Realm = "GDTE"},
    {ShopName = "Mossy Merchant's Breeze Mart", Location = "Everhaven",          Realm = "GDTE"},
    {ShopName = "Bilgemont Castle",             Location = "BilgemontPort",      Realm = "GDTE"},
    {ShopName = "Fish Market Stall",            Location = "Granfiddich",        Realm = "GDTE"},
    {ShopName = "Food Market Stall",            Location = "Granfiddich",        Realm = "GDTE"},
    {ShopName = "Tool Market Stall",            Location = "Granfiddich",        Realm = "GDTE"},
}

function p.getShopData(frame)
	local shopName = frame.args[1]
	
    for _, shop in ipairs(shops) do
        if shop.ShopName == shopName then
            return shop
        end
    end
    return "Shop not found"
end

function p.getShopData_AsTableRow(frame)
    -- Extract the shop name from the frame arguments
    local shopName = frame.args[1]

    for _, shop in ipairs(shops) do
        if shop.ShopName == shopName then
            return string.format("|-\n| [[%s]] | [[%s]] | [[%s]]", shop.ShopName, shop.Location, shop.Realm)
        end
    end
    return string.format("Shop %s not found", shopName)
end

function p.getAllShops()
    return shops
end

function p.getAllShops_AsTableRows()
    local rows = {}
    for _, shop in ipairs(shops) do
        table.insert(rows, string.format("|-\n| [[%s]] | [[%s]] | [[%s]]", shop.ShopName, shop.Location, shop.Realm))
    end
    return table.concat(rows, "\n")
end

return p