Module:Shop: Difference between revisions

From Coral Island Wiki
Jump to navigation Jump to search
(remove notext)
Tag: Reverted
m (Reverted edits by Salty Nori (talk) to last revision by Admin coral island)
Tag: Rollback
Line 87: Line 87:
link = '',
link = '',
amount = value,
amount = value,
notext = true,
size = '16'
size = '16'
})
})

Revision as of 01:57, 2 January 2024

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

local p = {}
local lib = require('Module:Feature')
local Icon = require('Module:Icon')._main
local category = mw.html.create()

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame)
	local delim = args.delim or ';'
	
	--start out and figure out cols
	local out = mw.html.create('table'):addClass('article-table fandom-table sortable ' .. (args.class or ''))
	local default = {}
	table.insert(default, (args.firstcol or 'Item'))
	table.insert(default, (args.secondcol or 'Price'))
	local types = p.HashTypes(out, args.cols, default)
	
	--build out
	for _, raw in ipairs(args) do
		local data = lib.split(raw, delim)
		data.size = args.size or '25' -- first column image size
		out:node(p.ItemRow(data, types, default))
	end
	
	out:node(require('Module:Namespace detect').main{main=category})
	
	return out
end

function p.HashTypes(container, custom, default)
	local types = default
	if lib.isNotEmpty(custom) then
		local new = lib.split(custom, ';')
		for _, type in ipairs(new) do
			if lib.isNotEmpty(type) then
				table.insert(types, type)
			end
		end
	end
	if container then
		local tr = container:tag('tr')
		for _, name in ipairs(types) do
			tr:tag('th'):wikitext(name)
		end
	end
	return types
end

function p.splitParams(entry, paramDelim)
	if entry:find('{.-}') then
		local params = string.match(entry, '{(.-)}')
		entry = entry:gsub('{.-}', '')
		params = lib.split(params, paramDelim)
		local returns = {}
		for i, param in ipairs(params) do
			local name, val = string.match(param, '^%s*(.-)%s*=%s*(.-)%s*$')
			if name ~= nil and name ~= '' and val ~= nil then --named params
				returns[name] = val
			elseif param ~= nil and param ~= '' then --unnamed params
				table.insert(returns, param)
			end
		end
		return returns
	end
	return {entry}
end

function p.ItemRow(data, types)
	local row = mw.html.create('tr')
	for i, type in ipairs(types) do
		local cell = row:tag('td')
		if lib.isNotEmpty(data[i]) then
			local value = data[i]
			if i == 1 and type == 'Item' then
				local info = p.splitParams(value, '$')
				local item = info.item or info[1]
				local size = info.size or data.size
				cell:node(Icon{
					name = info.img or item,
					link = item,
					size = size,
					note = info.note
				})
				category:wikitext('[[Category:Sells ', string.lower(item), ']]')
			elseif i == 2 and type == 'Price' and tonumber(value) ~= nil then
				cell:node(Icon{
					name = 'Coin',
					link = '',
					amount = value,
					notext = true,
					size = '16'
				})
				cell:attr('data-sort-value', value)
			elseif i == 2 and type == 'Price' and value:find('%*%d') then
				local items = lib.split(value, ',')
				for _, item in ipairs(items) do
					local name, amount = string.match(item, '^(.-)*(.-)$')
					cell:node(Icon{
						name = name,
						link = lib.ternary(name == 'Coin', '', name),
						amount = amount,
						notext = lib.ternary(name == 'Coin', true, false),
						size = lib.ternary(name == 'Coin', '16', '20')
					})
					category:wikitext('[[Category:Shop with ', string.lower(name), ' as currency]]')
				end
			else
				cell:wikitext(value)
			end
		end
	end
	return row
end

return p