/
sqlite3.lua
334 lines (237 loc) · 10.3 KB
/
sqlite3.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2016 Timo 'Timothy' Smit
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- at your option any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
local constants = require "luascripts.wolfadmin.util.constants"
local util = require "luascripts.wolfadmin.util.util"
local settings = require "luascripts.wolfadmin.util.settings"
local tables = require "luascripts.wolfadmin.util.tables"
local stats = require "luascripts.wolfadmin.players.stats"
local luasql = require "luasql.sqlite3"
local sqlite3 = {}
local env = assert(luasql.sqlite3())
local con = nil
local cur = nil
-- players
function sqlite3.addplayer(guid, ip)
cur = assert(con:execute("INSERT INTO `player` (`guid`, `ip`) VALUES ('"..util.escape(guid).."', '"..util.escape(ip).."')"))
end
function sqlite3.updateplayerip(guid, ip)
cur = assert(con:execute("UPDATE `player` SET `ip`='"..util.escape(ip).."' WHERE `guid`='"..util.escape(guid).."'"))
end
function sqlite3.updateplayerlevel(id, level)
cur = assert(con:execute("UPDATE `player` SET `level_id`='"..tonumber(level).."' WHERE `id`='"..tonumber(id).."'"))
end
function sqlite3.getplayerid(clientid)
return sqlite3.getplayer(stats.get(clientid, "playerGUID"))["id"]
end
function sqlite3.getplayer(guid)
cur = assert(con:execute("SELECT * FROM `player` WHERE `guid`='"..util.escape(guid).."'"))
local player = cur:fetch({}, "a")
cur:close()
return player
end
-- levels
function sqlite3.addlevel(id, name)
cur = assert(con:execute("INSERT INTO `level` (`id`, `name`) VALUES ('"..tonumber(id).."', '"..util.escape(name).."')"))
end
function sqlite3.updatelevel(id, name)
cur = assert(con:execute("UPDATE `level` SET `name`='"..util.escape(name).."' WHERE `id`='"..tonumber(id).."'"))
end
function sqlite3.getlevel(id)
cur = assert(con:execute("SELECT * FROM `level` WHERE `id`='"..tonumber(id).."'"))
local level = cur:fetch({}, "a")
cur:close()
return level
end
-- aliases
function sqlite3.addalias(playerid, alias, lastused)
cur = assert(con:execute("INSERT INTO `alias` (`player_id`, `alias`, `cleanalias`, `lastused`, `used`) VALUES ("..tonumber(playerid)..", '"..util.escape(alias).."', '"..util.escape(util.removeColors(alias)).."', "..tonumber(lastused)..", 1)"))
end
function sqlite3.updatealias(aliasid, lastused)
cur = assert(con:execute("UPDATE `alias` SET `lastused`="..tonumber(lastused)..", `used`=`used`+1 WHERE `id`='"..util.escape(aliasid).."'"))
end
function sqlite3.getaliasescount(playerid)
cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `alias` WHERE `player_id`="..tonumber(playerid)..""))
local count = tonumber(cur:fetch({}, "a")["count"])
cur:close()
return count
end
function sqlite3.getaliases(playerid, limit, offset)
limit = limit or 30
offset = offset or 0
cur = assert(con:execute("SELECT * FROM `alias` WHERE `player_id`="..tonumber(playerid).." ORDER BY `used` DESC LIMIT "..tonumber(limit).." OFFSET "..tonumber(offset)))
local aliases = {}
local row = cur:fetch({}, "a")
while row do
table.insert(aliases, tables.copy(row))
row = cur:fetch(row, "a")
end
cur:close()
return aliases
end
function sqlite3.getaliasbyid(aliasid)
cur = assert(con:execute("SELECT * FROM `alias` WHERE `id`="..tonumber(aliasid)..""))
local alias = cur:fetch({}, "a")
cur:close()
return alias
end
function sqlite3.getaliasbyname(playerid, aliasname)
cur = assert(con:execute("SELECT * FROM `alias` WHERE `player_id`="..tonumber(playerid).." AND `alias`='"..util.escape(aliasname).."'"))
local alias = cur:fetch({}, "a")
cur:close()
return alias
end
function sqlite3.getlastalias(playerid)
cur = assert(con:execute("SELECT * FROM `alias` WHERE `player_id`="..tonumber(playerid).." ORDER BY `lastused` DESC LIMIT 1"))
local alias = cur:fetch({}, "a")
cur:close()
return alias
end
-- level history
function sqlite3.addsetlevel(playerid, level, adminid, datetime)
cur = assert(con:execute("INSERT INTO `player_level` (`player_id`, `level`, `admin_id`, `datetime`) VALUES ("..tonumber(playerid)..", "..tonumber(level)..", "..tonumber(adminid)..", "..tonumber(datetime)..")"))
end
function sqlite3.getlevelscount(playerid)
cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `player_level` WHERE `player_id`="..tonumber(playerid)..""))
local count = tonumber(cur:fetch({}, "a")["count"])
cur:close()
return count
end
function sqlite3.getlevels(playerid, limit, offset)
limit = limit or 30
offset = offset or 0
cur = assert(con:execute("SELECT * FROM `player_level` WHERE `player_id`="..tonumber(playerid).." LIMIT "..tonumber(limit).." OFFSET "..tonumber(offset)))
local levels = {}
local row = cur:fetch({}, "a")
while row do
table.insert(levels, tables.copy(row))
row = cur:fetch(row, "a")
end
cur:close()
return levels
end
-- warns
function sqlite3.addwarn(playerid, reason, adminid, datetime)
cur = assert(con:execute("INSERT INTO `warn` (`player_id`, `reason`, `admin_id`, `datetime`) VALUES ("..tonumber(playerid)..", '"..util.escape(reason).."', "..tonumber(adminid)..", "..tonumber(datetime)..")"))
end
function sqlite3.removewarn(warnid)
cur = assert(con:execute("DELETE FROM `warn` WHERE `id`="..tonumber(warnid)..""))
end
function sqlite3.getwarnscount(playerid)
cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `warn` WHERE `player_id`="..tonumber(playerid)..""))
local count = tonumber(cur:fetch({}, "a")["count"])
cur:close()
return count
end
function sqlite3.getwarns(playerid, limit, offset)
limit = limit or 30
offset = offset or 0
cur = assert(con:execute("SELECT * FROM `warn` WHERE `player_id`="..tonumber(playerid).." LIMIT "..tonumber(limit).." OFFSET "..tonumber(offset)))
local warns = {}
local row = cur:fetch({}, "a")
while row do
table.insert(warns, tables.copy(row))
row = cur:fetch(row, "a")
end
cur:close()
return warns
end
function sqlite3.getwarn(warnid)
cur = assert(con:execute("SELECT * FROM `warn` WHERE `id`="..tonumber(warnid)..""))
local warn = cur:fetch({}, "a")
cur:close()
return warn
end
-- maps
function sqlite3.addmap(mapname, lastplayed)
cur = assert(con:execute("INSERT INTO `map` (`name`, `lastplayed`) VALUES ('"..util.escape(mapname).."', "..tonumber(lastplayed)..")"))
end
function sqlite3.updatemap(mapid, lastplayed)
cur = assert(con:execute("UPDATE `map` SET `lastplayed`="..tonumber(lastplayed).." WHERE `id`="..tonumber(mapid)..""))
end
function sqlite3.getmap(mapname)
cur = assert(con:execute("SELECT * FROM `map` WHERE `name`='"..util.escape(mapname).."'"))
local map = cur:fetch({}, "a")
cur:close()
return map
end
-- records
function sqlite3.addrecord(mapid, recorddate, recordtype, record, playerid)
cur = assert(con:execute("INSERT INTO `record` (`map_id`, `date`, `type`, `record`, `player_id`) VALUES ("..tonumber(mapid)..", "..tonumber(recorddate)..", "..tonumber(recordtype)..", "..tonumber(record)..", "..tonumber(playerid)..")"))
end
function sqlite3.updaterecord(mapid, recorddate, recordtype, record, playerid)
cur = assert(con:execute("UPDATE `record` SET `date`="..tonumber(recorddate)..", `record`="..tonumber(record)..", `player_id`="..tonumber(playerid).." WHERE `map_id`="..tonumber(mapid).." AND `type`="..tonumber(recordtype)..""))
end
function sqlite3.removeallrecords()
cur = assert(con:execute("TRUNCATE `record`"))
end
function sqlite3.removerecords(mapid)
cur = assert(con:execute("DELETE FROM `record` WHERE `map_id`="..tonumber(mapid)..""))
end
function sqlite3.getrecords(mapid)
cur = assert(con:execute("SELECT * FROM `record` WHERE `map_id`="..tonumber(mapid)..""))
local records = {}
local row = cur:fetch({}, "a")
while row do
local typestr = ""
if tonumber(row["type"]) == constants.RECORD_KILL then
typestr = "ks"
elseif tonumber(row["type"]) == constants.RECORD_DEATH then
typestr = "ds"
elseif tonumber(row["type"]) == constants.RECORD_REVIVE then
typestr = "rs"
end
records[typestr.."player"] = tonumber(row["player_id"])
records[typestr.."record"] = tonumber(row["record"])
row = cur:fetch({}, "a")
end
cur:close()
return records
end
function sqlite3.getrecordscount(mapid)
cur = assert(con:execute("SELECT COUNT(*) AS `count` FROM `record` WHERE `map_id`="..tonumber(mapid)..""))
local count = cur:fetch({}, "a")
cur:close()
return count["count"]
end
function sqlite3.getrecord(mapid, recordtype)
cur = assert(con:execute("SELECT * FROM `record` WHERE `map_id`="..tonumber(mapid).." AND `type`="..tonumber(recordtype)..""))
local row = cur:fetch({}, "a")
cur:close()
if row then
local record, typestr = {}, ""
if tonumber(row["type"]) == constants.RECORD_KILL then
typestr = "ks"
elseif tonumber(row["type"]) == constants.RECORD_DEATH then
typestr = "ds"
elseif tonumber(row["type"]) == constants.RECORD_REVIVE then
typestr = "rs"
end
record[typestr.."player"] = tonumber(row["player"])
record[typestr.."record"] = tonumber(row["record"])
return record
end
end
function sqlite3.isconnected()
return (con ~= nil)
end
function sqlite3.start()
con = env:connect(settings.get("db_file"))
if not con then
return
end
-- enable foreign key enforcement
cur = assert(con:execute("PRAGMA foreign_keys=1"))
end
function sqlite3.close(doSave)
end
return sqlite3