forked from CsokiHUN/fl_dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.lua
255 lines (196 loc) · 6.72 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
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
function createSQLColumn(name)
local p = promise.new()
local exists = MySQL.scalar.await("SHOW COLUMNS FROM `users` LIKE '" .. name .. "'")
if exists then
return p:resolve(false)
end
MySQL.query([[
ALTER TABLE `users`
ADD COLUMN `]] .. name .. [[` INT(11) NULL DEFAULT '0';
]], function()
p:resolve(true)
end)
return p
end
CreateThread(function()
Citizen.Await(createSQLColumn("playedTime"))
Citizen.Await(createSQLColumn("premiumPoints"))
for _, player in pairs(GetPlayers()) do
local sb = Player(player).state
if not sb.joinTime then
Player(player).state.joinTime = os.time()
end
if not sb.playedTime then
loadPlayerPlayedTime(player)
end
loadPlayerPP(player)
end
end)
AddEventHandler("esx:playerLoaded", function(player)
Player(player).state.joinTime = os.time()
loadPlayerPlayedTime(player)
end)
function loadPlayerPlayedTime(player)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer then
return
end
local playedTime = MySQL.scalar.await("SELECT playedTime FROM users WHERE identifier = ?", { xPlayer.identifier })
Player(player).state.playedTime = playedTime
end
function savePlayedTime(player)
local xPlayer = ESX.GetPlayerFromId(player)
if not xPlayer then
return
end
local sb = Player(player).state
local oldTime = sb.playedTime or 0
local joinTime = sb.joinTime or os.time()
local newTime = oldTime + (os.time() - joinTime)
exports.oxmysql:update("UPDATE users SET playedTime = ? WHERE identifier = ?", { newTime, xPlayer.identifier })
end
AddEventHandler("playerDropped", function()
savePlayedTime(source)
end)
ESX.RegisterServerCallback("requestServerAdmins", function(source, cb)
local result = {}
for _, player in pairs(GetPlayers()) do
local xPlayer = ESX.GetPlayerFromId(player)
if xPlayer and ADMIN_GROUPS[xPlayer.getGroup()] then
table.insert(result, {
id = player,
group = xPlayer.getGroup(),
name = GetPlayerName(player),
})
end
end
cb(result)
end)
ESX.RegisterServerCallback("requestPlayerDatas", function(source, cb)
local result = {}
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then
return cb({})
end
table.insert(result, { title = "ID", value = source })
table.insert(result, { title = "Steam Név", value = GetPlayerName(source) })
table.insert(result, { title = "Születési idő", value = xPlayer.variables.dateofbirth })
table.insert(result, { title = "Magasság", value = xPlayer.variables.height .. "cm" })
for _, account in pairs(xPlayer.accounts) do
table.insert(
result,
{ title = account.label, value = account.money .. "$", color = account.money >= 0 and "green" or "red" }
)
end
local currentPP = getPlayerPP(source)
table.insert(result, {
title = "Prémium Pont",
value = currentPP .. "PP",
color = currentPP >= 0 and "green" or "red",
})
local vehicleCount = MySQL.Sync.fetchScalar(
"SELECT COUNT(*) FROM owned_vehicles WHERE owner = ?",
{ xPlayer.identifier }
)
table.insert(result, { title = "Járművek", value = vehicleCount .. "db" })
table.insert(result, { title = "Group", value = xPlayer.getGroup() })
table.insert(result, { title = "Munka", value = xPlayer.job and xPlayer.job.label or "Ismeretlen" })
table.insert(result, { title = "Rang", value = xPlayer.job and xPlayer.job.grade_label or "Ismeretlen" })
table.insert(
result,
{ title = "Fizetés", value = xPlayer.job and xPlayer.job.grade_salary .. "$" or "Nincs 😢", color = "green" }
)
table.insert(result, { title = "Identifier", value = xPlayer.identifier, color = "red", blur = true })
local sb = Player(source).state
local joinTime = sb.joinTime or os.time()
local currentSessionTime = os.time() - joinTime
local playedTime = (sb.playedTime or 0) + currentSessionTime
table.insert(result, { title = "Játszott idő", value = secondsToClock(playedTime), color = "yellow" })
table.insert(result, { title = "Online Idő", value = secondsToClock(currentSessionTime), color = "yellow" })
cb(result, xPlayer.getName())
end)
ESX.RegisterServerCallback("requestPlayerVehicles", function(source, cb)
local xPlayer = ESX.GetPlayerFromId(source)
if not xPlayer then
return cb({})
end
MySQL.query("SELECT * FROM owned_vehicles WHERE owner = ?", { xPlayer.identifier }, function(result)
cb(result)
end)
end)
function IsValidItem(name)
if not name then
return false
end
if GetResourceState("ox_inventory"):find("start") then
return exports.ox_inventory:Items(name) or false
end
ESX = exports.es_extended:getSharedObject() --refresh esx obj
return ESX.Items[name]
end
ESX.RegisterServerCallback("requestPremiumStuff", function(source, cb)
local items = {}
for _, item in pairs(PREMIUM.items) do
if IsValidItem(item.name) then
item.label = ESX.GetItemLabel(item.name)
if item.label then
item.img = getItemImagePath(item.name)
table.insert(items, item)
end
end
end
cb(getPlayerPP(source), items)
end)
ESX.RegisterServerCallback("buyPremiumItem", function(source, cb, item, typ, label)
local xPlayer = ESX.GetPlayerFromId(source)
local currentPP = getPlayerPP(source)
local item = getItemData(item, typ)
if not item or not item.price then
return cb(currentPP)
end
if currentPP < item.price then
return chatbox("Nincs elég prémium pontod!", { 255, 0, 0 }, source)
end
if typ == "items" then
if not IsValidItem(item.name) then
return chatbox(("Érvénytelen tárgy! %s"):format(item.name), {255, 0, 0}, source)
end
if not xPlayer.canCarryItem(item.name, 1) then
chatbox("Tárgy nemfér el nálad!", { 255, 0, 0 }, source)
return cb(currentPP)
end
xPlayer.addInventoryItem(item.name, 1)
takePlayerPP(source, item.price)
chatbox("Sikeres vásároltál egy " .. ESX.GetItemLabel(item.name) .. " tárgyat", { 0, 255, 0 }, source)
elseif typ == "vehicles" then
takePlayerPP(source, item.price)
local plate = GeneratePlate()
MySQL.insert("INSERT INTO owned_vehicles SET owner = ?, plate = ?, vehicle = ?", {
xPlayer.identifier,
plate,
json.encode({ model = GetHashKey(tostring(item.name)), plate = plate }),
})
chatbox("Sikeres jármű vásárlás. Model: " .. label, { 0, 255, 0 }, source)
return cb(getPlayerPP(source), { item = item, plate = plate })
elseif typ == "money" then
takePlayerPP(source, item.price)
xPlayer.addMoney(item.name)
chatbox("Sikeres vásároltál " .. item.name .. "$-t", { 0, 255, 0 }, source)
end
cb(getPlayerPP(source))
end)
function GeneratePlate()
local plate = ""
local str = "abcdefghijklmnopqrstuvwxyz"
for index = 1, 3 do
plate = plate .. string.char(str:byte(math.random(1, #str)))
end
plate = plate:upper() .. " "
for index = 1, 3 do
plate = plate .. math.random(0, 9)
end
if MySQL.scalar.await("SELECT plate FROM owned_vehicles WHERE plate = ?", { plate }) then
return GeneratePlate()
end
return plate
end