11if not SERVER then return end
22
3- require (" mysqloo" )
4- include (" sh_weightmanager.lua" )
5- include (" sh_playerweight.lua" )
6-
73local function Message (msg )
8- print (" [TTT WeightSystem MySQL] " .. msg .. " ." )
9- end
10-
11- function DateTime ()
12- return os.date ( " %y-%m-%d %H:%M:%S" )
13- end
14-
15- local db = mysqloo .connect (WeightSystem .Database .Host , WeightSystem .Database .User , WeightSystem .Database .Password , WeightSystem .Database .DatabaseName , WeightSystem .Database .Port )
16- local queue = {}
17-
18-
19- function DatabaseExists ()
20- ExecuteQuery (" SELECT count(*) FROM information_schema.tables WHERE table_name = '" .. WeightSystem .Database .TableName .. " '" , function (data )
21- if table .Count (data ) <= 0 then
22- Message (WeightSystem .Database .TableName .. " table does NOT exist" )
23- return false
24- else
25- Message (WeightSystem .Database .TableName .. " exists" )
26- return true
27- end
28- end )
29-
30- return false -- if it could not run most likely failed.
31- end
32-
33- function ExecuteQuery (str , callback )
34-
35- callback = callback or function () end
36- local q = db :query (str )
37- function q :onSuccess (data )
38- callback (data )
39- end
40-
41- function q :onError (err )
42- local status = db :status ()
43- if status == mysqloo .DATABASE_NOT_CONNECTED or status == mysqloo .DATABASE_CONNECTING then
44- Message (" Inserting missed query into queue: " .. str )
45- table.insert ( queue , { str , callback } )
46-
47- if status == mysqloo .DATABASE_NOT_CONNECTED then
48- Message (" Attempting reconnect to database!" )
49- db :connect ()
50- end
51- return
52- end
53- end
54- q :start ()
4+ print (" [TTT WeightSystem] " .. msg .. " ." )
555end
566
57- function db :onConnected ()
58- Message (" Connected to database" )
59- DatabaseExists ()
60-
61- ExecuteQuery (" CREATE TABLE IF NOT EXISTS " .. WeightSystem .Database .TableName .. " (Id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT, SteamId BIGINT UNSIGNED NOT NULL, Weight SMALLINT UNSIGNED NOT NULL, LastUpdated DATETIME DEFAULT '" .. DateTime () .. " ', PRIMARY KEY (Id))" )
62-
63- Message (" Running commands that were lost" )
64- for k , v in pairs ( queue ) do
65- ExecuteQuery ( v [ 1 ], v [ 2 ] )
66- end
67- queue = {}
68- -- Delete records older then 3 weeks, just clean out the database a little bit.
69- ExecuteQuery (" DELETE FROM " .. WeightSystem .Database .TableName .. " WHERE DATEDIFF(CURDATE(), LastUpdated) >= 21" )
70- end
71-
72- function db :onConnectionFailed (err )
73-
74- for k ,v in pairs (player .GetAll ()) do
75- v :PrintMessage ( HUD_PRINTTALK , " [Weight System] Could not connect to database, please ensure the settings are filled out properly." )
76- end
77-
78- Message (" Connection to database failed" )
79- end
80-
81-
82- function UpdatePlayerWeight (ply , weight )
83- local newWeight = weight or ply :GetWeight ()
84- if ply :IsPlayer () and not ply :IsBot () then
85- ExecuteQuery (" UPDATE " .. WeightSystem .Database .TableName .. " SET Weight = " .. newWeight .. " , LastUpdated = '" .. DateTime () .. " ' WHERE steamid = " .. ply :SteamID64 ())
86- end
87- end
88-
89- -- Connect database on Initialize
90- hook .Add (" Initialize" , " TTTKS_Initialize" , function ()
91- db :connect ()
92- end )
93-
94- -- When player joins check if they have a record already, if not create one and set their default weight.
95- hook .Add (" PlayerInitialSpawn" , " TTTWS_PlayerInitialSpawn" , function (ply )
96- if ply :IsPlayer () and not ply :IsBot () then
97- ExecuteQuery (" SELECT Weight FROM " .. WeightSystem .Database .TableName .. " WHERE SteamId = " .. ply :SteamID64 (), function (data )
98- if table .Count (data ) > 0 then
99- Message (ply :GetName () .. " exists in database, setting player weight to: " .. data [1 ].Weight )
100- ply :SetWeight ( data [1 ].Weight )
101- else
102- local defaultWeight = DefaultWeight ()
103- Message (ply :GetName () .. " does not exist in database, creating user and setting default weight to: " .. defaultWeight )
104- ExecuteQuery (" INSERT INTO " .. WeightSystem .Database .TableName .. " ( SteamId, Weight, LastUpdated ) VALUES ( '" .. ply :SteamID64 () .. " ', " .. defaultWeight .. " , '" .. DateTime () .. " ' )" )
105- ply :SetWeight ( defaultWeight )
106- end
107- end )
108- end
109-
110- end )
7+ local storage = WeightSystem .StorageType
8+ if storage == " sqlite" then
9+ Message (" Loading with SQLite" )
10+ include (" sv_database_sqlite.lua" )
11+ elseif storage == " mysql" then
12+ Message (" Loading with MySQL" )
13+ include (" sv_database_mysql.lua" )
14+ end
0 commit comments