Module:Gifts

From Roots of Pacha Wiki
Jump to navigation Jump to search

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

local p = {}

local giftsTable = mw.loadData('Module:Gifts/data')

function p.getAllFrom(frame)
	local characterName = frame.args[1]
	local giftData = giftsTable[characterName]
	
	local universalLovesException = Set { "Ata", "Jag", "Krak", "Ron" }
	local universalLoves = not universalLovesException[characterName] and "<li>{{Name|Mango}}" or ""
	local universalLikesException = Set { "Gin", "Grob", "Jizu", "Nokk", "Ron" }
	local universalLikes = not universalLikesException[characterName] and "<li>{{Name|Juice|W|Mango Juice}}<li>{{Name|Juice||Pomegranate Juice}}" or ""
	local universalHatesException = Set { "Vor", "Ada", "Brah", "Croll", "Gin", "Grob", "Jag" }
	local universalHates = not universalHatesException[characterName] and "<li>{{Name|Poop}}" or ""
	
	if giftData then
		local template = [=[ 
 {| class="pacha no-bullets" style="width:65%; list-style: none;"
 ! style="width:15%;" |
 ! Items
 |-
 ! Love
 | {{Cols|]=].. universalLoves .. getItems(giftData.lovesItems) .. getTags(giftData.lovesTags) ..[=[ }}
 |-
 ! Like
 | {{Cols|]=].. universalLikes .. getItems(giftData.likeItems) .. getTags(giftData.likesTags) ..[=[ }}
 
 |-
 ! Neutral
 | ]=].. getItems(giftData.neutralItems) .. getTags(giftData.neutralTags) ..[=[
 
 |-
 ! Dislike
 | {{Cols|]=].. getItems(giftData.dislikesItems) .. getTags(giftData.dislikesTags) ..[=[ }}
 
 |-
 ! Hate
 | {{Cols|]=].. universalHates  .. getItems(giftData.hatesItems) .. getTags(giftData.hatesTags) ..[=[ }}
 
 |}
]=]

		return frame:preprocess(template)
	else
		return "Data not found! Please refer '''[[module:Gifts/data]]'''"
	end
end

function getItems(itemsList)
	local template = ""
	for _, item in ipairs(itemsList) do
		template = template .. "<li>{{Name|" .. item .. "}}"
	end

	return template:find("Name") and template or "<center>All other items</center>"
end

function getTags(tagsList)
	local template = ""
	for _, tag in ipairs(tagsList) do
		template = template .. "<li>{{Any|" .. tag .. "}}"
	end

	return template:find("Any") and template or ""
end

function Set (list)
   local set = {}
   for _, l in ipairs(list) do set[l] = true end
   return set
end

return p