Module:Skills/Summoning

From Melvor Idle
< Module:Skills
Revision as of 20:15, 27 June 2024 by Slash (talk | contribs) (Ceil Summoning recipes and fixed max abyssal recipe costs)

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

local p = {}

local Constants = require('Module:Constants')
local Shared = require('Module:Shared')
local Common = require('Module:Common')
local GameData = require('Module:GameData')
local SkillData = GameData.skillData
local Modifiers = require('Module:Modifiers')
local Skills = require('Module:Skills')
local Items = require('Module:Items')
local Icons = require('Module:Icons')

function p.getMarkTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local realmName = args.realm
	local realm = Skills.getRealmFromName(realmName)
	if realm == nil then
		return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
	end
	local skillID = 'Summoning'

	local result = ''
	result = result..'{| class="wikitable sortable stickyHeader"'
	result = result..'\r\n|- class="headerRow-0"'
	result = result..'\r\n!colspan="2"|Mark!!Requirements'
	result = result..'!!Tier!!Discovered in'

	local Familiars = GameData.getEntities(SkillData.Summoning.recipes,
		function(obj)
			return Skills.getRecipeRealm(obj) == realm.id
		end
	)
	table.sort(Familiars, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)

	local rowArray = {}
	for i, Fam in ipairs(Familiars) do
		local level = Skills.getRecipeLevel(skillID, Fam)
		local reqText = Skills.getRecipeRequirementText(SkillData.Summoning.name, Fam)
		local item = Items.getItemByID(Fam.productID)
		if item ~= nil then
			local rowText = '|-'
			rowText = rowText..'\r\n|data-sort-value="'..item.name..'"|'..Icons.Icon({item.name, type='mark', notext=true, size='50'})
			rowText = rowText..'||' .. Icons.getExpansionIcon(Fam.id) .. Icons.Icon({item.name, 'Mark of the ' .. item.name, type='mark', noicon=true})
			rowText = rowText..'||style="text-align:right" data-sort-value="' .. level .. '"|' .. reqText
			rowText = rowText..'||style="text-align:right"|'..Fam.tier
			local discoveredArray = {}
			for j, SkillID in ipairs(Fam.skillIDs) do
				table.insert(discoveredArray, Icons.Icon({Constants.getSkillName(SkillID), type='skill'}))
			end
			rowText = rowText..'||'..table.concat(discoveredArray, '<br/>')
			table.insert(rowArray, rowText)
		end
	end

	result = result..'\r\n'..table.concat(rowArray, '\r\n')
	result = result..'\r\n|}'
	return result
end

function p.getTabletTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local realmName = args.realm
	local realm = Skills.getRealmFromName(realmName)
	if realm == nil then
		return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
	end
	local skillID = 'Summoning'

	local result = ''
	result = result..'{| class="wikitable sortable stickyHeader"'
	result = result..'\r\n|- class="headerRow-0"'
	result = result..'\r\n!colspan="2"|Name!!Requirements'
	result = result..'!!Tier!!Effect!!' .. Icons.Icon({'Melee', notext=true, nolink=true}) .. ' Max Hit!!Shard Cost!!Secondary!!Creation XP'

	local Familiars = GameData.getEntities(SkillData.Summoning.recipes,
		function(recipe)
			return Skills.getRecipeRealm(recipe) == realm.id
		end
	)
	table.sort(Familiars, function(a, b) return Skills.standardRecipeSort(skillID, a, b) end)

	local rowArray = {}
	for i, Fam in ipairs(Familiars) do
		local level, isAbyssal = Skills.getRecipeLevelRealm(skillID, Fam)
		local baseXP = Fam.baseAbyssalExperience or Fam.baseExperience
		local reqText = Skills.getRecipeRequirementText(SkillData.Summoning.name, Fam)
		local item = Items.getItemByID(Fam.productID)
		if item ~= nil then
			local maxHit, maxHitText = Items._getItemStat(item, 'summoningMaxhit'), ''
			if maxHit == nil then
				maxHitText = 'class="table-na" data-sort-value="-1"| N/A'
			else
				maxHit = maxHit * 10
				maxHitText = 'style="text-align:right;" data-sort-value="' .. maxHit .. '"|' .. Shared.formatnum(maxHit)
			end
			local effectDesc = Modifiers.getModifiersText(item.modifiers, false, false, 10)
			local rowText = '|-'
			rowText = rowText..'\r\n|data-sort-value="'..item.name..'"|'..Icons.Icon({item.name, type='item', notext=true, size='50'})
			rowText = rowText..'||' .. Icons.getExpansionIcon(Fam.id) .. Icons.Icon({item.name, type='item', noicon=true})
			rowText = rowText..'||style="text-align:right" data-sort-value="' .. level .. '"|' .. reqText
			rowText = rowText..'||style="text-align:right"|'..Fam.tier
			rowText = rowText..'||'..effectDesc..'||'..maxHitText

			-- Create item requirements text
			local ShardCostArray, OtherCostArray = {}, {}
			-- Shards
			for j, cost in ipairs(Fam.itemCosts) do
				local shard = Items.getItemByID(cost.id)
				if shard ~= nil then
					table.insert(ShardCostArray, Icons.Icon({shard.name, type='item', notext=true, qty=cost.quantity}))
				end
			end
			rowText = rowText..'||style="text-align:right"|'..table.concat(ShardCostArray, ', ')..'&nbsp;'

			-- Other costs
			local recipeCost = 0
			if isAbyssal == true then
				recipeCost = SkillData.Summoning.recipeAPCost
			else
				recipeCost = SkillData.Summoning.recipeGPCost
			end
			local currencyCostText = Common.getCostString({ ["items"] = {}, ["currencies"] = Fam.currencyCosts})
			if currencyCostText ~= nil then
				table.insert(OtherCostArray, currencyCostText)
			end
			for j, nonShardID in ipairs(Fam.nonShardItemCosts) do
				local nonShard = Items.getItemByID(nonShardID)
				if nonShard ~= nil then
					local itemValue = math.max(nonShard.sellsFor, 20)
					local nonShardQty = math.max(1, math.ceil(recipeCost / itemValue))
					table.insert(OtherCostArray, Icons.Icon({nonShard.name, type='item', notext=true, qty=nonShardQty}))
				end
			end
			rowText = rowText..'||style="text-align:right"|'..table.concat(OtherCostArray, "<br/>'''OR''' ")
			rowText = rowText..'||style="text-align:right" data-sort-value="' .. baseXP .. '"|' .. Shared.formatnum(baseXP)
			table.insert(rowArray, rowText)
		end
	end

	result = result..'\r\n'..table.concat(rowArray, '\r\n')

	result = result..'\r\n|}'
	return result
end

function p._getSynergyTable(familiarIDs)
	local skillID = 'Summoning'
	local result = ''
	result = result..'{| class="wikitable sortable stickyHeader"'
	result = result..'\r\n|- class="headerRow-0"'
	result = result..'\r\n!colspan="2"|Familiar 1!!colspan="2"|Familiar 2!!Effect!!Requirements'

	local recipesByID, famNames = {}, {}
	for i, recipe in ipairs(SkillData.Summoning.recipes) do
		recipesByID[recipe.id] = recipe
		local item = Items.getItemByID(recipe.productID)
		if item ~= nil then
			famNames[recipe.id] = item.name
		end
	end

	local synergyList = GameData.getEntities(SkillData.Summoning.synergies,
		function(synergy)
			for i, summonID in ipairs(synergy.summonIDs) do
				if Shared.contains(familiarIDs, summonID) then
					return true
				end
			end
			return false
		end)
	table.sort(synergyList,
		function (a, b)
			local recA1, recB1 = recipesByID[a.summonIDs[1]], recipesByID[b.summonIDs[1]]

			if ((recA1.abyssalLevel or 0) == (recB1.abyssalLevel or 0)) and (recA1.level == recB1.level) then
				return (
					(a.summonIDs[1] == b.summonIDs[1] and a.summonIDs[2] < b.summonIDs[2])
					or a.summonIDs[1] < b.summonIDs[1]
				 )
			else
				return Skills.standardRecipeSort(skillID, recA1, recB1)
			end
		end
	)

	local rowArray = {}
	for i, syn in ipairs(synergyList) do
		local Fam1 = recipesByID[syn.summonIDs[1]]
		local Fam2 = recipesByID[syn.summonIDs[2]]
		if Fam1 ~= nil and Fam2 ~= nil then
			local FamName1 = famNames[Fam1.id] or 'Unknown'
			local FamName2 = famNames[Fam2.id] or 'Unknown'
			local synDesc = syn.customDescription
			if synDesc == nil then
				-- Generate description from modifiers
				synDesc = Modifiers.getModifiersText(syn.modifiers, false, false, 10) or ''
			end
			local rowText = '|-'
			rowText = rowText..'\r\n|data-sort-value="'..FamName1..'"|'..Icons.Icon({FamName1, type='item', notext=true, size='30'})
			rowText = rowText..'||' .. Icons.getExpansionIcon(Fam1.id) .. Icons.Icon({FamName1, type='item', noicon=true})
			rowText = rowText..'||data-sort-value="'..FamName2..'"|'..Icons.Icon({FamName2, type='item', notext=true, size='30'})
			rowText = rowText..'||' .. Icons.getExpansionIcon(Fam2.id) .. Icons.Icon({FamName2, type='item', noicon=true})
			rowText = rowText..'||'..synDesc

			local reqArray = {}
			local reqFam = (Skills.getRecipeLevel(skillID, Fam1) > Skills.getRecipeLevel(skillID, Fam2) and Fam1) or Fam2
			local reqLvl = Skills.getRecipeLevel(skillID, reqFam)
			table.insert(reqArray, Skills.getRecipeRequirementText(skillID, reqFam))
			table.insert(reqArray, FamName1..' Mark Level '..(Fam2.tier + 1))
			table.insert(reqArray, FamName2..' Mark Level '..(Fam1.tier + 1))

			rowText = rowText..'||data-sort-value="'..reqLvl..'"|'..table.concat(reqArray, '<br/>')

			table.insert(rowArray, rowText)
		end
	end

	result = result..'\r\n'..table.concat(rowArray, '\r\n')

	result = result..'\r\n|}'
	return result
end

function p.getSynergyTable(frame)
	local args = frame.args ~= nil and frame.args or frame
	local realmName = args.realm
	local realm = Skills.getRealmFromName(realmName)
	if realm == nil then
		return Shared.printError('Failed to find a realm with name ' .. (realmName or 'nil'))
	end

	local familiarIDs = {}
	for i, recipe in ipairs(SkillData.Summoning.recipes) do
		if Skills.getRecipeRealm(recipe) == realm.id then
			table.insert(familiarIDs, recipe.id)
		end
	end

	return p._getSynergyTable(familiarIDs)
end

function p.getFamiliarSynergyTable(frame)
	local famName = frame.args ~= nil and frame.args[1] or frame
	local familiarID = nil
	local familiarItem = Items.getItem(famName)
	if familiarItem == nil then
		return Shared.printError('Not a valid familiar')
	else
		for i, recipe in ipairs(SkillData.Summoning.recipes) do
			if recipe.productID == familiarItem.id then
				familiarID = recipe.id
				break
			end
		end
		if familiarID == nil then
			return Shared.printError('Not a valid familiar')
		else
			return p._getSynergyTable({ familiarID })
		end
	end
end

function p._getSkillSummoningBonusTable(skill)
	local rowArray = {}
	local famNames = {}

	-- Familiars
	for i, recipe in ipairs(SkillData.Summoning.recipes) do
		local item = Items.getItemByID(recipe.productID)
		if item ~= nil then
			famNames[recipe.id] = item.name
			if item.modifiers ~= nil and not Shared.tableIsEmpty(item.modifiers) then
				local famSkills = Modifiers.getModifierSkills(item.modifiers)
				if Shared.contains(famSkills, skill) then
					table.insert(rowArray, {Fam1 = item.name, FamID1 = item.id, Fam2 = nil, FamID2 = nil, Descrip = Modifiers.getModifiersText(item.modifiers, false)})
				end
			end
		end
	end

	-- Synergies
	for i, syn in ipairs(SkillData.Summoning.synergies) do
		local synSkills = Modifiers.getModifierSkills(syn.modifiers)
		if Shared.contains(synSkills, skill) then
			local FamName1 = famNames[syn.summonIDs[1]] or 'Unknown'
			local FamName2 = famNames[syn.summonIDs[2]] or 'Unknown'
			local synDesc = syn.customDescription
			if synDesc == nil then
				-- Generate description from modifiers
				synDesc = Modifiers.getModifiersText(syn.modifiers, false) or ''
			end
			table.insert(rowArray, {Fam1 = FamName1, FamID1 = syn.summonIDs[1], Fam2 = FamName2, FamID2 = syn.summonIDs[2], Descrip = synDesc})
		end
	end

	if Shared.tableIsEmpty(rowArray) then
		return ''
	end

	local resultPart = {}
	table.insert(resultPart, '{| class="wikitable sortable stickyHeader"')
  	table.insert(resultPart, '\r\n|- class="headerRow-0"')
	table.insert(resultPart, '\r\n!colspan="2"|Familiar 1!!colspan="2"|Familiar 2!!Effect')

	for i, rowItem in ipairs(rowArray) do
		table.insert(resultPart, '\r\n|-')
	    table.insert(resultPart, '\r\n|data-sort-value="'..rowItem.Fam1..'"|'..Icons.Icon({rowItem.Fam1, type='item', notext=true, size='30'}))
		table.insert(resultPart, '||' .. Icons.getExpansionIcon(rowItem.FamID1) .. Icons.Icon({rowItem.Fam1, type='item', noicon=true}))
		if rowItem.Fam2 ~= nil then
			table.insert(resultPart, '||data-sort-value="'..rowItem.Fam2..'"|'..Icons.Icon({rowItem.Fam2, type='item', notext=true, size='30'}))
			table.insert(resultPart, '||' .. Icons.getExpansionIcon(rowItem.FamID2) .. Icons.Icon({rowItem.Fam2, type='item', noicon=true}))
		else
			table.insert(resultPart, '|| || ')
		end
		table.insert(resultPart, '||'..(rowItem.Descrip or ' '))
	end

	table.insert(resultPart, '\r\n|}')
	return table.concat(resultPart)
end

function p.getSkillSummoningBonusTable(frame)
	local skillName = frame.args ~= nil and frame.args[1] or frame
	return p._getSkillSummoningBonusTable(skillName)
end

return p