-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.lua
212 lines (196 loc) · 8.32 KB
/
server.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
ESX = nil
TriggerEvent('esx:getSharedObject', function(obj) ESX = obj end)
function SocietyInvest(d, h, m)
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_police', function(account)
account.addMoney(Config.SocietyPolice)
end)
TriggerEvent('esx_addonaccount:getSharedAccount', 'society_ambulance', function(account)
account.addMoney(Config.SocietyAmbulance)
end)
end
function PayBills(d, h, m)
if Config.Debug then print("AutoPayBills start") end
CreateThread(function()
Wait(0)
MySQL.Async.fetchAll('SELECT * FROM billing', {}, function (result)
for i=1, #result, 1 do
Wait(100)--Slow down process
local xPlayer = ESX.GetPlayerFromIdentifier(result[i].identifier)
-- message player if connected
if xPlayer then
local accountMoney = xPlayer.getAccount('bank').money
if accountMoney > 0 then
if ESX.Math.Round(accountMoney/100*Config.MaxPercentPay) >= result[i].amount then
xPlayer.removeAccountMoney('bank', result[i].amount)
TriggerClientEvent('esx:showNotification', xPlayer.source, "Vous avez payer ".. ESX.Math.GroupDigits(result[i].amount).." sur une factures passé due")
if result[i].target_type == "society" then
TriggerEvent('esx_addonaccount:getSharedAccount', result[i].target, function(account)
account.addMoney(result[i].amount)
end)
elseif result[i].target_type == "player" then
local xTarget = ESX.GetPlayerFromIdentifier(result[i].target)
if xTarget then--target player is online
xTarget.addAccountMoney('bank', result[i].amount)
else--target player not online
MySQL.Async.fetchScalar('SELECT accounts FROM users WHERE identifier = @identifier',
{
['@identifier'] = result[i].target
}, function(targetAccounts)
local xtargetAccounts = json.decode(targetAccounts)
xtargetAccounts.bank = xtargetAccounts.bank + result[i].amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(xtargetAccounts),
['@identifier'] = result[i].target
})
end)
end
else
print("BanSql Error : Invalid target_type '",result[i].target_type,"' in sql table 'billing'")
end
MySQL.Sync.execute('DELETE FROM billing WHERE id = @id',
{
['@id'] = result[i].id
})
if Config.Debug then print(xPlayer.name.." pay "..result[i].amount.." from bill to "..result[i].target) end
else
local amount = ESX.Math.Round(accountMoney/100*Config.MaxPercentPay)
xPlayer.removeAccountMoney('bank', amount)
TriggerClientEvent('esx:showNotification', xPlayer.source, "Vous avez payer ".. ESX.Math.GroupDigits(amount).." sur une factures passé due")
if result[i].target_type == "society" then
TriggerEvent('esx_addonaccount:getSharedAccount', result[i].target, function(account)
account.addMoney(amount)
end)
elseif result[i].target_type == "player" then
local xTarget = ESX.GetPlayerFromIdentifier(result[i].target)
if xTarget then
xTarget.addAccountMoney('bank', amount)
else
MySQL.Async.fetchScalar('SELECT accounts FROM users WHERE identifier = @identifier',
{
['@identifier'] = result[i].target
}, function(targetAccounts)
local xtargetAccounts = json.decode(targetAccounts)
xtargetAccounts.bank = xtargetAccounts.bank + amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(xtargetAccounts),
['@identifier'] = result[i].target
})
end)
end
else
print("BanSql Error : Invalid target_type '",result[i].target_type,"' in sql table 'billing'")
end
MySQL.Sync.execute('UPDATE billing SET amount = amount - @amount WHERE id = @id',
{
['@amount'] = amount,
['@id'] = result[i].id
})
if Config.Debug then print(xPlayer.name.." pay "..(amount).." from bill to "..result[i].target) end
end
end
else -- pay rent either way
MySQL.Async.fetchScalar('SELECT accounts FROM users WHERE identifier = @identifier',
{
['@identifier'] = result[i].identifier
}, function(jsonAccounts)
local accounts = json.decode(jsonAccounts)
if accounts.bank > 0 then
if ESX.Math.Round(accounts.bank/100*Config.MaxPercentPay) >= result[i].amount then
accounts.bank = accounts.bank - result[i].amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(accounts),
['@identifier'] = result[i].identifier
})
if result[i].target_type == "society" then
TriggerEvent('esx_addonaccount:getSharedAccount', result[i].target, function(account)
account.addMoney(result[i].amount)
end)
elseif result[i].target_type == "player" then
local xTarget = ESX.GetPlayerFromIdentifier(result[i].target)
if xTarget then--target player is online
xTarget.addAccountMoney('bank', result[i].amount)
else--target player not online
MySQL.Async.fetchScalar('SELECT accounts FROM users WHERE identifier = @identifier',
{
['@identifier'] = result[i].target
}, function(targetAccounts)
local xtargetAccounts = json.decode(targetAccounts)
xtargetAccounts.bank = xtargetAccounts.bank + result[i].amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(xtargetAccounts),
['@identifier'] = result[i].target
})
end)
end
else
print("BanSql Error : Invalid target_type '",result[i].target_type,"' in sql table 'billing'")
end
MySQL.Sync.execute('DELETE FROM billing WHERE `id` = @id',
{
['@id'] = result[i].id
})
if Config.Debug then print(result[i].identifier.." pay "..(result[i].amount).." from bill to "..result[i].target) end
else
local amount = ESX.Math.Round(accounts.bank/100*Config.MaxPercentPay)
accounts.bank = accounts.bank - amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(accounts),
['@identifier'] = result[i].identifier
})
MySQL.Sync.execute('UPDATE billing SET amount = amount - @amount WHERE id = @id',
{
['@amount'] = amount,
['@id'] = result[i].id
})
if result[i].target_type == "society" then
TriggerEvent('esx_addonaccount:getSharedAccount', result[i].target, function(account)
account.addMoney(accounts.bank/100*Config.MaxPercentPay)
end)
elseif result[i].target_type == "player" then
local xTarget = ESX.GetPlayerFromIdentifier(result[i].target)
if xTarget then
xTarget.addAccountMoney('bank', amount)
else
MySQL.Async.fetchScalar('SELECT accounts FROM users WHERE identifier = @identifier',
{
['@identifier'] = result[i].target
}, function(targetAccounts)
local xtargetAccounts = json.decode(targetAccounts)
xtargetAccounts.bank = xtargetAccounts.bank + amount
MySQL.Sync.execute('UPDATE users SET accounts = @accounts WHERE identifier = @identifier',
{
['@accounts'] = json.encode(xtargetAccounts),
['@identifier'] = result[i].target
})
end)
end
else
print("BanSql Error : Invalid target_type '",result[i].target_type,"' in sql table 'billing'")
end
if Config.Debug then print(result[i].identifier.." pay "..(amount).." from bill to "..result[i].target) end
end
end
end)
end
end
end)
end)
end
if Config.SocietyInvest then
TriggerEvent('cron:runAt', 22, 0, SocietyInvest)
end
if Config.AutoPayBills then
TriggerEvent('cron:runAt', 8, 0, PayBills)
end
if Config.Debug then
RegisterCommand("test", function(source, args, raw)
if source == 0 then
PayBills()
end
end, true)
end