Module:GameData: Difference between revisions

sortByOrderTable: Protect against errors from unsorted elements
(Update to handle multiple source data pages)
(sortByOrderTable: Protect against errors from unsorted elements)
Line 47: Line 47:
for i, skillObj in ipairs(GameData.skillData) do
for i, skillObj in ipairs(GameData.skillData) do
local _, localID = p.getLocalID(skillObj.skillID)
local _, localID = p.getLocalID(skillObj.skillID)
        if localID ~= nil then
if localID ~= nil then
            skillData[localID] = skillObj.data
skillData[localID] = skillObj.data
        end
end
end
end
    return skillData
return skillData
end
end


Line 105: Line 105:
error('Property name must be a string', 2)
error('Property name must be a string', 2)
end
end
    local entData, useCache = nil, false
local entData, useCache = nil, false
    if type(entityType) == 'string' then
if type(entityType) == 'string' then
        entData = GameData[entityType]
entData = GameData[entityType]
        useCache = true
useCache = true
    else
else
        -- Function was passed a table of entities rather than a entity type
-- Function was passed a table of entities rather than a entity type
        entData = entityType
entData = entityType
    end
end
if entData == nil then
if entData == nil then
error('No such entity type: ' .. entityType, 2)
error('No such entity type: ' .. entityType, 2)
Line 130: Line 130:
-- 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
        if useCache then
if useCache then
    setCache(entityType, entity.id, idx)
setCache(entityType, entity.id, idx)
        end
end
if entity[propName] == propValue then
if entity[propName] == propValue then
return entity
return entity
Line 160: Line 160:
end
end
local entData, useCache = nil, false
local entData, useCache = nil, false
    if type(entityType) == 'string' then
if type(entityType) == 'string' then
        entData = GameData[entityType]
entData = GameData[entityType]
        useCache = true
useCache = true
    else
else
        -- Function was passed a table of entities rather than a entity type
-- Function was passed a table of entities rather than a entity type
        entData = entityType
entData = entityType
    end
end
if entData == nil and type(entityType) == 'string' then
if entData == nil and type(entityType) == 'string' then
error('No such entity type: ' .. entityType, 2)
error('No such entity type: ' .. entityType, 2)
Line 192: Line 192:
-- 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:
-- Example - Sorts combat areas into the same order as displayed in game:
--     p.sortByOrderTable(p.rawData.combatAreas, p.rawData.combatAreaDisplayOrder)
--   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
    local orderIdx = {}
local orderIdx = {}
    for idx, v in ipairs(orderTable) do
for idx, v in ipairs(orderTable) do
        orderIdx[v] = idx
orderIdx[v] = idx
    end
end
    -- Determine if user-specified or default paramater values are to be used
-- Determine if user-specified or default paramater values are to be used
    if type(keepUnsorted) ~= 'boolean' then
if type(keepUnsorted) ~= 'boolean' then
        keepUnsorted = true
keepUnsorted = true
    end
end
    if type(idKey) ~= 'string' then
if type(idKey) ~= 'string' then
        idKey = 'id'
idKey = 'id'
    end
end
    if type(orderFunc) ~= 'function' then
if type(orderFunc) ~= 'function' then
        orderFunc = function(k1, k2)
orderFunc = function(k1, k2)
            return orderIdx[k1[idKey]] < orderIdx[k2[idKey]]
local o1, o2 = orderIdx[k1[idKey]], orderIdx[k2[idKey]]
        end
if o1 == nil or o2 == nil then
    end
return false
else
return orderIdx[k1[idKey]] < orderIdx[k2[idKey]]
end
end
end


    -- Build unsorted result table, removing unsorted elements if requested
-- Build unsorted result table, removing unsorted elements if requested
    local resultTable = {}
local resultTable = {}
    local resultItemCount = 0
local resultItemCount = 0
    for idx, v in ipairs(dataTable) do
for idx, v in ipairs(dataTable) do
        local keyVal = v[idKey]
local keyVal = v[idKey]
        if keyVal ~= nil then
if keyVal ~= nil then
            if keepUnsorted or orderIdx[keyVal] ~= nil then
if keepUnsorted or orderIdx[keyVal] ~= nil then
                resultItemCount = resultItemCount + 1
resultItemCount = resultItemCount + 1
                resultTable[resultItemCount] = v
resultTable[resultItemCount] = v
            end
end
        end
end
    end
end
    -- Sort table
-- Sort table
    table.sort(resultTable, orderFunc)
table.sort(resultTable, orderFunc)
    return resultTable
return resultTable
end
end


return p
return p