Jump to content
Menü aufrufen
Toggle preferences menu
Persönliches Menü aufrufen
Nicht angemeldet
Ihre IP-Adresse wird öffentlich sichtbar sein, wenn Sie Änderungen vornehmen.

Die Dokumentation für dieses Modul kann unter Modul:Entity/Facet/Consumable/doc erstellt werden

require('strict')

--- @module Entity/Facet/Consumable
--- Consumable facet. Data-driven: matches any entity carrying `apiData.food`
--- (Food items, Drink items, and — once Commodity.enrich attaches it — edible
--- commodities). Replaces the former Item/Food and Item/Drink subtype modules,
--- which were the same facet split under single inheritance. The only real
--- difference between them was Drink's `can_be_reclosed` row, now rendered
--- data-driven below.

local format = require('Module:Entity/Format')
local DietaryEffect = require('Module:DietaryEffect')
local Boolean = require('Module:Boolean')
local msg = require('Module:Entity/Message').msg

local p = {}

--- Data-driven match: presence of the `food` payload. Nil-safe; never reads
--- kind identity. Returns a strict boolean.
---
--- @param apiData table|nil
--- @return boolean
function p.matches(apiData)
	return apiData ~= nil and apiData.food ~= nil
end

--- @param ctx EntityHookContext
--- @return table[] Ordered list of section entries with key field
function p.getSections(ctx)
	local apiData = ctx.apiData
	local food = apiData.food
	if not food then
		return {}
	end

	local items = {}

	if food.nutritional_density_rating then
		table.insert(items, { label = msg('NDR'), content = tostring(food.nutritional_density_rating) })
	end

	if food.hydration_efficacy_index then
		table.insert(items, { label = msg('HEI'), content = tostring(food.hydration_efficacy_index) })
	end

	-- Effects render as coloured badges (green up = beneficial, red down =
	-- detrimental, grey = None / unknown) linking to their canonical effect page.
	-- See Module:DietaryEffect.
	local effectsContent = DietaryEffect.renderBadges(food.effects)
	if effectsContent then
		table.insert(items, { label = msg('Effects'), content = effectsContent })
	end

	if food.one_shot_consume ~= nil then
		table.insert(items, { label = msg('Single use'), content = Boolean.render(food.one_shot_consume) })
	end

	-- Reclosable was Drink-only under the old subtypes. Gate on presence so it
	-- collapses for food and appears for drink, without branching on kind.
	if food.can_be_reclosed ~= nil then
		table.insert(items, { label = msg('Reclosable'), content = Boolean.render(food.can_be_reclosed) })
	end

	if #items == 0 then
		return {}
	end

	return {
		{
			key = 'consumable',
			label = msg('Consumable'),
			collapsible = true,
			items = items,
		},
	}
end

--- Contributes consumable facets to structured data so edible entities (food /
--- drink items and edible commodities) are queryable uniformly: nutritional
--- density rating, hydration efficacy index, and effects. NDR / HEI arrive as
--- strings from the API and are coerced to numbers for range / sort queries;
--- effects stays a list (stored as a multi-valued property). Each field is
--- omitted when absent so the stored set stays clean. Nil-safe even though the
--- facet only runs when matches() is true.
---
--- @param ctx EntityHookContext
--- @return table<string, any>
function p.getStructuredData(ctx)
	local apiData = ctx.apiData
	local food = apiData.food
	if not food then
		return {}
	end
	local effects = food.effects
	if type(effects) == 'table' then
		-- The API sends a literal 'None' on effect-less foods; drop it so the
		-- stored set and the short description stay clean.
		local clean = {}
		for _, e in ipairs(effects) do
			if e ~= nil and e ~= 'None' then
				clean[#clean + 1] = e
			end
		end
		effects = clean[1] and clean or nil
	end
	return {
		ndr = tonumber(food.nutritional_density_rating),
		hei = tonumber(food.hydration_efficacy_index),
		effects = effects,
	}
end

--- Optional short-description adjective: the consumable's effects, each
--- localized, joined with the localized and-word, returned as a compound stem
--- ending in '-' so the kind hyphen-joins its noun ("Toxisch-Lebensmittel").
--- The literal 'None' and empty lists yield nil so the plain description stands.
---
--- @param ctx EntityHookContext
--- @return string|nil
function p.getShortDescriptionPrefix(ctx)
	local apiData = ctx.apiData
	local effects = apiData.food and apiData.food.effects
	if type(effects) ~= 'table' then
		return nil
	end
	local parts = {}
	for _, e in ipairs(effects) do
		if e ~= nil and e ~= 'None' then
			parts[#parts + 1] = msg(e)
		end
	end
	if #parts == 0 then
		return nil
	end
	return table.concat(parts, msg(' and ')) .. '-'
end

return p
Zuletzt geändert
Urheberrecht
Cookies helfen uns bei der Bereitstellung dieses Wikis. Durch die Nutzung des Star Citizen Wiki erklärst du dich damit einverstanden, dass wir Cookies speichern.