-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRollRecorder.lua
123 lines (101 loc) · 4.24 KB
/
RollRecorder.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
if not RollRecorderDB then
RollRecorderDB = {}
end
local addonName = "RollRecorder"
local frame = CreateFrame("Frame", addonName)
-- Initialize playerRolls with saved data or an empty table
local playerRolls = {}
local function leftPad(str, length)
return string.format("%-" .. length .. "s", str)
end
-- Function to right pad a string with spaces
local function rightPad(str, length)
return string.format("%" .. length .. "s", str)
end
-- Function to handle system messages
local function OnChatMsgSystem(_, event, msg)
local playerNamePattern = "(%a+)%s+rolls"
local valuePattern = "rolls%s+(%d+)"
local rollBetweenPattern = "%((.-)%)"
local playerName = string.match(msg, playerNamePattern)
local rollValue = tonumber(string.match(msg, valuePattern))
local rollBetween = string.match(msg, rollBetweenPattern)
if event == "CHAT_MSG_SYSTEM" and msg:find("rolls") then
if playerName and rollValue and rollBetween == '1-100' then
-- Check if the player is already in the table
if not playerRolls[playerName] then
playerRolls[playerName] = { total = 0, count = 0, rolls = {"","","","",""} }
end
-- Record the roll
playerRolls[playerName].total = playerRolls[playerName].total + tonumber(rollValue)
playerRolls[playerName].count = playerRolls[playerName].count + 1
-- Store the last 5 rolls
table.insert(playerRolls[playerName].rolls, rollValue)
if #playerRolls[playerName].rolls > 5 then
table.remove(playerRolls[playerName].rolls, 1)
end
-- Debugging messages
--print("Total Rolls for " .. rightPad(playerName,14) .. ": " .. rightPad(playerRolls[playerName].count,6) .. " Average for " .. playerName .. ": " .. playerRolls[playerName].total / playerRolls[playerName].count)
end
end
end
-- Function to handle ADDON_LOADED event
local function ADDON_LOADED_Tasks()
--print("Hello")
-- Load saved data for each player individually
for playerName, data in pairs(RollRecorderDB) do
playerRolls[playerName] = data
end
end
-- Function to handle PLAYER_LOGOUT event
local function LogOutTasks()
-- Save data on logout for each player individually
for playerName, data in pairs(playerRolls) do
RollRecorderDB[playerName] = data
end
end
-- Function to display results
local function displayResults()
-- Create a frame
local myFrame = CreateFrame("Frame", "MyAddonFrame", UIParent, "BasicFrameTemplateWithInset");
myFrame:SetSize(300, 200);
myFrame:SetPoint("CENTER", UIParent, "CENTER");
myFrame:SetMovable(true);
myFrame:EnableMouse(true);
myFrame:RegisterForDrag("LeftButton");
myFrame:SetScript("OnDragStart", myFrame.StartMoving);
myFrame:SetScript("OnDragStop", myFrame.StopMovingOrSizing);
-- Add a title
myFrame.title = myFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlight");
myFrame.title:SetPoint("CENTER", myFrame.TitleBg, "LEFT", 125, 0);
myFrame.title:SetText("History Avg Count Last 5 Rolls");
-- Add content based on playerRolls data, including the last 5 rolls
local contentText = ""
for playerName, data in pairs(playerRolls) do
local average = string.format("%.3f", data.total / data.count) or 0
contentText = contentText .. leftPad(playerName,14) .. leftPad(average,10) .. leftPad(data.count,13) .. table.concat(data.rolls, ", ") .. "\n"
end
myFrame.content = myFrame:CreateFontString(nil, "OVERLAY", "GameFontNormal");
myFrame.content:SetPoint("TOPLEFT", myFrame, "TOPLEFT", 10, -30);
myFrame.content:SetText(contentText);
-- Show the frame
myFrame:Show();
end
-- Register events
frame:RegisterEvent("CHAT_MSG_SYSTEM")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGOUT")
-- Register slash command
SLASH_ROLLRECORDER1 = "/RollRecorder"
SlashCmdList["ROLLRECORDER"] = function()
displayResults()
end
frame:SetScript("OnEvent", function(self, event, msg)
if event == "CHAT_MSG_SYSTEM" then
OnChatMsgSystem(self, event, msg)
elseif event == "ADDON_LOADED" then
ADDON_LOADED_Tasks()
elseif event == "PLAYER_LOGOUT" then
LogOutTasks()
end
end)