From 3cd06c52f10c468a5996835be574b12568f036f2 Mon Sep 17 00:00:00 2001 From: Jelle Meeus Date: Mon, 5 Aug 2024 16:02:44 +0200 Subject: [PATCH] basic sync data --- DKPClient.lua | 144 ++++++++++++++++++++++++++++++++++++++++++++++++++ DKPServer.lua | 94 ++++++++++++++++++++++++++++++++ dkp.lua | 36 ------------- 3 files changed, 238 insertions(+), 36 deletions(-) create mode 100644 DKPClient.lua create mode 100644 DKPServer.lua delete mode 100644 dkp.lua diff --git a/DKPClient.lua b/DKPClient.lua new file mode 100644 index 0000000..5f58d70 --- /dev/null +++ b/DKPClient.lua @@ -0,0 +1,144 @@ +local ADDON_NAME = "AIODKP" +local AIO = AIO or require("AIO") +if AIO.AddAddon() then + return +end +local DKPHandlers = {} +local DKP = {} +AIO.AddHandlers(ADDON_NAME, DKPHandlers) + +-- local find, format, gmatch, gsub, tolower, match, toupper, join, split, trim = string.find, string.format, string.gmatch, string.gsub, string.lower, string.match, string.upper, string.join, string.split, string.trim + +DKP.Config = { + addonMsgPrefix = "|TInterface/MoneyFrame/UI-GoldIcon:14:14:2:0|t|cfffff800DKP|r", +} + +local Status = { + PENDING = 1, + BIDDING = 2, + ASSIGNED = 3, +} + +local Separator = { + ARG = ";", + ELEMENT = "+", + LIST_ELEMENT = "^", + MESSAGE = "&", + SUBLIST_ELEMENT = "/", +} + +-- print with addon prefix +function DKP.print(message) + print(DKP.Config.addonMsgPrefix .. " " .. message) +end +function DKP.Error(message) + print(DKP.Config.addonMsgPrefix .. " ERROR: " .. message) +end + +local frame = CreateFrame("Frame", "DKPFrame", UIParent) +frame:SetSize(800, 500) +frame:SetPoint("CENTER") +frame:SetToplevel(true) +frame:SetClampedToScreen(true) + +-- This enables saving of the position of the frame over reload of the UI or restarting game +AIO.SavePosition(frame) + +-- baseframe: Enable dragging +frame:RegisterForDrag("LeftButton") +frame:SetMovable(true) +frame:EnableMouse(true) +frame:SetScript("OnDragStart", frame.StartMoving) +frame:SetScript("OnHide", frame.StopMovingOrSizing) +frame:SetScript("OnDragStop", frame.StopMovingOrSizing) + +-- baseframe: Close button +frame.closeButton = CreateFrame("Button", nil, frame, "UIPanelCloseButton") +frame.closeButton:SetPoint("TOPRIGHT", frame, "TOPRIGHT") +frame.closeButton:SetScript("OnClick", function(self) + frame:Hide() +end) +-- baseframe: title +frame.title = frame:CreateFontString(nil, "OVERLAY") +frame.title:SetFontObject("GameFontNormalMed3") +frame.title:SetPoint("TOP", frame, "TOP", 0, -15) +frame.title:SetText("DKP") +frame:SetPoint("CENTER") +frame:SetToplevel(true) +frame:SetClampedToScreen(true) +-- baseframe: Set background +frame:SetBackdrop({ + bgFile = "Interface/DialogFrame/UI-DialogBox-Background", + edgeFile = "Interface/DialogFrame/UI-DialogBox-Border", + tile = true, tileSize = 32, edgeSize = 32, + insets = { left = 11, right = 12, top = 12, bottom = 11 } +}) +frame:SetBackdropColor(0, 0, 0, 1) + +-- Handlers +function DKPHandlers.ShowFrame(player) + frame:Show() +end + +function DKP.Split(str, sep) + print("DKP.Split") + local t = {} + for s in str:gmatch("([^"..sep.."]+)") do + table.insert(t, s) + end + return t +end + +local Item = {} +Item.__index = Item +DKP.Item = Item +function Item:CreateForId(itemId) + itemId = tonumber(itemId) + print("Item:CreateForId: ", itemId) + local item = {id = itemId, isBound = false} + setmetatable(item, Item) + item:PopulateStaticProperties() + return item +end + +function Item:PopulateStaticProperties() + print("Item:PopulateStaticProperties") + print(self.id) + self.name, self.link, self.quality, self.ilvl, self.minLevel, self.itemType, self.itemSubType, _, self.equipLoc, self.texture, _, self.classId, self.subclassId = GetItemInfo(self.id) + if not self.name then + DKP:Error("Invalid itemId", self.id) + end +end + +function DKP.DecodeRow(encodedStr) + DKP.print("DKP.DecodeRow") + print(encodedStr) + local elements = DKP.Split(encodedStr, Separator.LIST_ELEMENT) + local item = Item:CreateForId(elements[2]) + local row = { + id = tonumber(elements[1]), + status = tonumber(elements[3]), + } + item:PopulateStaticProperties() + row.item = item + return row +end + +function DKPHandlers.SyncResponse(player, encodedItems) + DKP.print("SyncResponse") + print(encodedItems) + local splitRows = DKP.Split(encodedItems, Separator.ELEMENT) + DKP.rows = {} + for _, row in pairs(splitRows) do + print("Row: ", row) + local decodedRow = DKP.DecodeRow(row) + table.insert(DKP.rows, decodedRow) + print(decodedRow.item.link) + end + -- print + for _, row in pairs(DKP.rows) do + DKP.print(string.format("%d %s", row.id, row.item.link)) + end + frame:Show() +end + diff --git a/DKPServer.lua b/DKPServer.lua new file mode 100644 index 0000000..b142f83 --- /dev/null +++ b/DKPServer.lua @@ -0,0 +1,94 @@ + +local ADDON_NAME = "AIODKP" +local AIO = AIO or require("AIO") +local DKPHandlers = {} +local DKP = {} +AIO.AddHandlers(ADDON_NAME, DKPHandlers) + +local PLAYER_EVENT_ON_STORE_NEW_ITEM = 53 +local PLAYER_EVENT_ON_COMMAND = 42 -- (event, player, command, chatHandler) - player is nil if command used from console. Can return false + +local Blacklist = { + [47241]=true, -- Emblem of Triumph +} + +local Status = { + PENDING = 1, + BIDDING = 2, + ASSIGNED = 3, +} + +local Separator = { + ARG = ";", + ELEMENT = "+", + LIST_ELEMENT = "^", + MESSAGE = "&", + SUBLIST_ELEMENT = "/", +} + +local sessions = {} + +local function OnPlayerEventOnStoreNewItem(event, player, item, count) + print("OnPlayerEventOnStoreNewItem") + print(player:GetName() .. " won " .. item:GetName() .. " (x" .. count .. ")") + -- check if item is on blacklist + if Blacklist[item:GetEntry()] then print("OnPlayerEventOnStoreNewItem:blacklist") return end + -- add item to session tied to instance + local instanceId = player:GetInstanceId() + -- get session from instanceId + sessions[instanceId] = sessions[instanceId] or {} -- new session if not exists + sessions[instanceId].rows = sessions[instanceId].rows or {} -- new rows if not exists + local rowId = #sessions[instanceId].rows+1 + local itemRow = {id=rowId, item=item, itemId=item:GetEntry(), guid=item:GetGUIDLow(), status=Status.PENDING} + table.insert(sessions[instanceId].rows, rowId, itemRow) + print(string.format("Added item (Entry: %d, GUIDLow: %d) to session instanceId %d", item:GetEntry(), item:GetGUIDLow(), instanceId)) + -- remove item from player + item:SaveToDB() -- Save item to DB before we remove it from the player + player:SaveToDB() -- must be called before RemoveItem else crash + player:RemoveItem(item, count) +end + +RegisterPlayerEvent(PLAYER_EVENT_ON_STORE_NEW_ITEM, OnPlayerEventOnStoreNewItem) + +function DKP.EncodeRow(row) + return table.concat({row.id, row.itemId, row.status}, Separator.LIST_ELEMENT) +end + +function DKP.EncodeSession(session) + local encodedRows = {} + local rows = session.rows or {} + for _, row in pairs(rows) do + local encodedRow = DKP.EncodeRow(row) + table.insert(encodedRows, encodedRow) + end + return table.concat(encodedRows, Separator.ELEMENT) +end + +function DKPHandlers.RequestSync(player) + PrintInfo(string.format("%s:DKPHandlers.RequestSync(player) by account-name (%d-%s)", ADDON_NAME, player:GetAccountId(), player:GetName())) + local instanceId = player:GetInstanceId() + local session = sessions[instanceId] or {} -- new session if not exists + local encodedSession = DKP.EncodeSession(session) + AIO.Handle(player, ADDON_NAME, "SyncResponse", encodedSession) +end + +local function OnCommand(event, player, command) + PrintInfo(string.format("%s:OnCommand %s by account-name (%d-%s)", ADDON_NAME, command, player:GetAccountId(), player:GetName())) + if command == "dkp" then + PrintInfo(string.format("%s:OnCommand .dkp by account-name (%d-%s)", ADDON_NAME, player:GetAccountId(), player:GetName())) + AIO.Handle(player, ADDON_NAME, "ShowFrame") + return false + end + if command == "dkpopen" then + PrintInfo(string.format("%s:OnCommand .dkpopen by account-name (%d-%s)", ADDON_NAME, player:GetAccountId(), player:GetName())) + -- DKPHandlers.RequestPayout(player, true) + return false + end + if command == "dkpsync" then + PrintInfo(string.format("%s:OnCommand .dkpsync by account-name (%d-%s)", ADDON_NAME, player:GetAccountId(), player:GetName())) + DKPHandlers.RequestSync(player) + return false + end +end + +RegisterPlayerEvent(PLAYER_EVENT_ON_COMMAND, OnCommand) diff --git a/dkp.lua b/dkp.lua deleted file mode 100644 index ca72155..0000000 --- a/dkp.lua +++ /dev/null @@ -1,36 +0,0 @@ - -local PLAYER_EVENT_ON_STORE_NEW_ITEM = 53 - -local isEntryOnBlacklist = { - [47241]=true, -- Emblem of Triumph -} - -local Status = { - PENDING = 1, - BIDDING = 2, - ASSIGNED = 3, -} - -local sessions = {} - -local function OnPlayerEventOnStoreNewItem(event, player, item, count) - print("OnPlayerEventOnStoreNewItem") - print(player:GetName() .. " won " .. item:GetName() .. " (x" .. count .. ")") - -- check if item is on blacklist - if isEntryOnBlacklist[item:GetEntry()] then print("OnPlayerEventOnStoreNewItem:blacklist") return end - -- add item to session tied to instance - local instanceId = player:GetInstanceId() - -- get session from instanceId - local session = sessions[instanceId] or {} -- new session if not exists - session.rows = session.rows or {} - local rowId = #session.rows+1 - local itemRow = {id=rowId, item=item, status=Status.PENDING} - table.insert(session.rows, rowId, itemRow) - print(string.format("Added item (Entry: %d GUIDLow: %d) to session instanceId %d", item:GetEntry(), item:GetGUIDLow(), instanceId)) - -- remove item from player - item:SaveToDB() -- Save item to DB before we remove it from the player - player:SaveToDB() -- must be called before RemoveItem else crash - player:RemoveItem(item, count) -end - -RegisterPlayerEvent(PLAYER_EVENT_ON_STORE_NEW_ITEM, OnPlayerEventOnStoreNewItem)