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 cargo
local model
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]
  cargo = cargo or require('Module:CargoUtil')
  local page = cargo.query{tables='Pages', fields='iname', where = 'type = "Unit" AND (_pageName = "'..iname..'" OR iname = "'..iname..'")'}[1]
  if page then
  	iname = page.iname
  end
  model = model or require('Module:Data').model
  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 = (cargo.query{tables='Pages',fields='_pageName',where='type="Unit" and iname="'..iname..'"'}[1] or {_pageName = mw.loadData('Module:Data/Extra/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

-- Non cargo version
function p._icon2(args)
	local data = args.data or {}
	local iname = data.iname
	if not iname then return end
	local icon = data.icon and 'PortraitsM,'..data.icon or 'ItemIcon,IT_UNKNOWN'
	local link = nil
	if args['link'] ~= 'none' then
		link = data._pageName
	end
	local name = data.name
	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')
		output:addClass(args.classes):addClass(args.float)
		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]
	  cargo = cargo or require('Module:CargoUtil')
    local data = args.data or cargo.query{tables='Unit',fields='_pageName,server',where='iname="'..iname..'"'}[1] or {}

    model = model or require('Module:Data').model
    local name = model.getLoc(data._pageName, 'name')
  
    local link = nil
    if data._pageName ~= nil then
      link = (cargo.query{tables='Pages',fields='_pageName',where='type="Unit" and iname="'..iname..'"'}[1] or {_pageName = mw.loadData('Module:Data/Extra/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