WOW_Collection_System/SM_DataProvider.lua

611 lines
23 KiB
Lua

-- SM_DataProvider.lua
-- 数据提供者基类
SM_Collections.DataProvider = {}
function SM_Collections.DataProvider:new()
local obj = {}
setmetatable(obj, self)
self.__index = self
return obj
end
function SM_Collections.DataProvider:GetMounts(callback) error("GetMounts must be implemented by subclass") end
function SM_Collections.DataProvider:GetCompanions(callback) error("GetCompanions must be implemented by subclass") end
function SM_Collections.DataProvider:GetCards(callback) error("GetCards must be implemented by subclass") end
function SM_Collections.DataProvider:GetItems(callback) error("GetItems must be implemented by subclass") end
function SM_Collections.DataProvider:GetAttributes(callback) error("GetAttributes must be implemented by subclass") end
function SM_Collections.DataProvider:GetAttributesForSpecificItem(itemId, itemType, callback) error("GetAttributesForSpecificItem must be implemented by subclass") end
-- 本地数据提供者
SM_Collections.LocalDataProvider = SM_Collections.DataProvider:new()
function SM_Collections.LocalDataProvider:GetMounts(callback)
local mounts = {
{
id = 458,
name = "棕马",
description = "暴风城的卫兵最喜欢这种坐骑,因为它既有耐力又很温顺。",
source = "在暴风城与艾尔文森林交界处的哨兵岭找寻见习骑士,提供棕马。",
obtained = true,
mountType = "陆地" -- 添加类型属性
},
{
id = 459,
name = "灰狼",
description = "这种狼喜欢在雾中狩猎,依靠烟雾般的毛皮隐藏自己,不被猎物发现。",
source = "在奥格瑞玛找Ogunaro Wolfrunner获得。",
obtained = true,
mountType = "陆地" -- 添加类型属性
},
{
id = 36702,
name = "炽炎角鹰兽",
description = "这种强大的角鹰兽有燃烧的翅膀,在天空中飞行时留下一道火焰轨迹。",
source = "在雷霆崖附近的塔伦米尔从空中护卫队领取任务,完成后获得。",
obtained = false,
canFly = true, -- 飞行标识
mountType = "飞行" -- 添加类型属性
},
{
id = 64731,
name = "海龟坐骑",
description = "这种古老的海龟曾经在世界各地的海洋中游荡,现在它愿意背负你穿越水域。",
source = "在北荆棘谷的海岸钓鱼有很小几率获得。",
obtained = true,
canSwim = true, -- 水下标识
mountType = "水下" -- 添加类型属性
},
}
if SM_Collections.Config.Debug then
end
callback(mounts)
end
-- 小伙伴
function SM_Collections.LocalDataProvider:GetCompanions(callback)
local companions = {
{
id = 10673,
name = "灰猫",
description = "这只灰猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "完成东泉森林的一系列任务后获得。",
obtained = true,
petType = "野兽" -- 添加宠物类型
},
{
id = 10674,
name = "黄猫",
description = "这只黄猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "在艾尔文森林的黄猫巢穴处有低几率获得。",
obtained = true,
petType = "野兽" -- 添加宠物类型
},
{
id = 10675,
name = "黑纹灰猫",
description = "这只黑纹灰猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "在艾尔文森林的黑纹灰猫巢穴处有低几率获得。",
obtained = false,
petType = "野兽" -- 添加宠物类型
},
{
id = 10676,
name = "虎皮猫",
description = "这只虎皮猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "在艾尔文森林的虎皮猫巢穴处有低几率获得。",
obtained = true,
petType = "野兽" -- 添加宠物类型
},
{
id = 10677,
name = "黑尾白猫",
description = "这只黑尾白猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "在艾尔文森林的黑尾白猫巢穴处有低几率获得。",
obtained = true,
petType = "野兽" -- 添加宠物类型
},
{
id = 10678,
name = "黑斑白猫",
description = "这只黑斑白猫在东泉森林被发现,是一只温顺的生物,喜欢跟随冒险者。",
source = "在艾尔文森林的黑斑白猫巢穴处有低几率获得。",
obtained = false,
petType = "野兽" -- 添加宠物类型
},
}
if SM_Collections.Config.Debug then
end
callback(companions)
end
-- 卡牌
function SM_Collections.LocalDataProvider:GetCards(callback)
local cards = {
{
id = 11502,
name = "拉格纳罗斯",
description = "在艾泽拉斯的青山上游荡的风元素,传说它们是四大元素之一的风之创造物。",
source = "在荒风地带完成风元素相关任务后获得。",
obtained = true,
cardType = "普通" -- 添加卡牌类型
},
{
id = 21435,
name = "暗影恐魔",
description = "夜晚的恐怖猎手,擅长从黑暗中突袭猎物。",
source = "在暮色森林的夜晚狩猎,有几率获得。",
obtained = true,
cardType = "精英" -- 添加卡牌类型
},
{
id = 31438,
name = "翱翔的蓝龙",
description = "传说中的蓝龙,魔法的掌控者,是艾泽拉斯古老的守护者之一。",
source = "完成蓝龙军团相关的史诗任务链获得。",
obtained = false,
cardType = "稀有" -- 添加卡牌类型
},
{
id = 37994,
name = "水元素",
description = "火焰领主拉格纳罗斯,火元素位面的统治者,强大的火焰之力使大地颤抖。",
source = "在熔火之心击败拉格纳罗斯后有几率获得。",
obtained = true,
cardType = "世界BOSS" -- 添加卡牌类型
},
}
if SM_Collections.Config.Debug then
end
callback(cards)
end
-- 物品
function SM_Collections.LocalDataProvider:GetItems(callback)
local items = {
{
id = 1,
name = "神秘头盔",
description = "一顶散发着神秘力量的头盔...",
source = "在黑暗神殿有几率掉落",
obtained = true
},
{
id = 2,
name = "神圣长袍",
description = "教会祭司穿着的长袍...",
source = "在雷霆崖祭司处购买",
obtained = false
}
}
if SM_Collections.Config.Debug then
end
callback(items)
end
-- 属性
function SM_Collections.LocalDataProvider:GetAttributes(callback)
local attributes = {}
if SM_Collections.Config.Debug then
end
callback(attributes)
end
-- 特定物品的属性
function SM_Collections.LocalDataProvider:GetAttributesForSpecificItem(itemId, itemType, callback)
local attributeMap = {
mount = {
[458] = { -- 棕马
{ Name = "移动速度", value = 60, isPositive = true },
{ Name = "耐力", value = 10, isPositive = true },
},
[459] = { -- 灰狼
{ Name = "移动速度", value = 60, isPositive = true },
{ Name = "野性", value = 15, isPositive = true },
},
[36702] = { -- 炽炎角鹰兽
{ Name = "移动速度", value = 100, isPositive = true },
{ Name = "飞行速度", value = 280, isPositive = true },
{ Name = "火焰伤害", value = 5, isPositive = true },
},
[64731] = { -- 海龟坐骑
{ Name = "移动速度", value = 50, isPositive = true },
{ Name = "游泳速度", value = 100, isPositive = true },
{ Name = "水下呼吸", value = 300, isPositive = true },
},
},
companion = {
[10673] = { -- 小鹿
{ Name = "幸运", value = 5, isPositive = true },
{ Name = "护甲", value = -10, isPositive = false },
},
[10674] = { -- 小兔子
{ Name = "移动速度", value = 2, isPositive = true },
{ Name = "精神", value = 5, isPositive = true },
},
[10675] = { -- 小火元素
{ Name = "火焰抗性", value = 10, isPositive = true },
{ Name = "冰霜抗性", value = -10, isPositive = false },
},
[10676] = { -- 小机器人
{ Name = "工程学技能", value = 5, isPositive = true },
{ Name = "自然抗性", value = -5, isPositive = false },
},
[10677] = { -- 迷你地狱火
{ Name = "暗影伤害", value = 5, isPositive = true },
{ Name = "暗影抗性", value = 10, isPositive = true },
},
[10678] = { -- 小农夫
{ Name = "食物恢复效果", value = 10, isPositive = true },
{ Name = "烹饪技能", value = 5, isPositive = true },
},
},
card = {
[17] = { -- 风之元素
{ Name = "自然伤害", value = 50, isPositive = true },
{ Name = "闪避", value = 2, isPositive = true },
},
[23] = { -- 暗影猎手
{ Name = "潜行", value = 10, isPositive = true },
{ Name = "暴击", value = 3, isPositive = true },
},
[24] = { -- 蓝龙
{ Name = "法术强度", value = 100, isPositive = true },
{ Name = "智力", value = 20, isPositive = true },
{ Name = "法力回复", value = 10, isPositive = true },
},
[35] = { -- 拉格纳罗斯
{ Name = "火焰伤害", value = 100, isPositive = true },
{ Name = "亡灵控制", value = 100, isPositive = true },
{ Name = "生命汲取", value = 50, isPositive = true },
},
}
}
local attributes = attributeMap[itemType] and attributeMap[itemType][itemId] or {}
if SM_Collections.Config.Debug then
end
callback(attributes)
end
-- 服务端数据提供者
SM_Collections.ServerDataProvider = SM_Collections.DataProvider:new()
-- 坐骑
function SM_Collections.ServerDataProvider:GetMounts(callback)
self:SendServerRequest("GET_MOUNTS", {}, function(response)
if response and response.success then
if SM_Collections.Config.Debug then
--
end
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetMounts(callback)
else
callback({})
end
end
end)
end
-- 小伙伴
function SM_Collections.ServerDataProvider:GetCompanions(callback)
self:SendServerRequest("GET_COMPANIONS", {}, function(response)
if response and response.success then
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetCompanions(callback)
else
callback({})
end
end
end)
end
-- 卡牌
function SM_Collections.ServerDataProvider:GetCards(callback)
self:SendServerRequest("GET_CARDS", {}, function(response)
if response and response.success then
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetCards(callback)
else
callback({})
end
end
end)
end
-- 物品
function SM_Collections.ServerDataProvider:GetItems(callback)
self:SendServerRequest("GET_ITEMS", {}, function(response)
if response and response.success then
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetItems(callback)
else
callback({})
end
end
end)
end
-- 属性
function SM_Collections.ServerDataProvider:GetAttributes(callback)
self:SendServerRequest("GET_ATTRIBUTES", {}, function(response)
if response and response.success then
if SM_Collections.Config.Debug then
--
end
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetAttributes(callback)
else
callback({})
end
end
end)
end
-- 特定物品的属性
function SM_Collections.ServerDataProvider:GetAttributesForSpecificItem(itemId, itemType, callback)
local requestData = {
itemId = itemId,
itemType = itemType
}
self:SendServerRequest("GET_ITEM_ATTRIBUTES", requestData, function(response)
if response and response.success then
if SM_Collections.Config.Debug then
--
end
callback(response.data)
else
if SM_Collections.Config.FallbackToLocal then
SM_Collections.LocalDataProvider:GetAttributesForSpecificItem(itemId, itemType, callback)
else
callback({})
end
end
end)
end
-- 发送服务端请求
function SM_Collections.ServerDataProvider:SendServerRequest(command, data, callback)
if SM_Collections.Config.Debug then
--
end
-- 确保pendingCallbacks表已初始化
if not SM_Collections.pendingCallbacks then
SM_Collections.pendingCallbacks = {}
end
local requestMessage = command
-- 处理特殊请求
if command == "GET_ITEM_ATTRIBUTES" and data then
requestMessage = command .. "|" .. data.itemType .. "|" .. data.itemId
end
--
-- 发送消息给服务端
-- 尝试通过两种方式发送,增加成功机会
SendAddonMessage("SM_C_COLLECTIONS_REQUEST", requestMessage, "GUILD")
-- 保存回调函数
if command == "GET_MOUNTS" then
SM_Collections.pendingCallbacks.mounts = callback
elseif command == "GET_ATTRIBUTES" then
SM_Collections.pendingCallbacks.attributes = callback
elseif command == "GET_COMPANIONS" then
SM_Collections.pendingCallbacks.companions = callback
elseif command == "GET_CARDS" then
SM_Collections.pendingCallbacks.cards = callback
elseif command == "GET_ITEMS" then
SM_Collections.pendingCallbacks.items = callback
elseif command == "GET_ITEM_ATTRIBUTES" then
SM_Collections.pendingCallbacks.item_attributes = callback
end
-- 设置超时处理
local timeoutFrame = CreateFrame("Frame")
timeoutFrame.command = command
timeoutFrame.data = data
timeoutFrame.startTime = GetTime()
timeoutFrame.timeoutSeconds = SM_Collections.Config.ServerTimeout / 1000
timeoutFrame:SetScript("OnUpdate", function(self, elapsed)
if GetTime() - self.startTime >= self.timeoutSeconds then
local callbackKey
if self.command == "GET_MOUNTS" then
callbackKey = "mounts"
elseif self.command == "GET_ATTRIBUTES" then
callbackKey = "attributes"
elseif self.command == "GET_COMPANIONS" then
callbackKey = "companions"
elseif self.command == "GET_CARDS" then
callbackKey = "cards"
elseif self.command == "GET_ITEMS" then
callbackKey = "items"
elseif self.command == "GET_ITEM_ATTRIBUTES" then
callbackKey = "item_attributes"
end
if SM_Collections.pendingCallbacks and SM_Collections.pendingCallbacks[callbackKey] then
if SM_Collections.Config.FallbackToLocal then
local localProvider = SM_Collections.LocalDataProvider:new()
if self.command == "GET_MOUNTS" then
localProvider:GetMounts(callback)
elseif self.command == "GET_ATTRIBUTES" then
localProvider:GetAttributes(callback)
elseif self.command == "GET_COMPANIONS" then
localProvider:GetCompanions(callback)
elseif self.command == "GET_CARDS" then
localProvider:GetCards(callback)
elseif self.command == "GET_ITEMS" then
localProvider:GetItems(callback)
elseif self.command == "GET_ITEM_ATTRIBUTES" and self.data then
localProvider:GetAttributesForSpecificItem(self.data.itemId, self.data.itemType, callback)
end
else
callback({ success = false, data = {} })
end
SM_Collections.pendingCallbacks[callbackKey] = nil
end
self:SetScript("OnUpdate", nil)
end
end)
end
-- 数据管理器
SM_Collections.DataManager = {
-- 获取当前使用的数据提供者
GetProvider = function(self)
if SM_Collections.Config.UseServerData then
return SM_Collections.ServerDataProvider
else
return SM_Collections.LocalDataProvider
end
end,
-- 获取坐骑数据
GetMounts = function(self, callback)
self:GetProvider():GetMounts(callback)
end,
-- 获取小伙伴数据
GetCompanions = function(self, callback)
self:GetProvider():GetCompanions(callback)
end,
-- 获取卡牌数据
GetCards = function(self, callback)
self:GetProvider():GetCards(callback)
end,
-- 获取物品数据
GetItems = function(self, callback)
self:GetProvider():GetItems(callback)
end,
-- 获取特定物品的属性加成
GetAttributesForSpecificItem = function(self, itemId, itemType, callback)
self:GetProvider():GetAttributesForSpecificItem(itemId, itemType, callback)
end,
-- 更新坐骑状态
UpdateMountStatus = function(self, mountID, isObtained)
-- 更新本地数据
if SM_Collections.LocalDataProvider.cachedMounts then
for i, mount in ipairs(SM_Collections.LocalDataProvider.cachedMounts) do
if mount.id == mountID then
mount.obtained = isObtained
break
end
end
end
-- 如果使用服务器数据,也更新服务器缓存
if SM_Collections.Config.UseServerData and SM_Collections.ServerDataProvider.cachedMounts then
for i, mount in ipairs(SM_Collections.ServerDataProvider.cachedMounts) do
if mount.id == mountID then
mount.obtained = isObtained
break
end
end
end
end,
-- 更新小伙伴状态
UpdateCompanionStatus = function(self, companionID, isObtained)
-- 更新本地数据
if SM_Collections.LocalDataProvider.cachedCompanions then
for i, companion in ipairs(SM_Collections.LocalDataProvider.cachedCompanions) do
if companion.id == companionID then
companion.obtained = isObtained
break
end
end
end
-- 如果使用服务器数据,也更新服务器缓存
if SM_Collections.Config.UseServerData and SM_Collections.ServerDataProvider.cachedCompanions then
for i, companion in ipairs(SM_Collections.ServerDataProvider.cachedCompanions) do
if companion.id == companionID then
companion.obtained = isObtained
break
end
end
end
end,
-- 更新卡牌状态
UpdateCardStatus = function(self, cardID, isObtained)
-- 更新本地数据
if SM_Collections.LocalDataProvider.cachedCards then
for i, card in ipairs(SM_Collections.LocalDataProvider.cachedCards) do
if card.id == cardID then
card.obtained = isObtained
break
end
end
end
-- 如果使用服务器数据,也更新服务器缓存
if SM_Collections.Config.UseServerData and SM_Collections.ServerDataProvider.cachedCards then
for i, card in ipairs(SM_Collections.ServerDataProvider.cachedCards) do
if card.id == cardID then
card.obtained = isObtained
break
end
end
end
end,
-- 更新物品状态
UpdateItemStatus = function(self, itemID, isObtained)
-- 更新本地数据
if SM_Collections.LocalDataProvider.cachedItems then
for i, item in ipairs(SM_Collections.LocalDataProvider.cachedItems) do
if item.id == itemID then
item.obtained = isObtained
break
end
end
end
-- 如果使用服务器数据,也更新服务器缓存
if SM_Collections.Config.UseServerData and SM_Collections.ServerDataProvider.cachedItems then
for i, item in ipairs(SM_Collections.ServerDataProvider.cachedItems) do
if item.id == itemID then
item.obtained = isObtained
break
end
end
end
end
}
-- 兼容性坐骑数据
SM_Collections.Mounts = {
[458] = { 284, 2, 0, "Brown Horse", "A favorite among Stormwind's guards thanks to its patience and stamina.", 2, "|cFFFFD200Vendor: |rWorld Vendors|n|cFFFFD200Cost: |r1|TINTERFACE\\MONEYFRAME\\UI-GOLDICON.BLP:0|t" },
[459] = { 4268, 2, 64, "Gray Wolf", "This breed of wolf prefers hunting in the fog, relying on its smoky hide to camouflage it from unsuspecting prey.", 2, "|cFFFFD200Vendor: |rOgunaro Wolfrunner|n|cFFFFD200Zone: |rOrgrimmar|n|cFFFFD200Cost: |r1|TINTERFACE\\MONEYFRAME\\UI-GOLDICON.BLP:0|t" },
[36702] = { 21354, 2, 64, "White Stallion", "The powerful and unyielding white stallion features heavily in the myths of ancient human tribes.", 2, "|cFFFFD200Legacy|r" },
[64731] = { 34187, 2, 0, "Black Stallion", "Rumored to be favored by SI:7 for night missions due to its dark coat.", 2, "|cFFFFD200Vendor: |rUnger Statforth|n|cFFFFD200Zone: |rWetlands|n|cFFFFD200Cost: |r1|TINTERFACE\\MONEYFRAME\\UI-GOLDICON.BLP:0|t" },
[578] = { 356, 2, 64, "Black Wolf", "Can howl loudly enough to be heard for miles.", 2, "|cFFFFD200Vendor: |rOgunaro Wolfrunner|n|cFFFFD200Zone: |rOrgrimmar|n|cFFFFD200Cost: |r1|TINTERFACE\\MONEYFRAME\\UI-GOLDICON.BLP:0|t" },
}