加入TOC缓存

This commit is contained in:
尚美 2025-06-14 23:52:32 +08:00
parent d2cbfa6339
commit 4c1e0b8b6a
2 changed files with 108 additions and 128 deletions

View File

@ -1,5 +1,7 @@
-- ItemCollection/SM_ItemCollectionUI.lua
-- 物品收藏界面实现
SM_CollectionsDB = SM_CollectionsDB or {}
SM_CollectionData.Config = {
CameraOption = "Classic" -- 默认使用Classic视角
}
@ -18,13 +20,16 @@ SM_ItemCollectionUI.itemsPerPage = 18 -- 每页显示18个物品
SM_ItemCollectionUI.totalPages = 1
SM_ItemCollectionUI.pageButtons = {}
SM_ItemCollectionUI.lastUpdateTime = 0 -- 上次更新时间
SM_ItemCollectionUI.CACHE_EXPIRE_TIME = 3600 -- 缓存过期时间1小时
-- 初始化物品收藏界面
function SM_ItemCollectionUI:Init()
-- 只做一次全局声明绝不重置SM_CollectionsDB或itemCache
-- 从其他文件加载必要的模块
if not SM_CollectionData then
LoadAddOn("SM_CollectionSystem")
end
-- 初始化物品槽位数据
self:InitSlotData()
self.initialized = true
@ -45,31 +50,22 @@ function SM_ItemCollectionUI:InitSlotData()
{ name = "腰带", invType = "INVTYPE_WAIST", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Waist" },
{ name = "", invType = "INVTYPE_LEGS", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Legs" },
{ name = "", invType = "INVTYPE_FEET", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Feet" },
{ name = "远程", invType = "INVTYPE_RANGED", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Ranged" },
{ name = "主手", invType = "INVTYPE_WEAPONMAINHAND", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-MainHand" },
{ name = "副手", invType = "INVTYPE_WEAPONOFFHAND", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Relic" },
}
local ICON_SLOTS = {
{ name = "远程", invType = "INVTYPE_RANGED", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Ranged" },
{ name = "颈部", invType = "INVTYPE_NECK", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Neck" },
{ name = "戒指", invType = "INVTYPE_FINGER", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-RFinger" },
{ name = "戒指", invType = "INVTYPE_FINGER", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Finger" },
{ name = "饰品", invType = "INVTYPE_TRINKET", items = {}, icon = "Interface\\PaperDoll\\UI-PaperDoll-Slot-Trinket" },
}
-- 合并所有槽位到一个数组
local ALL_SLOTS = {}
for i, slot in ipairs(MODEL_SLOTS) do
table.insert(ALL_SLOTS, slot)
self.ALL_SLOTS = MODEL_SLOTS
-- 动态生成类型映射表,保证和槽位定义一致
self.typeToInvType = {}
for _, slot in ipairs(self.ALL_SLOTS) do
self.typeToInvType[slot.name] = slot.invType
end
for i, slot in ipairs(ICON_SLOTS) do
table.insert(ALL_SLOTS, slot)
end
self.MODEL_SLOTS = MODEL_SLOTS
self.ICON_SLOTS = ICON_SLOTS
self.ALL_SLOTS = ALL_SLOTS
-- 不再需要主手武器ID列表因为我们现在统一处理所有武器
end
-- 隐藏所有按钮
@ -850,7 +846,7 @@ end
-- 显示物品收藏界面
function SM_ItemCollectionUI:ShowItemCollectionUI(parent)
-- 确保已初始化
-- 确保已初始化防止ALL_SLOTS未初始化导致缓存数据无法分类
if not self.initialized then
self:Init()
end
@ -858,7 +854,6 @@ function SM_ItemCollectionUI:ShowItemCollectionUI(parent)
-- 创建新的物品分栏面板
local panel = CreateFrame("Frame", "SM_CollectionsItemsPanel", parent)
panel:SetAllPoints()
-- 存储当前面板引用,这是关键
self.currentPanel = panel
-- 隐藏右侧召唤按钮
@ -868,34 +863,24 @@ function SM_ItemCollectionUI:ShowItemCollectionUI(parent)
-- 确保属性加成框架存在并显示
if SM_Collections.MainFrame and SM_Collections.MainFrame.RightSidePanel then
-- 先清理旧的属性文本
if SM_Collections.MainFrame.RightSidePanel.attributeTexts then
for _, text in ipairs(SM_Collections.MainFrame.RightSidePanel.attributeTexts) do
text:Hide()
end
-- 重置数组
SM_Collections.MainFrame.RightSidePanel.attributeTexts = {}
end
if not SM_Collections.MainFrame.RightSidePanel.AttributeBonusFrame then
-- 创建属性加成框架
local attributeFrame = CreateFrame("Frame", nil, SM_Collections.MainFrame.RightSidePanel)
attributeFrame:SetSize(180, 30)
attributeFrame:SetPoint("TOP", SM_Collections.MainFrame.RightSidePanel, "TOP", 0, -140)
local attributeTitle = attributeFrame:CreateFontString(nil, "OVERLAY")
attributeTitle:SetFont("Fonts\\ZYKai_T.ttf", 14, "OUTLINE")
attributeTitle:SetPoint("TOP", 0, 0)
attributeTitle:SetText("属性加成")
attributeTitle:SetTextColor(1, 0.82, 0)
SM_Collections.MainFrame.RightSidePanel.AttributeBonusFrame = attributeFrame
end
-- 显示属性加成框架
SM_Collections.MainFrame.RightSidePanel.AttributeBonusFrame:Show()
-- 初始化属性文本数组,如果不存在
if not SM_Collections.MainFrame.RightSidePanel.attributeTexts then
SM_Collections.MainFrame.RightSidePanel.attributeTexts = {}
end
@ -910,114 +895,108 @@ function SM_ItemCollectionUI:ShowItemCollectionUI(parent)
waitingText:Show()
panel.waitingText = waitingText
-- 在处理数据前清空并重置所有槽位的物品数组
-- 读取SavedVariables缓存只判断不赋值
local cacheValid = false
if SM_CollectionsDB and SM_CollectionsDB.itemCache then
local cache = SM_CollectionsDB.itemCache
if cache.items and #cache.items > 0 then
cacheValid = true
end
end
if cacheValid then
print("[SM_ItemCollectionUI] 读取缓存,物品数量:"..#SM_CollectionsDB.itemCache.items)
if panel.waitingText then panel.waitingText:Hide() end
self:ProcessAndShowItems(SM_CollectionsDB.itemCache.items, panel)
else
-- 请求数据
for _, slot in ipairs(self.ALL_SLOTS) do
slot.items = {}
end
local slotTypeMap = {}
for _, slot in ipairs(self.ALL_SLOTS) do
slotTypeMap[slot.name] = slot.invType
end
function self:GetItemEquipLocFromServerData(serverItemType, nini)
if slotTypeMap[serverItemType] then
return slotTypeMap[serverItemType]
end
return nil
end
SM_Collections.DataManager:GetItems(function(items)
if panel.waitingText then panel.waitingText:Hide() end
if items and #items > 0 then
-- 只在有数据时才写入缓存
SM_CollectionsDB.itemCache = {
items = items,
lastUpdate = time()
}
self:ProcessAndShowItems(items, panel)
else
-- 没有数据,显示提示
if panel.waitingText then panel.waitingText:SetText("没有获取到任何物品数据,请检查服务端或本地数据配置!") end
end
end)
end
return panel
end
function SM_ItemCollectionUI:ProcessAndShowItems(items, panel)
print("[SM_ItemCollectionUI] ProcessAndShowItems, items数量"..#items)
-- 清空所有槽位的物品数组
for _, slot in ipairs(self.ALL_SLOTS) do
slot.items = {}
end
-- 创建槽位映射表使用已定义的ALL_SLOTS数据
-- 创建槽位映射表
local slotTypeMap = {}
for _, slot in ipairs(self.ALL_SLOTS) do
slotTypeMap[slot.name] = slot.invType
slotTypeMap[slot.invType] = slot
end
-- 新增: 辅助函数,根据服务器返回的物品类型获取对应的装备位置类型
function self:GetItemEquipLocFromServerData(serverItemType, nini)
print(" 辅助函数1根据服务器返回的物品类型获取对应的装备位置类型= ", serverItemType, nini)
-- 直接转换中文类型名称到装备位置
if slotTypeMap[serverItemType] then
return slotTypeMap[serverItemType]
end
-- 如果无法匹配返回nil
return nil
end
-- 从服务器请求物品数据
SM_Collections.DataManager:GetItems(function(items)
-- 隐藏等待提示
if panel.waitingText then
panel.waitingText:Hide()
end
-- 显示调试信息
--print("[物品收藏] 从服务器获取到 " .. #items .. " 个物品")
-- 按物品类型整理到各个槽位中
for i, item in ipairs(items) do
-- 确保物品ID是有效的数值
local itemID = tonumber(item.id)
if itemID and itemID > 0 then
-- 从服务器数据中获取物品类型
local itemEquipLoc = nil
-- 首先使用服务器返回的itemType字段
if item.itemType and item.itemType ~= "" then
-- 使用新增的辅助函数解析服务器返回的类型
itemEquipLoc = self:GetItemEquipLocFromServerData(item.itemType, i)
-- 分类物品到各个槽位
for i, item in ipairs(items) do
local itemID = tonumber(item.id)
if itemID and itemID > 0 then
local itemEquipLoc = nil
-- 兼容缓存数据的itemType为中文或invType
if item.itemType and item.itemType ~= "" then
if self.typeToInvType and self.typeToInvType[item.itemType] then
itemEquipLoc = self.typeToInvType[item.itemType]
else
itemEquipLoc = item.itemType
end
-- 查找对应的槽位并添加物品
local slotFound = false
for j, slot in ipairs(self.ALL_SLOTS) do
if slot.invType == itemEquipLoc then
-- 避免重复添加相同物品ID
local exists = false
for _, existingID in ipairs(slot.items) do
if existingID == itemID then
exists = true
break
end
end
if not exists then
-- 将物品添加到对应的槽位
table.insert(slot.items, itemID)
slotFound = true
end
break
end
end
local slotFound = false
if itemEquipLoc and slotTypeMap[itemEquipLoc] then
local slot = slotTypeMap[itemEquipLoc]
local exists = false
for _, existingID in ipairs(slot.items) do
if existingID == itemID then exists = true break end
end
if not slotFound then
print(string.format("[物品收藏] 警告: 物品 %d (%s) 未找到对应槽位,类型: %s服务器类型%s", itemID, item.name,
tostring(itemEquipLoc), tostring(item.itemType)))
if not exists then
table.insert(slot.items, itemID)
slotFound = true
end
else
print("[物品收藏] 跳过无效的物品ID: " .. tostring(item.id))
end
if not slotFound then
print(string.format("[SM_ItemCollectionUI] 警告: 物品 %d (%s) 未找到对应槽位itemType: %s, 转换后: %s", itemID, item.name or "", tostring(item.itemType), tostring(itemEquipLoc)))
end
end
-- 调用我们新添加的调试函数打印更详细的信息
self:DebugPrintItemArrays()
-- 创建新按钮,无需重用旧按钮
self:CreateButtons()
self:ShowButtons()
-- 确保主窗口的右侧面板可见
if SM_Collections.MainFrame and SM_Collections.MainFrame.RightSidePanel then
SM_Collections.MainFrame.RightSidePanel:Show()
end
self:DebugPrintItemArrays()
self:CreateButtons()
self:ShowButtons()
if SM_Collections.MainFrame and SM_Collections.MainFrame.RightSidePanel then
SM_Collections.MainFrame.RightSidePanel:Show()
end
panel:Show()
if #self.buttons > 0 then
local firstButton = self.buttons[1]
self.currentSelectedButton = firstButton
if firstButton.SelectedTexture then
firstButton.SelectedTexture:Show()
end
panel:Show()
-- 初始化显示:默认选中第一个槽位,并立即显示其内容
if #self.buttons > 0 then
-- 选中第一个按钮
local firstButton = self.buttons[1]
self.currentSelectedButton = firstButton
-- 显示选中状态
if firstButton.SelectedTexture then
firstButton.SelectedTexture:Show()
end
-- 显示对应的物品预览
self:ShowItemPreview(firstButton.slotData, panel)
end
end)
return panel
self:ShowItemPreview(firstButton.slotData, panel)
end
end
-- 添加一个调试函数,用于打印物品数组状态

View File

@ -2,6 +2,7 @@
## Title: |cffc600a3收藏系统|r - by SM_Core
## Notes-zhCN: |n|cff00ff00功能概览:|r|n|n尚美核心专用插件装备、卡牌、坐骑收藏获得属性
## Dependencies: SM_CoreAddons
## SavedVariables: SM_CollectionsDB
# 主要框架和工具
SM_CollectionSystem.xml