Module:FoodDetails

De Eco - Wiki Français
Version datée du 19 novembre 2025 à 01:24 par BuloBon (discussion | contributions)
(diff) ← Version précédente | Version actuelle (diff) | Version suivante → (diff)
Aller à :navigation, rechercher

Ce module fournit la fonctionnalité back-end du Template:FoodDetails.

Si le modèle est utilisé, ce module crée un tableau en utilisant les informations provenant des modules suivants :


local p = {}

function p.main()
	local wiki = ''

	-- import the required modules
	local itemData = mw.loadData("Module:ItemData")
	local recipeData = mw.loadData("Module:RecipeData")
	local plantData = mw.loadData("Module:PlantData")

	-- assign modules for use
	local items = itemData.items
	local recipes = recipeData.recipes
	local plants = plantData.plants

	-- table header
	local header = [=[
<table class="wikitable sortable">
<tr>
<th>Food</th>
<th>Calories</th>
<th data-sort-type="number" style="color: red;">Carbs</th>
<th style="color: orange;">Protein</th>
<th style="color: yellow;">Fat</th>
<th style="color: lightgreen;">Vitamins</th>
<th>Nutrition</th>
<th>Density (/100kcal)</th>
<th>Skill Point Contribution</th>
<th>Created With</th>
<th>Skill Needed</th>
<th>Skill Level</th>
<th data-sort-type="number">Base Crafting Time (Min)</th>
</tr>
]=]

	wiki = wiki .. header

	for itemName, v in pairs(items) do
		if v.group == "Food" then
			local row = "<tr>\n"

			-- nutrition
			local nutrition = (v.vitamins or 0) + (v.carbs or 0) + (v.protein or 0) + (v.fat or 0)
			local sp_contribution = nutrition * (v.calories or 0)

			row = row .. "<td> [[" .. itemName .. "]] </td>\n"
			row = row .. "<td>" .. (v.calories or 0) .. "</td>\n"
			row = row .. "<td>" .. (v.carbs or 0) .. "</td>\n"
			row = row .. "<td>" .. (v.protein or 0) .. "</td>\n"
			row = row .. "<td>" .. (v.fat or 0) .. "</td>\n"
			row = row .. "<td>" .. (v.vitamins or 0) .. "</td>\n"
			row = row .. "<td>" .. nutrition .. "</td>\n"
			row = row .. "<td>" .. (v.density or 0) .. "</td>\n"
			row = row .. "<td>" .. sp_contribution .. "</td>\n"

			------------------------------------------------------------------
			-- FIND SOURCE: recipe or plant
			------------------------------------------------------------------

			local created = "-"
			local skill = "-"
			local skill_level = "-"
			local baseCraftTime = "-"

			local recipe = recipes[itemName]
			local plant = nil

			-- 1) RECIPE FOUND
			if recipe then
				if recipe.craftStn and recipe.craftStn[1] then
					created = "[[" .. recipe.craftStn[1][1] .. "]]"
				end

				if recipe.skillNeeds and recipe.skillNeeds[1] then
					skill = "[[" .. recipe.skillNeeds[1][1] .. "]]"
					skill_level = tonumber(recipe.skillNeeds[1][2]) or "-"
				else
					skill = "none"
					skill_level = "-"
				end

				baseCraftTime = tonumber(recipe.baseCraftTime)
				if not baseCraftTime or baseCraftTime == 0 then
					baseCraftTime = "?"
				end

			else
				-- 2) NO RECIPE → CHECK PLANT
				for _, plantInfo in pairs(plants) do
					if plantInfo.resourceItem == "[[" .. itemName .. "]]" then
						plant = plantInfo
						break
					end
				end

				if plant then
					skill = "[[Gathering]]"
					skill_level = 0

					if plant.harvestTool and plant.killOnHarvest == "Yes" then
						created = "[[" .. plant.harvestTool .. "]]"
					else
						created = "none"
					end

					baseCraftTime = 0
				end
			end

			------------------------------------------------------------------

			row = row .. "<td>" .. created .. "</td>\n"
			row = row .. "<td>" .. skill .. "</td>\n"
			row = row .. "<td>" .. skill_level .. "</td>\n"
			row = row .. "<td>" .. baseCraftTime .. "</td>\n"
			row = row .. "</tr>\n"

			wiki = wiki .. row
		end
	end

	wiki = wiki .. "</table>"
	return wiki
end

return p