Module:Data: Difference between revisions

From Kerbal Space Program 2 Wiki
Jump to navigation Jump to search
help
updated NS ref
 
(39 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {}
local p = {}
local getArgs = require('Module:Arguments').getArgs
local getArgs = require('Module:Arguments').getArgs
-- args:
-- subject: The subject entity to get data from.
--- E.g., "Kerbol", "LW-Sw "SWERV""
-- param: The parameter / parameter chain to access from the data page.
--- E.g., "mass", "max_temp", "modules|engine|isp_vac", "modules|generator|outputs|units"


function p.getData(frame)
function p.getData(frame)
-- Get arguments from the invoke
-- Get arguments from the invoke
local args = getArgs(frame) -- E.g., args = {"Example", "modules", "engine", "isp", "vac"}
-- E.g., args = {"Example/engine", "modules", "engine", "isp", "vac"}
dataJson = _getData(args)
local args = getArgs(frame, {frameOnly = true})
-- Load the JSON from the Data: page
-- Pass arguments to the helper function
--local dataJson = mw.loadJsonData("Data:"..args[1])
dataJson = p._getData(args)
return dataJson
--local dataJson = mw.title.new("Data:"..args[1]):getContent()
--dataJson = mw.text.jsonDecode(dataJson)
-- This approach doesn't return anything useable, apparently
--local dataTab = require("Module:Example")
--local jsonData = dataTab.data
-- Iterate through the JSON using the remaining args
--for i = 2, #args do
-- Each loop steps into the next level JSON table,
-- leaving the final value at the end
--dataJson = dataJson[args[i]]
--end
-- This line returns the value as a proper string
--return dataJson[args[2]][args[3]][args[4]][args[5]]
-- This, for some reason, returns a "table" despite coming back as a
-- string when testing the same code in the console.
return dataJson--[args[2]]["engine"]["isp"]["vac"]
end
end


function p._getData(args)
function p._getData(args)
local dataJson = mw.loadJsonData("Data:"..args[1])
-- Load the data from the JSON page, parsed as a table of tables
-- Iterate through the JSON using the remaining args
local dataJson = mw.loadJsonData("GameData:"..args[1])
for i = 2, #args do
-- Each loop steps into the next level JSON table,  
-- Iterate through dataJson using the args values as keys to
-- leaving the final value at the end
-- step down into the table per argument
dataJson = dataJson[args[i]]
for k, v in pairs(args) do
if k > 1 then
dataJson = dataJson[v]
end
end
end
-- This line returns the value as a proper string
--return dataJson[args[2]][args[3]][args[4]][args[5]]
-- This, for some reason, returns a "table" despite coming back as a
-- string when testing the same code in the console.
return dataJson--[args[2]]["engine"]["isp"]["vac"]
end
-- This func returns a "string" to the #invoke
function p.test()
    local dataJson = mw.loadJsonData("Data:Example")
    local temp = dataJson["modules"]["engine"]


    return(temp["isp"]["vac"])
-- Send the final, requested value back to the main function
return dataJson
end
end


return p
return p

Latest revision as of 05:25, 14 February 2025

The Data module exists to translate information from Data: pages to templates/articles.

Arguments

<1> (Entity): This argument is taken as the title of the intended Data: page to search through.

E.g.: "Kerbol", "LV-SW "SWERV""

<2+> (Parameter chain): Arguments 2 and beyond define the parameter chain to search on the data page.

E.g.: "mass", "max_temp", "modules|engine|isp_vac", "modules|generator|outputs|units"

Functions

getData: The primary function of the module. Accepts the full argument set to determine the parameter to return from the subject.

getFuelLabels: Used for engine infobox construction. Accepts a part name (only works for engines) for <1> and optionally "alt" for <2>. Returns a line-break separated list of the propellant names used by an engine mode. Should probably be moved to a different Module.

getFuelValues: Used for engine infobox construction. Accepts a part name (only works for engines) for <1> and optionally "alt" for <2>. Returns a line-break separated list of the propellant use rates of an engine mode. Should probably be moved to a different Module.

Examples

A list of example JSON sets can be found at GameData:Example.

{{#invoke:Data|getData|LV-SW "SWERV"|mass}} - Returns the mass of the "SWERV" in tons, 10.00.

{{#invoke:Data|getData|LV-3000 "Tuba"|modules|engine|thrust|vac}} - Returns the vacuum thrust of the "Tuba" in kilonewtons, 510.0.


local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.getData(frame)
	-- Get arguments from the invoke
	-- E.g., args = {"Example/engine", "modules", "engine", "isp", "vac"}
	local args = getArgs(frame, {frameOnly = true})
	-- Pass arguments to the helper function
	dataJson = p._getData(args)
	return dataJson
end

function p._getData(args)
	-- Load the data from the JSON page, parsed as a table of tables
	local dataJson = mw.loadJsonData("GameData:"..args[1])
	
	-- Iterate through dataJson using the args values as keys to 
	-- step down into the table per argument
	for k, v in pairs(args) do
		if k > 1 then
			dataJson = dataJson[v]
		end
	end

	-- Send the final, requested value back to the main function
	return dataJson
end

return p