Difference between revisions of "Module:Items"

From Nookipedia, the Animal Crossing wiki
(Creating)
 
m (Function capitalization)
(32 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
local p = {}
 
local p = {}
function p.outputFurniture (frame)
+
 
 +
function insert(str1, str2, pos)
 +
    return str1:sub(1,pos)..str2..str1:sub(pos+1)
 +
end
 +
 
 +
function split(str, pattern)
 +
    local out = {}
 +
    local i = 1
 +
 
 +
    local split_start, split_end = string.find(str, pattern, i)
 +
    while split_start do
 +
        out[#out+1] = string.sub(str, i, split_start - 1)
 +
        i = split_end + 1
 +
        split_start, split_end = string.find(str, pattern, i)
 +
    end
 +
    out[#out+1] = string.sub(str, i)
 +
   
 +
    return out
 +
end
 +
 
 +
function p.italicizeGameInTitle(frame)
 +
local title = frame.args[1]
 +
    local openParenthIndex = string.len(title) - (title:reverse()):find("%(")
 +
    local closeParenthIndex = string.len(title) -  (title:reverse()):find("%)")
 +
 
 +
    local newTitle = title
 +
    newTitle = insert(newTitle, "''", openParenthIndex + 1)
 +
    newTitle = insert(newTitle, "''", closeParenthIndex + 2)
 +
 
 +
    local root = mw.html.create()
 +
root:wikitext(newTitle)
 +
return newTitle
 +
end
 +
 
 +
function p.outputSortedFurniture (frame)
 
     local game = frame.args[2]
 
     local game = frame.args[2]
 
     local data = mw.text.jsonDecode(frame.args[1])
 
     local data = mw.text.jsonDecode(frame.args[1])
    tbl = {}
+
     table.sort(data, function(k1, k2) return string.len(k1.name) < string.len(k2.name) end)
     table.foreach(data, function(k, v) table.insert(tbl, { [k] = v }) end )
 
    table.sort(tbl, function(k, v) return string.len(table.foreach(k, function(k2, v2) return k2 end)) < string.len(table.foreach(v, function(k2, v2) return k2 end)) end)
 
 
     local furnitureOutput = ""
 
     local furnitureOutput = ""
     table.foreach(tbl, function(k, v)
+
     for k, v in ipairs(data) do
         table.foreach(tbl[k], function(k2, v2) furnitureOutput = furnitureOutput .. frame:expandTemplate{ title = "House Furn Cell", args = { item = k2, count = v2['count'], img = v2['img'], game = game } } end ) end )
+
         furnitureOutput = furnitureOutput .. frame:expandTemplate{ title = "HouseItem", args = { item = v['name'], count = v['count'], img = v['img'], note = v['note'], game = game } }
 +
    end
 
     return furnitureOutput
 
     return furnitureOutput
 
end
 
end
 +
 +
function p.outputAvailabilitiesFromList (frame)
 +
    local data = frame.args[1]
 +
    local availabilityTable = split( data, "," )
 +
    local availability = ""
 +
    local availabilityList = ""
 +
    local mainAvailability = ""
 +
    local availabilityText = ""
 +
    local availabilityOutput = ""
 +
    for k, avail in ipairs(availabilityTable) do
 +
        if avail:match("%((.-)%)") then
 +
        availabilityText = avail:match("(.+)%(")
 +
            availability = frame:expandTemplate{ title = "Availability", args = { availabilityText, note = avail:match("%((.-)%)") } }
 +
        else
 +
        availabilityText = avail
 +
            availability = frame:expandTemplate{ title = "Availability", args = { availabilityText } }
 +
        end
 +
       
 +
        availabilityList = availabilityList .. availability
 +
    end
 +
 +
local root = mw.html.create()
 +
root:wikitext(availabilityList)
 +
 +
return root
 +
end
 +
 +
function p.outputVillagerFromList (frame)
 +
    local data = frame.args[1]
 +
    local villagerTable = split( data, "," )
 +
    local villager = ""
 +
    local villagerList = ""
 +
    local mainVillager = ""
 +
    local villagerOutput = ""
 +
    local argument = "Villager"
 +
    for k, villa in ipairs(villagerTable) do
 +
        villager = frame:expandTemplate{ title = "Availability", args = { argument, villa } }
 +
        villagerList = villagerList .. villager
 +
    end
 +
 +
local root = mw.html.create()
 +
root:wikitext(villagerList)
 +
 +
return root
 +
end
 +
 
return p
 
return p

Revision as of 21:07, August 29, 2021

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

local p = {}

function insert(str1, str2, pos)
    return str1:sub(1,pos)..str2..str1:sub(pos+1)
end

function split(str, pattern)
    local out = {}
    local i = 1

    local split_start, split_end = string.find(str, pattern, i)
    while split_start do
        out[#out+1] = string.sub(str, i, split_start - 1)
        i = split_end + 1
        split_start, split_end = string.find(str, pattern, i)
    end
    out[#out+1] = string.sub(str, i)
    
    return out
end

function p.italicizeGameInTitle(frame)
	local title = frame.args[1]
    local openParenthIndex = string.len(title) - (title:reverse()):find("%(")
    local closeParenthIndex = string.len(title) -  (title:reverse()):find("%)")

    local newTitle = title
    newTitle = insert(newTitle, "''", openParenthIndex + 1)
    newTitle = insert(newTitle, "''", closeParenthIndex + 2)

    local root = mw.html.create()
	root:wikitext(newTitle)
	return newTitle
end

function p.outputSortedFurniture (frame)
    local game = frame.args[2]
    local data = mw.text.jsonDecode(frame.args[1])
    table.sort(data, function(k1, k2) return string.len(k1.name) < string.len(k2.name) end)
    local furnitureOutput = ""
    for k, v in ipairs(data) do
        furnitureOutput = furnitureOutput .. frame:expandTemplate{ title = "HouseItem", args = { item = v['name'], count = v['count'], img = v['img'], note = v['note'], game = game } }
    end
    return furnitureOutput
end

function p.outputAvailabilitiesFromList (frame)
    local data = frame.args[1]
    local availabilityTable = split( data, "," )
    local availability = ""
    local availabilityList = ""
    local mainAvailability = ""
    local availabilityText = ""
    local availabilityOutput = ""
    for k, avail in ipairs(availabilityTable) do
        if avail:match("%((.-)%)") then
        	availabilityText = avail:match("(.+)%(")
            availability = frame:expandTemplate{ title = "Availability", args = { availabilityText, note = avail:match("%((.-)%)") } }
        else
        	availabilityText = avail
            availability = frame:expandTemplate{ title = "Availability", args = { availabilityText } }
        end
        
        availabilityList = availabilityList .. availability
    end

	local root = mw.html.create()
	root:wikitext(availabilityList)
	
	return root
end

function p.outputVillagerFromList (frame)
    local data = frame.args[1]
    local villagerTable = split( data, "," )
    local villager = ""
    local villagerList = ""
    local mainVillager = ""
    local villagerOutput = ""
    local argument = "Villager"
    for k, villa in ipairs(villagerTable) do
        villager = frame:expandTemplate{ title = "Availability", args = { argument, villa } }
        villagerList = villagerList .. villager
    end

	local root = mw.html.create()
	root:wikitext(villagerList)
	
	return root
end

return p