Module:Shop: Difference between revisions

From Coral Island Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 1: Line 1:
-- copied from Coral Island wiki
local p = {}
local p = {}
local lib = require('Module:Feature')
local lib = require('Module:Feature')

Latest revision as of 10:39, 2 January 2024

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

-- copied from Coral Island wiki
local p = {}
local lib = require('Module:Feature')
local Icon = require('Module:Icon')._main
local category = mw.html.create()
local _Currencies = {['Coin']=true, ['Merit Points']=true}

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
		data.currency = args.currency or 'Coin'
		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
				local q    = info.q or 0
				cell:node(Icon{
					name = info.img or item,
					link = item,
					size = size,
					note = info.note,
					q    = q
				})
				category:wikitext('[[Category:Sells ', string.lower(item), ']]')
			elseif i == 2 and type == 'Price' and tonumber(value) ~= nil then
				cell:node(Icon{
					name = data.currency,
					link = '',
					amount = value,
					notext = true,
					size = '16'
				})
				cell:attr('data-sort-value', value)
				category:wikitext('[[Category:Shop with ', string.lower(data.currency), ' as currency]]')
			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, '^(.-)*(.-)$')
					if _Currencies[name] then cell:attr('data-sort-value', amount) end
					if name == 'Coin' then
					cell:node(Icon{
						name = name,
						link = name,
						amount = amount,
						notext = lib.ternary(_Currencies[name], true, false),
						size = lib.ternary(_Currencies[name], '16', '20')
					})
				    else
					cell:node(Icon{
						name = name,
						link = name,
						amount = amount,
						size = lib.ternary(_Currencies[name], '16', '20')
					})
				    end
					category:wikitext('[[Category:Shop with ', string.lower(name), ' as currency]]')
				end
			else
				cell:wikitext(value)
			end
		end
	end
	return row
end

return p