Civilization Wiki
Register
Advertisement

Documentation for this module may be created at Module:Data/doc

-- Game specific functions:
-- Use the game's suffix to reach the appropriate subpage.
-- Example: Data/CivRev2

local utils = require("Module:TemplateUtils")


--To get a simple text value
function GetValue(path,entry,field)
	local data = mw.loadData("Module:Data/"..path)
	
	local value = data[entry][field]
	if type(value) == "table" then
		return table.concat(value, ", ")
	else
		return tostring(value)
	end
end

function GetSubValue(path,t,entry,field)
	local data = mw.loadData("Module:Data/"..path)
	
	local value = data[t][entry][field]
	if type(value) == "table" then
		return table.concat(value, ", ")
	else
		return tostring(value)
	end
end

--To get all of the keys of a game's datatype
function GetKeys(path,separator)
	local data = mw.loadData("Module:Data/"..path)
	
	local t = {}
	for k,v in pairs(data) do
		table.insert(t,k)
	end
	table.sort(t)
	
	return table.concat(t, separator)
end

--To get all of the keys of a game's datatype
function ModuleToJSON(path)
	local JSON = require("Module:JSON")
	local data = mw.loadData("Module:Data/"..path)
	
	return JSON:encode(utils.RecreateTable(data))
end

local p = {}
function p.GetValue(frame)
	return GetValue(frame.args[1],frame.args[2],frame.args[3])
end
function p.GetValuePreProcessed(frame)
	return  utils.PreProcess(frame,GetValue(frame.args[1],frame.args[2],frame.args[3]))
end
function p.GetSubValue(frame)
	return GetSubValue(frame.args[1],frame.args[2],frame.args[3],frame.args[4])
end
function p.GetSubValuePreProcessed(frame)
	return utils.PreProcess(frame,GetSubValue(frame.args[1],frame.args[2],frame.args[3],frame.args[4]))
end
function p.GetLinkListFromPath(frame)
    local path = mw.text.split(frame.args[1],'>');
    
	local data = mw.loadData("Module:Data/"..path[1])
	local value = data[path[2]][frame.args[2]]
	if (value == nil) then
	    return ""
    else
        return utils.LinkAll(data[path[2]][frame.args[2]],frame.args[3])
    end
end
function p.GetValueFromPath(frame)
    local path = mw.text.split(frame.args[1],'>');
    if (#path == 2) then
        return GetValue(path[1],path[2],frame.args[2])
    else
        return GetSubValue(path[1],path[2],path[3],frame.args[2])
    end
end
function p.GetValueFromPathPreProcessed(frame)
    local value = p.GetValueFromPath(frame)
    if (value == "nil") then
        return frame.args[3] or ""
    else
        return utils.PreProcess(frame, p.GetValueFromPath(frame))
    end
end
function p.GetKeys(frame)
	return GetKeys(frame.args[1],frame.args[2] or "<br/>")
end
function p.ModuleToJSON(frame)
	return ModuleToJSON(frame.args[1])
end

function p.SelectFromWhere(from,whereName,whereValue)
	for key,value in pairs(from) do
		if value[whereName] and value[whereName] == whereValue then
			return value
		end
	end
	return nil
end

function p.SelectAllFromWhere(from,whereName,whereValue)
	local result = {}
	for key,value in pairs(from) do
		if value[whereName] and value[whereName] == whereValue then
			table.insert(result, value)
		end
	end
	return result
end

function p.SelectAllKeysFromWhere(from,whereName,whereValue)
	local result = {}
	for key,value in pairs(from) do
		if value[whereName] and value[whereName] == whereValue then
			table.insert(result, key)
		end
	end
	return result
end

return p
Advertisement