· 10 years ago · Mar 09, 2016, 04:51 PM
1/*
2- info: https://facepunch.com/showthread.php?t=1220537
3*/
4
5require( "mysqloo" )
6
7local HOST = "dbmwg.nn.pe"
8local USERNAME = "dbmwgnnp_players"
9local PASS = "tralala1"
10local DB_NAME = USERNAME
11
12local db = mysqloo.connect(HOST,USERNAME ,PASS, DB_NAME, 3306 )
13local queue = {}
14local firstCheck = false
15
16--[[SQL Check]]
17function IsSQLOn()
18
19 if GetGlobalBool("UseSQL") then
20 if GetGlobalBool("SQLInSingleplayer") and !game.IsDedicated() then
21 return true
22 elseif game.IsDedicated() then
23 return true
24 end
25 end
26
27 return false
28
29end
30
31-- [[CONNECT/CREATE]]
32function db:onConnected()
33
34 if !IsSQLOn() then return end
35
36 --[[if mwg table doesn't exist create it]]
37 if !firstCheck then
38 db_create()
39 firstCheck = true
40 end
41
42 for k, v in pairs( queue ) do
43 query( v[ 1 ], v[ 2 ] )
44 end
45 queue = {}
46
47end
48
49function db:onConnectionFailed( err )
50
51 if !IsSQLOn() then return end
52
53 print( "Connection to database failed!" )
54 print(err)
55 Log("Connection to database failed! \n-->"..err,255)
56
57end
58
59db:connect()
60
61function query( sql, callback )
62
63 local q = db:query( sql )
64
65 function TryAgain()
66 table.insert( queue, { sql, callback } )
67 db:connect()
68 end
69
70 if q == nil then
71 TryAgain()
72 return
73 end
74
75
76 function q:onError( err )
77
78 if db:status() == mysqloo.DATABASE_NOT_CONNECTED then
79 TryAgain()
80 return
81 end
82
83 print( "Query Errored, error:", err, " sql: ", sql )
84 Log("Query Errored, error: "..err.." sql: "..sql,255)
85
86
87 end
88
89 function q:onSuccess( data )
90 if callback != nil then
91 callback( data )
92 end
93 end
94
95 q:start()
96
97end
98
99--[[CREATE DB]]
100function db_create()
101
102 query([[
103 CREATE TABLE IF NOT EXISTS MWG_PLAYERS
104 (
105 UniqueID BIGINT,
106 Stats VARCHAR(10000),
107 Loadout VARCHAR(10000),
108 SteamID64 VARCHAR(30),
109 Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
110 PRIMARY KEY (UniqueID));
111 ]])
112
113 query([[
114 CREATE TABLE IF NOT EXISTS MWG_LOGS
115 (
116 `Key` INT(11) AUTO_INCREMENT,
117 Type TINYINT NOT NULL,
118 Log TEXT NOT NULL,
119 Timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
120 PRIMARY KEY (`Key`));
121 ]])
122
123end
124
125--[[INSERT/UPDATE/SELECT]]
126
127// Create a new row in the database for data to be put in
128function db_prepare_row(id,steamid64)
129
130 query([[
131 INSERT IGNORE INTO MWG_PLAYERS
132 VALUES (]]..id..[[,NULL,NULL,NULL,NULL);
133 ]])
134
135 db_addsteamid(id,steamid64)
136end
137
138function db_addlog(msg,type)
139
140 query([[
141 INSERT INTO MWG_LOGS(Type,Log)
142 VALUES (]]..type..[[,']]..msg..[[');
143 ]])
144
145end
146
147// Add steamid64 to add site compatibility
148function db_addsteamid(id,steamid64)
149
150 query([[
151 UPDATE MWG_PLAYERS
152 SET SteamID64 = ']]..steamid64..[['
153 WHERE UniqueID = ]]..id..[[;
154 ]])
155
156end
157
158// Load loadout
159function db_loadloadout(ply)
160
161 query([[
162 SELECT Loadout
163 FROM MWG_PLAYERS
164 WHERE UniqueID = ]]..ply:UniqueID()..[[;
165 ]],function(data)
166 local loadout = data[1].Loadout or nil
167 InitLoadout(ply,loadout)
168 end)
169
170end
171
172// Load stats
173function db_loadstats(ply)
174
175 query([[
176 SELECT Stats
177 FROM MWG_PLAYERS
178 WHERE UniqueID = ]]..ply:UniqueID()..[[;
179 ]],function(data)
180 local stats = data[1].Stats or nil
181 InitStats(ply,stats)
182 end)
183
184end
185
186// Update loadout
187function db_updateloadout(id,loadout)
188
189 if loadout == nil then return end
190
191 query([[
192 UPDATE MWG_PLAYERS
193 SET Loadout = ']]..loadout..[['
194 WHERE UniqueID = ]]..id..[[;
195 ]])
196
197end
198
199// Update stats
200function db_updatestats(id,stats)
201
202 if stats == nil then return end
203
204 query([[
205 UPDATE MWG_PLAYERS
206 SET Stats = ']]..stats..[['
207 WHERE UniqueID = ]]..id..[[;
208 ]])
209
210end