59 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
			
		
		
	
	
			59 lines
		
	
	
		
			1.9 KiB
		
	
	
	
		
			Lua
		
	
	
	
	
	
| local addonname, shared = ...
 | |
| ---@cast shared HeimdallShared
 | |
| ---@cast addonname string
 | |
| local ModuleName = "StinkyCache"
 | |
| 
 | |
| ---@diagnostic disable-next-line: missing-fields
 | |
| shared.StinkyCache = {}
 | |
| function shared.StinkyCache.Init()
 | |
| 	shared.stinkyCache.stinkies = {}
 | |
| 
 | |
| 	---@param name string
 | |
| 	local function AskCommander(name)
 | |
| 		if Heimdall_Data.config.stinkyCache.debug then
 | |
| 			print(string.format("[%s] Asking commander %s about %s", ModuleName,
 | |
| 				Heimdall_Data.config.stinkyCache.commander, name))
 | |
| 		end
 | |
| 		local messageParts = { "isstinky", name }
 | |
| 		local message = table.concat(messageParts, "|")
 | |
| 		SendAddonMessage(Heimdall_Data.config.addonPrefix,
 | |
| 			message, "WHISPER",
 | |
| 			Heimdall_Data.config.stinkyCache.commander)
 | |
| 		return
 | |
| 	end
 | |
| 
 | |
| 	local addonMessageFrame = CreateFrame("Frame")
 | |
| 	addonMessageFrame:RegisterEvent("CHAT_MSG_ADDON")
 | |
| 	addonMessageFrame:SetScript("OnEvent", function(self, event, msg, sender, ...)
 | |
| 		if sender == Heimdall_Data.config.stinkyCache.commander then
 | |
| 			if Heimdall_Data.config.stinkyCache.debug then
 | |
| 				print(string.format("[%s] Received stinky from commander %s: %s", ModuleName,
 | |
| 					Heimdall_Data.config.stinkyCache.commander, msg))
 | |
| 			end
 | |
| 			local name, value = {strsplit("|", msg)}
 | |
| 			shared.stinkyCache.stinkies[name] = { value = value, timestamp = time() }
 | |
| 		else
 | |
| 			if Heimdall_Data.config.stinkyCache.debug then
 | |
| 				print(string.format("[%s] Received stinky from non-commander %s: %s", ModuleName, sender, msg))
 | |
| 			end
 | |
| 			local parts = {strsplit("|", msg)}
 | |
| 			local command, name = parts[1], parts[2]
 | |
| 			if parts[1] == "isstinky" then
 | |
| 				local res = Heimdall_Data.config.stinkies[parts[2]]
 | |
| 			end
 | |
| 		end
 | |
| 	end)
 | |
| 
 | |
| 	setmetatable(shared.stinkyCache.stinkies, {
 | |
| 		__index = function(self, key)
 | |
| 			local value = rawget(self, key)
 | |
| 			local now = GetTime()
 | |
| 			if value == nil or now - value.timestamp > Heimdall_Data.config.stinkyCache.ttl then
 | |
| 				AskCommander(key)
 | |
| 			end
 | |
| 			return rawget(self, key)
 | |
| 		end
 | |
| 	})
 | |
| 	print("[Heimdall] StinkyCache module loaded")
 | |
| end
 |