Ugh. Ban mathematics.

Hai,

I’m currently coding a MySQL admin modification (daunting, isn’t it). Yes; this means everything is done through MySQL (pretty much). Logging, banning, player data, times, etc.

However. Ban mathematics has stumped me. I’m unsure of how I should go about saving a date when a player is banned, and then retrieving it and working out whether the time the player was banned for has been reached.

For example:
Minge 1 is banned for 1847 minutes on 2010-02-15 at 13:48:42.

When Minge 1 joins I want to check whether it’s been 1847 minutes since the date on which the minge was banned. However I can’t work anything out that’ll calculate it: I keep hitting mental roadblocks.

Here’s the code I have and have been experimenting with (as you can see from my comments I can’t quite get there…)
I say, myself, that the code is pretty clean so it shouldn’t be hard to read. (: Though I suppose every programmer has his/her opinions on clean code.

[lua]// Check the ban field.
MyBanned, Success, Error = mysql.query( SF.Connection, “SELECT * FROM sf_bans WHERE steamid = '” … pl:SteamID() … “’” );

// Check that it was a success.
if ( !Success ) then

// IF it wasn't display failed and return.
SF.Utility.Debug( "5: MYSQL ERROR: " .. Error );
return;

end

//local Date = SF.Utility.Year … “-” … os.date( “%m” ) … “-” … os.date( “%d” ) … " " … os.date( “%H” ) … “:” … os.date( “%M” ) … “:” … os.date( “%S” );

// Get data from the query.
local BanDate = MyBanned[ 1 ][ 9 ];
local BanLength = MyBanned[ 1 ][ 10 ];

// Get the current date.
local CurrentDate = SF.Utility.Year … “-” … os.date( “%m” ) … “-” … os.date( “%d” ) … " " … os.date( “%H” ) … “:” … os.date( “%M” ) … “:” … os.date( “%S” );

// minge 1 is banned on 2010-02-15 13:37:15
// minge 1 is banned for 1440 minutes
// to work this out we need to… do some complicated mathematics ^^
// ok first things first, let’s convert these minutes to hours. 60 minutes in 1 hour > minutes /60 = hours.

/*local Hours = BanLength / 60;// Amount of hours the minge is banned for.

// Let’s attempt to add these hours onto the existing hours.
local BannedHours = string.Explode( BanDate, " " )[ 2 ];// This should get the HH:MM:SS bit.
BannedHours = string.Explode( BannedHours[, “:” )[ 1 ]; // This should get the HH bit.

local CalculateHours = BannedHours + Hours;// The minge will be unbanned in this many hours.*/

// Idea scrapped, would of been too inaccurate. We haz to do this through minutes.
// BanLength = minutes.

// BanDate = YYYY-MM-DD HH:MM:SS
local BannedMinutes = string.Explode( BanDate, " " )[ 2 ]; // HH:MM:SS
BannedMinutes = string.Explode( BannedMinutes, “:” )[ 2 ]; // MM

// Add the minutes on…
//local CalculateMinutes = BannedMinutes + BanLength;
// Wait; no. Let’s make it all into seconds, then add those seconds, then from the final seconds work out hh:mm:ss on mm:dd.
// Gah, I’m confused. Let’s go visit FacePunch.[/lua]

Any ideas on some kind of mathematics I could use? I’m completely stumped.

Thanks again,
Elf.

I’m not a programmer but, Mayby give every client a variable such as “isBanned” and then kick clients if their variable is positive.

once again i’m not a programmer, Just trowing out idea’s

Also, maybe something like , Everytime you ban someone, He gets a variable (if he gets banned at 12:00 for like 30 min) “is time 12:30” and if it is, then set (above idea) above variable to negative

Can’t you get the unix/POSIX time of the date they were banned and then just add the bantime?

If the resulting time is above nows time then they’re still banned.

Noh, because it’s when they join and client variables don’t stay all the time.

I suppose that’s an option, I’ll look it up on the Wiki. Thanks for that.

That gets current unix time.

So when they’re banned if I do os.time and throw it into the database. Then when they join it checks os.time with the os.time in the database using http://wiki.garrysmod.com/?title=Os.difftime and if it’s < 0 then they’re unbanned?

Thanks again for your help.

No, you store the time os.time() + banLength and when a client joins, you check if the current time is later than that time.

Ah, alright. Thanks all for your help!

Oh and just out of question, what do you guys think about me releasing a basic MySQL admin modification sometime in the future?

Bit of a hint:
[lua]
local CurrentDate = SF.Utility.Year … “-” … os.date( “%m” ) … “-” … os.date( “%d” ) … " " … os.date( “%H” ) … “:” … os.date( “%M” ) … “:” … os.date( “%S” );
// Is the same as
local CurrentDate = SF.Utility.Year … os.date ("-%m-%d %H:%M:%S")
[/lua]

Ohh, never thought of doing it that way (you’d think PHP would of told me that, really).

Thanks (:

[editline]17:34[/editline]

Just finished & tested it: it seems to work perfectly, thanks a lot.

(Code for the curious):
[lua]
// Make sure they actually have a ban section.
if ( table.Count( UserBan ) > 0 ) then

		// Get the ostime they were banned.
		local OSTime 	= UserBan[ 1 ][ 11 ];
		// Time they were banned for.
		local BanTime 	= ( UserBan[ 1 ][ 10 ] ) * 60;
		
		// Get the current os time.
		local NowTime	= os.time();
		
		// Add the times.
		local Added		= OSTime + BanTime;
		// So if it was 123456 when you were banned and you were banned for 50.. hmm, hang on a minute this needs to be done in seconds.
		// Ok how do you make minutes into seconds, as ban time is minutes.
		// Right.. so 60 seconds in a minute.
		// So if you had 5 * 60 = 300. So 300 seconds. So we want to x the minutes by 60.
		
		// Check that we're now ahead of that time.
		if ( NowTime &lt; Added && OSTime ~= 0 ) then
			
			// Get the reason for the ban.
			local Reason = UserBan[ 1 ][ 8 ];
			
			// Debug.
			SF.Utility.Debug( "Prevented " .. pl:SteamID() .. " (" .. pl:Name() .. ") from playing (" .. Reason .. ").", "1" );
			
			// Kick the mingebag.
			RunConsoleCommand( "kickid", pl:UserID(), Reason );
			
			return;
			
		end
		
	end

[/lua]

As Overv said; I just made a column called ostime and stored os.time() in it. I do * 60 on the “length” field as you ban for minutes and os.time works in seconds. (:
Thanks again!