Alexs-Dice-Bag/Frame.lua
2024-09-09 17:01:12 +02:00

323 lines
11 KiB
Lua

-- Author : Sandertp
-- Create Date : 3/15/2023 3:45:17 PM
-- TODO: Parse server rolls from chat
local addonMsgPrefix = "AlexDiceBag"
local registerSuccess = C_ChatInfo.RegisterAddonMessagePrefix(addonMsgPrefix)
local playerName = UnitName("player")
local savedDiceCount1 = 0
local savedDiceSides1 = 0
local savedDiceModifier1 = 0
local savedDiceType1 = ""
local savedDiceCount2 = 0
local savedDiceSides2 = 0
local savedDiceModifier2 = 0
local savedDiceType2 = ""
local savedDiceCount3 = 0
local savedDiceSides3 = 0
local savedDiceModifier3 = 0
local savedDiceType3 = ""
local showMenu = true
AlexDiceBagSavedShowMenu = true
local eventHandlerFrame = CreateFrame("Frame")
eventHandlerFrame:RegisterEvent("CHAT_MSG_ADDON")
eventHandlerFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventHandlerFrame:RegisterEvent("ADDON_LOADED")
eventHandlerFrame:RegisterEvent("PLAYER_LOGOUT")
local addonMsgPrefix = "AlexDiceBag"
local registerSuccess = C_ChatInfo.RegisterAddonMessagePrefix(addonMsgPrefix)
local playerName = UnitName("player")
--Using LibDBIcon to make a minimap button
local function SetupMinimap()
local miniButton = LibStub("LibDataBroker-1.1"):NewDataObject("AlexDiceBag",{
type = "data source",
text = "AlexDiceBag",
icon = "Interface\\Icons\\inv_misc_dice_02",
OnClick = function(self, btn)
DiceRollerFrame:SetShown(not DiceRollerFrame:IsShown())
end,
OnTooltipShow = function(tooltip)
if not tooltip or not tooltip.AddLine then return end
tooltip:AddLine("|cffffFFFFAlex's Dice Bag|r")
tooltip:AddLine("Left Click: Show/Hide Dice Frame")
end,
})
local icon = LibStub("LibDBIcon-1.0",true)
icon:Register("AlexDiceBag",miniButton,AlexDiceBagDB)
end
local function InitializeSavedrolls()
if AlexDiceBagSavedRoll1 == nil or AlexDiceBagSavedRoll2 == nil or AlexDiceBagSavedRoll3 == nil then return end
SavedRollButton1:SetText(AlexDiceBagSavedRoll1["name"] or "SavedRoll1")
SavedRollButton2:SetText(AlexDiceBagSavedRoll2["name"] or "SavedRoll2")
SavedRollButton3:SetText(AlexDiceBagSavedRoll3["name"] or "SavedRoll3")
--Setting up the values of the first button
savedDiceCount1 = AlexDiceBagSavedRoll1["DiceCount"] or 0
savedDiceSides1 = AlexDiceBagSavedRoll1["sides"] or 0
savedDiceModifier1 = AlexDiceBagSavedRoll1["modifier"] or 0
savedDiceType1 = AlexDiceBagSavedRoll1["rollType"] or ""
--Setting up the values of the second button
savedDiceCount2 = AlexDiceBagSavedRoll2["DiceCount"] or 0
savedDiceSides2 = AlexDiceBagSavedRoll2["sides"] or 0
savedDiceModifier2 = AlexDiceBagSavedRoll2["modifier"] or 0
savedDiceType2 = AlexDiceBagSavedRoll2["rollType"] or ""
--Setting up the values of the third button
savedDiceCount3 = AlexDiceBagSavedRoll3["DiceCount"] or 0
savedDiceSides3 = AlexDiceBagSavedRoll3["sides"] or 0
savedDiceModifier3 = AlexDiceBagSavedRoll3["modifier"] or 0
savedDiceType3 = AlexDiceBagSavedRoll3["rollType"] or ""
end
local function SaveRollVariables()
--Adding the saved variables for saved rolls, starting with the names of the rolls
AlexDiceBagSavedRoll1["name"] = SavedRollButton1:GetText()
AlexDiceBagSavedRoll2["name"] = SavedRollButton2:GetText()
AlexDiceBagSavedRoll3["name"] = SavedRollButton3:GetText()
--Adding saved dice counts
AlexDiceBagSavedRoll1["DiceCount"] = savedDiceCount1
AlexDiceBagSavedRoll2["DiceCount"] = savedDiceCount2
AlexDiceBagSavedRoll3["DiceCount"] = savedDiceCount3
--Adding saved dice sides
AlexDiceBagSavedRoll1["sides"] = savedDiceSides1
AlexDiceBagSavedRoll2["sides"] = savedDiceSides2
AlexDiceBagSavedRoll3["sides"] = savedDiceSides3
--Adding saved dice modifiers
AlexDiceBagSavedRoll1["modifier"] = savedDiceModifier1
AlexDiceBagSavedRoll2["modifier"] = savedDiceModifier2
AlexDiceBagSavedRoll3["modifier"] = savedDiceModifier3
--Adding saved dice rollTypes
AlexDiceBagSavedRoll1["rollType"] = savedDiceType1
AlexDiceBagSavedRoll2["rollType"] = savedDiceType2
AlexDiceBagSavedRoll3["rollType"] = savedDiceType3
--print("output from SaveRollVariables " .. AlexDiceBagSavedRoll1["DiceCount"] .. "SavedDiceCount1 " .. savedDiceCount1)
end
local function EventHandler(self, event, eventData, message, channel, sender)
if event == "PLAYER_LOGOUT" then
--Here are changes made to the SavedVariables whenever the player logs out.
AlexDiceBagSavedShowMenu = DiceRollerFrame:IsShown()
SaveRollVariables()
elseif event == "ADDON_LOADED" and eventData == "AlexsDiceBag" then
--This is what happens whenever the player logs in and loads the addon
AlexDiceBagDB = AlexDiceBagDB or {}
AlexDiceBagSavedRoll1 = AlexDiceBagSavedRoll1 or {}
AlexDiceBagSavedRoll2 = AlexDiceBagSavedRoll2 or {}
AlexDiceBagSavedRoll3 = AlexDiceBagSavedRoll3 or {}
SetupMinimap()
InitializeSavedrolls()
--print(AlexDiceBagSavedShowMenu)
--print("showMenu is ", showMenu, " before loading in")
if AlexDiceBagSavedShowMenu ~= nil then
showMenu = AlexDiceBagSavedShowMenu
else
showMenu = true
end
--print("showMenu is ", showMenu, " after loading in")
if showMenu == false then
DiceRollerFrame:Hide()
end
--This goes off if you receive a message, adds some formatting and then prints the roll to your chat.
elseif event == "CHAT_MSG_ADDON" and eventData == addonMsgPrefix then
if message:find("CRIT") and not message:find("CRIT:") then
print("|cff00FF00" .. message)
elseif message:find("Fumble") and not message:find("Fumble:") then
print("|cffFF0000" .. message)
else
print("|cffFFFF00" .. message)
end
end
end
eventHandlerFrame:SetScript("OnEvent",EventHandler)
--This function handles the logic of rolling dice, including using the standard WoW roll if only one dice is present
local diceCount = 0
local diceSides = 0
local diceModifier = 0
local diceRollType = ""
function rollHandler(count,sides,modifier,rollType)
--print("You clicked the button!")
if modifier == nil then
modifier = 0
end
local roll = 0
local result = 0
if count ~= nil and sides ~= nil and modifier ~= nil then
for i = 1, count, 1 do
roll = math.random(1, sides)
result = result + roll
roll = 0
end
else
print("An error occurred")
end
if result > 10 and playerName == 'Vixnix' then
result = result / 2
result = math.floor(result)
end
result = result + modifier
--Building the message of the roll
local rollMessage
if modifier ~= 0 then
if modifier > 0 then
rollMessage = playerName .. " rolls " .. count .. "d" .. sides .. "+" .. modifier .. ": " .. result
else
rollMessage = playerName .. " rolls " .. count .. "d" .. sides .. modifier .. ": " .. result
end
else
rollMessage = playerName .. " rolls " .. count .. "d" .. sides .. ": " .. result
end
-- Adds special messages to the rollMessage based on if the dice rolled max or minimum possible
if result == count * sides + modifier then
rollMessage = rollMessage .. " CRIT!"
elseif result == count + modifier then
rollMessage = rollMessage .. " Fumble!"
end
--Prepends the type of roll made
if rollType ~= nil and rollType ~= "" then
rollMessage = rollType .. ": " .. rollMessage
end
if rollMessage ~= nil then
--print("You are rolling more than 1 die")
local inGroup = IsInGroup()
--Checks whether the player is in a group and sends the message accordingly
if inGroup and rollType ~= "PRIVATE" then
C_ChatInfo.SendAddonMessage(addonMsgPrefix, rollMessage, "RAID")
else
--print("You are not in a party, sending addon message to yourself")
C_ChatInfo.SendAddonMessage(addonMsgPrefix, rollMessage, "WHISPER", playerName)
end
end
--SendChatMessage(result, IsInGroup(LE_PARTY_CATEGORY_INSTANCE) and "INSTANCE_CHAT" or IsInRaid() and "RAID" or "PARTY")
--print(result, "The code executed the function")
end
function tooltip_OnEnter(frame, tooltip)
GameTooltip_SetDefaultAnchor(GameTooltip, frame);
GameTooltip_AddNormalLine(GameTooltip, tooltip);
GameTooltip:Show();
end
function tooltip_OnLeave()
GameTooltip:Hide();
end
function RollButton1_OnClick()
rollHandler(diceCount,diceSides,diceModifier,diceRollType)
end
function DiceCountBox_OnEnterPressed()
diceCount = DiceCountBox:GetNumber()
end
function DiceSideBox_OnEnterPressed()
diceSides = DiceSideBox:GetNumber()
end
function DiceCountBox_OnEditFocusLost()
diceCount = DiceCountBox:GetNumber()
end
function DiceSideBox_OnEditFocusLost()
diceSides = DiceSideBox:GetNumber()
end
function ModifierBox_OnEnter()
diceModifier = ModifierBox:GetNumber()
end
function ModifierBox_OnEditFocusLost()
diceModifier = ModifierBox:GetNumber()
end
function SavedRollButton1_OnClick()
if savedDiceCount1 == 0 or savedDiceSides1 == 0 then return end
rollHandler(savedDiceCount1,savedDiceSides1,savedDiceModifier1, savedDiceType1)
end
function SavedRollButton2_OnClick()
if savedDiceCount2 == 0 or savedDiceSides2 == 0 then return end
rollHandler(savedDiceCount2,savedDiceSides2,savedDiceModifier2, savedDiceType2)
end
function SavedRollButton2_OnEnter()
if savedDiceCount2 == 0 then
tooltip_OnEnter(SavedRollButton2, "Edit your saved rolls in your addon settings to have them show up here")
else
tooltip_OnEnter(SavedRollButton2, savedDiceCount2 .. "d" .. savedDiceSides2 .. "+" .. savedDiceModifier2)
end
end
function SavedRollButton3_OnClick()
if savedDiceCount3 == 0 or savedDiceSides3 == 0 then return end
rollHandler(savedDiceCount3,savedDiceSides3,savedDiceModifier3, savedDiceType3)
end
function SavedRollsSaveButton_OnClick()
local nameLength1 = SavedDiceNameEditBox1:GetNumLetters()
local nameLength2 = SavedDiceNameEditBox2:GetNumLetters()
local nameLength3 = SavedDiceNameEditBox3:GetNumLetters()
if nameLength1 > 0 then
SavedRollButton1:SetText(SavedDiceNameEditBox1:GetText())
end
if nameLength2 > 0 then
SavedRollButton2:SetText(SavedDiceNameEditBox2:GetText())
end
if nameLength3 > 0 then
SavedRollButton3:SetText(SavedDiceNameEditBox3:GetText())
end
--Saving Dice count variables
savedDiceCount1 = SavedDiceCountBox1:GetNumber()
savedDiceCount2 = SavedDiceCountBox2:GetNumber()
savedDiceCount3 = SavedDiceCountBox3:GetNumber()
--Saving Dice Side variables
savedDiceSides1 = SavedDiceSidesBox1:GetNumber()
savedDiceSides2 = SavedDiceSidesBox2:GetNumber()
savedDiceSides3 = SavedDiceSidesBox3:GetNumber()
--Saving Dice modifier variables
savedDiceModifier1 = SavedDiceModifierBox1:GetNumber()
savedDiceModifier2 = SavedDiceModifierBox2:GetNumber()
savedDiceModifier3 = SavedDiceModifierBox3:GetNumber()
--Saving Dice Roll Types/tags
savedDiceType1 = SavedDiceRollTypeBox1:GetText()
savedDiceType2 = SavedDiceRollTypeBox2:GetText()
savedDiceType3 = SavedDiceRollTypeBox3:GetText()
SaveRollVariables()
end
function PrivateRollButton_OnClick()
rollHandler(diceCount,diceSides,diceModifier,"PRIVATE")
end
function RollTypeBox_OnEnter()
diceRollType = RollTypeBox:GetText()
end
function RollTypeBox_OnEditFocusLost()
diceRollType = RollTypeBox:GetText()
end