Module:Render/Unit

From The Alchemist Code Wiki
Jump to: navigation, search

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

local model  = require('Module:Data').model
local unitPageName = require('Module:Data/Extra/UnitPageName')
local linkUtils = require('Module:LinkUtils')

local p = {}

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

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

  local unitIconDir = 'PortraitsM'

  local icon = 'ItemIcon,IT_UNKNOWN'
  if data.img then
    icon = unitIconDir..','..data.img
  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="Unit" and iname="'..iname..'"'})[1] or {_pageName = unitPageName[iname] or linkUtils.wikiSafeName(name)})._pageName or nil
    end
  end
  
  link = linkUtils.wikiSafeName(link)
  name = linkUtils.wikiSafeName(name)

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

  local rarity = 0

  -- Generate the image
  local imageOutput = string.format(
    '[[File:Game,%s.png|%spx%s%s]]',
    icon, size,
    name and name ~= '' and '|'..name or '',
    link and link ~= '' and '|link='..link or ''
  )
  
  -- Generate the container around the image
  local output = mw.html.create()
  if args['size'] == 'small' then
    output:wikitext(imageOutput)
    if displayName then
      output:wikitext(' '..displayName)
    end
  else
    output = output:tag('div'):addClass('item-icon'):css('width', size..'px')
    if args['classes'] ~= nil then
      output:addClass(args['classes'])
    end
    if args['float'] ~= nil and args['float'] ~= '' then
      output:addClass(args['float'])
    end
    output:newline():tag('div'):wikitext(imageOutput)
    if displayName then
        output:newline():tag('div'):wikitext(displayName)
    end
    output:newline()
  end
  
  return tostring(output)
end

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

function p._pageName(args)
    local iname = args[1]
    local data = args.data or model.query('Unit','_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="Unit" and iname="'..iname..'"'})[1] or {_pageName = unitPageName[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