· 8 years ago · Jan 27, 2018, 06:54 PM
1-- Name: Daily Chest System
2-- Details: Limits the amount of time between the same chest being opened by the same player.
3-- Usage: Modify the configuration below to suit your needs.
4-- Website: https://github.com/RStijn
5
6-- Init
7local CHEST = {}
8
9-- Config
10-- set chest gameobject ID's at bottom
11local HOURS = 24 -- How many hours before a player can loot again
12local ACCOUNTLIMIT = true -- TRUE: Limits entire account. FALSE: Limits only this character
13-- Message to show when player already looted a chest recently
14local ALREADYLOOTED = "You already looted this chest. Please check back within 24 hours."
15-- GameObject ID's you want to limit per player.
16CHEST[1] = 850014
17CHEST[2] = 850006
18CHEST[3] = 850009
19
20
21-- Functions
22local function registerNewLoot(pid, goGuid)
23 CharDBQuery("INSERT INTO `looted_chests` (`player`, `objectGuid`, `lootTime`) VALUES ('".. pid .. "', '" .. goGuid .. "', UNIX_TIMESTAMP());")
24end
25
26local function canLoot(pid, goGuid)
27 local q = CharDBQuery("SELECT `id`, `lootTime` FROM `looted_chests` WHERE `player` = " .. pid .. " AND `objectGuid` = " .. goGuid .. ";")
28 local now = tonumber(CharDBQuery("SELECT UNIX_TIMESTAMP()"):GetRow(1)["UNIX_TIMESTAMP()"])
29
30 -- if possible
31 if q == nil then
32 return true
33 elseif tonumber(q:GetRow(1)["lootTime"]) + (HOURS * 3600) < now then
34 CharDBQuery("DELETE FROM `looted_chests` WHERE `id` = "..q:GetRow(1)["id"])
35 return true
36 else
37 return false
38 end
39end
40
41local function blockLoot(player, go)
42 go:SetLootState(3)
43 player:SendBroadcastMessage(ALREADYLOOTED);
44end
45
46local function handleLoot(player, go)
47 -- Get ID's
48 local pid = 0
49 if ACCOUNTLIMIT then
50 pid = player:GetAccountId()
51 else
52 pid = player:GetPlayerGUID()
53 end
54 local goGuid = go:GetGUIDLow()
55
56 -- Handle loot
57 if canLoot(pid, goGuid) then
58 registerNewLoot(pid, goGuid)
59 else
60 blockLoot(player, go)
61 end
62end
63
64local function onLootStateChanged(event, go, state)
65 if state == 2 then
66 player = go:GetLootRecipient()
67 handleLoot(player, go)
68 end
69
70end
71
72-- Register chests
73local i = 1
74while CHEST[i] do
75 RegisterGameObjectEvent(CHEST[i], 9, onLootStateChanged)
76 i = i + 1
77end
78
79-- Create sql table if needed
80CharDBQuery("CREATE TABLE IF NOT EXISTS `looted_chests` (`id` int(11) NOT NULL AUTO_INCREMENT, `player` int(11) NOT NULL, `objectGuid` int(11) NOT NULL, `lootTime` int(11) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;")