Module:Infos sur le commerce

De Minecraft Wiki
Aller à la navigation Aller à la recherche
[créer] [purger]Documentation
Ce module n'a pas de documentation. Si vous savez utiliser ce module, veuillez la créer.
local p = {}
local namespace = mw.title.getCurrentTitle().nsText
local spriteFile = require("Module:SpriteFile")
local dropsline = require("Module:Ligne de butin")
local tradeTable = require("Module:Tableau de commerce")
local refUsed = false
local note_list = {} -- Track note names that have already been used, so we don't output them twice

function p.match_wanted(frame)
	return p._main(frame:getParent().args, 'Objet voulu')
end

function p.match_given(frame)
	return p._main(frame:getParent().args, 'Objet proposé')
end

function p._main(args, primary)
	local itemName = args.name or args[1]
	local smwItemName
	
	if not itemName then
		itemName = mw.title.getCurrentTitle().text
	end
	smwItemName = itemName
	local query = {
		'[[' .. primary .. '::' .. smwItemName .. ']]',
		'?Commerce JSON',
		limit = 500
	}
	
	local smwdata = mw.smw.ask(query)
	
	local retTable = {}
	if smwdata then
		for i,v in ipairs(smwdata) do
			if type(v['Commerce JSON']) == "table" then -- if a page has a drop happening multiple times it is returned in a table instead of a string
				for j,k in ipairs(v['Commerce JSON']) do
					local tradeJSON = mw.text.jsonDecode(v['Commerce JSON'][j] or '{}')
					table.insert(retTable, makeTradeLine(itemName, tradeJSON))
				end
			else
				local tradeJSON = mw.text.jsonDecode(v['Commerce JSON'] or '{}')
				table.insert(retTable, makeTradeLine(itemName, tradeJSON))
			end
		end
	end
	
	-- Create the html table to display results
	local t = mw.html.create('table')
	t	:addClass('wikitable sortable autosort=1,a')
		:css('text-align', 'center')
		:tag('tr')
		:tag('th'):wikitext('Villageois'):attr('rowspan', 2)
		:tag('th'):wikitext('Probabilité'):attr('colspan', 2)
		:tag('th'):wikitext('Le villageois veut'):attr('rowspan', 2)
		:tag('th'):wikitext('Le joueur reçoit'):attr('rowspan', 2)
		:tag('tr')
		:tag('th'):wikitext("[[JE]]")
		:tag('th'):wikitext("[[BE]]")
		
	-- insert all the drop lines we made before
	for i,v in ipairs(retTable) do
		t:node(v)
	end
	
	-- Set an error category if there are no results.
	if #retTable == 0 and namespace == '' then
		t:node():wikitext('[[Catégorie:Infos sur le commerce vide]]')
	end

	local ret = mw.html.create():wikitext(tostring(t) .. mw.getCurrentFrame():extensionTag{name="references", args = {group='t'}})

	return ret
end

function makeTradeLine(itemName, data)
	local profession = data['profession']
	local level = data['level']
	local java_probability = data['java_probability']
	local bedrock_probability = data['bedrock_probability']
	local given_item = data['given_item']
	local given_quant = data['given_quant']
	local given_sprite = data['given_sprite']
	local wanted_item = data['wanted_item']
	local wanted_quant = data['wanted_quant']
	local wanted_sprite = data['wanted_sprite']
	local wanted_item_2 = data['wanted_item_2']
	local wanted_quant_2 = data['wanted_quant_2']
	local wanted_sprite_2 = data['wanted_sprite_2']
	local given_note = data['given_note']
	local given_note_text = data['given_note_text']
	local wanted_note = data['wanted_note']
	local wanted_note_text = data['wanted_note_text']
	
	if not profession then
		mw.log("Erreur profession vide")
		mw.logObject(data)
		return nil
	end

	local ret = mw.html.create('tr')
	
	local level_sort_key = 0
	if level == 'Novice' then level_sort_key = 1 end
	if level == 'Apprenti' then level_sort_key = 2 end
	if level == 'Compagnon' then level_sort_key = 3 end
	if level == 'Expert' then level_sort_key = 4 end
	if level == 'Maître' then level_sort_key = 5 end
	
	local nameTag = ret:css('text-align', 'center')
		:tag('td')
		:css('text-align', 'left')
		:attr('data-sort-value', level_sort_key..profession)
		-- :wikitext('[[File:Villager badge ', level, '.png|link=|16x16px]] ')
		:wikitext(spriteFile.link({name = "EntitéSprite", align = 'middle', id = profession:lower()}))
		:wikitext(" [[Commerce#" .. profession .. "|" .. profession .. " " .. level:lower() .. "]]")
	ret:tag('td'):wikitext(java_probability, '%')
	ret:tag('td'):wikitext(bedrock_probability, '%')

	local wanted = ret:tag('td')
	wanted:wikitext(tradeTable.formatItemOutput(wanted_item, wanted_quant, wanted_item_2, wanted_quant_2, wanted_sprite, wanted_sprite_2))
	if wanted_note_text then
		refUsed = true -- Used to set references at footer
		if wanted_note ~= nil and note_list[wanted_note] == nil then
			note_list[wanted_note] = wanted_note_text
		end
		wanted:wikitext(mw.getCurrentFrame():extensionTag{ name='ref', content=wanted_note_text, args = {name=wanted_note or '', group='t'}})
	end
	
	local given = ret:tag('td'):wikitext(tradeTable.formatItemOutput(given_item, given_quant, nil, nil, given_sprite))
	if given_note_text then
		refUsed = true -- Used to set references at footer
		if given_note ~= nil and note_list[given_note] == nil then
			note_list[given_note] = given_note_text
		end
		given:wikitext(mw.getCurrentFrame():extensionTag{ name='ref', content=given_note_text, args = {name=given_note or '', group='t'}})
	end
	
	return ret:done()
end
return p