forked from Cloticc/Eluna-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lotteryDB.lua
348 lines (297 loc) · 13.3 KB
/
lotteryDB.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
--[[
Lottery script for eluna using a database to store the lottery numbers and tickets bought by players.
This script might be bugs open an issue on github if you find any bugs.
creates 2 tables in the database lottery and lottery_tickets if they don't exist already
you need to add "lottery" numbers to the database manually for now (might add a command for it later.)
]]
local npcId = 43295
local lottery_tickets = {}
-- local GOSSIP_ACTION_INFO_DEF = 1000
-- local GOSSIP_SENDER_MAIN = 1
local menuId = 0x7FFFFFFF
-----------------------------------------------------
-- Events
local PLAYER_EVENT_ON_LOGIN = 3
local PLAYER_EVENT_ON_LOGOUT = 4
local GAME_EVENT_START = 34
local GAME_EVENT_STOP = 35
local GOSSIP_EVENT_ON_HELLO = 1
local GOSSIP_EVENT_ON_SELECT = 2
-----------------------------------------------------
--color picker function :D
local function SetColor(colorId, text)
return "|c" .. colorId .. text .. "|r"
end
-- player:GossipMenuAddItem(0, SetColor("FF7D2DBE", "Buy a ticket"), 0, 1) example of how to use SetColor function to change the color of the text
-----------------------------------------------------
----this part will create a table in the database if it doesn't exist
local lotteryExe = WorldDBExecute([[
CREATE TABLE IF NOT EXISTS lottery (
week_number INT UNSIGNED NOT NULL,
number1 INT UNSIGNED NOT NULL,
number2 INT UNSIGNED NOT NULL,
number3 INT UNSIGNED NOT NULL,
number4 INT UNSIGNED NOT NULL,
number5 INT UNSIGNED NOT NULL,
number6 INT UNSIGNED NOT NULL,
amount INT UNSIGNED NOT NULL,
PRIMARY KEY (week_number)
)
]])
local lotteryTicketExe = WorldDBExecute([[
CREATE TABLE IF NOT EXISTS lottery_tickets (
ticket_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
week_number INT UNSIGNED NOT NULL,
player_guid INT UNSIGNED NOT NULL,
player_account_id INT UNSIGNED NOT NULL,
number1 INT UNSIGNED NOT NULL,
number2 INT UNSIGNED NOT NULL,
number3 INT UNSIGNED NOT NULL,
number4 INT UNSIGNED NOT NULL,
number5 INT UNSIGNED NOT NULL,
number6 INT UNSIGNED NOT NULL,
PRIMARY KEY (ticket_id),
FOREIGN KEY (week
_number) REFERENCES lottery(week_number)
)
]])
if (lotteryExe == false) then
print("Error creating lottery table")
end
if (lotteryTicketExe == false) then
print("Error creating lottery_tickets table")
end
-----------------------------------------------------
local function LotteryOnLogin(event, player)
local guid = player:GetGUIDLow()
lottery_tickets[guid] = {}
local tickets = WorldDBQuery("SELECT * FROM lottery_tickets WHERE player_guid=" .. guid)
-----------------------------------------------------
if (tickets) then
while (tickets:NextRow()) do
local ticket = {
ticket_id = tickets:GetUInt32(0),
week_number = tickets:GetUInt32(1),
player_guid = tickets:GetUInt32(2),
player_account_id = tickets:GetUInt32(3),
number1 = tickets:GetUInt32(4),
number2 = tickets:GetUInt32(5),
number3 = tickets:GetUInt32(6),
number4 = tickets:GetUInt32(7),
number5 = tickets:GetUInt32(8),
number6 = tickets:GetUInt32(9)
}
--Add the ticket to the lookup table
if lottery_tickets[guid] == nil then
lottery_tickets[guid] = {}
end
lottery_tickets[guid][ticket.ticket_id] = ticket
end
end
end
RegisterPlayerEvent(PLAYER_EVENT_ON_LOGIN, LotteryOnLogin)
--------------------------------------------------------
-- --RegisterPlayerEvent for when a player logs out
-- function LotteryOnLogout(event, player)
-- --Remove the entry from the lottery_tickets table
-- if (lottery_tickets[player:GetGUIDLow()]) then
-- lottery_tickets[player:GetGUIDLow()] = nil
-- end
-- end
-- RegisterPlayerEvent(PLAYER_EVENT_ON_LOGOUT, LotteryOnLogout)
local function LotteryOnLogout(event, player)
lottery_tickets[player:GetGUIDLow()] = nil
end
RegisterPlayerEvent(PLAYER_EVENT_ON_LOGOUT, LotteryOnLogout)
--------------------------------------------------------------------------------------------------------------------
local function LotteryTicketEventEnd(event, gameeventid)
if (gameeventid == 89) then
-- local week_number = WorldDBQuery("SELECT week_number FROM lottery WHERE week_number=(SELECT MAX(week_number) FROM lottery)"):GetUInt32(0);
local week_number =
WorldDBQuery("SELECT week_number FROM lottery WHERE week_number=" .. lottery_tickets.week_number):GetUInt32(
0
)
--Get the lottery numbers for the current week
local number1 = WorldDBQuery("SELECT number1 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local number2 = WorldDBQuery("SELECT number2 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local number3 = WorldDBQuery("SELECT number3 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local number4 = WorldDBQuery("SELECT number4 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local number5 = WorldDBQuery("SELECT number5 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local number6 = WorldDBQuery("SELECT number6 FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local amount = WorldDBQuery("SELECT amount FROM lottery WHERE week_number=" .. week_number):GetUInt32(0)
local winners = 0
local winnings = 0
--Get all lottery tickets for the current week
local tickets = WorldDBQuery("SELECT * FROM lottery_tickets WHERE week_number=" .. week_number)
--Loop through all tickets and check if any of them match the lottery numbers
if (tickets) then
while (tickets:NextRow()) do
local ticket_number1 = tickets:GetUInt32(4)
local ticket_number2 = tickets:GetUInt32(5)
local ticket_number3 = tickets:GetUInt32(6)
local ticket_number4 = tickets:GetUInt32(7)
local ticket_number5 = tickets:GetUInt32(8)
local ticket_number6 = tickets:GetUInt32(9)
local player_lowguid = tickets:GetUInt32(2)
if (ticket_number1 == number1 and ticket_number2 == number2 and ticket_number3 == number3 and
ticket_number4 == number4 and
ticket_number5 == number5 and
ticket_number6 == number6)
then
--Send mail to the player with the amount of money won
winners = winners + 1
end
winnings = amount / winners
SendMail(
"Lottery Winner!",
"Congratulations! You have won the lottery and have been awarded " .. winnings .. " copper!",
player_lowguid,
0,
0,
0,
winnings
)
end
end
end
end
RegisterServerEvent(GAME_EVENT_STOP, LotteryTicketEventEnd)
-----------------------------------------------------
--Generates the initial lottery numbers.
local function LotteryTicketEventBegin(event, gameeventid)
if (gameeventid == 89) then
local week_number =
WorldDBQuery("SELECT week_number FROM lottery ORDER BY week_number DESC LIMIT 1"):GetUInt32(0)
local week = 0
if (week_number == 0) then
week = 1
else
week = week_number + 1
end
local number1 = math.random(1, 32)
local number2 = math.random(1, 32)
local number3 = math.random(1, 32)
local number4 = math.random(1, 32)
local number5 = math.random(1, 32)
local number6 = math.random(1, 32)
WorldDBExecute(
"REPLACE lottery (week_number, number1, number2, number3, number4, number5, number6, amount) VALUES (" ..
week ..
", " ..
number1 ..
", " ..
number2 ..
", " .. number3 .. ", " .. number4 .. ", " .. number5 .. ", " .. number6 .. ", 0)"
)
end
end
RegisterServerEvent(GAME_EVENT_START, LotteryTicketEventBegin)
--------------------------------------------------------------------------------------------------------------------
local function LotteryGossipNpc(event, player, creature)
local jackpot_amount =
WorldDBQuery("SELECT amount FROM lottery WHERE week_number=(SELECT MAX(week_number) FROM lottery)"):GetUInt32(0)
local jackpot_amount_gold = math.floor(jackpot_amount / 10000)
-- player:GossipSetText(string.format("Welcome to the lottery!. Current jackpot: %u gold!", jackpot_amount_gold)) ---- simple version
-- player:GossipSetText(string.format(SetColor("FF1B1B1B", "Welcome to the lottery!. Current jackpot: %u gold!"), jackpot_amount_gold)) ---- color version
player:GossipSetText(
string.format(
SetColor("FF000000", "Welcome to the lottery!. Current jackpot: ") .. SetColor("FFFFD700", "%u") .. " gold!"
,
jackpot_amount_gold
)
) ---- color version with gold color
local ticket_count = 0
if lottery_tickets[player:GetGUIDLow()] then
for ticket_key, ticket in pairs(lottery_tickets[player:GetGUIDLow()]) do
local ticket_string =
string.format(
"Ticket #%u: %u, %u, %u, %u, %u, %u",
ticket.ticket_id,
ticket.number1,
ticket.number2,
ticket.number3,
ticket.number4,
ticket.number5,
ticket.number6
)
player:GossipMenuAddItem(0, ticket_string, 0, 0)
ticket_count = ticket_count + 1
end
end
--made check so if u got 3 tickets u cant buy more so the player knows he cant buy more tickets.
if (ticket_count == 3) then
player:GossipMenuAddItem(0, "You have reached the maximum amount of tickets you can purchase.", 0, 0)
end
if (ticket_count < 3) then
-- player:GossipMenuAddItem(0, "Purchase Lottery Ticket", 0, 1, GOSSIP_ACTION_INFO_DEF + 1) ????
player:GossipMenuAddItem(0, "Purchase Lottery Ticket", 0, 1)
end
player:GossipSendMenu(menuId, creature)
end
local function LotteryGossipNpcSelect(event, player, creature, sender, intid)
if (intid == 1) then
--Check if the player has enough money
if (player:GetCoinage() < 100000) then
creature:SendUnitSay("You do not have enough money to buy a lottery ticket!", 0)
else
--Subtract the money from the player
player:ModifyMoney(-100000)
--Generate the 6 lottery numbers
local number1 = math.random(1, 32)
local number2 = math.random(1, 32)
local number3 = math.random(1, 32)
local number4 = math.random(1, 32)
local number5 = math.random(1, 32)
local number6 = math.random(1, 32)
--Get the current week number
local week_number =
WorldDBQuery("SELECT week_number FROM lottery ORDER BY week_number DESC LIMIT 1"):GetUInt32(0)
--Increase the amount in the pot by 70000 copper
WorldDBQuery("UPDATE lottery SET amount=amount+70000 WHERE week_number=" .. week_number)
--Insert the ticket into the database
WorldDBQuery(
"INSERT INTO lottery_tickets (week_number, player_guid, player_account_id, number1, number2, number3, number4, number5, number6) VALUES ("
..
week_number ..
", " ..
player:GetGUIDLow() ..
", " ..
player:GetAccountId() ..
", " ..
number1 ..
", " ..
number2 ..
", " ..
number3 ..
", " ..
number4 .. ", " .. number5 .. ", " .. number6 .. ")"
)
--Add the ticket to the players session
local new_ticket = {
ticket_id = WorldDBQuery("SELECT ticket_id FROM lottery_tickets ORDER BY ticket_id DESC LIMIT 1"):
GetUInt32(
0
),
week_number = week_number,
player_guid = player:GetGUIDLow(),
player_account_id = player:GetAccountId(),
number1 = number1,
number2 = number2,
number3 = number3,
number4 = number4,
number5 = number5,
number6 = number6
}
if (lottery_tickets[player:GetGUIDLow()] == nil) then
lottery_tickets[player:GetGUIDLow()] = {}
end
lottery_tickets[player:GetGUIDLow()][new_ticket.ticket_id] = new_ticket
creature:SendUnitSay("Your lottery ticket has been purchased! Good luck!", 0)
end
end
LotteryGossipNpc(event, player, creature)
end
RegisterCreatureGossipEvent(npcId, GOSSIP_EVENT_ON_HELLO, LotteryGossipNpc)
RegisterCreatureGossipEvent(npcId, GOSSIP_EVENT_ON_SELECT, LotteryGossipNpcSelect)
local function onSpawnNpc(event, creature) creature:SetNPCFlags(3) end
RegisterCreatureEvent(npcId, 5, onSpawnNpc)