Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Data: Difference between revisions

From Kerbal Space Program 2 Wiki
(previous test worked, next attempt)
(overall commenting cleanup / overhaul)
 
(45 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"}
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("Data:"..args[1])
-- Load the JSON from the Data: page
-- 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
 
----- PROBABLY MOVE EVERYTHING UNDER THIS LINE TO A DIFFERENT MODULE -------
 
-- Re-format the resource names in part data to be displayed
function p._formatResource(resource)
if resource == "monopropellant" then return "Monopropellant" end
if resource == "methane" then return "Methane" end
if resource == "oxidizer" then return "Oxidizer" end
if resource == "intake_air" then return "Intake Air" end
if resource == "solid_fuel" then return "Solid Fuel" end
if resource == "hydrogen" then return "Hydrogen" end
if resource == "xenon" then return "Xenon" end
if resource == "electric_charge" then return "Electric Charge" end
end
 
function p.getFuelLabels(frame)
local args = getArgs(frame)
propellants = p._getFuelLabels(args)
return propellants
end
 
function p._getFuelLabels(args)
-- Load the data from the JSON page, parsed as a table of tables
local dataJson = mw.loadJsonData("Data:"..args[1])
local dataJson = mw.loadJsonData("Data:"..args[1])
dataJson = dataJson[args[2]]
local propellants = ""
local mode = ""
--local dataJson = mw.title.new("Data:"..args[1]):getContent()
-- Engine alt mode handling
--dataJson = mw.text.jsonDecode(dataJson)
if args[2] == "alt" then
mode = dataJson["modules"]["engine"]["alt_mode"]["propellants"]
-- This approach doesn't return anything useable, apparently
else
--local dataTab = require("Module:Example")
mode = dataJson["modules"]["engine"]["propellants"]
--local jsonData = dataTab.data
end
-- Iterate through the JSON using the remaining args
-- Build the propellants label list
for i = 3, 5 do
for k, v in pairs(mode) do
-- Each loop steps into the next level JSON table,
if k == "units" then
-- leaving the final value at the end
-- Dummy line
dataJson = dataJson[args[i]]
mw.logObject(k)
else
propellants = propellants..p._formatResource(k).." ("..mode["units"][k]..")<br>"
end
end
end
return propellants
-- 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


-- This func returns a "string" to the #invoke
function p.getFuelValues(frame)
function p.test()
local args = getArgs(frame)
    local dataJson = mw.loadJsonData("Data:Example")
propellants = p._getFuelValues(args)
    local temp = dataJson["modules"]["engine"]
return propellants
end


    return(temp["isp"]["vac"])
function p._getFuelValues(args)
-- Load the data from the JSON page, parsed as a table of tables
local dataJson = mw.loadJsonData("Data:"..args[1])
local propellants = ""
local mode = ""
-- Engine alt mode handling
if args[2] == "alt" then
mode = dataJson["modules"]["engine"]["alt_mode"]["propellants"]
else
mode = dataJson["modules"]["engine"]["propellants"]
end
-- Build the propellants values list
for k, v in pairs(mode) do
if k == "units" then
-- Dummy line
mw.logObject(k)
else
propellants = propellants..v.."<br>"
end
end
return propellants
end
end


return p
return p

Latest revision as of 19:31, 6 December 2024

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

Arguments[edit source]

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

E.g.: "Kerbol", "LW-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[edit source]

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[edit source]

A list of example JSON sets can be found at Data: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("Data:"..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

----- PROBABLY MOVE EVERYTHING UNDER THIS LINE TO A DIFFERENT MODULE -------

-- Re-format the resource names in part data to be displayed
function p._formatResource(resource)
	if resource == "monopropellant" then return "Monopropellant" end
	if resource == "methane" then return "Methane" end
	if resource == "oxidizer" then return "Oxidizer" end
	if resource == "intake_air" then return "Intake Air" end
	if resource == "solid_fuel" then return "Solid Fuel" end
	if resource == "hydrogen" then return "Hydrogen" end
	if resource == "xenon" then return "Xenon" end
	if resource == "electric_charge" then return "Electric Charge" end
end

function p.getFuelLabels(frame)
	local args = getArgs(frame)	
	propellants = p._getFuelLabels(args)
	return propellants
end

function p._getFuelLabels(args)
	-- Load the data from the JSON page, parsed as a table of tables
	local dataJson = mw.loadJsonData("Data:"..args[1])
	local propellants = ""
	local mode = ""
	
	-- Engine alt mode handling
	if args[2] == "alt" then
		mode = dataJson["modules"]["engine"]["alt_mode"]["propellants"]
	else
		mode = dataJson["modules"]["engine"]["propellants"]
	end
	
	-- 	Build the propellants label list
	for k, v in pairs(mode) do
		if k == "units" then
			-- Dummy line
			mw.logObject(k)
		else
			propellants = propellants..p._formatResource(k).." ("..mode["units"][k]..")<br>"
		end
	end
	return propellants
end

function p.getFuelValues(frame)
	local args = getArgs(frame)	
	propellants = p._getFuelValues(args)
	return propellants
end

function p._getFuelValues(args)
	-- Load the data from the JSON page, parsed as a table of tables
	local dataJson = mw.loadJsonData("Data:"..args[1])
	local propellants = ""
	local mode = ""
	
	-- Engine alt mode handling
	if args[2] == "alt" then
		mode = dataJson["modules"]["engine"]["alt_mode"]["propellants"]
	else
		mode = dataJson["modules"]["engine"]["propellants"]
	end
	
	-- 	Build the propellants values list
	for k, v in pairs(mode) do
		if k == "units" then
			-- Dummy line
			mw.logObject(k)
		else
			propellants = propellants..v.."<br>"
		end
	end
	return propellants
end

return p
MediaWiki Appliance - Powered by TurnKey Linux