forked from NotCoffee418/Eluna-Lua-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
daily_chest_system.lua
78 lines (67 loc) · 2.35 KB
/
daily_chest_system.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
-- Name: Daily Chest System
-- Details: Limits the amount of time between the same chest being opened by the same player.
-- Usage: Modify the configuration below to suit your needs.
-- Website: https://github.com/RStijn
-- Config
local CHEST = {
-- GameObject ID's you want to limit per player.
500000,
500001,
500002,
}
-- set chest gameobject ID's at bottom
local HOURS = 24 -- How many hours before a player can loot again
local ACCOUNTLIMIT = true -- TRUE: Limits entire account. FALSE: Limits only this character
-- Message to show when player already looted a chest recently
local ALREADYLOOTED = "You already looted this chest. Please check back within 24 hours."
-- Functions
local function registerNewLoot(pid, goGuid)
CharDBQuery("INSERT INTO `looted_chests` (`player`, `objectGuid`, `lootTime`) VALUES ('".. pid .. "', '" .. goGuid .. "', UNIX_TIMESTAMP());")
end
local function canLoot(pid, goGuid)
local q = CharDBQuery("SELECT `id`, `lootTime` FROM `looted_chests` WHERE `player` = " .. pid .. " AND `objectGuid` = " .. goGuid .. ";")
local now = tonumber(CharDBQuery("SELECT UNIX_TIMESTAMP()"):GetRow(1)["UNIX_TIMESTAMP()"])
-- if possible
if q == nil then
return true
elseif tonumber(q:GetRow(1)["lootTime"]) + (HOURS * 3600) < now then
CharDBQuery("DELETE FROM `looted_chests` WHERE `id` = "..q:GetRow(1)["id"])
return true
else
return false
end
end
local function blockLoot(player, go)
go:SetLootState(3)
player:SendBroadcastMessage(ALREADYLOOTED);
end
local function handleLoot(player, go)
-- Get ID's
local pid = 0
if ACCOUNTLIMIT then
pid = player:GetAccountId()
else
pid = player:GetPlayerGUID()
end
local goGuid = go:GetGUIDLow()
-- Handle loot
if canLoot(pid, goGuid) then
registerNewLoot(pid, goGuid)
else
blockLoot(player, go)
end
end
local function onLootStateChanged(event, go, state)
if state == 2 then
local player = go:GetLootRecipient()
if player then
handleLoot(player, go)
end
end
end
-- Register chests
for k, chest_entry in ipairs(CHEST) do
RegisterGameObjectEvent(chest_entry, 9, onLootStateChanged)
end
-- Create sql table if needed
CharDBQuery("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 ;")