FANDOM


local p = {}
 
local faction_img_data = mw.loadData( 'Module:Factiongfx/faction_tip_link.json' ) -- load json
local getArgs = require('Module:Arguments').getArgs
local strlower = string.lower -- Convert to lower case
 
-- Get data from json page given main type name and piece name
function getFactionInfo(faction_type, faction_piece)
    faction_type = strlower(faction_type)
 
    local faction_DB = mw.loadData("Module:Factiongfx/faction_tip_link.json")
    local faction_chunk = faction_DB[faction_type]
 
    if faction_chunk and faction_DB[faction_type][faction_piece] then
       return faction_DB[faction_type][faction_piece]
    end
end
 
function p.Factiongfx(frame) -- Implements {{Factiongfx}}
	local args = getArgs(frame)
	return p._Factiongfx(args)
end
 
function p._Factiongfx( args )
	local img_faction = args['faction'] -- use faction param for faction
	-- local img_faction_arg = args['faction'] -- DEBUGGING
	if args['faction'] == nil then -- if faction=nil
	    img_faction = args[1] -- use param 1
	    if args[1] == nil then -- if param 1 is nil
	        img_faction = 'neutral' -- use neutral
	    end
	elseif args[1] == nil then -- if param 1 is also nil
	    img_faction = 'neutral' -- use neutral
	end
	if img_faction ~= nil then -- if not nil, make lowercase
		img_faction = string.lower(img_faction)
    end
	-- img_faction_arg = img_faction -- use param 1 -- DEBUGGING
	-- if img_faction_arg == nil then -- DEBUGGING
	--  img_faction_arg = 'unset'
	-- end
	local img_size = args['size'] -- use size param for size
	if args['size'] == nil then -- if size=nil
	    img_size = args[2] -- use param 2
	    if args[2] == nil then -- if param 2 is nil
	        img_size = '32px' -- use 32px
	    end
	elseif args[2] == nil then -- if param 2 is also nil
	    img_size = '32px' -- use 32px
	end
    -- img_abbr_size will eventually be img_size without 'px' for use in filename
	local img_abbr_size = img_size
	img_size = string.lower(img_size) -- make sure img_size is lowercase
	if img_size == 'small' then
	    img_size = '15px'
    elseif img_size == 'medium' then
	    img_size = '32px'
    elseif img_size == 'large' then
	    img_size = '32px'
    elseif img_size == 'xlarge' then
	    img_size = '64px'
    end
	if mw.ustring.find(img_size, 'px') then
	    img_abbr_size = mw.text.trim(img_size, "%l")
	elseif img_abbr_size == nil then
	    img_abbr_size = '32'
	end
	local img_filename = img_faction .. img_abbr_size .. '.png'
	local img_wikitext = '[[File:' .. img_filename .. '|link=]]'
	local img_tip = 'Neutral'
	local img_link = 'Neutral'
 
    local faction_index = img_faction
 
    -- strip spaces for indexing
    faction_index = mw.ustring.gsub(faction_index, "%s", "")
 
    -- strip apostrophes for indexing
    faction_index = mw.ustring.gsub(faction_index, "\'", "")
 
    -- strip dashes for indexing
    faction_index = mw.ustring.gsub(faction_index, "\-", "")
 
 
	-- Get tool tip for image
	if faction_index ~= nil and getFactionInfo(faction_index, "tip") ~= nil then
	    img_tip = getFactionInfo(faction_index, "tip")
	end
 
	-- Get link for image
	if faction_index ~= nil and getFactionInfo(faction_index, "link") ~= nil then
	    img_link = getFactionInfo(faction_index, "link")
	end
 
	-- Get faction filename prefix for image, if file specified in JSON
	if faction_index ~= nil and getFactionInfo(faction_index, "file") ~= nil then
	    img_faction = getFactionInfo(faction_index, "file")
	end
 
    -- setup image file name
    img_filename = img_faction .. '_' .. img_abbr_size .. '.png'
 
    -- Build filename and image wikitext
	if img_faction ~= nil and img_tip ~= nil and img_link ~= nil then
	    local tmp_faction_frag = img_filename;
	    -- replace lower case boss with Boss
	    tmp_faction_frag = mw.ustring.gsub( tmp_faction_frag, "boss", "Boss" );
	    -- replace lower case mob with Mob
	    tmp_faction_frag = mw.ustring.gsub( tmp_faction_frag, "mob", "Mob" );
	    -- replace lower case npc with NPC
	    tmp_faction_frag = mw.ustring.gsub( tmp_faction_frag, "npc", "NPC" );
	    -- replace lower case elite with Elite
	    tmp_faction_frag = mw.ustring.gsub( tmp_faction_frag, "elite", "Elite" );
	    -- replace lower case rare with Rare
	    tmp_faction_frag = mw.ustring.gsub( tmp_faction_frag, "rare", "Rare" );
	    img_filename = tmp_faction_frag;
	    -- Build image wikitext
	    img_wikitext = '[[File:' .. img_filename .. '|' .. img_tip .. '|link=' .. img_link .. ']]' -- .. '&nbsp;<small>(faction_index:&nbsp;' .. faction_index .. ',&nbsp;img_faction:&nbsp;' .. img_faction .. ',&nbsp;img_link:&nbsp;' .. img_link .. ')</small>' -- DEBUGGING
    end
 
	-- Return image wikitext
	return img_wikitext
end
 
return p
Community content is available under CC-BY-SA unless otherwise noted.