Module:Loadout: Difference between revisions

From Illerai
Jump to navigation Jump to search
m 1 revision imported
imported>BigDiesel2m
m moving string cleaning code to a separate subfunction for easier reuse
 
Line 5: Line 5:
local pouchmodule = require('Module:Rune pouch')
local pouchmodule = require('Module:Rune pouch')
local spellmodule = require('Module:Spells')
local spellmodule = require('Module:Spells')
local static = require('Module:Static')


local slotarray = {
local slotarray = {
Line 69: Line 70:
[55] = '28',
[55] = '28',
}
}
function p._cleanItem(item)
if item ~= nil then
local cleaned = mw.text.trim(item)
if cleaned ~= '' then
cleaned = cleaned:gsub("^%l", string.upper)
if string.match(cleaned, "^([^;/]*)[;/]") ~= nil then
cleaned = string.match(cleaned, "^([^;/]*)[;/]")
end
return cleaned
end
end
end


function p._getID(name)
function p._getID(name)
Line 83: Line 97:
if type(id) == 'number' then
if type(id) == 'number' then
table.insert(idarray, id)
table.insert(idarray, id)
idcount = idcount + 1
elseif type(id) == 'table' then
table.insert(idarray, math.min(unpack(id)))
idcount = idcount + 1
idcount = idcount + 1
end
end
Line 145: Line 162:
     -- This section creates the loadout section if there is a loadoutname provided
     -- This section creates the loadout section if there is a loadoutname provided
     if args['loadoutname'] then
     if args['loadoutname'] then
     local icon = args['loadouticon']:gsub("^%l", string.upper) or 'Weird gloop'
    local lookupdict = static.lookups or {}
     local icon = p._cleanItem(args[icon]) or 'Weird gloop'
local loadoutstr = 'banktags,1,'..args['loadoutname']..','..p._getID(icon)..',layout'
local loadoutstr = 'banktags,1,'..args['loadoutname']..','..p._getID(icon)..',layout'
for slot, item in pairs(loadoutmap) do
for slot, item in pairs(loadoutmap) do
if args[item] then
if p._cleanItem(args[item]) then
local cleanitem = args[item]:gsub("^%l", string.upper)
local cleanitem = p._cleanItem(args[item])
cleanitem = mw.text.trim(cleanitem)
if lookupdict[cleanitem] ~= nil then
if cleanitem ~= '' then
loadoutstr = loadoutstr..','..tostring(slot)..','..tostring(lookupdict[cleanitem])
else
local id = p._getID(mw.text.trim(cleanitem))
local id = p._getID(mw.text.trim(cleanitem))
loadoutstr = loadoutstr..','..tostring(slot)..','..tostring(id)
if id ~= nil then
lookupdict[cleanitem] = id
loadoutstr = loadoutstr..','..tostring(slot)..','..tostring(lookupdict[cleanitem])
end
end
end
end
end
end
end
static.lookups = lookupdict
    local row = mw.html.create('tr'):attr('style', 'text-align:center')
    local row = mw.html.create('tr'):attr('style', 'text-align:center')

Latest revision as of 23:00, 4 November 2024

Module documentation
This documentation is transcluded from Module:Loadout/doc. [edit] [history] [purge]
Module:Loadout requires Module:Equipment.
Module:Loadout requires Module:Inventory.
Module:Loadout requires Module:Rune pouch.
Module:Loadout requires Module:Spells.
Module:Loadout requires Module:Static.

This template should be used when there is no documentation for a Module or Template.


local p = {}

local invmodule = require('Module:Inventory')
local equipmodule = require('Module:Equipment')
local pouchmodule = require('Module:Rune pouch')
local spellmodule = require('Module:Spells')
local static = require('Module:Static')

local slotarray = {
	'head',
	'cape',
	'neck',
	'ammo',
	'ammo2',
	'weapon',
	'ammoquantity',
	'ammo2quantity',
	'weaponquantity',
	'torso',
	'legs',
	'shield',
	'gloves',
	'boots',
	'ring'}

local loadoutmap = {
	[1] = 'head',
	[2] = 'ammo2',
	[4] = '1',
	[5] = '2',
	[6] = '3',
	[7] = '4',
	[8] = 'cape',
	[9] = 'neck',
	[10] = 'ammo',
	[12] = '5',
	[13] = '6',
	[14] = '7',
	[15] = '8',
	[16] = 'weapon',
	[17] = 'torso',
	[18] = 'shield',
	[20] = '9',
	[21] = '10',
	[22] = '11',
	[23] = '12',
	[25] = 'legs',
	[28] = '13',
	[29] = '14',
	[30] = '15',
	[31] = '16',
	[32] = 'gloves',
	[33] = 'boots',
	[34] = 'ring',
	[36] = '17',
	[37] = '18',
	[38] = '19',
	[39] = '20',
	[40] = 'rune1',
	[41] = 'rune2',
	[42] = 'rune3',
	[44] = '21',
	[45] = '22',
	[46] = '23',
	[47] = '24',
	[48] = 'rune4',
	[52] = '25',
	[53] = '26',
	[54] = '27',
	[55] = '28',
}

function p._cleanItem(item)
	if item ~= nil then
		local cleaned = mw.text.trim(item)
		if cleaned ~= '' then
			cleaned = cleaned:gsub("^%l", string.upper)
			if string.match(cleaned, "^([^;/]*)[;/]") ~= nil then
				cleaned = string.match(cleaned, "^([^;/]*)[;/]")
			end
			return cleaned
		end
	end
end

function p._getID(name)
	local query = {
		'[[Item name::'..name..']]',
		'?Item ID'
	}
	local smw_data = mw.smw.ask(query)
	local idarray = {}
	local idcount = 0
	if smw_data then
		for _, subobj in ipairs(smw_data) do
			local id = subobj['Item ID']
			if type(id) == 'number' then
				table.insert(idarray, id)
				idcount = idcount + 1
			elseif type(id) == 'table' then
				table.insert(idarray, math.min(unpack(id)))
				idcount = idcount + 1
			end
		end
		if idcount == 0 then
			mw.log('No IDs found: '..name)
		else
			return math.min(unpack(idarray))
		end
	else
		mw.log('SMW lookup failed: '..name)
	end
end

function p.main(frame)
    local args = frame:getParent().args
    return p._main(args)
end

function p._main(args)
	local itemsdict = {align = 'center'}
    for i=1,28 do
    	itemsdict[i] = mw.text.trim(args[i] or '')
    end
    local inventory = invmodule._main(itemsdict,'inventory')
    
    local equipdict = {align = 'center'}
    for _, slot in ipairs(slotarray) do
    	if args[slot] then
    		equipdict[slot] = mw.text.trim(args[slot])
		end
	end
    local equipment = equipmodule._main(equipdict)
    
    local pouchdict = {
    	align = 'center',
    	[1] = mw.text.trim(args['rune1'] or ''),
    	[2] = mw.text.trim(args['rune2'] or ''),
    	[3] = mw.text.trim(args['rune3'] or ''),
    	[4] = mw.text.trim(args['rune4'] or ''),
    }
    local pouch = pouchmodule._main(pouchdict)
    
    local spelldict = {
    	align = 'center',
    	[1] = mw.text.trim(args['spell1'] or ''),
    	[2] = mw.text.trim(args['spell2'] or ''),
    	[3] = mw.text.trim(args['spell3'] or ''),
    	[4] = mw.text.trim(args['spell4'] or ''),
    	[5] = mw.text.trim(args['spell5'] or ''),
    }
    local spells = spellmodule._main(spelldict)

    local tbl = mw.html.create('table')
    local row = mw.html.create('tr')
    row:tag('td'):wikitext(tostring(equipment)):done():tag('td'):wikitext(tostring(inventory)):done()
    tbl:node(row)
    local row = mw.html.create('tr')
    row:tag('td'):wikitext(tostring(spells)):done():tag('td'):wikitext(tostring(pouch)):done()
    tbl:node(row)
    
    -- This section creates the loadout section if there is a loadoutname provided
    if args['loadoutname'] then
    	local lookupdict = static.lookups or {}
    	local icon = p._cleanItem(args[icon]) or 'Weird gloop'
		local loadoutstr = 'banktags,1,'..args['loadoutname']..','..p._getID(icon)..',layout'
		
		for slot, item in pairs(loadoutmap) do
			if p._cleanItem(args[item]) then
				local cleanitem = p._cleanItem(args[item])
				if lookupdict[cleanitem] ~= nil then
					loadoutstr = loadoutstr..','..tostring(slot)..','..tostring(lookupdict[cleanitem])
				else
					local id = p._getID(mw.text.trim(cleanitem))
					if id ~= nil then
						lookupdict[cleanitem] = id
						loadoutstr = loadoutstr..','..tostring(slot)..','..tostring(lookupdict[cleanitem])
					end
				end
			end
		end
		static.lookups = lookupdict
	
	    local row = mw.html.create('tr'):attr('style', 'text-align:center')
	    row:tag('td'):wikitext(tostring('<code class="loadout-code" style="display:none">'..loadoutstr..'</code>')):attr('colspan', '2'):done()
	    tbl:node(row)
    end
    
    return tbl
end

return p