The Alchemist Code Wiki

READ MORE

The Alchemist Code Wiki
Register
Advertisement

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

local util_cargo = require('Module:CargoUtil')
local linkUtils = require('Module:LinkUtils')

local p = {}
local h = {}

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]
  if not iname then return nil end
  local page = util_cargo.queryAndCast{
  	tables = 'Pages',
  	fields = '_pageName, iname',
  	where = {
  		'type = "Unit"',
  		'_pageName = "'..iname..'" OR iname = "'..iname..'"',
  	},
  	limit = 1,
  }[1]
  if page then iname = page.iname end
  local data = args.data or util_cargo.queryAndCast{tables = 'Unit',fields = '_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 = h.getName(data._pageName)
  
  local link = nil
  if args['link'] ~= 'none' then
    if data._pageName ~= nil then
        link = (page 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]
    local data = args.data or util_cargo.queryAndCast{tables='Unit',fields='_pageName,server',where='iname="'..iname..'"'}[1] or {}

    local name = h.getName(data._pageName)
  
    local link = nil
    if data._pageName ~= nil then
      link = (util_cargo.queryAndCast{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

function h.getName(_pageName, lang)
	if _pageName == nil then return '???' end
	local rows = util_cargo.queryAndCast{
		tables = 'UnitLoc',
		fields = 'name,lang',
		where = ('_pageName=%q AND name<>""'):format(_pageName)
	}
	local names = {}
	for i, row in ipairs(rows) do
		names[row.lang] = row.name
	end
	return names[lang] or names.english or names.japanese or ''
end

return p
Advertisement