mirror of
https://github.com/etlegacy/wolfadmin.git
synced 2025-04-16 13:51:59 +00:00
Implemented pagination for !showbans, !listlevels and !listaliases (issue #54)
This commit is contained in:
parent
f286ab78bc
commit
7445e7b173
6 changed files with 113 additions and 13 deletions
|
@ -34,6 +34,18 @@ function warns.get(clientId, warnId)
|
|||
end
|
||||
end
|
||||
|
||||
function warns.getcount(clientId)
|
||||
local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"]
|
||||
|
||||
return db.getwarnscount(playerid)
|
||||
end
|
||||
|
||||
function warns.getlimit(clientId, start, limit)
|
||||
local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"]
|
||||
|
||||
return db.getwarns(playerid, start, limit)
|
||||
end
|
||||
|
||||
function warns.add(clientId, reason, adminId, datetime)
|
||||
local playerid = db.getplayer(stats.get(clientId, "playerGUID"))["id"]
|
||||
local adminid = db.getplayer(stats.get(adminId, "playerGUID"))["id"]
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
local util = require "luascripts.wolfadmin.util.util"
|
||||
local settings = require "luascripts.wolfadmin.util.settings"
|
||||
local pagination = require "luascripts.wolfadmin.util.pagination"
|
||||
local db = require "luascripts.wolfadmin.db.db"
|
||||
local commands = require "luascripts.wolfadmin.commands.commands"
|
||||
local stats = require "luascripts.wolfadmin.players.stats"
|
||||
|
@ -57,7 +58,10 @@ function commandListAliases(clientId, cmdArguments)
|
|||
end
|
||||
|
||||
local player = db.getplayer(stats.get(cmdClient, "playerGUID"))["id"]
|
||||
local aliases = db.getaliases(player)
|
||||
|
||||
local count = db.getaliasescount(player)
|
||||
local limit, offset = pagination.calculate(count, 30, tonumber(cmdArguments[2]))
|
||||
local aliases = db.getaliases(player, limit, offset)
|
||||
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dAliases for ^7"..et.gentity_get(cmdClient, "pers.netname").."^d:\";")
|
||||
for _, alias in pairs(aliases) do
|
||||
|
@ -67,8 +71,9 @@ function commandListAliases(clientId, cmdArguments)
|
|||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^7"..spaces..alias["alias"].." ^7"..string.format("%8s", alias["used"]).." times\";")
|
||||
end
|
||||
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \"^dlistaliases: ^9"..#aliases.." known aliases for ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9 (open console for the full list).\";")
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^9Showing results ^7"..(offset + 1).." ^9- ^7"..limit.." ^9of ^7"..count.."^9.\";")
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \"^dlistaliases: ^9aliases for ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9were printed to the console.\";")
|
||||
|
||||
return true
|
||||
end
|
||||
commands.addadmin("listaliases", commandListAliases, "f", "display all known aliases for a player", "^9[^3name|slot#^9]", function() return (settings.get("db_type") == "cfg") end)
|
||||
commands.addadmin("listaliases", commandListAliases, "f", "display all known aliases for a player", "^9[^3name|slot#^9] ^9(^hoffset^9)", function() return (settings.get("db_type") == "cfg") end)
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
local util = require "luascripts.wolfadmin.util.util"
|
||||
local settings = require "luascripts.wolfadmin.util.settings"
|
||||
local pagination = require "luascripts.wolfadmin.util.pagination"
|
||||
local db = require "luascripts.wolfadmin.db.db"
|
||||
local commands = require "luascripts.wolfadmin.commands.commands"
|
||||
local stats = require "luascripts.wolfadmin.players.stats"
|
||||
|
@ -73,7 +74,10 @@ function commandListLevels(clientId, cmdArguments)
|
|||
end
|
||||
|
||||
local player = db.getplayer(stats.get(cmdClient, "playerGUID"))["id"]
|
||||
local levels = db.getlevels(player)
|
||||
|
||||
local count = db.getlevelscount(player)
|
||||
local limit, offset = pagination.calculate(count, 30, tonumber(cmdArguments[2]))
|
||||
local levels = db.getlevels(player, limit, offset)
|
||||
|
||||
if not (levels and #levels > 0) then
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dlistlevels: ^9there are no recorded levels for player ^7"..et.gentity_get(cmdClient, "pers.netname").."^9.\";")
|
||||
|
@ -83,9 +87,10 @@ function commandListLevels(clientId, cmdArguments)
|
|||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^f"..string.format("%4s", level["id"]).." ^7"..string.format("%-20s", util.removeColors(db.getlastalias(level["admin_id"])["alias"])).." ^f"..os.date("%d/%m/%Y", level["datetime"]).." ^7"..level["level"].."\";")
|
||||
end
|
||||
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^9Showing results ^7"..(offset + 1).." ^9- ^7"..limit.." ^9of ^7"..count.."^9.\";")
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \"^dlistlevels: ^9recorded levels for ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9were printed to the console.\";")
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
commands.addadmin("listlevels", commandListLevels, "s", "display all levels on the server")
|
||||
commands.addadmin("listlevels", commandListLevels, "s", "display all levels on the server", (settings.get("db_type") == "cfg" and nil or "^9(^3name|slot#^9) ^9(^hoffset^9)"))
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
local util = require "luascripts.wolfadmin.util.util"
|
||||
local settings = require "luascripts.wolfadmin.util.settings"
|
||||
local pagination = require "luascripts.wolfadmin.util.pagination"
|
||||
local db = require "luascripts.wolfadmin.db.db"
|
||||
|
||||
local commands = require "luascripts.wolfadmin.commands.commands"
|
||||
|
@ -47,7 +48,9 @@ function commandShowWarns(clientId, cmdArguments)
|
|||
return true
|
||||
end
|
||||
|
||||
local playerWarns = warns.get(cmdClient)
|
||||
local count = warns.getcount(cmdClient)
|
||||
local limit, offset = pagination.calculate(count, 30, tonumber(cmdArguments[2]))
|
||||
local playerWarns = warns.getlimit(cmdClient, limit, offset)
|
||||
|
||||
if not (playerWarns and #playerWarns > 0) then
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^dshowwarns: ^9there are no warnings for player ^7"..et.gentity_get(cmdClient, "pers.netname").."^9.\";")
|
||||
|
@ -57,9 +60,10 @@ function commandShowWarns(clientId, cmdArguments)
|
|||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^f"..string.format("%4s", warn["id"]).." ^7"..string.format("%-20s", util.removeColors(db.getlastalias(warn["admin_id"])["alias"])).." ^f"..os.date("%d/%m/%Y", warn["datetime"]).." ^7"..warn["reason"].."\";")
|
||||
end
|
||||
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"^9Showing results ^7"..(offset + 1).." ^9- ^7"..limit.." ^9of ^7"..count.."^9.\";")
|
||||
et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat "..clientId.." \"^dshowwarns: ^9warnings for ^7"..et.gentity_get(cmdClient, "pers.netname").." ^9were printed to the console.\";")
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
commands.addadmin("showwarns", commandShowWarns, "R", "display warnings for a specific player", "^9[^3name|slot#^9]", function() return (settings.get("g_warnHistory") == 0 or settings.get("db_type") == "cfg") end)
|
||||
commands.addadmin("showwarns", commandShowWarns, "R", "display warnings for a specific player", "^9[^3name|slot#^9] ^9(^hoffset^9)", function() return (settings.get("g_warnHistory") == 0 or settings.get("db_type") == "cfg") end)
|
|
@ -64,8 +64,20 @@ function mysql.updatealias(aliasid, lastused)
|
|||
cur = assert(con:execute("UPDATE `alias` SET `lastused`="..tonumber(lastused)..", `used`=`used`+1 WHERE `id`='"..util.escape(aliasid).."'"))
|
||||
end
|
||||
|
||||
function mysql.getaliases(playerid)
|
||||
cur = assert(con:execute("SELECT * FROM `alias` WHERE `player_id`="..tonumber(playerid).." ORDER BY `used` DESC"))
|
||||
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)))
|
||||
local numrows = cur:numrows()
|
||||
local aliases = {}
|
||||
|
||||
|
@ -110,8 +122,20 @@ function mysql.addsetlevel(playerid, level, adminid, datetime)
|
|||
cur = assert(con:execute("INSERT INTO `level` (`player_id`, `level`, `admin_id`, `datetime`) VALUES ("..tonumber(playerid)..", "..tonumber(level)..", "..tonumber(adminid)..", "..tonumber(datetime)..")"))
|
||||
end
|
||||
|
||||
function mysql.getlevels(playerid)
|
||||
cur = assert(con:execute("SELECT * FROM `level` WHERE `player_id`="..tonumber(playerid)..""))
|
||||
function mysql.getlevelscount(playerid)
|
||||
cur = assert(con:execute("SELECT COUNT(`id`) AS `count` FROM `level` WHERE `player_id`="..tonumber(playerid)..""))
|
||||
|
||||
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
|
||||
|
||||
cur = assert(con:execute("SELECT * FROM `level` WHERE `player_id`="..tonumber(playerid).." LIMIT "..tonumber(limit).." OFFSET "..tonumber(offset)))
|
||||
local numrows = cur:numrows()
|
||||
local levels = {}
|
||||
|
||||
|
@ -133,8 +157,21 @@ function mysql.removewarn(warnid)
|
|||
cur = assert(con:execute("DELETE FROM `warn` WHERE `id`="..tonumber(warnid)..""))
|
||||
end
|
||||
|
||||
function mysql.getwarns(playerid)
|
||||
cur = assert(con:execute("SELECT * FROM `warn` WHERE `player_id`="..tonumber(playerid)..""))
|
||||
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)))
|
||||
|
||||
local numrows = cur:numrows()
|
||||
local warns = {}
|
||||
|
||||
|
|
37
luascripts/util/pagination.lua
Normal file
37
luascripts/util/pagination.lua
Normal file
|
@ -0,0 +1,37 @@
|
|||
|
||||
-- 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 pagination = {}
|
||||
|
||||
function pagination.calculate(count, limit, offset)
|
||||
limit = limit or 30
|
||||
offset = offset or 0
|
||||
|
||||
if offset < 0 then
|
||||
offset = count + offset
|
||||
end
|
||||
|
||||
limit = limit + offset
|
||||
|
||||
if limit > count then
|
||||
limit = count
|
||||
end
|
||||
|
||||
return limit, offset
|
||||
end
|
||||
|
||||
return pagination
|
Loading…
Reference in a new issue