Anonymous

Module:GameData: Difference between revisions

From Melvor Idle
getEntityByProperty: Extend to allow first property to be a table of entities
(sortByOrderTable: Initial implementation)
(getEntityByProperty: Extend to allow first property to be a table of entities)
Line 68: Line 68:
end
end


-- Takes an entity type & property name/value, returning an object representing the
-- Takes an entity type (or entity list) & property name/value, returning an object representing the
-- entity with the given property (if found). If not found, then nil is returned.
-- entity with the given property (if found). If not found, then nil is returned.
function p.getEntityByProperty(entityType, propName, propValue)
function p.getEntityByProperty(entityType, propName, propValue)
if type(entityType) ~= 'string' then
if type(entityType) ~= 'string' and type(entityType) ~= 'table' then
error('Entity type name must be a string', 2)
error('Entity type name must be a string or table', 2)
elseif type(propName) ~= 'string' then
elseif type(propName) ~= 'string' then
error('Property name must be a string', 2)
error('Property name must be a string', 2)
end
end
local entData = GameData[entityType]
    local entData, useCache = nil, false
    if type(entityType) == 'string' then
        entData = GameData[entityType]
        useCache = true
    else
        -- Function was passed a table of entities rather than a entity type
        entData = entityType
    end
if entData == nil then
if entData == nil then
error('No such entity type: ' .. entityType, 2)
error('No such entity type: ' .. entityType, 2)
Line 85: Line 92:
end
end
-- Check if this ID is already cached
-- Check if this ID is already cached
if propName == 'id' then
if propName == 'id' and useCache then
local cacheIdx = getCache(entityType, propValue)
local cacheIdx = getCache(entityType, propValue)
if cacheIdx ~= nil then
if cacheIdx ~= nil then
Line 94: Line 101:
-- Cache miss or property isn't ID, so scan the entity data sequentially
-- Cache miss or property isn't ID, so scan the entity data sequentially
for idx, entity in ipairs(entData) do
for idx, entity in ipairs(entData) do
setCache(entityType, entity.id, idx)
        if useCache then
    setCache(entityType, entity.id, idx)
        end
if entity[propName] == propValue then
if entity[propName] == propValue then
return entity
return entity
Line 144: Line 153:
-- idKey specifies the ID key to sort upon. Default: id
-- idKey specifies the ID key to sort upon. Default: id
-- orderFunc specifies a custom order function if the default behaviour is not desired
-- orderFunc specifies a custom order function if the default behaviour is not desired
-- Example - Sorts combat areas into the same order as displayed in game:
--      p.sortByOrderTable(p.rawData.combatAreas, p.rawData.combatAreaDisplayOrder)
function p.sortByOrderTable(dataTable, orderTable, keepUnsorted, idKey, orderFunc)
function p.sortByOrderTable(dataTable, orderTable, keepUnsorted, idKey, orderFunc)
     -- Create index table from orderTable
     -- Create index table from orderTable