Module:Data/Sources

From The Alchemist Code Wiki
Jump to: navigation, search

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

return function(dataModel)
  local function makeSourceFunc(tableName, linksKey, customWhere)
    return function(input, columns, loc, where)
      local hash = {}
      columns = '_pageName,iname'..(columns and ','..columns or '')
      for _,inNode in ipairs(input) do
        if type(inNode) == 'string' then inNode = {iname = inNode} end
        for _,outNode in ipairs(dataModel.query(tableName, columns, {where = (where and '('..where..') AND ' or '')..'('..customWhere(inNode)..')'})) do
          if hash[outNode.iname] == nil then
            for _,param in ipairs(loc or {}) do outNode[param] = dataModel.getLoc(outNode._pageName, param, loc.lang) end
            outNode._links = inNode._links or {}
            outNode._links[linksKey] = {inNode}
            hash[outNode.iname] = outNode
          else
            local links = hash[outNode.iname]._links[linksKey]
            links[#links+1] = inNode
          end
        end
      end
      local entries = {}
      for _,v in pairs(hash) do entries[#entries+1] = v end
      return entries
    end
  end
  
  local p = {}
  
  p.ability = {
    job = makeSourceFunc('Job', 'ability', function(node) return 'fixabl="'..node.iname..'"' end),
    artifact = makeSourceFunc('Artifact', 'ability', function(node) return 'abils HOLDS "'..node.iname..'"' end),
  }
  
  p.skill = {
    ability = makeSourceFunc('Ability', 'skill', function(node) return 'skl1="'..node.iname
                                                                ..'" OR skl2="'..node.iname
                                                                ..'" OR skl3="'..node.iname
                                                                ..'" OR skl4="'..node.iname
                                                                ..'" OR skl5="'..node.iname
                                                                ..'" OR skl6="'..node.iname
                                                                ..'" OR skl7="'..node.iname
                                                                ..'" OR skl8="'..node.iname
                                                                ..'" OR skl9="'..node.iname
                                                                ..'" OR skl10="'..node.iname..'"' end),
    artifact = makeSourceFunc('Artifact', 'skill', function(node) return 'attack1="'..node.iname
                                                                  ..'" OR attack2="'..node.iname
                                                                  ..'" OR attack3="'..node.iname
                                                                  ..'" OR attack4="'..node.iname
                                                                  ..'" OR attack5="'..node.iname
                                                                  ..'" OR equip1="'..node.iname
                                                                  ..'" OR equip2="'..node.iname
                                                                  ..'" OR equip3="'..node.iname
                                                                  ..'" OR equip4="'..node.iname
                                                                  ..'" OR equip5="'..node.iname..'"' end),
    item = makeSourceFunc('Item', 'skill', function(node) return 'skill="'..node.iname..'"' end),
  }
  
  p.buff = {
    skill = makeSourceFunc('Skill', 'buff', function(node) return 's_buff="'..node.iname..'" OR t_buff="'..node.iname..'"' end),
  }
  
  p.cond = {
    skill = makeSourceFunc('Skill', 'cond', function(node) return 's_cond="'..node.iname..'" OR t_cond="'..node.iname..'"' end),
  }
  
  --function p.cond.ability(input, columns, loc, where) return p.skill.ability(p.cond.skill(input), columns, loc, where) end
  --function p.cond.job(input, columns, loc, where) return p.ability.job(p.cond.ability(input), columns, loc, where) end
  
  return p
end