The Alchemist Code Wiki

READ MORE

The Alchemist Code Wiki
Advertisement

Documentation for this module may be created at Module:Page/Condition/sandbox/doc

local model = require("Module:Data").model
local cargo = require('Module:CargoUtil')
local util_table = require('Module:TableUtil')

-- local render_gear_icon = require('Module:Render/Gear')._icon2
-- local render_item_icon = require('Module:Render/Item')._icon2
-- local render_job_icon = require('Module:Render/Job')._icon2

local render = function ( args ) return args.iname or args[1] end
local render_gear_icon = render
local render_item_icon = render
local render_job_icon = render


local p = {}
local p2 = {
  sources = function(frame)
    local args = require('Module:Arguments').getArgs(frame, {
      parentFirst = true,
    })
    local conditionName = args[2]
    local server = args.server or 'gl'
    local lang = args.lang
    
    local function tablesFor(whereType, msg)
      local conditions = model.query('Conditions', 'iname', {where = whereType..' AND server="'..server..'" AND conds HOLDS '..args[1]})
      local skills = model.sources.cond.skill(conditions, nil, {'name', 'expr', lang = lang}, 'server="'..server..'"')
      local abilities = model.sources.skill.ability(skills, nil, nil, 'server="'..server..'"')
      
      local sb = {}
      ----------
      -- Jobs --
      ----------
      local jobs = model.sources.ability.job(abilities, nil, nil, 'server="'..server..'"')
      if #jobs > 0 then
        table.sort(jobs, function(a, b) return a.iname < b.iname end)
        -- TODO: Need to support abilities beyond main
        
        sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are fixed to a job: ===\n{| class="wikitable frozen-header sortable" \n!Job\n!Skill\n!Description'
        for _, job in ipairs(jobs) do
          -- TODO: Wire into Module:Render/Job
          sb[#sb+1] = '\n|-\n| '..(#job._links.skill > 1 and 'rowspan='..#job._links.skill..' | ' or '')
          sb[#sb+1] = render_job_icon({job.iname, size='small'})
          for i, skill in ipairs(job._links.skill) do
            sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr
          end
        end
        sb[#sb+1] = '\n|}'
      end
      
      ---------------
      -- Artifacts --
      ---------------
      local artifacts = model.sources.ability.artifact(abilities, 'icon,rini', nil, 'server="'..server..'"')
      for _, artifact in ipairs(model.sources.skill.artifact(skills, 'icon,rini', nil, 'server="'..server..'"')) do
        for _, skill in ipairs(artifact._links.skill) do skill.name = 'Basic Attack' end
        artifacts[#artifacts+1] = artifact
      end
      if #artifacts > 0 then
        table.sort(artifacts, function(a, b) return a.iname < b.iname end)
        
        sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are provided by gear: === \n{| class="wikitable frozen-header sortable" \n!Gear\n!Skill\n!Description'
        for _, artifact in ipairs(artifacts) do
          sb[#sb+1] = '\n|-\n| '..(#artifact._links.skill > 1 and 'rowspan='..#artifact._links.skill..' | ' or '')
          sb[#sb+1] = render_gear_icon({artifact.iname, data = artifact, size = 'small'})
          for i, skill in ipairs(artifact._links.skill) do
            sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr
          end
        end
        sb[#sb+1] = '\n|}'
      end
      
      -----------
      -- Items --
      -----------
      local items = model.sources.skill.item(skills, 'icon,rare,type', {'expr', lang = lang}, 'server="'..server..'"')
      if #items > 0 then
        table.sort(items, function(a, b) return a.iname < b.iname end)
        
        sb[#sb+1] = '\n=== Consumables which '..msg..' '..conditionName..' upon use: === \n{| class="wikitable frozen-header sortable" \n!Item\n!Description'
        for _, item in ipairs(items) do
          sb[#sb+1] = '\n|-\n| '..render_item_icon({item.iname, data = item, size = 'small'})..'\n| '..item.expr
        end
        sb[#sb+1] = '\n|}'
      end
      
      sb[#sb+1] = '[[Category:Condition]]'
      
      return table.concat(sb)
    end
    
    return table.concat({
      '== Skills Relating to this Condition ==',
      tablesFor('Conditions.type<>1 AND Conditions.type<>5', 'inflict'),
      -- tablesFor('Conditions.type=1', 'cure'),
      -- tablesFor('Conditions.type=5', 'grant immunity to'),
    }, '\n')
  end}
local h = {}

function h.quote(s)
  if not s then return end
  return ('%q'):format(s)
end

function p.sources(frame)
  local args = require('Module:Arguments').getArgs(frame, {
    parentFirst = true,
  })
  local conditionName = args[2]
  local server = args.server or 'gl'
  local lang = args.lang

	function x(tableName, linksKey, customWhere, input, columns, loc, where)
	  local hash = {}
	  for _,inNode in ipairs(input) do
	    if type(inNode) == 'string' then
	    	inNode = {iname = inNode}
	    end
	    for _,outNode in ipairs(cargo.query{
				tables = tableName,
				fields = {'_pageName','iname',columns},
				where = {
					where,
					customWhere(inNode),
				},
      }) do
	      if hash[outNode.iname] == nil then
	        for _,param in ipairs(loc or {}) do
	        	outNode[param] = model.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
  
  local function tablesFor(whereType, msg)
    local conditions = cargo.query{
      tables = 'Conditions',
      fields = 'iname',
      where = {
        whereType,
        ('server = "%s"'):format(server),
        'conds HOLDS '..args[1],
      }
    }

    local skills = model.sources.cond.skill(conditions, nil, {'name', 'expr', lang = lang}, 'server="'..server..'"')

    local skillDict = {}
    for i, v in ipairs(skills) do
    	local skill = {
    		_pageName = v._pageName,
    		iname = v.iname,
    		name = v.name,
    		expr = v.expr,
    	}
    	util_table.pushDict(skillDict, v.iname, skill)
    end

    local abilities = model.sources.skill.ability(skills, nil, nil, 'server="'..server..'"')

    local skill_ids = util_table.concat(skillDict, ',', h.quote)
    local rows = cargo.query{
    	tables = 'Ability',
    	fields = {
    		'_pageName, iname',
    		'skl1, skl2, skl3, skl4, skl5, skl6, skl7, skl8, skl9, skl10',
    	},
    	where = {
    		table.concat({
	    		('skl1 IN (%s)'):format(skill_ids),
	    		('skl2 IN (%s)'):format(skill_ids),
	    		('skl3 IN (%s)'):format(skill_ids),
	    		('skl4 IN (%s)'):format(skill_ids),
	    		('skl5 IN (%s)'):format(skill_ids),
	    		('skl6 IN (%s)'):format(skill_ids),
	    		('skl7 IN (%s)'):format(skill_ids),
	    		('skl8 IN (%s)'):format(skill_ids),
	    		('skl9 IN (%s)'):format(skill_ids),
	    		('skl10 IN (%s)'):format(skill_ids),
	    	}, ' OR '),
    	},
    }
    local abilitiesDict = {}
    for i, row in ipairs(rows) do
    	local ability = {
    		_pageName = row._pageName,
    		iname = row.iname,
    		skills = {},
    	}
    	for i=1,10 do
    		local k = row['skl'..i]
    		local skill = skillDict[k]
    		if skill then
    			table.insert(ability.skills, skill)
    			skill.ability = ability
    		end
    	end
    	util_table.pushDict(abilitiesDict, ability.iname, ability)
    end

    mw.logObject(abilitiesDict[1], 'ab2')
    
    local sb = {}
    ----------
    -- Jobs --
    ----------

    local jobs = x(
      'Job', -- tables
      'ability', -- linksKey
      function(node) return 'fixabl="'..node.iname..'"' end, -- customWhere
      abilities, -- input
      nil, -- columns
      nil, -- loc
      'server="'..server..'"' -- where
    )
    local ability_ids = util_table.concat(abilitiesDict, ',', h.quote)
    local jobs2 = cargo.query{
    	tables = 'Job',
    	fields = '_pageName, iname',
    	where = {
    		('server = "%s"'):format(server),
    		('fixabl IN (%s)'):format(ability_ids),
    	},
    }
    local ids1 = util_table.extractValueToList(jobs, 'iname')
    local ids2 = util_table.extractValueToList(jobs2, 'iname')
    table.sort(ids1)
    table.sort(ids2)
    ids1 = table.concat(ids1,' | ')
    ids2 = table.concat(ids2,' | ')
    if ids1 ~= ids2 then
    	mw.log(ids1)
    	mw.log(ids2)
    end

    -- mw.logObject(jobs[1], 'job1')
    -- mw.logObject(jobs2[1], 'job2')

    if #jobs > 0 then
      table.sort(jobs, function(a, b) return a.iname < b.iname end)
      -- TODO: Need to support abilities beyond main
      
      sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are fixed to a job: ===\n{| class="wikitable frozen-header sortable" \n!Job\n!Skill\n!Description'
      for _, job in ipairs(jobs) do
        -- TODO: Wire into Module:Render/Job
        sb[#sb+1] = '\n|-\n| '..(#job._links.skill > 1 and 'rowspan='..#job._links.skill..' | ' or '')
        sb[#sb+1] = render_job_icon({job.iname, size='small'})
        for i, skill in ipairs(job._links.skill) do
          sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr
        end
      end
      sb[#sb+1] = '\n|}'
    end
    
    ---------------
    -- Artifacts --
    ---------------
    local artifacts = model.sources.ability.artifact(abilities, 'icon,rini', nil, 'server="'..server..'"')
    for _, artifact in ipairs(model.sources.skill.artifact(skills, 'icon,rini', nil, 'server="'..server..'"')) do
      for _, skill in ipairs(artifact._links.skill) do skill.name = 'Basic Attack' end
      artifacts[#artifacts+1] = artifact
    end
    if #artifacts > 0 then
      table.sort(artifacts, function(a, b) return a.iname < b.iname end)
      
      sb[#sb+1] = '\n=== Skills which '..msg..' '..conditionName..' that are provided by gear: === \n{| class="wikitable frozen-header sortable" \n!Gear\n!Skill\n!Description'
      for _, artifact in ipairs(artifacts) do
        sb[#sb+1] = '\n|-\n| '..(#artifact._links.skill > 1 and 'rowspan='..#artifact._links.skill..' | ' or '')
        sb[#sb+1] = render_gear_icon({artifact.iname, data = artifact, size = 'small'})
        for i, skill in ipairs(artifact._links.skill) do
          sb[#sb+1] = (i > 1 and '\n|-' or '')..'\n| '..skill.name..'\n| '..skill.expr
        end
      end
      sb[#sb+1] = '\n|}'
    end
    
    -----------
    -- Items --
    -----------
    -- local items = model.sources.skill.item(skills, 'icon,rare,type', {'expr', lang = lang}, 'server="'..server..'"')
    local t = {}
    for i, item in ipairs(cargo.query{
      tables = {
      	'Item',
      	'ItemLoc',
      	'Pages',
      },
      join = {
      	'Item.iname = ItemLoc.iname',
	      'Item.iname = Pages.iname',
	    },
      fields = {
	      'Pages._pageName=_pageName',
	      'Item.iname=iname',
	      'Item.icon=icon',
	      'Item.rare=rare',
	      'Item.type=type',
	      'ItemLoc.expr=expr',
	      'ItemLoc.lang=lang',
	    },
      where = {
        ('Item.server="%s"'):format(server),
        ('ItemLoc.lang in ("english", "japanese")'),
        ('Item.skill IN (%s)'):format(skill_ids),
      }
    }) do
      local k = item.iname
      t[k] = t[k] or {}
      t[k][item.lang] = item
    end
    local items = {}
    for k, v in pairs(t) do
      items[#items+1] = v.english or v.japanese
    end

    -- mw.logObject(items)
    if #items > 0 then
      table.sort(items, function(a, b) return a.iname < b.iname end)
      
      sb[#sb+1] = '\n=== Consumables which '..msg..' '..conditionName..' upon use: === \n{| class="wikitable frozen-header sortable" \n!Item\n!Description'
      for _, item in ipairs(items) do
        sb[#sb+1] = '\n|-\n| '..render_item_icon({item.iname, data = item, size = 'small'})..'\n| '..(item.expr or '')
      end
      sb[#sb+1] = '\n|}'
    end
    
    sb[#sb+1] = '[[Category:Condition]]'
    
    return table.concat(sb)
  end
  
  return table.concat({
    '== Skills Relating to this Condition ==',
    tablesFor('Conditions.type<>1 AND Conditions.type<>5', 'inflict'),
    -- tablesFor('Conditions.type=1', 'cure'),
    -- tablesFor('Conditions.type=5', 'grant immunity to'),
  }, '\n')
end

function p.test()
  local that = p2 --require('Module:Page/Condition')
  local this = p
  local util_vars = require('Module:VarsUtil')
  util_vars.setVar('cargo query count', 0)
  local a = that.sources{0, 'poison'}
  local countA = util_vars.getVar('cargo query count')

  util_vars.setVar('cargo query count', 0)
  local b = this.sources{0, 'poison'}
  local countB = util_vars.getVar('cargo query count')
  if a ~= b then
    mw.log("============================================================")
    mw.log("============================== A ===========================")
    mw.log("============================================================")
    mw.log(a)
    mw.log("============================================================")
    mw.log("============================== B ===========================")
    mw.log("============================================================")
    mw.log(b)
  end
  mw.log(countA, countB)
end

return p
Advertisement