2016-02-13 11:19:37 +00:00
-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
2016-02-16 13:10:00 +00:00
-- Copyright (C) 2015-2016 Timo 'Timothy' Smit
2016-02-13 11:19:37 +00:00
-- 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 "
2016-09-04 20:05:03 +00:00
local tables = require " luascripts.wolfadmin.util.tables "
2016-02-13 11:19:37 +00:00
local stats = require " luascripts.wolfadmin.players.stats "
2016-08-04 18:11:09 +00:00
local luasql = require " luasql.mysql "
2016-02-13 11:19:37 +00:00
local mysql = { }
local env = assert ( luasql.mysql ( ) )
local con = nil
local cur = nil
2016-03-09 13:46:44 +00:00
-- players
2016-02-13 11:19:37 +00:00
function mysql . addplayer ( guid , ip )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " INSERT INTO `player` (`guid`, `ip`) VALUES (' " .. util.escape ( guid ) .. " ', ' " .. util.escape ( ip ) .. " ') " ) )
2016-02-13 11:19:37 +00:00
end
2016-09-04 17:58:34 +00:00
function mysql . updateplayerip ( guid , ip )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " UPDATE `player` SET `ip`=' " .. util.escape ( ip ) .. " ' WHERE `guid`=' " .. util.escape ( guid ) .. " ' " ) )
2016-02-13 11:19:37 +00:00
end
2016-09-04 17:58:34 +00:00
function mysql . updateplayerlevel ( guid , level )
cur = assert ( con : execute ( " UPDATE `player` SET `level`=' " .. tonumber ( level ) .. " ' WHERE `guid`=' " .. util.escape ( guid ) .. " ' " ) )
end
2016-02-13 11:19:37 +00:00
function mysql . getplayerid ( clientid )
return mysql.getplayer ( stats.get ( clientid , " playerGUID " ) ) [ " id " ]
end
function mysql . getplayer ( guid )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " SELECT * FROM `player` WHERE `guid`=' " .. util.escape ( guid ) .. " ' " ) )
2016-02-13 11:19:37 +00:00
local player = cur : fetch ( { } , " a " )
cur : close ( )
return player
end
2016-09-04 17:58:34 +00:00
-- levels
function mysql . addlevel ( id , name )
cur = assert ( con : execute ( " INSERT INTO `level` (`id`, `name`) VALUES (' " .. tonumber ( id ) .. " ', ' " .. util.escape ( name ) .. " ') " ) )
end
function mysql . updatelevel ( id , name )
cur = assert ( con : execute ( " UPDATE `level` SET `name`=' " .. util.escape ( name ) .. " ' WHERE `id`=' " .. tonumber ( id ) .. " ' " ) )
end
function mysql . getlevel ( id )
cur = assert ( con : execute ( " SELECT * FROM `level` WHERE `id`=' " .. tonumber ( id ) .. " ' " ) )
local level = cur : fetch ( { } , " a " )
cur : close ( )
return level
end
2016-03-09 13:46:44 +00:00
-- aliases
2016-02-13 11:19:37 +00:00
function mysql . addalias ( playerid , alias , lastused )
2016-03-09 10:39:32 +00:00
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) " ) )
2016-02-13 11:19:37 +00:00
end
function mysql . updatealias ( aliasid , lastused )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " UPDATE `alias` SET `lastused`= " .. tonumber ( lastused ) .. " , `used`=`used`+1 WHERE `id`=' " .. util.escape ( aliasid ) .. " ' " ) )
2016-02-13 11:19:37 +00:00
end
2016-03-09 17:21:28 +00:00
function mysql . 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 mysql . 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 ) ) )
2016-08-05 20:15:58 +00:00
2016-02-13 11:19:37 +00:00
local aliases = { }
2016-08-05 20:15:58 +00:00
local row = cur : fetch ( { } , " a " )
while row do
2016-09-04 20:05:03 +00:00
table.insert ( aliases , tables.copy ( row ) )
2016-08-05 20:15:58 +00:00
row = cur : fetch ( row , " a " )
2016-02-13 11:19:37 +00:00
end
2016-08-05 20:15:58 +00:00
2016-02-13 11:19:37 +00:00
cur : close ( )
return aliases
end
function mysql . getaliasbyid ( aliasid )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " SELECT * FROM `alias` WHERE `id`= " .. tonumber ( aliasid ) .. " " ) )
2016-02-13 11:19:37 +00:00
local alias = cur : fetch ( { } , " a " )
cur : close ( )
return alias
end
function mysql . getaliasbyname ( playerid , aliasname )
2016-03-09 10:39:32 +00:00
cur = assert ( con : execute ( " SELECT * FROM `alias` WHERE `player_id`= " .. tonumber ( playerid ) .. " AND `alias`=' " .. util.escape ( aliasname ) .. " ' " ) )
2016-02-13 11:19:37 +00:00
local alias = cur : fetch ( { } , " a " )
cur : close ( )
return alias
end
function mysql . getlastalias ( playerid )
2016-03-09 10:39:32 +00:00
cur = assert ( con : execute ( " SELECT * FROM `alias` WHERE `player_id`= " .. tonumber ( playerid ) .. " ORDER BY `lastused` DESC LIMIT 1 " ) )
2016-02-13 11:19:37 +00:00
local alias = cur : fetch ( { } , " a " )
cur : close ( )
return alias
end
2016-09-04 19:48:03 +00:00
-- level history
2016-02-13 11:19:37 +00:00
function mysql . addsetlevel ( playerid , level , adminid , datetime )
2016-09-04 19:48:03 +00:00
cur = assert ( con : execute ( " INSERT INTO `player_level` (`player_id`, `level`, `admin_id`, `datetime`) VALUES ( " .. tonumber ( playerid ) .. " , " .. tonumber ( level ) .. " , " .. tonumber ( adminid ) .. " , " .. tonumber ( datetime ) .. " ) " ) )
2016-02-13 11:19:37 +00:00
end
2016-03-09 17:21:28 +00:00
function mysql . getlevelscount ( playerid )
2016-09-04 19:48:03 +00:00
cur = assert ( con : execute ( " SELECT COUNT(`id`) AS `count` FROM `player_level` WHERE `player_id`= " .. tonumber ( playerid ) .. " " ) )
2016-03-09 17:21:28 +00:00
local count = tonumber ( cur : fetch ( { } , " a " ) [ " count " ] )
cur : close ( )
return count
end
function mysql . getlevels ( playerid , limit , offset )
limit = limit or 30
offset = offset or 0
2016-09-04 19:48:03 +00:00
cur = assert ( con : execute ( " SELECT * FROM `player_level` WHERE `player_id`= " .. tonumber ( playerid ) .. " LIMIT " .. tonumber ( limit ) .. " OFFSET " .. tonumber ( offset ) ) )
2016-08-05 20:15:58 +00:00
2016-02-13 11:19:37 +00:00
local levels = { }
2016-08-05 20:15:58 +00:00
local row = cur : fetch ( { } , " a " )
2016-02-13 11:19:37 +00:00
2016-08-05 20:15:58 +00:00
while row do
2016-09-04 20:05:03 +00:00
table.insert ( levels , tables.copy ( row ) )
2016-08-05 20:15:58 +00:00
row = cur : fetch ( row , " a " )
2016-02-13 11:19:37 +00:00
end
cur : close ( )
return levels
end
2016-03-09 13:46:44 +00:00
-- warns
2016-02-13 11:19:37 +00:00
function mysql . addwarn ( playerid , reason , adminid , datetime )
2016-03-09 10:39:32 +00:00
cur = assert ( con : execute ( " INSERT INTO `warn` (`player_id`, `reason`, `admin_id`, `datetime`) VALUES ( " .. tonumber ( playerid ) .. " , ' " .. util.escape ( reason ) .. " ', " .. tonumber ( adminid ) .. " , " .. tonumber ( datetime ) .. " ) " ) )
2016-02-13 11:19:37 +00:00
end
function mysql . removewarn ( warnid )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " DELETE FROM `warn` WHERE `id`= " .. tonumber ( warnid ) .. " " ) )
2016-02-13 11:19:37 +00:00
end
2016-03-09 17:21:28 +00:00
function mysql . 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 mysql . 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 ) ) )
2016-02-13 11:19:37 +00:00
local warns = { }
2016-08-05 20:15:58 +00:00
local row = cur : fetch ( { } , " a " )
2016-02-13 11:19:37 +00:00
2016-08-05 20:15:58 +00:00
while row do
2016-09-04 20:05:03 +00:00
table.insert ( warns , tables.copy ( row ) )
2016-08-05 20:15:58 +00:00
row = cur : fetch ( row , " a " )
2016-02-13 11:19:37 +00:00
end
2016-08-05 20:15:58 +00:00
2016-02-13 11:19:37 +00:00
cur : close ( )
return warns
end
function mysql . getwarn ( warnid )
2016-03-09 10:36:05 +00:00
cur = assert ( con : execute ( " SELECT * FROM `warn` WHERE `id`= " .. tonumber ( warnid ) .. " " ) )
2016-02-13 11:19:37 +00:00
local warn = cur : fetch ( { } , " a " )
cur : close ( )
return warn
end
2016-03-09 13:46:44 +00:00
-- maps
function mysql . addmap ( mapname , lastplayed )
cur = assert ( con : execute ( " INSERT INTO `map` (`name`, `lastplayed`) VALUES (' " .. util.escape ( mapname ) .. " ', " .. tonumber ( lastplayed ) .. " ) " ) )
end
function mysql . updatemap ( mapid , lastplayed )
cur = assert ( con : execute ( " UPDATE `map` SET `lastplayed`= " .. tonumber ( lastplayed ) .. " WHERE `id`= " .. tonumber ( mapid ) .. " " ) )
end
function mysql . 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 mysql . 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 mysql . 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 mysql . removeallrecords ( )
cur = assert ( con : execute ( " TRUNCATE `record` " ) )
end
function mysql . removerecords ( mapid )
cur = assert ( con : execute ( " DELETE FROM `record` WHERE `map_id`= " .. tonumber ( mapid ) .. " " ) )
end
function mysql . getrecords ( mapid )
cur = assert ( con : execute ( " SELECT * FROM `record` WHERE `map_id`= " .. tonumber ( mapid ) .. " " ) )
2016-08-05 20:15:58 +00:00
2016-03-09 13:46:44 +00:00
local records = { }
2016-08-05 20:15:58 +00:00
local row = cur : fetch ( { } , " a " )
while row do
2016-03-09 13:46:44 +00:00
local typestr = " "
2016-08-05 20:15:58 +00:00
if tonumber ( row [ " type " ] ) == constants.RECORD_KILL then
2016-03-09 13:46:44 +00:00
typestr = " ks "
2016-08-05 20:15:58 +00:00
elseif tonumber ( row [ " type " ] ) == constants.RECORD_DEATH then
2016-03-09 13:46:44 +00:00
typestr = " ds "
2016-08-05 20:15:58 +00:00
elseif tonumber ( row [ " type " ] ) == constants.RECORD_REVIVE then
2016-03-09 13:46:44 +00:00
typestr = " rs "
end
2016-08-05 20:15:58 +00:00
records [ typestr .. " player " ] = tonumber ( row [ " player_id " ] )
records [ typestr .. " record " ] = tonumber ( row [ " record " ] )
row = cur : fetch ( { } , " a " )
2016-03-09 13:46:44 +00:00
end
cur : close ( )
return records
end
function mysql . 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 mysql . 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
2016-02-13 11:19:37 +00:00
function mysql . isconnected ( )
return ( con ~= nil )
end
function mysql . start ( )
2016-08-07 16:55:26 +00:00
con = env : connect ( settings.get ( " db_database " ) , settings.get ( " db_username " ) , settings.get ( " db_password " ) , settings.get ( " db_hostname " ) , settings.get ( " db_port " ) )
if not con then
return
end
2016-02-13 11:19:37 +00:00
end
function mysql . close ( doSave )
end
2016-09-04 16:22:26 +00:00
return mysql