AltSystem/Roll.lua

85 lines
2.9 KiB
Lua

-- AltSystem Roll Logic
-- Handles /roll 20, captures the result, and applies modifiers.
AltSystem = AltSystem or {}
local pendingRollType = nil -- "attack" or "defense"
-- Perform a roll: triggers the WoW native /roll 20 command
function AltSystem:PerformRoll(rollType)
pendingRollType = rollType
RandomRoll(1, 20)
end
-- Listen for the system message that contains the roll result
local rollListener = CreateFrame("Frame")
rollListener:RegisterEvent("CHAT_MSG_SYSTEM")
rollListener:SetScript("OnEvent", function(self, event, message)
if not pendingRollType then return end
-- Match the roll result pattern: "PlayerName rolls X (1-20)"
local roll = message:match("rolls (%d+) %(1%-20%)")
if not roll then return end
local rollValue = tonumber(roll)
local rollType = pendingRollType
pendingRollType = nil
AltSystem:CalculateAndDisplayResult(rollType, rollValue)
end)
-- Calculate the final result based on the roll type and selected modifiers
function AltSystem:CalculateAndDisplayResult(rollType, rollValue)
local state = AltSystem.State
local skill = AltSystem.Data.Skills[state.selectedSkillIndex]
local item = AltSystem.Data.Items[state.selectedItemIndex]
local skillMod = skill and skill.modifier or 0
local itemMod = item and item.modifier or 0
local total = rollValue
local breakdown = "Roll: " .. rollValue
if rollType == "attack" then
-- Attack Roll = roll + skill modifier + item modifier
total = rollValue + skillMod + itemMod
breakdown = breakdown .. "\nSkill: " .. FormatModifier(skillMod)
if itemMod ~= 0 then
breakdown = breakdown .. " | Item: " .. FormatModifier(itemMod)
end
breakdown = breakdown .. "\n|cffffd100Attack Total: " .. total .. "|r"
elseif rollType == "defense" then
-- Defense Roll = roll + skill modifier + item modifier + defense modifier + shield modifier
local defense = AltSystem.Data.Defenses[state.selectedDefenseIndex]
local defenseMod = defense and defense.modifier or 0
local shieldMod = state.shieldEnabled and AltSystem.Data.ShieldModifier or 0
total = rollValue + skillMod + itemMod + defenseMod + shieldMod
breakdown = breakdown .. "\nSkill: " .. FormatModifier(skillMod)
if itemMod ~= 0 then
breakdown = breakdown .. " | Item: " .. FormatModifier(itemMod)
end
breakdown = breakdown .. "\nDefense: " .. FormatModifier(defenseMod)
if shieldMod ~= 0 then
breakdown = breakdown .. " | Shield: " .. FormatModifier(shieldMod)
end
breakdown = breakdown .. "\n|cff00ccffDefense Total: " .. total .. "|r"
end
if AltSystem.ResultText then
AltSystem.ResultText:SetText(breakdown)
end
end
-- Format a modifier value with sign
function FormatModifier(value)
if value >= 0 then
return "+" .. value
else
return tostring(value)
end
end