· 7 years ago · Sep 06, 2018, 01:52 PM
1include("static_data.lua")
2
3/*---------------------------------------------------------------------------
4MySQL and SQLite connectivity
5---------------------------------------------------------------------------*/
6if file.Exists("lua/includes/modules/gmsv_mysqloo.dll", true) or file.Exists("lua/includes/modules/gmsv_mysqloo_i486.dll", true) then
7 require("mysqloo")
8end
9
10local CONNECTED_TO_MYSQL = false
11DB.MySQLDB = nil
12
13function DB.Begin()
14 if not CONNECTED_TO_MYSQL then sql.Begin() end
15end
16
17function DB.Commit()
18 if not CONNECTED_TO_MYSQL then sql.Commit() end
19end
20
21function DB.Query(query, callback)
22 if CONNECTED_TO_MYSQL then
23 if DB.MySQLDB and DB.MySQLDB:status() == mysqloo.DATABASE_NOT_CONNECTED then
24 DB.ConnectToMySQL(RP_MySQLConfig.Host, RP_MySQLConfig.Username, RP_MySQLConfig.Password, RP_MySQLConfig.Database_name, RP_MySQLConfig.Database_port)
25 end
26
27 local query = DB.MySQLDB:query(query)
28 local data
29 query.onData = function(Q, D)
30 data = data or {}
31 data[#data + 1] = D
32 end
33
34 query.onError = function(Q, E) Error(E) callback() DB.Log("MySQL Error: ".. E) end
35 query.onSuccess = function()
36 if callback then callback(data) end
37 end
38 query:start()
39 return
40 end
41 sql.Begin()
42 local Result = sql.Query(query)
43 sql.Commit() -- Otherwise it won't save, don't ask me why
44 if callback then callback(Result) end
45 return Result
46end
47
48function DB.QueryValue(query, callback)
49 if CONNECTED_TO_MYSQL then
50 if DB.MySQLDB and DB.MySQLDB:status() == mysqloo.DATABASE_NOT_CONNECTED then
51 DB.ConnectToMySQL(RP_MySQLConfig.Host, RP_MySQLConfig.Username, RP_MySQLConfig.Password, RP_MySQLConfig.Database_name, RP_MySQLConfig.Database_port)
52 end
53
54 local query = DB.MySQLDB:query(query)
55 local data
56 query.onData = function(Q, D)
57 data = D
58 end
59 query.onSuccess = function()
60 for k,v in pairs(data or {}) do
61 callback(v)
62 return
63 end
64 callback()
65 end
66 query.onError = function(Q, E) Error(E) callback() DB.Log("MySQL Error: ".. E) end
67 query:start()
68 return
69 end
70 callback(sql.QueryValue(query))
71end
72
73function DB.ConnectToMySQL(host, username, password, database_name, database_port)
74 if not mysqloo then Error("MySQL modules aren't installed properly!") DB.Log("MySQL Error: MySQL modules aren't installed properly!") end
75 local databaseObject = mysqloo.connect(host, username, password, database_name, database_port)
76
77 databaseObject.onConnectionFailed = function(msg)
78 Error("Connection failed! " ..tostring(msg))
79 DB.Log("MySQL Error: Connection failed! "..tostring(msg))
80 end
81
82 databaseObject.onConnected = function()
83 DB.Log("MySQL: Connection to external database "..host.." succeeded!")
84 CONNECTED_TO_MYSQL = true
85
86 DB.Init() -- Initialize database
87 end
88 databaseObject:connect()
89 DB.MySQLDB = databaseObject
90end
91
92/*---------------------------------------------------------
93 Database initialize
94 ---------------------------------------------------------*/
95function DB.Init()
96 DB.Begin()
97 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_cvars(var char(20) NOT NULL, value INTEGER NOT NULL, PRIMARY KEY(var));")
98 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_tspawns(id INTEGER NOT NULL, map char(30) NOT NULL, team INTEGER NOT NULL, x NUMERIC NOT NULL, y NUMERIC NOT NULL, z NUMERIC NOT NULL, PRIMARY KEY(id));")
99 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_salaries(steam char(20) NOT NULL, salary INTEGER NOT NULL, PRIMARY KEY(steam));")
100 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_wallets(steam char(20) NOT NULL, amount INTEGER NOT NULL, PRIMARY KEY(steam));")
101 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_jailpositions(map char(30) NOT NULL, x NUMERIC NOT NULL, y NUMERIC NOT NULL, z NUMERIC NOT NULL, lastused NUMERIC NOT NULL, PRIMARY KEY(map, x, y, z));")
102 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_rpnames(steam char(20) NOT NULL, name char(35) NOT NULL, PRIMARY KEY(steam));")
103 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_zspawns(map char(30) NOT NULL, x NUMERIC NOT NULL, y NUMERIC NOT NULL, z NUMERIC NOT NULL);")
104 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_doors(map char(30) NOT NULL, idx INTEGER NOT NULL, title char(25), locked INTEGER(1), disabled INTEGER(1), PRIMARY KEY(map, idx));")
105 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_groupdoors(map char(30) NOT NULL, idx INTEGER NOT NULL, teams char(50) NOT NULL, title char(25) NOT NULL, PRIMARY KEY(map, idx));")
106 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_teamdoors(map char(30) NOT NULL, idx INTEGER NOT NULL, teams char(50) NOT NULL, title char(25) NOT NULL, PRIMARY KEY(map, idx));")
107 DB.Query("CREATE TABLE IF NOT EXISTS darkrp_consolespawns(id INTEGER NOT NULL PRIMARY KEY, map char(30) NOT NULL, x NUMERIC NOT NULL, y NUMERIC NOT NULL, z NUMERIC NOT NULL, pitch NUMERIC NOT NULL, yaw NUMERIC NOT NULL, roll NUMERIC NOT NULL);")
108 DB.Commit()
109
110 DB.SetUpNonOwnableDoors()
111 DB.SetUpGroupOwnableDoors()
112 DB.SetUpTeamOwnableDoors()
113 DB.LoadConsoles()
114
115 DB.Query("SELECT * FROM darkrp_cvars;", function(settings)
116 if settings then
117 local reset = false -- For the old SQLite Databases that had the "key" column instead of "var"
118 for k,v in pairs(settings) do
119 if v.key then reset = true end
120 RunConsoleCommand(v.var or v.key, v.value)
121 end
122 if reset then -- Renaming the column is impossible in SQLite, so do it the hard way
123 DB.Begin()
124 DB.Query("ALTER TABLE darkrp_cvars RENAME TO darkrp_cvars2;")
125 DB.Query("CREATE TABLE darkrp_cvars (var char(20) NOT NULL, value INTEGER NOT NULL, PRIMARY KEY(var));")
126 DB.Query("INSERT INTO darkrp_cvars SELECT * FROM darkrp_cvars2;")
127 DB.Query("DROP TABLE darkrp_cvars2;")
128 DB.Commit()
129 end
130 end
131 end)
132
133 -- Set the lastused of all jailpositions to 0 because the server just started
134 DB.Query("UPDATE darkrp_jailpositions SET lastused = 0;")
135
136 DB.JailPos = {}
137 DB.Query("SELECT COUNT(*) FROM darkrp_jailpositions;", function(num)
138 if num == 0 then
139 DB.CreateJailPos()
140 return
141 end
142 jail_positions = nil
143 DB.Query("SELECT * FROM darkrp_jailpositions;", function(data)
144 DB.JailPos = data or {}
145 end)
146 end)
147
148 DB.TeamSpawns = {}
149 DB.Query("SELECT COUNT(*) FROM darkrp_tspawns;", function(num)
150 if num == 0 then
151 DB.CreateSpawnPos()
152 return
153 end
154
155 team_spawn_positions = nil
156
157 DB.Query("SELECT * FROM darkrp_tspawns;", function(data)
158 DB.TeamSpawns = data or {}
159 end)
160 end)
161
162 zombieSpawns = {}
163 DB.Query("SELECT COUNT(*) FROM darkrp_zspawns;", function(num)
164 if num == 0 then
165 DB.CreateZombiePos()
166 return
167 end
168 DB.Query("SELECT * FROM darkrp_zspawns;", function(data)
169 zombieSpawns = data or {}
170 end)
171 end)
172
173 if CONNECTED_TO_MYSQL then -- In a listen server, the connection with the external database is often made AFTER the listen server host has joined,
174 --so he walks around with the settings from the SQLite database
175 for k,v in pairs(player.GetAll()) do
176 local SteamID = sql.SQLStr(v:SteamID())
177 DB.Query([[SELECT amount, salary, name FROM darkrp_wallets
178 LEFT OUTER JOIN darkrp_salaries ON darkrp_wallets.steam = darkrp_salaries.steam
179 LEFT OUTER JOIN darkrp_rpnames ON darkrp_wallets.steam = darkrp_rpnames.steam
180 WHERE darkrp_wallets.steam = ]].. SteamID ..[[
181 ;]],
182 function(data)
183 if not data then return end
184 local Data = data[1]
185 if Data.name then
186 v:SetDarkRPVar("rpname", Data.name)
187 end
188 if Data.salary then
189 v:SetSelfDarkRPVar("salary", Data.salary)
190 end
191 if Data.amount then
192 v:SetDarkRPVar("money", Data.amount)
193 end
194 end)
195 end
196 end
197end
198
199/*---------------------------------------------------------
200 positions
201 ---------------------------------------------------------*/
202function DB.CreateSpawnPos()
203 local map = string.lower(game.GetMap())
204 if not team_spawn_positions then return end
205
206 for k, v in pairs(team_spawn_positions) do
207 if v[1] == map then
208 DB.StoreTeamSpawnPos(v[2], Vector(v[3], v[4], v[5]))
209 end
210 end
211 team_spawn_positions = nil -- We're done with this now.
212end
213
214function DB.CreateZombiePos()
215 if not zombie_spawn_positions then return end
216 local map = string.lower(game.GetMap())
217
218 local once = false
219 DB.Begin()
220 for k, v in pairs(zombie_spawn_positions) do
221 if map == string.lower(v[1]) then
222 if not once then
223 DB.Query("DELETE FROM darkrp_zspawns;")
224 once = true
225 end
226 DB.Query("INSERT INTO darkrp_zspawns VALUES(" .. sql.SQLStr(map) .. ", " .. v[2] .. ", " .. v[3] .. ", " .. v[4] .. ");")
227 end
228 end
229 DB.Commit()
230end
231
232function DB.StoreZombies()
233 local map = string.lower(game.GetMap())
234 DB.Begin()
235 DB.Query("DELETE FROM darkrp_zspawns WHERE map = " .. sql.SQLStr(map) .. ";", function()
236 for k, v in pairs(zombieSpawns) do
237 local s = string.Explode(" ", v)
238 DB.Query("INSERT INTO darkrp_zspawns VALUES(" .. sql.SQLStr(map) .. ", " .. s[1] .. ", " .. s[2] .. ", " .. s[3] .. ");")
239 end
240 end)
241 DB.Commit()
242end
243
244local FirstZombieSpawn = true
245function DB.RetrieveZombies(callback)
246 if zombieSpawns and table.Count(zombieSpawns) > 0 and not FirstZombieSpawn then callback() return zombieSpawns end
247 FirstZombieSpawn = false
248 zombieSpawns = {}
249 DB.Query("SELECT * FROM darkrp_zspawns WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. ";", function(r)
250 if not r then callback() return end
251 for map, row in pairs(r) do
252 zombieSpawns[map] = tostring(Vector(row.x, row.y, row.z))
253 end
254 callback()
255 end)
256end
257
258function DB.RetrieveRandomZombieSpawnPos()
259 if #zombieSpawns < 1 then return end
260 local r = string.Explode(" ", table.Random(zombieSpawns))
261 r = Vector(r[1], r[2], r[3])
262 if not GAMEMODE:IsEmpty(Vector(r.x, r.y, r.z)) then
263 local found = false
264 for i = 40, 200, 10 do
265 if GAMEMODE:IsEmpty(Vector(r.x, r.y, r.z) + Vector(i, 0, 0)) then
266 found = true
267 return Vector(r.x, r.y, r.z) + Vector(i, 0, 0)
268 end
269 end
270
271 if not found then
272 for i = 40, 200, 10 do
273 if GAMEMODE:IsEmpty(Vector(r.x, r.y, r.z) + Vector(0, i, 0)) then
274 found = true
275 return Vector(r.x, r.y, r.z) + Vector(0, i, 0)
276 end
277 end
278 end
279
280 if not found then
281 for i = 40, 200, 10 do
282 if GAMEMODE:IsEmpty(Vector(r.x, r.y, r.z) + Vector(-i, 0, 0)) then
283 found = true
284 return Vector(r.x, r.y, r.z) + Vector(-i, 0, 0)
285 end
286 end
287 end
288
289 if not found then
290 for i = 40, 200, 10 do
291 if GAMEMODE:IsEmpty(Vector(r.x, r.y, r.z) + Vector(0, -i, 0)) then
292 found = true
293 return Vector(r.x, r.y, r.z) + Vector(0, -i, 0)
294 end
295 end
296 end
297 else
298 return Vector(r.x, r.y, r.z)
299 end
300
301 return Vector(r.x, r.y, r.z) + Vector(0,0,70)
302end
303
304function DB.CreateJailPos()
305 if not jail_positions then return end
306 local map = string.lower(game.GetMap())
307
308 local once = false
309 DB.Begin()
310 for k, v in pairs(jail_positions) do
311 if map == string.lower(v[1]) then
312 if not once then
313 DB.Query("DELETE FROM darkrp_jailpositions;", function()
314 DB.Query("INSERT INTO darkrp_jailpositions VALUES(" .. sql.SQLStr(map) .. ", " .. v[2] .. ", " .. v[3] .. ", " .. v[4] .. ", " .. 0 .. ");")
315 end)
316 DB.JailPos = {}
317 once = true
318 return
319 end
320 DB.Query("INSERT INTO darkrp_jailpositions VALUES(" .. sql.SQLStr(map) .. ", " .. v[2] .. ", " .. v[3] .. ", " .. v[4] .. ", " .. 0 .. ");")
321 end
322 end
323 DB.Commit()
324end
325
326function DB.StoreJailPos(ply, addingPos)
327 local map = string.lower(game.GetMap())
328 local pos = string.Explode(" ", tostring(ply:GetPos()))
329 DB.QueryValue("SELECT COUNT(*) FROM darkrp_jailpositions WHERE map = " .. sql.SQLStr(map) .. ";", function(already)
330 if not already or already == 0 then
331 DB.Query("INSERT INTO darkrp_jailpositions VALUES(" .. sql.SQLStr(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ");", function()
332 DB.Query("SELECT * FROM darkrp_jailpositions;", function(jailpos) DB.JailPos = jailpos end)
333 end)
334 GAMEMODE:Notify(ply, 0, 4, LANGUAGE.created_first_jailpos)
335 else
336 if addingPos then
337 DB.Query("INSERT INTO darkrp_jailpositions VALUES(" .. sql.SQLStr(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ");", function()
338 DB.Query("SELECT * FROM darkrp_jailpositions;", function(jailpos) DB.JailPos = jailpos end)
339 end)
340 GAMEMODE:Notify(ply, 0, 4, LANGUAGE.added_jailpos)
341 else
342 DB.Begin()
343 DB.Query("DELETE FROM darkrp_jailpositions WHERE map = " .. sql.SQLStr(map) .. ";")
344 DB.Query("INSERT INTO darkrp_jailpositions VALUES(" .. sql.SQLStr(map) .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ", " .. 0 .. ");", function()
345 DB.Query("SELECT * FROM darkrp_jailpositions;", function(jailpos) DB.JailPos = jailpos end)
346 end)
347 DB.Commit()
348 GAMEMODE:Notify(ply, 0, 5, LANGUAGE.reset_add_jailpos)
349 end
350 end
351 end)
352end
353
354function DB.RetrieveJailPos()
355 local map = string.lower(game.GetMap())
356 local r = DB.JailPos
357 if not r then return Vector(0,0,0) end
358
359 -- Retrieve the least recently used jail position
360 local now = CurTime()
361 local oldest = 0
362 local ret
363
364 for k, row in pairs(r) do
365 if row.map == map and (now - tonumber(row.lastused)) > oldest then
366 oldest = (now - tonumber(row.lastused))
367 ret = row
368 elseif row.map == map and oldest == 0 then
369 ret = row
370 end
371 end
372 -- Mark that position as having been used just now
373 if ret then DB.Query("UPDATE darkrp_jailpositions SET lastused = " .. CurTime() .. " WHERE map = " .. sql.SQLStr(map) .. " AND x = " .. ret.x .. " AND y = " .. ret.y .. " AND z = " .. ret.z .. ";", function()
374 DB.Query("SELECT * FROM darkrp_jailpositions;", function(jailpos) DB.JailPos = jailpos end)
375 end) end
376 return ret and Vector(ret.x, ret.y, ret.z)
377end
378
379function DB.SaveSetting(setting, value)
380 DB.Query("REPLACE INTO darkrp_cvars VALUES("..sql.SQLStr(setting)..","..sql.SQLStr(value)..");")
381end
382
383function DB.CountJailPos()
384 return table.Count(DB.JailPos or {})
385end
386
387local function FixDarkRPTspawnsTable() -- SQLite only
388 local FixTable = sql.Query("SELECT * FROM darkrp_tspawns;")
389 if not FixTable or (FixTable and FixTable[1] and not FixTable[1].id) then -- The old tspawns table didn't have an 'id' column, this checks if the table is out of date
390 sql.Query("DROP TABLE IF EXISTS darkrp_tspawns;") -- Remove the table and remake it
391 sql.Query("CREATE TABLE IF NOT EXISTS darkrp_tspawns(id INTEGER NOT NULL, map TEXT NOT NULL, team INTEGER NOT NULL, x NUMERIC NOT NULL, y NUMERIC NOT NULL, z NUMERIC NOT NULL, PRIMARY KEY(id));")
392 for k,v in pairs(FixTable or {}) do -- Put back the old data in the new format so the end user will not notice any changes, if there was nothing in the old table then loop through nothing
393 sql.Query("INSERT INTO darkrp_tspawns VALUES(NULL, "..sql.SQLStr(v.map)..", "..v.team..", "..v.x..", "..v.y..", "..v.z..");")
394 end
395 end
396end
397
398function DB.StoreTeamSpawnPos(t, pos)
399 if not CONNECTED_TO_MYSQL then FixDarkRPTspawnsTable() end -- Check if the server doesn't use an out of date version of this table
400 local map = string.lower(game.GetMap())
401 DB.QueryValue("SELECT COUNT(*) FROM darkrp_tspawns WHERE team = " .. t .. " AND map = " .. sql.SQLStr(map) .. ";", function(already)
402 already = tonumber(already)
403 local ID = 0
404 local found = false
405 for k,v in SortedPairs(DB.TeamSpawns or {}) do
406 if tonumber(v.id) == ID + 1 then
407 ID = tonumber(v.id)
408 found = true
409 else
410 ID = ID + 1
411 found = false
412 break
413 end
414 end
415 if found or ID == 0 then ID = ID + 1 end
416
417 if not already or already == 0 then
418 DB.Query("INSERT INTO darkrp_tspawns VALUES(".. ID .. ", ".. sql.SQLStr(map) .. ", " .. t .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ");", function()
419 DB.Query("SELECT * FROM darkrp_tspawns;", function(data) DB.TeamSpawns = data or {} end) end)
420 print(string.format(LANGUAGE.created_spawnpos, team.GetName(t)))
421 else
422 DB.RemoveTeamSpawnPos(t, function() -- Remove everything and create new
423 DB.Query("INSERT INTO darkrp_tspawns VALUES(".. ID .. ", ".. sql.SQLStr(map) .. ", " .. t .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ");", function()
424 DB.Query("SELECT * FROM darkrp_tspawns;", function(data) DB.TeamSpawns = data or {} end) end)
425 end)
426 print(string.format(LANGUAGE.updated_spawnpos, team.GetName(t)))
427 end
428 end)
429end
430
431function DB.AddTeamSpawnPos(t, pos)
432 if not CONNECTED_TO_MYSQL then FixDarkRPTspawnsTable() end -- Check if the server doesn't use an out of date version of this table
433 local map = string.lower(game.GetMap())
434 local ID = 0
435 local found = false
436 for k,v in SortedPairs(DB.TeamSpawns or {}) do
437 if tonumber(v.id) == ID + 1 then
438 ID = tonumber(v.id)
439 found = true
440 else
441 ID = ID + 1
442 found = false
443 break
444 end
445 end
446 if found or ID == 0 then ID = ID + 1 end
447
448 DB.Query("INSERT INTO darkrp_tspawns VALUES(".. ID .. ", " .. sql.SQLStr(map) .. ", " .. t .. ", " .. pos[1] .. ", " .. pos[2] .. ", " .. pos[3] .. ");", function()
449 DB.Query("SELECT * FROM darkrp_tspawns;", function(data) DB.TeamSpawns = data or {} end) end)
450end
451
452function DB.RemoveTeamSpawnPos(t, callback)
453 local map = string.lower(game.GetMap())
454 DB.Query("DELETE FROM darkrp_tspawns WHERE team = "..t..";", function()
455 DB.Query("SELECT * FROM darkrp_tspawns;", function(data) DB.TeamSpawns = data or {} end)
456 if callback then callback() end
457 end)
458end
459
460function DB.RetrieveTeamSpawnPos(ply)
461 local map = string.lower(game.GetMap())
462 local t = ply:Team()
463
464 local returnal = {}
465
466 if DB.TeamSpawns then
467 for k,v in pairs(DB.TeamSpawns) do
468 if v.map == map and tonumber(v.team) == t then
469 table.insert(returnal, Vector(v.x, v.y, v.z))
470 end
471 end
472 return (table.Count(returnal) > 0 and returnal) or nil
473 end
474end
475
476/*---------------------------------------------------------
477Players
478 ---------------------------------------------------------*/
479function DB.StoreRPName(ply, name)
480 if not name or string.len(name) < 2 then return end
481 ply:SetDarkRPVar("rpname", name)
482 DB.Query("REPLACE INTO darkrp_rpnames VALUES(" .. sql.SQLStr(ply:SteamID()) .. ", " .. sql.SQLStr(name) .. ");")
483end
484
485function DB.RetrieveRPNames(ply, name, callback)
486 DB.Query("SELECT COUNT(*) AS count FROM darkrp_rpnames WHERE name = "..sql.SQLStr(name)..
487 " AND steam <> 'UNKNOWN' AND steam <> 'STEAM_ID_PENDING'"..
488 " AND steam <> "..sql.SQLStr(ply:SteamID())..";", function(r)
489 callback(tonumber(r[1].count) > 0)
490 end)
491end
492
493function DB.RetrieveRPName(ply, callback)
494 DB.QueryValue("SELECT name FROM darkrp_rpnames WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";", callback)
495end
496
497function DB.StoreMoney(ply, amount)
498 if not ValidEntity(ply) then return end
499 if amount < 0 then return end
500 ply:SetDarkRPVar("money", math.floor(amount))
501
502 local steamID = ply:SteamID()
503 DB.Query("REPLACE INTO darkrp_wallets VALUES(" .. sql.SQLStr(steamID) .. ", " .. math.floor(amount) .. ");")
504end
505
506function DB.RetrieveMoney(ply) -- This is only run once when the player joins, there's no need for a cache unless the player keeps rejoining.
507 if not ValidEntity(ply) then return 0 end
508 local steamID = ply:SteamID()
509 local startingAmount = GetConVarNumber("startingmoney") or 500
510
511 DB.QueryValue("SELECT amount FROM darkrp_wallets WHERE steam = " .. sql.SQLStr(ply:SteamID()) .. ";", function(r)
512 if r then
513 ply:SetDarkRPVar("money", math.floor(r))
514 else
515 -- No record yet, setting starting cash to 500
516 DB.StoreMoney(ply, startingAmount)
517 end
518 end)
519end
520
521function DB.ResetAllMoney(ply,cmd,args)
522 if not ply:IsSuperAdmin() then return end
523 DB.Query("DELETE FROM darkrp_wallets;")
524 for k,v in pairs(player.GetAll()) do
525 v:SetDarkRPVar("money", GetConVarNumber("startingmoney") or 500)
526 end
527 if ply:IsPlayer() then
528 GAMEMODE:NotifyAll(0,4, string.format(LANGUAGE.reset_money, ply:Nick()))
529 else
530 GAMEMODE:NotifyAll(0,4, string.format(LANGUAGE.reset_money, "Console"))
531 end
532end
533concommand.Add("rp_resetallmoney", DB.ResetAllMoney)
534
535function DB.PayPlayer(ply1, ply2, amount)
536 if not ValidEntity(ply1) or not ValidEntity(ply2) then return end
537 ply1:AddMoney(-amount)
538 ply2:AddMoney(amount)
539end
540
541function DB.StoreSalary(ply, amount)
542 local steamID = ply:SteamID()
543 ply:SetSelfDarkRPVar("salary", math.floor(amount))
544 DB.Query("REPLACE INTO darkrp_salaries VALUES(" .. sql.SQLStr(steamID) .. ", " .. math.floor(amount) .. ");")
545
546 return amount
547end
548
549function DB.RetrieveSalary(ply, callback)
550 if not ValidEntity(ply) then return 0 end
551 local steamID = ply:SteamID()
552 local normal = GetConVarNumber("normalsalary")
553 if ply.DarkRPVars.salary then return callback and callback(ply.DarkRPVars.salary) end -- First check the cache.
554
555 DB.QueryValue("SELECT salary FROM darkrp_salaries WHERE steam = " .. sql.SQLStr(steamID) .. ";", function(r)
556 if not r then
557 ply:SetSelfDarkRPVar("salary", normal)
558 callback(normal)
559 else
560 callback(r)
561 end
562 end)
563end
564
565/*---------------------------------------------------------
566 Doors
567 ---------------------------------------------------------*/
568function DB.StoreDoorOwnability(ent)
569 local map = string.lower(game.GetMap())
570 ent.DoorData = ent.DoorData or {}
571 local nonOwnable = ent.DoorData.NonOwnable
572 DB.QueryValue("SELECT locked FROM darkrp_doors WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";", function(r)
573 if r and not nonOwnable then
574 DB.Query("UPDATE darkrp_doors SET disabled = 0 WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";")
575 elseif nonOwnable then
576 DB.Query("REPLACE INTO darkrp_doors VALUES(" .. sql.SQLStr(map) .. ", " .. ent:EntIndex() .. ", " .. sql.SQLStr(ent.DoorData.title or "") .. ", "..(tobool(r) and 1 or 0)..", 1);")
577 end
578 end)
579end
580
581function DB.StoreNonOwnableDoorTitle(ent, text)
582 ent.DoorData = ent.DoorData or {}
583 ent.DoorData.title = text
584 DB.Query("UPDATE darkrp_doors SET title = " .. sql.SQLStr(text) .. " WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. " AND idx = " .. ent:EntIndex() .. ";")
585end
586
587function DB.SetUpNonOwnableDoors()
588 DB.Query("SELECT idx, title, locked, disabled FROM darkrp_doors WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. ";", function(r)
589 if not r then return end
590
591 for _, row in pairs(r) do
592 local e = ents.GetByIndex(tonumber(row.idx))
593 if ValidEntity(e) then
594 e.DoorData = e.DoorData or {}
595 e.DoorData.NonOwnable = tobool(row.disabled)
596 e:Fire((tobool(row.locked) and "" or "un").."lock", "", 0)
597 e.DoorData.title = row.title
598 end
599 end
600 end)
601end
602
603function DB.StoreGroupDoorOwnability(ent)
604 local map = string.lower(game.GetMap())
605 ent.DoorData = ent.DoorData or {}
606
607 DB.QueryValue("SELECT COUNT(*) FROM darkrp_groupdoors WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";", function(r)
608 r = tonumber(r)
609 if not r then return end
610
611 if r > 0 and not ent.DoorData.GroupOwn then
612 DB.Query("DELETE FROM darkrp_groupdoors WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";")
613 elseif r == 0 and ent.DoorData.GroupOwn then
614 DB.Query("INSERT INTO darkrp_groupdoors VALUES(" .. sql.SQLStr(map) .. ", " .. ent:EntIndex() .. ", " .. sql.SQLStr(ent.DoorData.GroupOwn) .. ", " .. sql.SQLStr(ent.DoorData.title or "") .. ");")
615 elseif r == 1 then
616 DB.Query("UPDATE darkrp_groupdoors SET teams = "..sql.SQLStr(ent.DoorData.GroupOwn) .. " WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";")
617 end
618 end)
619end
620
621function DB.StoreTeamDoorOwnability(ent)
622 local map = string.lower(game.GetMap())
623 ent.DoorData = ent.DoorData or {}
624
625 DB.QueryValue("SELECT COUNT(*) FROM darkrp_teamdoors WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";", function(r)
626 r = tonumber(r)
627 if not r then return end
628
629 if r > 0 and not ent.DoorData.TeamOwn then
630 DB.Query("DELETE FROM darkrp_teamdoors WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() ..";")
631 elseif r == 0 and ent.DoorData.TeamOwn then
632 DB.Query("INSERT INTO darkrp_teamdoors VALUES(" .. sql.SQLStr(map) .. ", " .. ent:EntIndex() .. ", " .. sql.SQLStr(ent.DoorData.TeamOwn) .. ", " .. sql.SQLStr(ent.DoorData.Title or "") .. ");")
633 elseif r == 1 then
634 DB.Query("UPDATE darkrp_teamdoors SET teams = " .. sql.SQLStr(ent.DoorData.TeamOwn) .. " WHERE map = " .. sql.SQLStr(map) .. " AND idx = " .. ent:EntIndex() .. ";")
635 end
636 end)
637end
638
639function DB.StoreGroupOwnableDoorTitle(ent, text)
640 DB.Query("UPDATE darkrp_groupdoors SET title = " .. sql.SQLStr(text) .. " WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. " AND idx = " .. ent:EntIndex() .. ";")
641 ent.DoorData = ent.DoorData or {}
642 ent.DoorData.title = text
643end
644
645function DB.StoreTeamOwnableDoorTitle(ent, text)
646 DB.Query("UPDATE darkrp_teamdoors SET title = " .. sql.SQLStr(text) .. " WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. " AND idx = " .. ent:EntIndex() .. ";")
647 ent.DoorData = ent.DoorData or {}
648 ent.DoorData.title = text
649end
650
651function DB.SetUpGroupOwnableDoors()
652 DB.Query("SELECT idx, title, teams FROM darkrp_groupdoors WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. ";", function(r)
653 if not r then return end
654
655 for _, row in pairs(r) do
656 local e = ents.GetByIndex(tonumber(row.idx))
657 if ValidEntity(e) then
658 e.DoorData = e.DoorData or {}
659 e.DoorData.title = row.title
660 e.DoorData.GroupOwn = row.teams
661 end
662 end
663 end)
664end
665
666function DB.SetUpTeamOwnableDoors()
667 DB.Query("SELECT idx, title, teams FROM darkrp_teamdoors WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. ";", function(r)
668 if not r then return end
669
670 for _, row in pairs(r) do
671 local e = ents.GetByIndex(tonumber(row.idx))
672 if ValidEntity(e) then
673 e.DoorData = e.DoorData or {}
674 e.DoorData.title = row.title
675 e.DoorData.TeamOwn = row.teams
676 end
677 end
678 end)
679end
680
681function DB.LoadConsoles()
682 local map = string.lower(game.GetMap())
683 DB.Query("SELECT * FROM darkrp_consolespawns WHERE map = " .. sql.SQLStr(map) .. ";", function(data)
684 if data then
685 for k, v in pairs(data) do
686 local console = ents.Create("darkrp_console")
687 console:SetPos(Vector(tonumber(v.x), tonumber(v.y), tonumber(v.z)))
688 console:SetAngles(Angle(tonumber(v.pitch), tonumber(v.yaw), tonumber(v.roll)))
689 console:Spawn()
690 console.ID = v.id
691 end
692 else -- If there are no custom positions in the database, use the presets.
693 for k,v in pairs(RP_ConsolePositions) do
694 if v[1] == map then
695 local console = ents.Create("darkrp_console")
696 console:SetPos(Vector(RP_ConsolePositions[k][2], RP_ConsolePositions[k][3], RP_ConsolePositions[k][4]))
697 console:SetAngles(Angle(RP_ConsolePositions[k][5], RP_ConsolePositions[k][6], RP_ConsolePositions[k][7]))
698 console:Spawn()
699 console:Activate()
700
701 console.ID = "0"
702 end
703 end
704 end
705 RP_ConsolePositions = nil
706 end)
707end
708
709function DB.CreateConsole(ply, cmd, args)
710 if not ply:IsSuperAdmin() then return end
711
712 local tr = {}
713 tr.start = ply:EyePos()
714 tr.endpos = ply:EyePos() + 95 * ply:GetAimVector()
715 tr.filter = ply
716 local trace = util.TraceLine(tr)
717
718 local console = ents.Create("darkrp_console")
719 console:SetPos(trace.HitPos)
720 console:Spawn()
721 console:Activate()
722
723 DB.QueryValue("SELECT MAX(id) FROM darkrp_consolespawns;", function(Data)
724 console.ID = (tonumber(Data) and tostring(tonumber(Data) + 1)) or "1"
725 end)
726
727 ply:ChatPrint("Console spawned, move and freeze it to save it!")
728end
729concommand.Add("rp_CreateConsole", DB.CreateConsole)
730
731function DB.RemoveConsoles(ply, cmd, args)
732 if not ply:IsSuperAdmin() then return end
733 DB.Query("DELETE FROM darkrp_consolespawns WHERE map = " .. sql.SQLStr(string.lower(game.GetMap())) .. ";")
734end
735concommand.Add("rp_removeallconsoles", DB.RemoveConsoles)
736
737/*---------------------------------------------------------
738 Logging
739 ---------------------------------------------------------*/
740function DB.Log(text, force)
741 if (not util.tobool(GetConVarNumber("logging")) or not text) and not force then return end
742 if not DB.File then -- The log file of this session, if it's not there then make it!
743 if not file.IsDir("DarkRP_logs") then
744 file.CreateDir("DarkRP_logs")
745 end
746 DB.File = "DarkRP_logs/"..os.date("%m_%d_%Y %I_%M %p")..".txt"
747 file.Write(DB.File, os.date().. "\t".. text)
748 return
749 end
750 file.Append(DB.File, "\n"..os.date().. "\t"..(text or ""))
751end