Here I will post my changelogs and post what I felt in the making process of the function.
Kick and Ban:
These two were easy. I'm planning to do another type of banning that doesn't use the ban list file.
Current Code (30/4/2010)
[lua]function SOM.Kick(ply, cmd, args)
if ply:HasFlag("kick", true) then
local target = SOM.PlayerSearch(ply, args[1])
if target then
local reason = args[2] or "Kicked"
target:Kick(reason)
PrintMessage(HUD_PRINTTALK, target:Nick() .. " was kicked (\"" .. reason .. "\") by " .. ply:Nick())
end
end
end
SOM.AddConCommand("kick", SOM.Kick, "<nick> <reason (O)>", "kick")
function SOM.Ban(ply, cmd, args)
if ply:HasFlag("ban", true) then
local target = SOM.PlayerSearch(ply, args[1])
if target then
local time = tonumber(args[2]) or 0
local txt = "for " .. time .. " minute(s)"
if time == 0 then
txt = "forever"
end
local reason = args[3] or "Banned " .. txt
target:Ban(time, target:Nick() .. " banned by " .. ply:Nick())
target:Kick(reason)
PrintMessage(HUD_PRINTTALK, target:Nick() .. " was banned " .. txt .. " (\"" .. reason .. "\") by " .. ply:Nick())
end
end
end
SOM.AddConCommand("ban", SOM.Ban, "<nick> <time> <reason (O)>", "ban")[/lua]
Code of my draft admin mod's banning system.
[lua]-- My new invented way of banning.
if file.Exists("SAM/bans.txt") then
local loadp = file.Read("SAM/bans.txt")
SAM.Bans = glon.decode( loadp )
end
function SAMSaveBans()
local savep = glon.encode( SAM.Bans )
file.Write("SAM/bans.txt", savep)
end
function SAM.UnBan(ply, cmd, args)
if ply:IsValid() and !ply:SAMIsAdmin() then ply:PrintMessage(3, "Admin priviledges required") return end
if !args[1] then
PrintChat(ply, "ID - Time Remaining")
for k,v in pairs(SAM.Bans) do
PrintChat(ply, SAM.Bans[k]["SteamID"] .. " - " .. SAM.Bans[k]["Time"])
end
return
end
for k,v in pairs(SAM.Bans) do
if args[1] == SAM.Bans[k]["SteamID"] then
SAM.Bans[k] = nil
break
end
return
end
if ply:IsValid() then
BPC( "\"" .. ply:Nick() .. "\" has unbanned SteamID \"" .. args[1] .. "\" from the server." )
else
BPC( "Console has unbanned SteamID \"" .. args[1] .. "\" from the server." )
end
end
concommand.Add("sam_unban", SAM.UnBan)
SAMRegisterCMD("sam_unban", "/ub", "Unbans UniqueID. Leave blank for list.")
function SAM.Ban(ply, cmd, args)
if ply:IsValid() and !ply:SAMIsAdmin() then ply:PrintMessage(3, "Admin priviledges required") return end
if !args[2] then
PrintChat(ply, "Format: (Name) (Time [Minutes]) (Reason)")
return
end
Time = tonumber(args[2])
local Reason = tostring("Banned for " .. Time .. " minutes")
if args[3] != "" then
Reason = string.sub(table.concat(args, " "), string.len(args[1]) + string.len(args[2]) + 2)
end
local Target = pSearch(args[1], ply)
if Target == nil then return end
if ply:IsValid() and !ply:SAMHigherRank(Target) then ply:PrintMessage(3, "Target has a higher rank than you") return end
SAM.Bans[Target:IPAddress()] = {Time = Time * 60, SteamID = Target:SteamID()}
SAMSaveBans()
Target:Kick(tostring(Reason))
if ply:IsValid() then
BPC( "\"" .. ply:Nick() .. "\" has banned \"" .. Target:Nick() .. "\" from the server for " .. Time .. " minute(s)." )
else
BPC( "Console has banned " .. Target:Nick() .. " from the server for " .. Time .. " minute(s)." )
end
end
concommand.Add("sam_ban", SAM.Ban)
SAMRegisterCMD("sam_ban", "/b", "Bans players.")
function SAMBanBase()
for k,v in pairs(SAM.Bans) do
SAM.Bans[k]["Time"] = tonumber(SAM.Bans[k]["Time"]) - 1
if tonumber(SAM.Bans[k]["Time"]) < 1 then
SAM.Bans[k] = nil
GPC( "SteamID \"" .. SAM.Bans[k]["SteamID"] .. "\" has been automatically unbanned from the server as the ban time is up." )
end
end
SAMSaveBans()
end
timer.Create("SAMBanBase", 1, 0, SAMBanBase)[/lua]
Autocomplete like ULX:
This took me about 4 hours trying to figure out how to do it as simple as possible and finally completed it.
Quite messy. But if you know what the functions used in the functions are meant I'm sure you can read it.
[lua]require "datastream"
SOM.AC = {}
-- Cmds
if SERVER then
SOM.Commands = {}
function SOM.AddConCommand(cmd, func, help, flag)
SOM.AC[cmd] = {help, flag}
SOM.Commands[cmd] = func
end
end
if CLIENT then
SOM.AC = {}
local function RecieveAC(_,_,_,AC)
SOM.AC = AC
end
datastream.Hook( "SOMAutoComplete", RecieveAC )
local function RecieveFlags(_,_,_,Flags)
LocalPlayer().Flags = Flags
end
datastream.Hook( "SendMyFlags", RecieveFlags )
function SOM.AutoComplete(cmd, args)
local complete = {}
for k,v in pairs(SOM.AC) do
if table.HasValue(LocalPlayer().Flags, v[2]) and (string.find(k, string.sub(args, 2)) != nil or string.find(string.sub(args, 2), k) != nil or args == " ") then
table.insert(complete, cmd .. " " .. k)
if string.find(string.sub(args, 2), k) then
complete = {cmd .. " " .. k .. " " .. v[1]}
end
end
end
return complete
end
function SOM.Command(ply, cmd, args)
for k,v in pairs(args) do
if string.find(v, " ") != nil then
args[k] = "\"" .. v .. "\"" -- To not remove the quotes when you have more than 2 words in an argument.
end
end
ply:ConCommand("_som " .. string.Implode(" ", args))
end
concommand.Add("som", SOM.Command, SOM.AutoComplete)
end
if SERVER then
function SOM.Command(ply, cmd, args)
if SOM.Commands[args[1]] != nil then
local cargs = {}
table.Merge(cargs, args)
table.remove(cargs, 1)
SOM.Commands[args[1]](ply, cmd, cargs)
end
end
concommand.Add("_som", SOM.Command)
end[/lua]
Complete Autocomplete (30/4/2010)
[IMG]http://img714.imageshack.us/img714/2082/gmflatgrass0001b.jpg[/IMG]
Kick Autocomplete
[IMG]http://img193.imageshack.us/img193/8186/gmflatgrass0002k.jpg[/IMG]
Ban Autocomplete
[IMG]http://img188.imageshack.us/img188/8260/gmflatgrass0003d.jpg[/IMG]
aha I see what you did there
[B]SO[/B]CO[B]M[/B]
:P anyways thats nice
Sorry, you need to Log In to post a reply to this thread.