Module:Shop usage

From Coral Island Wiki
Jump to navigation Jump to search

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

local p = {}
local Parse = require('Module:Parser').getTemplateArgs
local Slib = require('Module:Shop')
local lib = require('Module:Feature')
local Icon = require('Module:Icon')._main
local Icon_list = require('Module:Icon list')._main

local Pages = {}
local display = {}

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		parentFirst=true,
		wrappers = {'Template:Shop usage'}
	})
	if lib.isNotEmpty(frame.args.pages) then
		local item = args[1] or mw.title.getCurrentTitle().rootText
		for page in string.gmatch(frame.args.pages, '(.-)%$%$%$') do
			local data = Parse(page, { only='Shop' } )
			local Pdata
			if type(data[1]) == 'table' then
				for num, call in pairs(data) do
					Pdata = p.ParseCall(call, item)
					if Pdata then
						break
					end
				end
			else
				Pdata = p.ParseCall(data, item)
			end
			if Pdata then
				Pdata.PAGENAME = page
				table.insert(Pages, Pdata)
			end
		end
		mw.logObject(Pages)
		return p._main(args, frame)
	else
		return 'No shop asks for this item as currency.'
	end
end

function p._main(args, frame)
	local temp_cols = { 'Price range', 'Season', 'Town rank', 'Requirement' }
	local cols = {}
	local _table = mw.html.create('table'):addClass('fandom-table article-table sortable')
	
	-- header row
	local tr = _table:tag('tr')
	tr:tag('th'):wikitext('Location')
	tr:tag('th'):wikitext('Item')
	tr:tag('th'):wikitext('Price')
	for _, col in ipairs(temp_cols) do
		if display[col] then
			table.insert(cols, col)
			tr:tag('th'):wikitext(col)
		end
	end
	
	-- rest of table
	for _, page in ipairs(Pages) do
		local tr = _table:tag('tr')
		tr:tag('td'):attr('rowspan', #page):wikitext('[[', page.PAGENAME, ']]')
		for i, call in pairs(page) do
			if tonumber(i) ~= nil then
				 mw.logObject(call) --debug
				if i>1 then tr = _table:tag('tr') end
				tr:tag('td'):node(Icon_list{call.ITEM_NAME})
				local td = tr:tag('td')
				if call.Price and tonumber(call.Price) ~= nil then
					td
						:node(Icon{
								'Coin',
								call.Price,
								size = '16',
								notext = 1,
								nolink = 1
							})
						:attr('data-sort-value', call.Price)
				elseif call.Price and call.Price:find('%*%d') then
					local items = lib.split(call.Price, ',')
					list = td:tag('span'):addClass('icon-list')
					for _, item in ipairs(items) do
						local name, amount = string.match(item, '^(.-)*(.-)$')
						if name == 'Coin' then td:attr('data-sort-value', amount) end
						list:node(Icon{
							name,
							amount,
							size = (name=='Coin' and '16' or '20'),
							notext = (name=='Coin' and '1' or nil),
							nolink = (name=='Coin' and '1' or nil)
						})
					end
				end
				for _, col in ipairs(cols) do
					local td = tr:tag('td')
					if call[col] then
						td:wikitext(call[col])
					end
				end
			end
		end
	end
	-- mw.logObject(_table) --debug
	return frame:preprocess(tostring(_table))
end


function p.ParseCall(call_data, items)
	local delim = call_data.delim or ';'
	local default = {}
	table.insert(default, (call_data.firstcol or 'Item'))
	table.insert(default, (call_data.secondcol or 'Price'))
	local types = Slib.HashTypes(nil, call_data.cols, default)
	local ret = {}
	local targets = {}
	for item in lib.gsplit(items, ';') do
		targets[string.lower(item)] = true
	end
	local function inString(data)
		for str in lib.gsplit(data, ',') do
			for item in lib.gsplit(str, '//') do
				local pre1 = lib.split(item, '/')
				local quality = pre1[2]
				
				local pre2 = lib.split(pre1[1], '*')
				local name = pre2[1]
				local amount = pre2[2] or '1'
				-- mw.logObject(string.lower(name), 'name')
				-- mw.logObject(mustHave, 'mustHave')
				
				if targets[string.lower(name)] then
					return true
				end
			end
		end
		return false
	end
	
	for row_num, row in lib.spairs(call_data, function(tVal, a, b) return tostring(a)<tostring(b) end) do
		if tonumber(row_num) ~= nil then
			local cols_data = lib.split(row, delim)
			if inString(cols_data[2]) then
				local params = {['ITEM_NAME'] = cols_data[1]}
				for col_num, col_value in ipairs(cols_data) do
					if col_num > 1 and types[col_num] and lib.isNotEmpty(col_value) then
						display[types[col_num]] = true
						params[types[col_num]] = col_value
					end
				end
				
				table.insert(ret, params)
			end
		end
	end
	if next(ret) then
		return ret
	end
end

return p