Module:SkillUnlocks: Difference between revisions

From Melvor Idle
No edit summary
No edit summary
Line 7: Line 7:


local Shared = require('Module:Shared')
local Shared = require('Module:Shared')
local Constants = require('Module:Constants')
local Icons = require('Module:Icons')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local Items = require('Module:Items')
local CombatAreas = require('Module:CombatAreas')


local WEARABLES = {'Armour', 'Ring', 'Amulet'}
local CHECK_ITEMS = {'Attack', 'Strength', 'Defence', 'Ranged', 'Magic',
local CHECK_ITEMS = {'Attack', 'Strength', 'Defence', 'Ranged', 'Magic'}
'Slayer'}
local CHECK_AREAS = {'Slayer'}
local TYPE_SORT_ORDER = {
['item'] = 1,
['combatArea'] = 2,
['slayerArea'] = 3,
['dungeon'] = 4
}
local VERBS_PER_SUBTYPE = {
['Weapon'] = 'Wield',
['Armour'] = 'Wear',
['Ring'] = 'Wear',  
['Amulet'] = 'Wear',
['combatArea'] = 'Access',
['slayerArea'] = 'Access',
['dungeon'] = 'Access'
}


function p._getAllItemsWithSkillRequirement(skillReqLabel)
function p._addItemsWithSkillRequirement(entityList, skillName)
local skillReqLabel = skillName:lower() .. 'LevelRequired'
local itemList = Items.getItems(function(item)
local itemList = Items.getItems(function(item)
-- Exclude Golbin Raid exclusives
-- Exclude Golbin Raid exclusives
Line 23: Line 42:
end)
end)
local itemsWithReqs = {}
-- TODO: This can probably be made into a generic function for each entity
for i, item in ipairs(itemList) do
for i, item in ipairs(itemList) do
local processedItem = {}
local processed = {}
processedItem.item = item
processed.entity = item
processedItem.skillLevel = Items._getItemStat(item, skillReqLabel)
processed.entityType = 'item'
table.insert(itemsWithReqs, processedItem)
processed.subType = item.type
processed.entityName = item.name
processed.skillLevel = Items._getItemStat(item, skillReqLabel)
table.insert(entityList, processed)
end
return entityList
end
 
-- This covers combat areas, Slayer areas, and dungeons
function p._addAreasWithSkillRequirement(entityList, skillName)
local areaList = CombatAreas.getAreas(function(area)
local hasSkillReq = false
for i, req in ipairs(area.entryRequirements) do
if req.type == 'SkillLevel' and req.skillID == Constants.getSkillID(skillName) then
hasSkillReq = true
end
end
return hasSkillReq
end)
for i, area in ipairs(areaList) do
local processed = {}
processed.entity = area
processed.entityType = area.type
processed.subType = area.type
processed.entityName = area.name
for a, req in ipairs(area.entryRequirements) do
if req.type == 'SkillLevel' and req.skillID == Constants.getSkillID(skillName) then
processed.skillLevel = req.level
end
end
table.insert(entityList, processed)
end
end
return itemsWithReqs
return entityList
end
end


function p._getSkillUnlockTable(skillName)
function p._getSkillUnlockTable(skillName)
local skillReqLabel = skillName:lower() .. 'LevelRequired'
-- local skillReqLabel = skillName:lower() .. 'LevelRequired'
-- What do we need to check for this skill? (we avoid checking everything
-- What do we need to check for this skill? (avoid checking everything for
-- for every skill to save time)
-- every skill to save time)
local itemList = {}
local entityList = {}
if Shared.contains(CHECK_ITEMS, skillName) then
if Shared.contains(CHECK_ITEMS, skillName) then
itemList = p._getAllItemsWithSkillRequirement(skillReqLabel)
entityList = p._addItemsWithSkillRequirement(entityList, skillName)
end
if Shared.contains(CHECK_AREAS, skillName) then
entityList = p._addAreasWithSkillRequirement(entityList, skillName)
end
end
if Shared.tableIsEmpty(itemList) then
if Shared.tableIsEmpty(entityList) then
-- TODO: Omit empty tables
-- TODO: More specific empty handling
return nil
return nil
end
end
-- Sort the list of items by level requirement
-- Sort the big list of everything
table.sort(itemList, function(a, b)  
table.sort(entityList, function(a, b)  
return a.skillLevel < b.skillLevel or (a.skillLevel == b.skillLevel and a.item.name < b.item.name)
-- Sort by level first
if a.skillLevel ~= b.skillLevel then
return a.skillLevel < b.skillLevel
-- Then by type
elseif TYPE_SORT_ORDER[a.entityType] ~= TYPE_SORT_ORDER[b.entityType] then
return TYPE_SORT_ORDER[a.entityType] < TYPE_SORT_ORDER[b.entityType]
-- And finally by entity name
else
return a.entityName < b.entityName
end
end)
end)
Line 60: Line 123:
table.insert(resultPart, '\r\n!Unlocks')
table.insert(resultPart, '\r\n!Unlocks')
-- Time to iterate the list!
-- Time to iterate!
local currentLevel = 0
local currentLevel = 0
for i, item in ipairs(itemList) do
for i, entity in ipairs(entityList) do
local itemLevel = Items._getItemStat(item.item, skillName .. 'LevelRequired')
local foundLevel = entity.skillLevel
-- Start a new row if the current item's level differs from the current
-- Start a new row if the current entity's level is higher than the
-- row
-- current row
if itemLevel ~= currentLevel then
if foundLevel ~= currentLevel then
currentLevel = itemLevel
currentLevel = foundLevel
table.insert(resultPart, '\r\n|-')
table.insert(resultPart, '\r\n|-')
table.insert(resultPart, '\r\n|' .. itemLevel)
table.insert(resultPart, '\r\n|' .. foundLevel)
table.insert(resultPart, '\r\n|')
table.insert(resultPart, '\r\n|')
else
else
Line 76: Line 139:
end
end
-- Append item to the column
-- Append entity to the column
local verb = ''
local verb = ''
if item.item.type == 'Weapon' then
if Shared.contains(VERBS_PER_SUBTYPE, entity.subType) then
verb = 'Wield '
verb = VERBS_PER_SUBTYPE[entity.subType] .. ' '
elseif Shared.contains(WEARABLES, item.item.type) then
verb = 'Wear '
end
end
table.insert(resultPart, verb .. Icons.Icon({item.item.name, type='item'}))
table.insert(resultPart, verb .. Icons.Icon({entity.entityName, type=entity.entityType}))
end
end

Revision as of 23:39, 23 April 2023

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

local p = {}

--This module polls various game data sources to produce a full list of skill
--level unlocks for each skill. The game has a hard-coded set of "milestones"
--for each skill that does the same thing, but it is not exhaustive and not
--permanently visible for most combat skills.

local Shared = require('Module:Shared')
local Constants = require('Module:Constants')
local Icons = require('Module:Icons')
local Items = require('Module:Items')
local CombatAreas = require('Module:CombatAreas')

local CHECK_ITEMS = {'Attack', 'Strength', 'Defence', 'Ranged', 'Magic', 
	'Slayer'}
local CHECK_AREAS = {'Slayer'}
local TYPE_SORT_ORDER = {
	['item'] = 1, 
	['combatArea'] = 2,
	['slayerArea'] = 3,
	['dungeon'] = 4
}
local VERBS_PER_SUBTYPE = {
	['Weapon'] = 'Wield',
	['Armour'] = 'Wear',
	['Ring'] = 'Wear', 
	['Amulet'] = 'Wear',
	['combatArea'] = 'Access',
	['slayerArea'] = 'Access',
	['dungeon'] = 'Access'
}

function p._addItemsWithSkillRequirement(entityList, skillName)
	local skillReqLabel = skillName:lower() .. 'LevelRequired'
	local itemList = Items.getItems(function(item)
		-- Exclude Golbin Raid exclusives
		if item.golbinRaidExclusive ~= nil and item.golbinRaidExclusive then
			return false
		end
		
		return Items._getItemStat(item, skillReqLabel) ~= nil
	end)
	
	-- TODO: This can probably be made into a generic function for each entity
	for i, item in ipairs(itemList) do
		local processed = {}
		processed.entity = item
		processed.entityType = 'item'
		processed.subType = item.type
		processed.entityName = item.name
		processed.skillLevel = Items._getItemStat(item, skillReqLabel)
		table.insert(entityList, processed)
	end
	
	return entityList
end

-- This covers combat areas, Slayer areas, and dungeons
function p._addAreasWithSkillRequirement(entityList, skillName)
	local areaList = CombatAreas.getAreas(function(area)
		local hasSkillReq = false
		for i, req in ipairs(area.entryRequirements) do
			if req.type == 'SkillLevel' and req.skillID == Constants.getSkillID(skillName) then
				hasSkillReq = true
			end
		end
		return hasSkillReq
	end)
	
	for i, area in ipairs(areaList) do
		local processed = {}
		processed.entity = area
		processed.entityType = area.type
		processed.subType = area.type
		processed.entityName = area.name
		for a, req in ipairs(area.entryRequirements) do
			if req.type == 'SkillLevel' and req.skillID == Constants.getSkillID(skillName) then
				processed.skillLevel = req.level
			end
		end
		table.insert(entityList, processed)
	end
	
	return entityList
end

function p._getSkillUnlockTable(skillName)
	-- local skillReqLabel = skillName:lower() .. 'LevelRequired'
	
	-- What do we need to check for this skill? (avoid checking everything for
	-- every skill to save time)
	local entityList = {}
	if Shared.contains(CHECK_ITEMS, skillName) then
		entityList = p._addItemsWithSkillRequirement(entityList, skillName)
	end
	if Shared.contains(CHECK_AREAS, skillName) then
		entityList = p._addAreasWithSkillRequirement(entityList, skillName)
	end
	
	if Shared.tableIsEmpty(entityList) then
		-- TODO: More specific empty handling
		return nil
	end
	
	-- Sort the big list of everything
	table.sort(entityList, function(a, b) 
		-- Sort by level first
		if a.skillLevel ~= b.skillLevel then
			return a.skillLevel < b.skillLevel
		-- Then by type
		elseif TYPE_SORT_ORDER[a.entityType] ~= TYPE_SORT_ORDER[b.entityType] then
			return TYPE_SORT_ORDER[a.entityType] < TYPE_SORT_ORDER[b.entityType]
		-- And finally by entity name
		else
			return a.entityName < b.entityName
		end
	end)
	
	-- Header and columns
	local resultPart = {}
	table.insert(resultPart, '{| class="wikitable"\r\n|-class="headerRow-0"')
	table.insert(resultPart, '\r\n!' .. Icons.Icon({skillName, type='skill', notext='true'}) .. ' Level')
	table.insert(resultPart, '\r\n!Unlocks')
	
	-- Time to iterate!
	local currentLevel = 0
	for i, entity in ipairs(entityList) do
		local foundLevel = entity.skillLevel
		
		-- Start a new row if the current entity's level is higher than the
		-- current row
		if foundLevel ~= currentLevel then
			currentLevel = foundLevel
			table.insert(resultPart, '\r\n|-')
			table.insert(resultPart, '\r\n|' .. foundLevel)
			table.insert(resultPart, '\r\n|')
		else
			table.insert(resultPart, '<br/>')
		end
		
		-- Append entity to the column
		local verb = ''
		if Shared.contains(VERBS_PER_SUBTYPE, entity.subType) then
			verb = VERBS_PER_SUBTYPE[entity.subType] .. ' '
		end
		
		table.insert(resultPart, verb .. Icons.Icon({entity.entityName, type=entity.entityType}))
	end
	
	table.insert(resultPart, '\r\n|}')

	return table.concat(resultPart)
end

function p.getSkillUnlockTable(frame)
	local skillName = frame.args ~= nil and frame.args[1]
	return p._getSkillUnlockTable(skillName)
end

return p