-
Notifications
You must be signed in to change notification settings - Fork 10
/
s.lua
210 lines (203 loc) · 9.02 KB
/
s.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
-----------------------------------------------------------------------------------------------------------------
-- Frameworks
-----------------------------------------------------------------------------------------------------------------
local framework = nil
if configs.framework == 'esx' then
TriggerEvent('esx:getSharedObject', function(obj) framework = obj end);
elseif configs.framework == 'qbcore' then
framework = exports['qb-core']:GetCoreObject()
end
function Notify(id, message, type, length)
TriggerClientEvent(configs.framework == 'esx' and 'esx:showNotification' or 'QBCore:Notify' , id, message, type, length)
end
function getIdentifier(src)
local identifier = ''
if configs.framework == 'esx' then
local xPlayer = framework.GetPlayerFromId(src)
identifier = xPlayer.getIdentifier()
elseif configs.framework == 'qbcore' then
local Player = framework.Functions.GetPlayer(src)
identifier = Player.PlayerData.citizenid
end
return identifier
end
function addMoney(src, amount)
if configs.framework == 'esx' then
local xPlayer = framework.GetPlayerFromId(src)
xPlayer.addAccountMoney('bank', amount)
elseif configs.framework == 'qbcore' then
local Player = framework.Functions.GetPlayer(src)
Player.Functions.AddMoney('bank', amount, 'iBetting Balance Withdraw')
end
end
function removeMoney(src, amount)
local hasEnough = false
if configs.framework == 'esx' then
local xPlayer = framework.GetPlayerFromId(src)
local accountMoney = xPlayer.getAccount('bank').money
if accountMoney >= amount then
xPlayer.removeAccountMoney('bank', amount)
hasEnough = true
end
elseif configs.framework == 'qbcore' then
local Player = framework.Functions.GetPlayer(src)
hasEnough = Player.Functions.RemoveMoney('bank', amount, 'iBetting Bet Placed')
end
return hasEnough
end
-----------------------------------------------------------------------------------------------------------------
-- configs
-----------------------------------------------------------------------------------------------------------------
local apiKey = ''
local adminId = 'char1:0ce13144566dd4bb91deff72f33d68332e7b525a' -- (ESX)char1:0ce13144566dd4bb91deff72f33d68332e7b525a or (QBCore)JLU37881
-----------------------------------------------------------------------------------------------------------------
-- variables
-----------------------------------------------------------------------------------------------------------------
local iBetting = {}
-----------------------------------------------------------------------------------------------------------------
-- load betting list data
-----------------------------------------------------------------------------------------------------------------
CreateThread(function()
MySQL.Sync.execute('SELECT * FROM bettinglist', nil, function(result)
if result then
for _, value in pairs(result) do
iBetting[value.keym] = value
end
end
end)
end)
-----------------------------------------------------------------------------------------------------------------
-- manager
-----------------------------------------------------------------------------------------------------------------
RegisterNetEvent('iBetting:manager')
AddEventHandler('iBetting:manager', function()
local id = source
local identifier = getIdentifier(id)
if identifier == adminId then
TriggerClientEvent('iBetting:manager', id, iBetting, apiKey)
end
end)
-----------------------------------------------------------------------------------------------------------------
-- playing
-----------------------------------------------------------------------------------------------------------------
RegisterNetEvent('iBetting:playing')
AddEventHandler('iBetting:playing', function()
local id = source
local identifier = getIdentifier(id)
MySQL.query('SELECT * FROM bettingbets WHERE userId = ? ORDER BY id DESC', {identifier}, function(result)
local playerBets = {}
if result then
for _, value in pairs(result) do
-- wining
if value.completed == 1 then
local amount = value.amount * value.odd
addMoney(id, amount)
MySQL.query('DELETE FROM bettingbets WHERE id = ?', {value.id})
--------------------
-- may send notify -
--------------------
-- player bets
else
playerBets[tostring(value.id)] = value
end
end
TriggerClientEvent('iBetting:playing', id, iBetting, playerBets)
end
end)
end)
-----------------------------------------------------------------------------------------------------------------
-- list
-----------------------------------------------------------------------------------------------------------------
RegisterNetEvent('iBetting:list')
AddEventHandler('iBetting:list', function(data)
local id = source
local identifier = getIdentifier(id)
if identifier == adminId then
-- create new match listing
if not iBetting[data.keym] then
MySQL.insert.await('INSERT INTO bettinglist (keym, sport, cham, away, home, awayIcon, homeIcon, odd0, odd1, odd2, time) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ', {
data.keym, data.sport, data.cham, data.away, data.home, data.awayIcon, data.homeIcon, data.odd0, data.odd1, data.odd2, data.time
})
Notify(id, 'The match listed and bet ready', "success", 5000)
-- update old match listing
else
MySQL.update.await('UPDATE bettinglist SET awayIcon = ?, homeIcon = ?, odd0 = ?, odd1 = ?, odd2 = ? WHERE keym = ?', {data.awayIcon, data.homeIcon, data.odd0, data.odd1, data.odd2, data.keym})
Notify(id, 'The match updated', "success", 5000)
end
iBetting[data.keym] = data
end
end)
-----------------------------------------------------------------------------------------------------------------
-- bet
-----------------------------------------------------------------------------------------------------------------
RegisterNetEvent('iBetting:bet')
AddEventHandler('iBetting:bet', function(data)
local id = source
local keym = data.keym; local bet = data.bet; local amount = tonumber(data.amount);
local currentTime = os.time(os.date("!*t"))
-- check the match exist and vaild time
if iBetting[keym] and currentTime < iBetting[keym].time then
if removeMoney(id, amount) then
local odd = 0
if bet == 0 then odd = iBetting[keym].odd0 elseif bet == 1 then odd = iBetting[keym].odd1 elseif bet == 2 then odd = iBetting[keym].odd2 end
MySQL.insert.await('INSERT INTO bettingbets (userId, keym, bet, odd, amount, data) VALUES (?, ?, ?, ?, ?, ?) ', {
getIdentifier(id), keym, bet, odd, amount, json.encode(iBetting[keym])
})
MySQL.query('SELECT * FROM bettingbets WHERE userId = ? ORDER BY id DESC', {xPlayer.getIdentifier()}, function(result)
local playerBets = {}
if result then
for index, value in pairs(result) do
playerBets[tostring(value.id)] = value
end
TriggerClientEvent('iBetting:playing', id, iBetting, playerBets)
end
end)
Notify(id, 'Place bet successfully', "success", 5000)
else
Notify(id, 'Do not have enough money to bet', "error", 5000)
end
end
end)
-----------------------------------------------------------------------------------------------------------------
-- cron for scores and wining calc
-----------------------------------------------------------------------------------------------------------------
CreateThread(function()
while true do
MySQL.query('SELECT sport FROM `bettinglist` GROUP BY sport', {}, function(sports)
if sports then
for _, sport in pairs(sports) do
PerformHttpRequest('https://api.the-odds-api.com/v4/sports/' .. sport.sport .. '/scores/?apiKey=' .. apiKey .. '&daysFrom=3&dateFormat=unix', function (errorCode, resultData, resultHeaders)
for _, result in pairs(json.decode(resultData)) do
if result.completed == true then
local homeScore = result.scores[1].score
local awayScore = result.scores[2].score
MySQL.query('SELECT * FROM bettingbets WHERE keym = ? AND completed = 0 ORDER BY id DESC', {result.id}, function(bets)
if bets then
for _, bet in pairs(result) do
if homeScore == awayScore then
if bet.bet == 2 then
MySQL.update.await('UPDATE bettingbets SET completed = 1 WHERE id = ?', {bet.id})
end
elseif awayScore > homeScore then
if bet.bet == 0 then
MySQL.update.await('UPDATE bettingbets SET completed = 1 WHERE id = ?', {bet.id})
end
else
if bet.bet == 1 then
MySQL.update.await('UPDATE bettingbets SET completed = 1 WHERE id = ?', {bet.id})
end
end
end
end
MySQL.query('DELETE FROM bettingbets WHERE keym = ? AND completed = 0', {result.id})
end)
end
end
end)
end
end
end)
-- wait next run every 5 hours
Wait(1000*60*5)
end
end)