307 lines
8.1 KiB
Lua
307 lines
8.1 KiB
Lua
-- Guda Database Module
|
|
-- Handles saving and loading character data
|
|
|
|
local addon = Guda
|
|
|
|
local DB = {}
|
|
addon.Modules.DB = DB
|
|
|
|
-- Current player info
|
|
local playerName, playerRealm, playerFaction
|
|
|
|
-- Initialize database
|
|
function DB:Initialize()
|
|
-- Get player info
|
|
playerName = UnitName("player")
|
|
playerRealm = GetRealmName()
|
|
playerFaction = UnitFactionGroup("player")
|
|
|
|
local fullName = playerName .. "-" .. playerRealm
|
|
|
|
-- Initialize global DB
|
|
if not Guda_DB then
|
|
Guda_DB = {
|
|
version = addon.VERSION,
|
|
characters = {},
|
|
}
|
|
end
|
|
|
|
-- Initialize character DB
|
|
if not Guda_CharDB then
|
|
Guda_CharDB = {
|
|
settings = {
|
|
showBankInBags = true,
|
|
showOtherChars = true,
|
|
bagColumns = 10,
|
|
bankBagColumns = 8,
|
|
bankColumns = 10,
|
|
sortMethod = "quality", -- quality, name, type
|
|
iconSize = 40,
|
|
iconSpacing = 0,
|
|
iconFontSize = 12,
|
|
showQualityBorderEquipment = true,
|
|
showQualityBorderOther = true,
|
|
showSearchBar = true,
|
|
hoverBagline = false,
|
|
hideFooter = false,
|
|
bgTransparency = 0.15,
|
|
showTrackedItems = false,
|
|
trackedItems = {},
|
|
},
|
|
}
|
|
end
|
|
|
|
-- Ensure new settings exist for existing installations
|
|
if Guda_CharDB.settings.bgTransparency == nil then
|
|
if Guda_CharDB.settings.frameOpacity ~= nil then
|
|
Guda_CharDB.settings.bgTransparency = 1.0 - Guda_CharDB.settings.frameOpacity
|
|
Guda_CharDB.settings.frameOpacity = nil -- Clean up old setting
|
|
else
|
|
Guda_CharDB.settings.bgTransparency = 0.15
|
|
end
|
|
end
|
|
if Guda_CharDB.settings.hideFooter == nil then
|
|
Guda_CharDB.settings.hideFooter = false
|
|
end
|
|
if Guda_CharDB.settings.hoverBagline == nil then
|
|
Guda_CharDB.settings.hoverBagline = false
|
|
end
|
|
if Guda_CharDB.settings.showQuestBar == nil then
|
|
Guda_CharDB.settings.showQuestBar = true
|
|
end
|
|
if Guda_CharDB.settings.showTrackedItems == nil then
|
|
Guda_CharDB.settings.showTrackedItems = false
|
|
end
|
|
if Guda_CharDB.settings.trackedItems == nil then
|
|
Guda_CharDB.settings.trackedItems = {}
|
|
end
|
|
if not Guda_CharDB.settings.bagColumns then
|
|
Guda_CharDB.settings.bagColumns = 10
|
|
end
|
|
if not Guda_CharDB.settings.bankBagColumns then
|
|
Guda_CharDB.settings.bankBagColumns = 8
|
|
end
|
|
if not Guda_CharDB.settings.bankColumns then
|
|
Guda_CharDB.settings.bankColumns = 10
|
|
end
|
|
if not Guda_CharDB.settings.iconSize then
|
|
Guda_CharDB.settings.iconSize = 40
|
|
end
|
|
if not Guda_CharDB.settings.iconSpacing then
|
|
Guda_CharDB.settings.iconSpacing = 0
|
|
end
|
|
if not Guda_CharDB.settings.iconFontSize then
|
|
Guda_CharDB.settings.iconFontSize = 12
|
|
end
|
|
if Guda_CharDB.settings.showQualityBorderEquipment == nil then
|
|
Guda_CharDB.settings.showQualityBorderEquipment = true
|
|
end
|
|
if Guda_CharDB.settings.showQualityBorderOther == nil then
|
|
Guda_CharDB.settings.showQualityBorderOther = true
|
|
end
|
|
if Guda_CharDB.settings.showSearchBar == nil then
|
|
Guda_CharDB.settings.showSearchBar = true
|
|
end
|
|
if Guda_CharDB.settings.questBarPinnedItems == nil then
|
|
Guda_CharDB.settings.questBarPinnedItems = {}
|
|
end
|
|
|
|
-- Initialize this character's data
|
|
if not Guda_DB.characters[fullName] then
|
|
local localizedClass, englishClass = UnitClass("player")
|
|
Guda_DB.characters[fullName] = {
|
|
name = playerName,
|
|
realm = playerRealm,
|
|
faction = playerFaction,
|
|
class = localizedClass,
|
|
classToken = englishClass, -- English uppercase token for RAID_CLASS_COLORS
|
|
level = UnitLevel("player"),
|
|
money = 0,
|
|
bags = {},
|
|
bank = {},
|
|
equipped = {}, -- Add equipped items storage
|
|
character = {}, -- Add character info storage
|
|
lastUpdate = time(),
|
|
}
|
|
else
|
|
-- Migration: Add classToken to existing characters
|
|
local char = Guda_DB.characters[fullName]
|
|
if not char.classToken then
|
|
local localizedClass, englishClass = UnitClass("player")
|
|
char.classToken = englishClass
|
|
addon:Debug("Added classToken to existing character")
|
|
end
|
|
|
|
-- Migration: Add equipped and character fields if they don't exist
|
|
if not char.equipped then
|
|
char.equipped = {}
|
|
addon:Debug("Added equipped field to existing character")
|
|
end
|
|
if not char.character then
|
|
char.character = {}
|
|
addon:Debug("Added character field to existing character")
|
|
end
|
|
end
|
|
|
|
addon:Debug("Database initialized for %s", fullName)
|
|
end
|
|
|
|
-- Get current player's full name
|
|
function DB:GetPlayerFullName()
|
|
return playerName .. "-" .. playerRealm
|
|
end
|
|
|
|
-- Get current character data
|
|
function DB:GetCurrentCharacter()
|
|
local fullName = self:GetPlayerFullName()
|
|
return Guda_DB.characters[fullName]
|
|
end
|
|
|
|
-- Get current character data (alias for compatibility)
|
|
function DB:GetCurrentCharacterData()
|
|
return self:GetCurrentCharacter()
|
|
end
|
|
|
|
-- Save bag data
|
|
function DB:SaveBags(bagData)
|
|
local char = self:GetCurrentCharacter()
|
|
if char then
|
|
char.bags = bagData
|
|
char.lastUpdate = time()
|
|
addon:Debug("Saved bag data")
|
|
end
|
|
end
|
|
|
|
-- Save bank data
|
|
function DB:SaveBank(bankData)
|
|
local char = self:GetCurrentCharacter()
|
|
if char then
|
|
char.bank = bankData
|
|
char.lastUpdate = time()
|
|
addon:Debug("Saved bank data")
|
|
end
|
|
end
|
|
|
|
-- Save equipment data
|
|
function DB:SaveEquipment(equipmentData)
|
|
local char = self:GetCurrentCharacter()
|
|
if char then
|
|
char.equipped = equipmentData.equipped
|
|
char.character = equipmentData.character
|
|
char.lastUpdate = time()
|
|
addon:Debug("Saved equipment data")
|
|
end
|
|
end
|
|
|
|
-- Save money
|
|
function DB:SaveMoney(copper)
|
|
local char = self:GetCurrentCharacter()
|
|
if char then
|
|
char.money = copper
|
|
char.level = UnitLevel("player")
|
|
addon:Debug("Saved money: %d copper", copper)
|
|
end
|
|
end
|
|
|
|
-- Get all characters (optionally filter by faction and/or realm)
|
|
function DB:GetAllCharacters(sameFactionOnly, currentRealmOnly)
|
|
local chars = {}
|
|
for fullName, data in pairs(Guda_DB.characters) do
|
|
local factionMatch = not sameFactionOnly or data.faction == playerFaction
|
|
local realmMatch = not currentRealmOnly or data.realm == playerRealm
|
|
if factionMatch and realmMatch then
|
|
table.insert(chars, {
|
|
fullName = fullName,
|
|
name = data.name,
|
|
realm = data.realm,
|
|
class = data.class,
|
|
classToken = data.classToken, -- English uppercase token for colors
|
|
level = data.level,
|
|
faction = data.faction,
|
|
money = data.money,
|
|
lastUpdate = data.lastUpdate,
|
|
})
|
|
end
|
|
end
|
|
|
|
-- Sort by name
|
|
table.sort(chars, function(a, b)
|
|
return a.name < b.name
|
|
end)
|
|
|
|
return chars
|
|
end
|
|
|
|
-- Get character's bags
|
|
function DB:GetCharacterBags(fullName)
|
|
local char = Guda_DB.characters[fullName]
|
|
return char and char.bags or {}
|
|
end
|
|
|
|
-- Get character's bank
|
|
function DB:GetCharacterBank(fullName)
|
|
local char = Guda_DB.characters[fullName]
|
|
return char and char.bank or {}
|
|
end
|
|
|
|
-- Get character's equipped items
|
|
function DB:GetCharacterEquipped(fullName)
|
|
local char = Guda_DB.characters[fullName]
|
|
return char and char.equipped or {}
|
|
end
|
|
|
|
-- Get character info
|
|
function DB:GetCharacterInfo(fullName)
|
|
local char = Guda_DB.characters[fullName]
|
|
return char and char.character or {}
|
|
end
|
|
|
|
-- Get total money across all characters (optionally filter by faction and/or realm)
|
|
function DB:GetTotalMoney(sameFactionOnly, currentRealmOnly)
|
|
local total = 0
|
|
for fullName, data in pairs(Guda_DB.characters) do
|
|
local factionMatch = not sameFactionOnly or data.faction == playerFaction
|
|
local realmMatch = not currentRealmOnly or data.realm == playerRealm
|
|
if factionMatch and realmMatch then
|
|
total = total + (data.money or 0)
|
|
end
|
|
end
|
|
return total
|
|
end
|
|
|
|
-- Get character setting
|
|
function DB:GetSetting(key)
|
|
-- SavedVariables may not be initialized yet when some UI OnLoad scripts run
|
|
if not Guda_CharDB or not Guda_CharDB.settings then
|
|
return nil
|
|
end
|
|
return Guda_CharDB.settings[key]
|
|
end
|
|
|
|
-- Set character setting
|
|
function DB:SetSetting(key, value)
|
|
-- Ensure tables exist even if called early
|
|
if not Guda_CharDB then
|
|
Guda_CharDB = { settings = {} }
|
|
elseif not Guda_CharDB.settings then
|
|
Guda_CharDB.settings = {}
|
|
end
|
|
Guda_CharDB.settings[key] = value
|
|
end
|
|
|
|
-- Cleanup old characters (not updated in 90 days)
|
|
function DB:CleanupOldCharacters()
|
|
local cutoff = time() - (90 * 24 * 60 * 60) -- 90 days
|
|
local removed = 0
|
|
|
|
for fullName, data in pairs(Guda_DB.characters) do
|
|
if data.lastUpdate and data.lastUpdate < cutoff then
|
|
Guda_DB.characters[fullName] = nil
|
|
removed = removed + 1
|
|
end
|
|
end
|
|
|
|
if removed > 0 then
|
|
addon:Print("Cleaned up %d old character(s)", removed)
|
|
end
|
|
end |