Anonymous

Module:Township: Difference between revisions

From Melvor Idle
2,086 bytes removed ,  27 October 2022
no edit summary
No edit summary
No edit summary
Line 3: Line 3:
local GameData = require('Module:GameData')
local GameData = require('Module:GameData')


-- Data Module
local DataDemo = require('Module:GauTest/DataDemo')
local DataFull = require('Module:GauTest/DataFull')
local DataTotH = require('Module:GauTest/DataTotH')
local Namespaces = {
melvorD = DataDemo,
melvorF = DataFull,
melvorTotH = DataTotH
}


local Data = {}
local Data = {}




-- For a table that is indexed but uses a key, use this function to find the correct element
function Data.tableMatch(tabl, property, value)
for _, elem in ipairs(tabl) do
if elem[property] == value then
return elem
end
end
return nil
end
-- For a table that is indexed but uses a key, use this function to find the correct element, when there are several duplicates elements
function Data.tableMatches(tabl, property, value)
local matches = {}
for _, elem in ipairs(tabl) do
if elem[property] == value then
table.insert(matches, elem)
end
end
return matches
end
-- Separates the namespace and id of a string
-- e.g. 'melvorD:Coal_Ore' will return {namespace='melvorD', id='Coal_Ore'}
-- e.g. 'Coal_Ore' will return {namespace=nil, id='Coal_Ore'}
function Data.splitID(text)
local split = Shared.splitString(text, ':')
local target_namespace = nil
local target_id = nil
if #split == 2 then
return {namespace=split[1], id=split[2]}
elseif #split == 1 then
return {id=split[1]}
else
return nil
end
end


-- Returns the namespace name (eventually we should use an icon?)
-- Returns the namespace name (eventually we should use an icon?)
Line 66: Line 20:
Data.Item = {}
Data.Item = {}


-- Get all the items with a property equal to value
function Data.Item.Match(property, value)
local items = {}
for namespace, data in pairs(Namespaces) do
for k, item in pairs(data.data.items) do
if item[property] == value then
local itemcopy = Shared.clone(item)
itemcopy._namespace = namespace
table.insert(items, itemcopy)
end
end
end
return items
end


-- Get item by id
function Data.Item.ByID(id)
local target = Data.splitID(id)
for namespace, data in pairs(Namespaces) do
if target.namespace == nil or namespace == target.namespace then
for k, item in pairs(data.data.items) do
if item.id == target.id then
local itemcopy = Shared.clone(item)
itemcopy._namespace = namespace
return itemcopy
end
end
end
end
return nil
end


-- Returns the recipe for the item of a desired skill.
-- Returns the recipe for the item of a desired skill.
572

edits