Module:Prayer

Revision as of 19:36, 3 December 2021 by Auron956 (talk | contribs) (_getPrayerEffect: Support listing enemy modifiers)

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

local p = {}

local SkillData = mw.loadData('Module:Skills/data')

local Shared = require('Module:Shared')
local Icons = require('Module:Icons')
local Constants = require('Module:Constants')
local Items = require('Module:Items')
local ItemSources = require('Module:Items/SourceTables')

function p.getPrayerByID(id)
  local result = Shared.clone(SkillData.Prayer[id + 1])
  if result ~= nil and result.id == nil then result.id = id end
  return result
end

function p.getPrayer(name)
  local result = nil
  for i, prayer in pairs(SkillData.Prayer) do
    local prayName = prayer.name
    if prayName == name then
      result = Shared.clone(prayer)
      result.id = i - 1
      break
    end
  end
  return result
end

function p.getPrayers(checkFunc)
	local result = {}
	for i, prayer in ipairs(SkillData.Prayer) do
		if checkFunc(prayer) then
			table.insert(result, prayer)
		end
	end
	return result
end

function p.getPrayerCost(prayer)
  local costLines = {}
  if prayer.pointsPerPlayer > 0 then
    local pluralString = prayer.pointsPerPlayer > 1 and 's' or ''
    table.insert(costLines, prayer.pointsPerPlayer..' Prayer Point'..pluralString..' per player attack')
  end
  if prayer.pointsPerEnemy > 0 then
    local pluralString = prayer.pointsPerEnemy > 1 and 's' or ''
    table.insert(costLines, prayer.pointsPerEnemy..' Prayer Point'..pluralString..' per enemy attack')
  end
  if prayer.pointsPerRegen > 0 then
    local pluralString = prayer.pointsPerRegen > 1 and 's' or ''
    table.insert(costLines, prayer.pointsPerRegen..' Prayer Point'..pluralString..' when health regenerates')
  end
  return table.concat(costLines, '<br/>')
end

function p.formatEffectLine(var, val)
  if var == 'prayerBonusDefence' then
    return '+'..val..'% [[Combat#Melee Evasion Rating|Melee Evasion Rating]]'
  elseif var == 'prayerBonusStrength' then
    return '+'..val..'% [[Combat#Melee Max Hit|Melee Strength]]'
  elseif var == 'prayerBonusAttack' then
    return '+'..val..'% [[Combat#Melee Accuracy Rating|Melee Accuracy Rating]]'
  elseif var == 'prayerBonusDefenceRanged' then
    return '+'..val..'% [[Combat#Ranged Evasion Rating|Ranged Evasion Rating]]'
  elseif var == 'prayerBonusAttackRanged' then
    return '+'..val..'% [[Combat#Ranged Accuracy Rating|Ranged Accuracy Rating]]'
  elseif var == 'prayerBonusStrengthRanged' then
    return '+'..val..'% [[Combat#Ranged Max Hit|Ranged Strength]]'
  elseif var == 'prayerBonusDefenceMagic' then
    return '+'..val..'% [[Combat#Magic Evasion Rating|Ranged Evasion Rating]]'
  elseif var == 'prayerBonusAttackMagic' then
    return '+'..val..'% [[Combat#Magic Accuracy Rating|Magic Accuracy Rating]]'
  elseif var == 'prayerBonusDamageMagic' then
    return '+'..val..'% [[Combat#Magic Max Hit|Magic Damage]]'
  elseif var == 'prayerBonusHitpoints' then
    return val..'x Restore Rate for [[Hitpoints]]'
  elseif var == 'prayerBonusProtectItem' then
    return 'Prevents losing an item upon [[Death]]'
  elseif var == 'prayerBonusProtectFromMelee' then
    return '85% chance to dodge Melee attacks'
  elseif var == 'prayerBonusProtectFromRanged' then
    return '85% chance to dodge Ranged attacks'
  elseif var == 'prayerBonusProtectFromMagic' then
    return '85% chance to dodge Magic attacks'
  elseif var == 'prayerBonusHitpointHeal' then
    return 'Heal +'..val..'% HP when HP falls below 10%'
  elseif var == 'prayerBonusDamageReduction' then
    return '+'..val..'% [[Damage Reduction]]'
  else
    return '+'..val..' of unknown bonus '..var..'[[Category:Unknown Prayer Bonus]]'
  end
end

function p._getPrayerEffect(prayer, asList)
  if asList == nil then asList = false end
  local chr = asList and '* ' or ''
  local bonusLines = {}

  if type(prayer.modifiers) == 'table' then
    for bonusKey, bonusVal in Shared.skpairs(prayer.modifiers) do
      table.insert(bonusLines, chr .. Constants._getModifierText(bonusKey, bonusVal, false))
    end
  end
  
  if type(prayer.enemyModifiers) == 'table' then
    for bonusKey, bonusVal in Shared.skpairs(prayer.enemyModifiers) do
  	  table.insert(bonusLines, chr .. 'Gives the enemy: ' .. Constants._getModifierText(bonusKey, bonusVal, false))
    end
  end

  --If there are no actual effects, just use the prayer's description  
  if Shared.tableCount(prayer.modifiers) == 0 then
    table.insert(bonusLines, chr..prayer.description)
  end

  if prayer.pointsPerPlayer > 0 then
    local val = 0.1 * prayer.pointsPerPlayer
    table.insert(bonusLines, chr.."+"..Shared.round(val, 1, 1).." Prayer XP per damage done")
  end
  if asList then
    return table.concat(bonusLines, '\r\n')
  else
    return table.concat(bonusLines, '<br/>')
  end
end

function p.getPrayerEffect(frame)
  local prayerName = frame.args ~= nil and frame.args[1] or frame[1]
  local asListTxt = frame.args ~= nil and frame.args[2] or frame[2]
  local asList = asListTxt ~= nil and asListTxt ~= 'false' and asListTxt ~= 'no'
  local prayer = p.getPrayer(prayerName)
  if prayer == nil then
    return "ERROR: Could not find a prayer named "..prayerName
  end

  return p._getPrayerEffect(prayer, asList)
end

function p._getPrayerStat(prayer, statName)
  if statName == "prayerCost" then
    return p.getPrayerCost(prayer)
  elseif statName == "prayerEffect" then
    return p._getPrayerEffect(prayer)
  elseif statName == "prayerEffectList" then
    return p.getPrayerEffect(prayer, true)
  elseif statName == 'prayerLevel' then
    return Icons._SkillReq('Prayer', prayer['prayerLevel'])
  else
    return prayer[statName]
  end
end

function p.getPrayerStat(frame)
  local prayerName = frame.args ~= nil and frame.args[1] or frame[1]
  local statName = frame.args ~= nil and frame.args[2] or frame[2]
  local prayer = p.getPrayer(prayerName)
  if prayer == nil then
    return "ERROR: Could not find a prayer named "..prayerName
  end

  return p._getPrayerStat(prayer, statName)
end

function p._getPrayerCategories(prayer)
  return '[[Category:Prayers]]'
end

function p.getPrayerCategories(frame)
  local prayerName = frame.args ~= nil and frame.args[1] or frame[1]
  local prayer = p.getPrayer(prayerName)
  if prayer == nil then
    return "ERROR: Could not find a prayer named "..prayerName
  end

  return p._getPrayerCategories(prayer)
end

function p.getPrayerTable(frame)
  local result = '{| class="wikitable sortable stickyHeader"'
  result = result..'\r\n|-class=headerRow-0'
  result = result..'\r\n!colspan="2"|Prayer!!'..Icons.Icon({"Prayer", type="skill", notext=true})..' Lvl'
  result = result..'!!Effects!!Point Cost'
  
  local prayerList = Shared.clone(SkillData.Prayer)
  table.sort(prayerList, function(a, b)
                           if a.prayerLevel == b.prayerLevel then
                             return a.name < b.name
                           else
                           	 return a.prayerLevel < b.prayerLevel
                           end
                         end)

  for i, prayer in ipairs(prayerList) do
    result = result..'\r\n|-'
    result = result..'\r\n|'..Icons.Icon({prayer.name, type='prayer', notext=true, size='50'})
    result = result..'||'..Icons.Icon({prayer.name, type='prayer', noicon=true})..'||'..prayer.prayerLevel
    result = result..'||'..p._getPrayerEffect(prayer)
    result = result..'||'..p.getPrayerCost(prayer)
  end

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

function p.getBonesTable(frame)
  local result = '{| class="wikitable sortable stickyHeader"'
  result = result..'\r\n|- class="headerRow-0"'
  result = result..'\r\n!colspan="2"|Bone!!Prayer Points!!Sources'
  
  local itemArray = Items.getItems(function(item) return item.prayerPoints ~= nil and item.prayerPoints > 0 end)

  table.sort(itemArray, function(a, b) return a.prayerPoints < b.prayerPoints end)

  for i, item in Shared.skpairs(itemArray) do
    result = result..'\r\n|-'
    result = result..'\r\n|'..Icons.Icon({item.name, type='item', notext=true, size='50'})
    result = result..'||'..Icons.Icon({item.name, type='item', noicon=true})
    result = result..'||style="text-align:right;"|'..item.prayerPoints
    result = result..'||'..ItemSources._getItemSources(item, false, false)
  end

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

  return result
end

return p