Module:Render/Job

From The Alchemist Code Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:Render/Job/doc

local model  = require('Module:Data').model
local jobPageName = require('Module:Data/Extra/JobPageName')
local linkUtils = require('Module:LinkUtils')

local p = {}

function p.icon(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        parentFirst = true,
        wrappers = {
            'Template:Job/Icon',
        }
    })
    return p._icon(args)
end

function p._icon(args)
  local iname = args[1]
  local jobpage = mw.ext.cargo.query('Pages', 'iname', {where = 'type = "Job" AND _pageName = "' .. iname .. '" OR iname = "' .. iname .. '"'})[1]
  if jobpage then
  	iname = jobpage.iname
  end
  local data = args.data or model.query('Job','_pageName,server,ac2d,mdl',{where='iname="'..iname..'"'})[1] or {}

  local jobIconDir = (args['size'] == 'large' and 'JobIconM,') or 'JobIcon,'

  local icon = 'ItemIcon,IT_UNKNOWN'
  if data.ac2d and data.ac2d ~= '' then
    icon = jobIconDir..data.ac2d
  elseif data.mdl and data.mdl ~= '' then
    icon = jobIconDir..data.mdl
  end

  local name = model.getLoc(data._pageName, 'name')
  
  local link = nil
  if args['link'] ~= 'none' then
    if data._pageName ~= nil then
        link = (mw.ext.cargo.query('Pages','_pageName',{where='type="Job" and iname="'..iname..'"'})[1] or {_pageName = jobPageName[iname] or linkUtils.wikiSafeName(name)})._pageName or nil
    end
  end

  local displayName = args['name'] ~= 'none' and (name and name ~= '' and (link and '[['..link..'|'..name..']]') or name) or nil
  
  local size = (args['size'] == 'large' and 128) or (args['size'] == 'small' and 32) or 64

  local rarity = 0

  -- Generate the image
  local imageOutput = {}
  table.insert(imageOutput, '[[File:Game,'..icon..'.png|'..size..'px')
  if name and name ~= '' then
    table.insert(imageOutput,'|'..name)
    if link and link ~= '' then
        table.insert(imageOutput, '|link='..link)
    end
  end
  table.insert(imageOutput, ']]')
  
  -- Generate the container around the image
  local output = {}
  if args['size'] == 'small' then
    table.insert(output, table.concat(imageOutput))
    if displayName then
        table.insert(output, ' '..displayName)
    end
  else
    local iconBackground = 'x-'..rarity..' y-2'
    local classList = args['classes'] ~= nil and ' '..args['classes'] or ''
    if args['float'] ~= nil and args['float'] ~= '' then
        classList = classList..' float'..args['float']
    end
    
    table.insert(output, '<div class="item-icon'..classList..'" style="width: '..size..'px">'.."\n")
    table.insert(output, '<div class="img '..iconBackground..'">')
    table.insert(output, table.concat(imageOutput))
    table.insert(output, '</div>')
    if displayName then
        table.insert(output, "\n<div>"..displayName..'</div>')
    end
    table.insert(output, "\n</div>")
  end
  
  return table.concat(output)
end

function p.pageName(frame)
    local args = require('Module:Arguments').getArgs(frame, {
        parentFirst = true,
        wrappers = {
            'Template:Job/Name',
        }
    })
    return p._pageName(args)
end

function p._pageName(args)
  local iname = args[1]
  local data = args.data or model.query('Job','_pageName,server',{where='iname="'..iname..'"'})[1] or {}

  local name = model.getLoc(data._pageName, 'name')
  
  local link = nil
  if data._pageName ~= nil then
    link = (mw.ext.cargo.query('Pages','_pageName',{where='type="Job" and iname="'..iname..'"'})[1] or {_pageName = jobPageName[iname] or linkUtils.wikiSafeName(name)})._pageName or nil
  end

  local displayName = (name and name ~= '' and (link and '[['..link..'|'..name..']]') or name) or iname
  
  return displayName
end

return p