Module:Get spawn info

Jump to navigation Jump to search
Documentation[create] [refresh]
This module has no documentation. If you know how to use this module, please create it.
-- Portions sourced from https://oldschool.runescape.wiki/w/Module:Get_drop_info 
-- Module based on https://minecraft.wiki/w/Module:Get_drop_info

require('strict')
local p = {}
local spriteFile = require("Module:SpriteFile")

local function tooltip(text, title)
	return tostring(mw.html.create('span')
		:addClass('explain')
		:attr('title', title)
		:wikitext(text)
	:allDone())
end

local function makeSpawnTable(data)
	local ret
	if #data > 0 then
		ret = mw.html.create('table')
			:addClass('wikitable sortable')
			:css('text-align', 'center')
			:css('float', 'left')
			:tag('tr')
				:tag('th')
					:wikitext('Category: ' .. data.category)
				:done()
				:tag('th')
					:wikitext('Java Edition')
					:attr('colspan', 3)
				:done()
				:tag('th')
					:wikitext('Bedrock Edition')
					:attr('colspan', 2)
				:done()
			:done()
			:tag('tr')
				:tag('th')
					:wikitext('Spawn area')
				:done()
				:tag('th')
					:wikitext(tooltip('Spawn weight', 'Weight relative to other spawn entries in the category'))
				:done()
				:tag('th')
					:wikitext('Spawn Chance')
				:done()
				:tag('th')
					:wikitext(tooltip('Group size', 'Number of mobs the game tries to spawn per attempt'))
				:done()
				:tag('th')
					:wikitext(tooltip('Spawn weight', 'Weight relative to other spawn entries in the category'))
				:done()
				:tag('th')
					:wikitext(tooltip('Group size', 'Number of mobs the game tries to spawn per attempt'))
				:done()
			:done()
		
		for _, v in ipairs(data) do
			ret:node(v.line)
		end
		
		ret:allDone()
	end
	
	return ret
end

local function makeSpawnLine(data, notes)
	local edition = data['Edition']
	local spawnBiome = data['Spawned in']
	local sizeText = data['Size']
	local weightText = data['Weight']
	local chanceText = data['Weight']
	local sortKey = data['Weight']
	
	if edition == 'java' then
		weightText = '<sup>' .. data['Weight'] .. '</sup>&frasl;<sub>' .. data['Total weight'] .. '</sub>'
		chanceText = math.floor(data['Weight'] / data['Total weight'] * 100 * 100 + 0.5) / 100 .. '%'
		sortKey = data['Weight'] / data['Total weight']
	end
	
	local note
	if data['Note'] then
		if notes[data['Note']] == nil then
			notes[data['Note']] = data['Note name'] .. notes.count
			notes.count = notes.count + 1
			note = mw:getCurrentFrame():extensionTag{name='ref', content=data['Note'], args={name=notes[data['Note']], group='note'}}
		elseif notes[data['Note']] then
			note = mw:getCurrentFrame():extensionTag{name='ref', args={name=notes[data['Note']], group='note'}}
		end
	end
	
	local ret = mw.html.create('tr')
		:tag('td')
			:css('text-align', 'left')
			:wikitext(spriteFile.link({name = "BiomeSprite", align = 'middle',  spawnBiome}))
			:wikitext(note)
		:done()
		:tag('td')
			:wikitext(weightText)
			:attr('data-sort-value', sortKey)
		:done()
		:tag(edition == 'java' and 'td' or '')
			:wikitext(edition == 'java' and chanceText or '')
		:done()
		:tag('td')
			:wikitext((sizeText:gsub('-', '&ndash;')))
		:done()
	:done()
	
	return { line = ret:allDone(), sortKey = sortKey }
end

function p.main(frame)
	local args = frame
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge( true )
	else
		frame = mw.getCurrentFrame()
	end
	
	local spawnedMob = args.name or args[1] or mw.title.getCurrentTitle().text
	spawnedMob = mw.text.trim(spawnedMob)
	
	local query = {
		'[[Spawned mob::' .. spawnedMob .. ']]',
		'?Spawn JSON',
		limit = 500
	}
	
	local smwData = mw.smw.ask(query)
	
	local javaTable = {}
	javaTable.edition = "Java"
	local bedrockTable = {}
	bedrockTable.edition = "Bedrock"
	local notes = {}
	notes.count = 0
	
	for _, v in ipairs(smwData or {}) do
		if type(v['Spawn JSON']) ~= "table" then
			v['Spawn JSON'] = { v['Spawn JSON'] }
		end
		
		for _, k in ipairs(v['Spawn JSON']) do
			local spawnJSON = mw.text.jsonDecode(k)
			if spawnJSON["Edition"] == 'java' then
				if not javaTable.category then
					javaTable.category = spawnJSON['Category']
				end
				table.insert(javaTable, makeSpawnLine(spawnJSON, notes))
			elseif spawnJSON["Edition"] == 'bedrock' then
				if not bedrockTable.category then
					bedrockTable.category = spawnJSON['Category']
				end
				table.insert(bedrockTable, makeSpawnLine(spawnJSON, notes))
			end
		end
	end
	
	local function sortTable(a, b)
		return a.sortKey > b.sortKey
	end
	
	table.sort(javaTable, sortTable)
	table.sort(bedrockTable, sortTable)
	
	assert(#javaTable + #bedrockTable > 0, 'No spawn info found for ' .. spawnedMob)
	
	-- Create a container to hold tables
	local ret = mw.html.create('div')
		:addClass('collapsible collapsetoggle-inline')
		:tag('div')
			:wikitext(spawnedMob .. " spawn table:")
		:done()
		:tag('div')
			:addClass('collapsible-content')
			:node(makeSpawnTable(javaTable))
			:node(makeSpawnTable(bedrockTable))
			:tag('div')
				:css('clear', 'both')
			:done()
			:wikitext(mw.getCurrentFrame():extensionTag{name = "references", args = {group = 'note'}})
		:done()
	:done()
	
	return ret:allDone()
end

return p

--[[ debug:
mw.log(p.main({name='Cow'}))
--]]