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/Mission/doc erstellt werden

require('strict')

--- @module Entity/Mission

local format = require('Module:Entity/Format')
local msg = require('Module:Entity/Message').msg
local Boolean = require('Module:Boolean')
local locationStarmap = require('Module:Entity/Location/Starmap')
local orderLines = require('Module:Entity/Orders/Lines').orderLines
local rewardLines = require('Module:Entity/Rewards/Lines').rewardLines
local Vocab = require('Module:Entity/Vocab')
local structuredData = require('Module:Entity/StructuredData')
local ReputationStyler = require('Module:ReputationStyler')

local GERMAN_CONTRACTS = {
	Appointment = 'Termin-Auftrag',
	['Bounty Hunter'] = 'Kopfgeldjagdauftrag',
	Collection = 'Sammelauftrag',
	Delivery = 'Lieferauftrag',
	['ECN Alert'] = 'ECN-Alarm-Auftrag',
	Hauling = 'Transportauftrag',
	Investigation = 'Ermittlungsauftrag',
	Maintenance = 'Wartungsauftrag',
	Mercenary = 'Söldnerauftrag',
	Priority = 'Prioritätsauftrag',
	Salvage = 'Bergungsauftrag',
}

--- `mission_type` values whose display label is not the raw API string. The API
--- carries 30-odd types and CIG adds more with each patch, so labels are derived
--- from the raw value (see `typeLabel`) and only genuine exceptions are listed
--- here: the two Wikelo buckets are both barter collections to a reader, and the
--- lone `local` record is one mis-tagged Klescher mission whose sibling
--- (`RepairO2Kiosk` vs `PU_RepairO2Kiosk`) is tagged Maintenance.
--- @type table<string, string>
local TYPE_LABELS = {
	['Wikelo - Other Items'] = 'Collection',
	['Wikelo - Vehicles'] = 'Collection',
	['local'] = 'Maintenance',
}

local SCOPE = {
	['BountyHunter_BountyHuntersGuild'] = 'Bounty Hunter',
}

local function resolveReputationScope(scope)
	local label = SCOPE[scope]
	if label and structuredData.backend() == 'smw' then
		return Vocab.toGerman('type', label)
	end
	return label or scope
end

local function cooldownLabel(lifetime)
	if not lifetime or not lifetime.label then
		return nil
	end
	if
		structuredData.backend() == 'smw'
		and type(lifetime.label) == 'string'
		and type(lifetime.respawn_time_seconds) == 'number'
	then
		local unit = lifetime.label:match('^%d+ (seconds?)$')
		if unit then
			return format.formatNum(lifetime.respawn_time_seconds)
				.. (lifetime.respawn_time_seconds == 1 and ' Sekunde' or ' Sekunden')
		end
		unit = lifetime.label:match('^%d+ (minutes?)$')
		if unit then
			local minutes = lifetime.respawn_time_seconds / 60
			return format.formatNum(minutes) .. (minutes == 1 and ' Minute' or ' Minuten')
		end
	end
	return lifetime.label
end

--- `mission_giver` is normally the better of the two faction names: it is the
--- specific contractor a reader can look up (MT Protection Services, BlacJac
--- Security, Miles Eckhart) where `faction.name` gives only the parent
--- corporation (microTech, ArcCorp, Eckhart Security). But on some records it is
--- an unresolved internal token instead, and those must not reach the page.
---
--- Matched on shape rather than by enumerating names, because the token set
--- grows with the data and the per-record `faction.name` is the right answer for
--- each: `MicroTechBountyDepartment` resolves to microTech on some records and
--- Hurston Dynamics on others. `XenoThreat` is deliberately NOT matched, being a
--- real faction name that happens to be unspaced.
--- @param giver string
--- @return boolean
local function isInternalToken(giver)
	return giver:find('_', 1, true) ~= nil
		or giver:find('~', 1, true) ~= nil
		or giver:find('BountyDepartment$') ~= nil
		or giver == 'N/A'
end

--- The faction to show and store: the mission giver, or that record's
--- `faction.name` when the giver is an internal token, or nil when neither is
--- usable (~100 records carry neither). One definition so the infobox row, the
--- stored `Faction` property and the short description can never disagree.
--- @param apiData table
--- @return string|nil
local function resolveFaction(apiData)
	local giver = apiData.mission_giver
	local factionName = apiData.faction and apiData.faction.name
	if type(giver) == 'string' and giver ~= '' and not isInternalToken(giver) then
		return giver
	end
	if type(factionName) == 'string' and factionName ~= '' then
		return factionName
	end
	return nil
end

--- A reputation standing display name: the record's own min_standing/
--- max_standing, else the nested reputation_prerequisite's, else nil. Each
--- level is optional in the corpus - a prerequisite without its standing
--- objects, or a standing without a name, is a real record shape - so every
--- index is guarded and a partial shape answers nil, never a raise. Guards
--- are truthiness: a false standing or name counts as absent, while an
--- empty-string name is present and still wins over the nested one.
--- @param apiData table
--- @param bound string 'min' or 'max'
--- @return string|nil
local function standingName(apiData, bound)
	local standing = apiData[bound .. '_standing']
	if standing and standing.name then
		return standing.name
	end
	local prerequisite = apiData.reputation_prerequisite
	if prerequisite then
		standing = prerequisite[bound .. '_standing']
		if standing and standing.name then
			return standing.name
		end
	end
	return nil
end

local function marksUnavailable(value)
	local x = value:lower()
	return x == 'false' or x == 'no' or (x == 'nein' and require('Module:Entity/StructuredData').backend() == 'smw')
end

local function payoutValue(args, apiData)
	local editorValue = args.payout
	if require('Module:Entity/StructuredData').backend() ~= 'smw' then
		return editorValue or apiData.reward_min
	end
	local value = editorValue
	if value == nil then
		value = apiData.reward_min
	end
	if value == nil then
		return nil
	end
	local number = value
	if type(value) == 'string' then
		if editorValue ~= nil then
			number = require('Module:Entity/StructuredData/Smw').parseGermanNumber(value)
		else
			number = value:match('^%-?%d*%.?%d+$') and tonumber(value) or nil
		end
	end
	if type(number) ~= 'number' or number % 1 ~= 0 then
		error('SMW: field uec value "' .. tostring(value) .. '" is not a finite integer', 0)
	end
	return number
end

local function resolveReputation(rank, scope)
	local styled = ReputationStyler.style(rank, scope)
	-- The styler answers a display string for a missing rank; German readers
	-- get the German word through the message dataset.
	if styled == 'Unspecified' then
		return msg('Unspecified')
	end
	return styled
end

--- The reader-facing name for a raw `mission_type`: an entry in TYPE_LABELS, or
--- the API string as-is. Derived rather than enumerated so a type CIG adds later
--- renders under its own name instead of erroring.
--- @param missionType string
--- @return string
local function typeLabel(missionType)
	return TYPE_LABELS[missionType] or missionType
end

--- The browse category for a type label, matching the names the retired
--- {{Contract}} template generated through {{Fixcaps}}: first letter upper, rest
--- lower, plus ' contracts'. So 'Bounty Hunter' routes to the existing
--- [[Category:Bounty hunter contracts]], not a near-miss duplicate.
--- @param label string
--- @return string
local function typeCategory(label)
	return mw.ustring.upper(mw.ustring.sub(label, 1, 1)) .. mw.ustring.lower(mw.ustring.sub(label, 2)) .. ' contracts'
end

local function linked(str)
	if type(str) == 'string' then
		return '[[' .. str .. ']]'
	end
	return str
end

local function fixTitle(title)
	title = string.gsub(title, '[%[%]]', '')
	title = string.gsub(title, '|', '-')
	return title
end

local p = {}

--- Canonical kind name, exposed as Data.get(args).kind (non-empty and
--- unique, enforced by the Registry conformance test).
p.name = 'Mission'

--- @type string
p.parent = 'Entity/Base'

--- @return EntityApiConfig[]
function p.getApiConfigs()
	return {
		{
			name = 'StarCitizenWikiAPI',
			endpoint = 'missions/%s',
			responseDataPath = 'data',
		},
	}
end

--- @param apiData table|nil
--- @return boolean
function p.matches(apiData)
	return apiData ~= nil and apiData.mission_type ~= nil
end

--- @param ctx EntityHookContext
--- @return table|nil { name, category, categories }
function p.getTypeInfo(ctx)
	local missionType = ctx.apiData.mission_type
	if type(missionType) ~= 'string' or missionType == '' then
		return nil
	end
	local label = typeLabel(missionType)
	local category = GERMAN_CONTRACTS[label] or false
	if structuredData.backend() == 'bucket' then
		category = typeCategory(label)
	end
	return {
		name = label,
		category = category,
		categories = { structuredData.backend() == 'smw' and 'Auftrag' or 'Contracts' },
	}
end

--- @param ctx EntityHookContext
--- @return table[]
function p.getSections(ctx)
	local apiData, args = ctx.apiData, ctx.args
	local typeInfo = p.getTypeInfo(ctx)
	local faction = resolveFaction(apiData)

	local data = {
		category = 'Verified',
		type = typeInfo
			and (structuredData.backend() == 'smw' and Vocab.toGerman('type', typeInfo.name) or typeInfo.name),
		cooldown = nil,
		shareable = 'Yes',
		available = 'Available',
		pickup = structuredData.backend() == 'smw' and 'Auftragsmanager' or 'Contract Manager',
		payout = {
			uec = nil,
			scrip = {
				-- Overwritten by whichever scrip the contract actually awards;
				-- MG Scrip is the default only because it is the common case.
				name = 'MG Scrip',
				amount = 0,
			},
		},
		requirements = {
			fee = nil,
			missions = {},
		},
		reputation = {
			scope = nil,
			gain = nil,
			min = nil,
			max = nil,
		},
		location = {
			systems = {},
		},
		reward = {
			items = {},
			blueprints = {},
		},
	}

	data.cooldown = cooldownLabel(apiData.lifetime)
	local availabilityArg = args.available
	if availabilityArg then
		data.available = marksUnavailable(availabilityArg) and 'Unavailable' or 'Available'
	elseif apiData.not_for_release then
		data.available = 'Unavailable'
	end
	if apiData.illegal then
		data.category = 'Unverified'
	end
	if apiData.shareable ~= nil and not apiData.shareable then
		data.shareable = 'No'
	end
	if args['pickup'] then
		data.pickup = args['pickup']
	elseif apiData.mission_giver == 'Wikelo' then
		data.pickup = 'Wikelo Emporium'
	end
	if apiData.cost then
		data.requirements.fee = apiData.cost
	end
	if apiData.prerequisite_groups and #apiData.prerequisite_groups > 0 then
		for _, group in ipairs(apiData.prerequisite_groups) do
			if group.missions and #group.missions > 0 then
				for _, mission in ipairs(group.missions) do
					if mission.variant_count then
						table.insert(
							data.requirements.missions,
							linked(fixTitle(mission.title)) .. ' x' .. tostring(mission.variant_count)
						)
					else
						table.insert(data.requirements.missions, linked(fixTitle(mission.title)))
					end
				end
			end
		end
	end

	data.payout.uec = payoutValue(args, apiData)

	if apiData.reward_items and #apiData.reward_items > 0 then
		for _, reward in ipairs(apiData.reward_items) do
			if reward.name == 'MG Scrip' or reward.name == 'Council Scrip' then
				data.payout.scrip.name = reward.name
				data.payout.scrip.amount = data.payout.scrip.amount + reward.amount
			end
		end
	end

	data.reputation.min = standingName(apiData, 'min')
	data.reputation.max = standingName(apiData, 'max')
	data.reputation.scope = apiData.reputation_gained
		and #apiData.reputation_gained > 0
		and resolveReputationScope(apiData.reputation_gained[1].scope)
	data.reputation.gain = apiData.reputation_amount

	-- Always an array in the current corpus, but defaulted so the hook tolerates a
	-- partial record (the API omitting the key, or a caller passing one field).
	data.location.systems = apiData.star_systems or {}

	local general = {
		key = 'general',
		items = {
			{ label = msg('Faction'), content = linked(faction) },
			{ label = msg('Category'), content = msg(data.category) },
			{ label = msg('Type'), content = data.type },
			{ label = msg('Contract Pickup'), content = linked(data.pickup) },
			{ label = msg('Shareable'), content = Boolean.render(data.shareable) },
			{ label = msg('Availability'), content = msg(data.available) },
			{ label = msg('Cooldown'), content = data.cooldown },
		},
	}

	local sections = { general }

	if data.payout.uec or data.payout.scrip.amount > 0 then
		table.insert(sections, {
			key = 'payout',
			label = msg('Payout'),
			collapsible = true,
			items = {
				{ label = 'aUEC', content = format.formatNum(data.payout.uec) },
				{
					label = data.payout.scrip.name,
					content = data.payout.scrip.amount > 0 and format.formatNum(data.payout.scrip.amount),
				},
			},
		})
	end

	if data.reputation.min or data.reputation.max or data.reputation.scope or data.reputation.gain then
		table.insert(sections, {
			key = 'reputation',
			label = msg('Reputation'),
			collapsible = true,
			items = {
				{ label = msg('Min'), content = resolveReputation(data.reputation.min, data.reputation.scope) },
				{ label = msg('Max'), content = resolveReputation(data.reputation.max, data.reputation.scope) },
				{ label = msg('Scope'), content = data.reputation.scope },
				{ label = msg('Gain'), content = format.formatNum(data.reputation.gain) },
			},
		})
	end

	table.insert(sections, {
		key = 'requirements',
		label = msg('Requirements'),
		collapsible = true,
		items = {
			{
				label = msg('Fee'),
				content = data.requirements.fee and format.formatNum(data.requirements.fee) .. ' aUEC',
			},
			{
				label = msg('Contracts'),
				content = #data.requirements.missions > 0 and table.concat(data.requirements.missions, ',<br/>'),
			},
		},
	})

	local location = {
		key = 'location',
		label = msg('Location'),
		collapsible = true,
		items = {},
	}

	local systems = {}
	local isSmw = require('Module:Entity/StructuredData').backend() == 'smw'
	for _, system in ipairs(data.location.systems) do
		-- An empty star_system names no system (the accessor nils it), so the
		-- row keeps the entries that do name one.
		local short = locationStarmap.systemLinkLabel(system)
		if short then
			if isSmw then
				-- SMW links the stored spelling: systemLinkTarget applies the
				-- writer's exact PAGE shaping then the canonicalization, and the
				-- label takes the same fold's short form, so an underscored or
				-- linked entry renders one clean link instead of nesting its
				-- wikitext inside the label or the target.
				local target = locationStarmap.systemLinkTarget(system)
				local label = msg('$1 system', { short })
				table.insert(systems, label == target and linked(target) or '[[' .. target .. '|' .. label .. ']]')
			else
				-- Bucket keeps the English wiki's exact target: the RAW value with
				-- ' system' appended, unshortened - the shape every systems row already
				-- on that wiki was built by. The label localizes from the same raw
				-- value, and its English fallback composes to exactly that target, so
				-- that wiki's rows stay the single links they always were.
				local target = system .. ' system'
				local label = msg('$1 system', { system })
				table.insert(systems, label == target and linked(target) or '[[' .. target .. '|' .. label .. ']]')
			end
		end
	end
	if #systems > 0 then
		table.insert(location.items, { label = msg('Systems'), content = format.joinAnd(systems) })
	end

	if #location.items > 0 then
		table.insert(sections, location)
	end

	return sections
end

--- @param ctx EntityHookContext
--- @return table<string, any>
function p.getStructuredData(ctx)
	local apiData, args = ctx.apiData, ctx.args
	local typeInfo = p.getTypeInfo(ctx)
	local faction = resolveFaction(apiData)
	local scrip
	local available = true

	local availabilityArg = args.available
	if availabilityArg then
		available = not marksUnavailable(availabilityArg)
	elseif apiData.not_for_release then
		available = false
	end
	if apiData.reward_items and #apiData.reward_items > 0 then
		for _, reward in ipairs(apiData.reward_items) do
			if reward.name == 'MG Scrip' or reward.name == 'Council Scrip' then
				scrip = (scrip or 0) + reward.amount
			end
		end
	end

	local orders = orderLines(apiData.hauling_orders)
	local rewards = rewardLines(apiData.reward_groups, apiData.blueprints)

	return {
		legality = apiData.illegal and 'unverified' or 'verified',
		type = typeInfo and typeInfo.name,
		faction = faction,
		systems = apiData.star_systems,
		uec = payoutValue(args, apiData),
		scrip = scrip,
		reputation_min = standingName(apiData, 'min') or 'None',
		reputation_max = standingName(apiData, 'max') or 'None',
		available = available,
		orders = #orders > 0 and orders or nil,
		rewards = #rewards > 0 and rewards or nil,
	}
end

--- German descriptions use the mapped mission category; Bucket retains its faction-based description.
---
--- @param ctx EntityHookContext
--- @return string|nil
function p.getShortDescription(ctx)
	local apiData, typeInfo = ctx.apiData, ctx.typeInfo
	local faction = resolveFaction(apiData)
	local typeName = typeInfo and typeInfo.name

	if not typeName then
		return nil
	end
	if structuredData.backend() == 'smw' then
		local description = GERMAN_CONTRACTS[typeName] or typeName .. '-Auftrag'
		if type(faction) == 'string' and faction ~= '' then
			return description .. ' von ' .. faction
		end
		return description
	end
	if type(faction) ~= 'string' or faction == '' then
		return string.format('%s contract', (typeName:gsub('^%l', string.upper)))
	end

	return string.format('%s %s contract', faction:lower():gsub('^%l', string.upper), typeName:lower())
end

--- @param ctx EntityHookContext
--- @return EntityItemData[]
function p.getExternalSiteItems(ctx)
	return {}
end

--- SCMDB as a footer action button, the same shape as the star systems'
--- Starmap button.
---
--- SCMDB keys its catalogue by the game's own contract GUID, which is the
--- value the API returns as `uuid`, so the page's uuid addresses the contract
--- there with no mapping table: `?g=` resolves against a contract id or one of
--- its contractDefinitionIds and the site then canonicalises the URL itself.
--- Its `?m=` form is a hash of the internal debug name, so it is not linkable
--- -- a rename breaks it silently and two contracts sharing a debug name
--- collide onto one URL.
---
--- Offered for every record with a uuid rather than gated on whether SCMDB
--- holds it. Its catalogue covers the contractor-given career contracts and
--- not the `PU_*` families, so a good fraction of ours are absent, but an id
--- it does not hold opens its mission browser rather than an error page.
---
--- @param ctx EntityHookContext
--- @return table[]
function p.getFooterButtons(ctx)
	local uuid = ctx.args.uuid or ctx.apiData.uuid
	if type(uuid) ~= 'string' or uuid == '' then
		return {}
	end
	return {
		{
			label = 'SCMDB',
			url = 'https://scmdb.net/?g=' .. uuid,
			class = 't-button--branded t-button--scmdb',
		},
	}
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.