Difference between revisions of "Module:DefInfo"

From RimWorld Wiki
Jump to navigation Jump to search
(workable prototype)
m (tab formatting changed)
Line 9: Line 9:
  
 
function find_key_in_table(key, table)
 
function find_key_in_table(key, table)
for k, v in pairs(table) do
+
  for k, v in pairs(table) do
if k == key then return v
+
    if k == key then return v
elseif type(v) == "table" then
+
    elseif type(v) == "table" then
local found = find_key_in_table(key, v)
+
      local found = find_key_in_table(key, v)
if found then return found end
+
      if found then return found end
end
+
    end
end
+
  end
 
end
 
end
  
 
function parent_defName(def, category)
 
function parent_defName(def, category)
local parent_name = category[def]["ParentName"]
+
  local parent_name = category[def]["ParentName"]
if type(category[parent_name]) == "table" then return parent_name end
+
  if type(category[parent_name]) == "table" then return parent_name end
 
end
 
end
  
 
function find_in_parents(tag, def, category)
 
function find_in_parents(tag, def, category)
if not category[def] then return def .. " not found in category" end
+
  if not category[def] then return def .. " not found in category" end
local parent_def = parent_defName(def, category)
+
  local parent_def = parent_defName(def, category)
if not parent_def then return tag .." not found in parent defs" end
+
  if not parent_def then return tag .." not found in parent defs" end
local found = find_key_in_table(tag, category[parent_def])
+
  local found = find_key_in_table(tag, category[parent_def])
if found then return found
+
  if found then return found
else
+
  else
found = find_in_parents(tag, parent_def, category)
+
    found = find_in_parents(tag, parent_def, category)
if found then return found end
+
    if found then return found end
end
+
  end
 
end
 
end
  
 
function query(tag, def, category)
 
function query(tag, def, category)
if not category[def] then return def .. " not found in category" end
+
  if not category[def] then return def .. " not found in category" end
local found = find_key_in_table(tag, category[def])
+
  local found = find_key_in_table(tag, category[def])
if found then return found end
+
  if found then return found end
found = find_in_parents(tag, def, category)
+
  found = find_in_parents(tag, def, category)
if found then return found end
+
  if found then return found end
return tag .. " not found"
+
  return tag .. " not found"
 
end
 
end
  
 
function p.query(frame)
 
function p.query(frame)
local category = nil
+
  local category = nil
if frame.args[1] == "Races" then category = Races end
+
  if frame.args[1] == "Races" then category = Races end
if frame.args[1] == "Biomes" then category = Biomes end
+
  if frame.args[1] == "Biomes" then category = Biomes end
local def = frame.args[2]
+
  local def = frame.args[2]
local tag = frame.args[3]
+
  local tag = frame.args[3]
return query(tag, def, category)
+
  return query(tag, def, category)
 
end
 
end
  

Revision as of 12:29, 14 March 2021

Dev version at Module:Test


-- This module is to contain code for parsing a defName parameter and returning
-- information about the Def from Module:DefInfo/data*.  See talk page for more
-- info

local p = {}

local Races = mw.loadData('Module:Test/data/races')
local Biomes = mw.loadData('Module:Test/data/biomes')

function find_key_in_table(key, table)
  for k, v in pairs(table) do
    if k == key then return v
    elseif type(v) == "table" then
      local found = find_key_in_table(key, v)
      if found then return found end
    end
  end
end

function parent_defName(def, category)
  local parent_name = category[def]["ParentName"]
  if type(category[parent_name]) == "table" then return parent_name end
end

function find_in_parents(tag, def, category)
  if not category[def] then return def .. " not found in category" end
  local parent_def = parent_defName(def, category)
  if not parent_def then return tag .." not found in parent defs" end
  local found = find_key_in_table(tag, category[parent_def])
  if found then return found
  else
    found = find_in_parents(tag, parent_def, category)
    if found then return found end
  end
end

function query(tag, def, category)
  if not category[def] then return def .. " not found in category" end
  local found = find_key_in_table(tag, category[def])
  if found then return found end
  found = find_in_parents(tag, def, category)
  if found then return found end
  return tag .. " not found"
end

function p.query(frame)
  local category = nil
  if frame.args[1] == "Races" then category = Races end
  if frame.args[1] == "Biomes" then category = Biomes end
  local def = frame.args[2]
  local tag = frame.args[3]
  return query(tag, def, category)
end

mw.log("Module:DefInfo:os.clock() " .. os.clock())

return p