• Checking if player is banned by taking a result from database(SQLite)
    2 replies, posted
Hello there, I didn't really like any of the other admin mods as they didn't quite fit my needs, so I decided to make my own, but I have a slight problem. I am trying to grab boolean "Expired" out of multiple results, and if expired is false but the ban is already expired by the time, I need to set expired to true. Table code: [CODE]sql.Query( "CREATE TABLE playerbans ( Name varchar(255), SteamID varchar(255), Admin varchar(255), Reason varchar(255), Length int, Unban int, Expired boolean )" )[/CODE] The way Length int is determined: input, seconds. The way Unban int is determined: os.time + Length What I'm trying to do: - If "Expired" is set to true, then return ture. - If "Expired" isn't set to true: Check if io.time() > Unban, if it is then return true. Any help would be appreciated.
Here's how I did it in my gamemode, I store and compare the time using unix time(seconds) : [code]function GM:PlayerAuthed(ply, steamid, uniqueid) local query = "SELECT * FROM bans WHERE steamid = '"..ply:SteamID64().."'" GetDataQuery(query, function(tbl) if tbl[1] == nil then return end local bantime = tbl[1].bantime local banduration = tbl[1].banduration if banduration == 0 then ply:Kick("You are permanently banned.") end local minuteduration = (bantime+banduration-os.time())/60 if os.time() < bantime+banduration then ply:Kick("Banned for "..parseAdminTime(minuteduration)) return elseif os.time() >= bantime+banduration then Query("DELETE FROM bans WHERE steamid = '"..ply:SteamID64().."'") return end end) end[/code] The concept is easy, if the current time (os.Time()) is lower than bantime(os.Time() when the ban happened) + banduration(in seconds) then the player is still banned so kick him. If the current time is higher then bantime+banduration then that means the player is unbanned so delete the ban entry in the database. I used pretty much the same code in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/CheckPassword]GM:CheckPassword[/url] to get a second layer. Here's the ban function too, might be useful : [code]function Player:RealBan(time, reason) if not IsValid(self) || self.Rank == 9 then return end if time ~= 0 then local seconds = time * 60 Query("INSERT INTO bans(steamid, steamname, bantime, banduration, reason) VALUES ('"..self:SteamID64().."', '"..self:Nick().."', '"..os.time().."', '"..seconds.."', '"..reason.."')") else Query("INSERT INTO bans(steamid, steamname, bantime, banduration) VALUES ('"..self:SteamID64().."', '"..self:Nick().."', '"..os.time().."', '0')") end self:Kick(reason) end[/code] [editline]17th May 2017[/editline] Just realized you wrote SQLite but the concept is fairly similar.
[QUOTE=bilbasio;52240741]Here's how I did it in my gamemode, I store and compare the time using unix time(seconds) : [code]function GM:PlayerAuthed(ply, steamid, uniqueid) local query = "SELECT * FROM bans WHERE steamid = '"..ply:SteamID64().."'" GetDataQuery(query, function(tbl) if tbl[1] == nil then return end local bantime = tbl[1].bantime local banduration = tbl[1].banduration if banduration == 0 then ply:Kick("You are permanently banned.") end local minuteduration = (bantime+banduration-os.time())/60 if os.time() < bantime+banduration then ply:Kick("Banned for "..parseAdminTime(minuteduration)) return elseif os.time() >= bantime+banduration then Query("DELETE FROM bans WHERE steamid = '"..ply:SteamID64().."'") return end end) end[/code] The concept is easy, if the current time (os.Time()) is lower than bantime(os.Time() when the ban happened) + banduration(in seconds) then the player is still banned so kick him. If the current time is higher then bantime+banduration then that means the player is unbanned so delete the ban entry in the database. I used pretty much the same code in [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/CheckPassword]GM:CheckPassword[/url] to get a second layer. Here's the ban function too, might be useful : [code]function Player:RealBan(time, reason) if not IsValid(self) || self.Rank == 9 then return end if time ~= 0 then local seconds = time * 60 Query("INSERT INTO bans(steamid, steamname, bantime, banduration, reason) VALUES ('"..self:SteamID64().."', '"..self:Nick().."', '"..os.time().."', '"..seconds.."', '"..reason.."')") else Query("INSERT INTO bans(steamid, steamname, bantime, banduration) VALUES ('"..self:SteamID64().."', '"..self:Nick().."', '"..os.time().."', '0')") end self:Kick(reason) end[/code] [editline]17th May 2017[/editline] Just realized you wrote SQLite but the concept is fairly similar.[/QUOTE] Thank you, I wrote something similar to what you did in sqlite realitive to my admin system. Solved.
Sorry, you need to Log In to post a reply to this thread.