Difference between revisions of "Module:Items"
From Nookipedia, the Animal Crossing wiki
SuperHamster (talk | contribs) m (SuperHamster moved page Module:Furniture to Module:Items without leaving a redirect: Potential use beyond just furniture) |
SuperHamster (talk | contribs) (+outputAvailability (takes in a string of availabilities separated by /, and outputs Availability templates)) |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
+ | |||
+ | 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.outputSortedFurniture (frame) | function p.outputSortedFurniture (frame) | ||
local game = frame.args[2] | local game = frame.args[2] | ||
Line 10: | Line 26: | ||
return furnitureOutput | return furnitureOutput | ||
end | end | ||
+ | |||
+ | function p.outputAvailability (frame) | ||
+ | local data = frame.args[1] | ||
+ | local availabilityTable = split( data, "/" ) | ||
+ | local availabilityOutput = "" | ||
+ | for k, avail in ipairs(availabilityTable) do | ||
+ | availabilityOutput = availabilityOutput .. frame:expandTemplate{ title = "Availability", args = { avail } } | ||
+ | end | ||
+ | |||
+ | return availabilityOutput | ||
+ | end | ||
+ | |||
return p | return p |
Revision as of 03:00, December 23, 2020
Documentation for this module may be created at Module:Items/doc
local p = {}
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.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.outputAvailability (frame)
local data = frame.args[1]
local availabilityTable = split( data, "/" )
local availabilityOutput = ""
for k, avail in ipairs(availabilityTable) do
availabilityOutput = availabilityOutput .. frame:expandTemplate{ title = "Availability", args = { avail } }
end
return availabilityOutput
end
return p