• Print

Author Topic: How to access UTime hours.  (Read 18096 times)

0 Members and 1 Guest are viewing this topic.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
How to access UTime hours.
« on: August 15, 2015, 09:35:08 pm »
Hey, I'm trying to make a command to view a players hours on a server with ulx and utime installed. I don't have access to a test server, so I was wondering if anyone could look this over and see if it'll work.
Code: [Select]
local CATEGORY_NAME = UTime

function ulx.viewHours (calling_ply, target_ply)
local hours = target_ply:GetUTimeTotalTime() / 3600
ULib.tsay(calling_ply, "#A's hours: " ..hours.., target_ply)
end

local seehours = ulx.command(CATEGORY_NAME, "ulx viewhours", ulx.viewHours, "!viewhours")
seehours:addParam:(type=ULib.cmds.PlayerArg)
seehours:defaultAccess( ULib.ACCESS_ADMIN )
seehours:help("View a player's hours on the server.")

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: How to access UTime hours.
« Reply #1 on: August 26, 2015, 04:03:30 am »
Install ulx, ulib, and utime on your singleplayer garry's mod, start up a singleplayer game with multiplayer ability. Add bots, test your addon that way. Though, quickly reviewing your addon, it appears it would work.

It's been a long time now since I've worked with lua, so this might be wrong, but as a suggestion, before you have target_ply:GetUTimeTotalTime(), I would add this:
Code: Lua
  1. local CATEGORY_NAME = UTime
  2.  
  3. function ulx.viewHours (calling_ply, target_ply)
  4. if target_ply:GetUTimeTotalTime() == nil || GetUTimeTotalTime() == nil then return end
  5.                 local hours = target_ply:GetUTimeTotalTime() / 3600
  6.                 ULib.tsay(calling_ply, "#A's hours: " ..hours.., target_ply)
  7. end
  8.  
  9. local seehours = ulx.command(CATEGORY_NAME, "ulx viewhours", ulx.viewHours, "!viewhours")
  10. seehours:addParam:(type=ULib.cmds.PlayerArg)
  11. seehours:defaultAccess( ULib.ACCESS_ADMIN )
  12. seehours:help("View a player's hours on the server.")
« Last Edit: August 26, 2015, 04:09:29 am by Bite That Apple »
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
Re: How to access UTime hours.
« Reply #2 on: August 26, 2015, 09:32:57 pm »
Thanks Apple, but with some minor fine-tuning, I got that part to work. Does anyone know how UTime creates players' uniqueIDs so I could make a steamID version of this?

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: How to access UTime hours.
« Reply #3 on: August 26, 2015, 10:28:24 pm »
Thanks Apple, but with some minor fine-tuning, I got that part to work. Does anyone know how UTime creates players' uniqueIDs so I could make a steamID version of this?

Utime doesn't create UniqueID's, that's just a default feature of Garry's Mod (most likely Source Games). ply:UniqueID(), though the function for it all is located in server side file of utime, where it creates the local database.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
Re: How to access UTime hours.
« Reply #4 on: August 27, 2015, 01:48:52 pm »
How would I access the UniqueID if I only have a player's SteamID to go with (they're not in the server)

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: How to access UTime hours.
« Reply #5 on: August 27, 2015, 03:33:30 pm »
How would I access the UniqueID if I only have a player's SteamID to go with (they're not in the server)

That's (one of) the problem(s) with UniqueIDs. You can only get the UniqueID of a player who is on the server. I wonder why Megiddo hasn't updated UTime to use SteamIDs, because UniqueIDs are absolutely horrible.

Lucky for you, I modified UTime to use SteamIDs. It's a bit more complicated than simply changing the UniqueID to a SteamID64 because I wanted to keep my existing players' times. I manually renamed the utime table to utime_old using SqliteBrowser, but that isn't a very good idea (as I learned, by accidentally corrupting my database). Instead, use this code to do it for you:

WARNING: Do NOT do this with players on your server, because there is a strong likelyhood that UTime will try to log someone's time while you are changing the tables and that might mess up your UTime table, maybe even your entire database if you're unlucky. I strongly recommend you password-protect your server while doing this, and do everything from console. If you want to be even more secure, backup your sv.db file before doing any of this. I am not responsible for any damages to your server, you do this at your own risk.

Save this code to a lua file and put it in garrysmod/lua/:
Code: Lua
  1. sql.Query( "ALTER TABLE utime RENAME TO utime_old;" )
  2. sql.Query( "DROP INDEX IDX_UTIME_PLAYER;" )
  3. sql.Query( "CREATE INDEX IDX_UTIME_OLD_PLAYER ON utime_old ( player DESC );" )

Then run this command in your console, replacing <filename> with what you saved your file as:
Code: [Select]
lua_openscript <filename>
If you want to, you can check that this worked by opening your sv.db file with an Sqlite browser and checking that there is a utime_old table and a IDX_UTIME_OLD_PLAYER index.

Now, for the actual logging (also don't do this with anyone online):

Change line 13 of garrysmod/addons/utime/lua/autorun/sv_utime.lua from this:
Code: Lua
  1. local uid = player:UniqueID()
  2.  
  3. to this:
  4.  
  5. local steamid64 = player:SteamID64()

Then change the if statement on lines 17-28 to this (the new if statement should extend to line 37):
Code: Lua
  1. if row then
  2.   if utime_welcome:GetInt() then
  3.     ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
  4.   end
  5.   sql.Query( "UPDATE utime SET lastvisit = " .. os.time() .. " WHERE player = " .. steamid64 .. ";" )
  6.   time = row.totaltime
  7. else
  8.   row = sql.QueryRow( "SELECT totaltime, lastvisit FROM utime_old WHERE player = " .. uid .. ";" )
  9.   if row then
  10.     time = row.totaltime
  11.     if utime_welcome:GetInt() then
  12.       ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
  13.     end
  14.     sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. steamid64 .. ", " .. time .. ", " .. os.time() .. " );" )
  15.   else
  16.     if utime_welcome:GetInt() then
  17.       ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
  18.     end
  19.     sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. steamid64 .. ", 0, " .. os.time() .. " );" )
  20.   end
  21. end
  22.  

Here is what the whole onJoin() function should look like at the end:
Code: Lua
  1. function onJoin( ply )
  2.         local steamid64 = ply:SteamID64()
  3.         local row = sql.QueryRow( "SELECT totaltime, lastvisit FROM utime WHERE player = " .. uid .. ";" )
  4.         local time = 0
  5.  
  6.         if row then
  7.                 if utime_welcome:GetInt() then
  8.                         ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
  9.                 end
  10.                 sql.Query( "UPDATE utime SET lastvisit = " .. os.time() .. " WHERE player = " .. steamid64 .. ";" )
  11.                 time = row.totaltime
  12.         else
  13.     row = sql.QueryRow( "SELECT totaltime, lastvisit FROM utime_old WHERE player = " .. uid .. ";" )
  14.     if row then
  15.       time = row.totaltime
  16.       if utime_welcome:GetInt() then
  17.         ULib.tsay( ply, "[UTime]Welcome back " .. ply:Nick() .. ", you last played on this server " .. os.date( "%c", row.lastvisit ) )
  18.       end
  19.       sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. steamid64 .. ", " .. time .. ", " .. os.time() .. " );" )
  20.     else
  21.       if utime_welcome:GetInt() then
  22.         ULib.tsay( ply, "[UTime]Welcome to our server " .. ply:Nick() .. "!" )
  23.       end
  24.       sql.Query( "INSERT into utime ( player, totaltime, lastvisit ) VALUES ( " .. steamid64 .. ", 0, " .. os.time() .. " );" )
  25.     end
  26.         end
  27.         ply:SetUTime( time )
  28.         ply:SetUTimeStart( CurTime() )
  29. end

Once you have edited it, save the file and re-upload it to your server. Change the map (ulx map <name> from console, no going on the server yet!) and wait until it has finished loading. Now you can join the server and test it. You should be able to see the new utime table, sorted by SteamID64s, and you should be in it along with your old time.

Feel free to ask me if you have any questions.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
Re: How to access UTime hours.
« Reply #6 on: August 27, 2015, 03:52:21 pm »
I assume if I wanted to use SteamID32 I could just switch that and make it return a string?

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: How to access UTime hours.
« Reply #7 on: August 27, 2015, 04:41:32 pm »
I assume if I wanted to use SteamID32 I could just switch that and make it return a string?
A bit unnecessary and will take up more memory, but yes, if you did it right, it would work fine.  You would have to change the database to have a string instead of number.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: How to access UTime hours.
« Reply #8 on: August 27, 2015, 05:05:12 pm »
As Aaron said, it is uneccesary, requires more memory, and requires more work. The SteamID64s work perfectly fine.
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Bite That Apple

  • Hero Member
  • *****
  • Posts: 858
  • Karma: 416
  • Apple Innovations 2010®
    • Fun 4 Everyone Gaming
Re: How to access UTime hours.
« Reply #9 on: August 27, 2015, 05:21:29 pm »
Well one reason UniqueID's are kinda better is that it's a number, rather then a string.

Strings are just possible mistakes, if I'm correct, it's easier for a CPU to match 1 = 1 rather then '1' = '1'.

SteamID64 bit is really only used for if you want to start using external equipment in my defense. Like if you wanted to be able to check people's utime information through a webpage, and they use steamapi login. I mean as complicated as that sounds, that's the only reason I'd use it. I have many addons (as you can tell in my signature), and many of them are (I have secret addons on my server) that mysql, because I did once have multiple servers. My database saved: UniqueID, SteamID, SteamID64, PlayerName (of course that got updated every time the player rejoined the server), and date of when the new line was made.
Quote from: John F. Kennedy 1963
A man may die, nations may rise and fall, but an idea lives on.

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: How to access UTime hours.
« Reply #10 on: August 27, 2015, 05:25:55 pm »
Yeah, I guess it all just depends on the situation.

SteamID64 can actually be converted to the STEAM_0: version.  There was some function out there that I could probably find.  So saving both is kind of unnecessary as well.  IMO, steamid64 is the way to go with databases.

Offline roastchicken

  • Respected Community Member
  • Sr. Member
  • *****
  • Posts: 476
  • Karma: 84
  • I write code
Re: How to access UTime hours.
« Reply #11 on: August 27, 2015, 05:40:45 pm »
SteamID64 can actually be converted to the STEAM_0: version.  There was some function out there that I could probably find.  So saving both is kind of unnecessary as well.  IMO, steamid64 is the way to go with databases.

Mr. President has one in AWarn2, but I didn't want to just rip his. I'm currently trying to make one from the info I can gather from https://forums.alliedmods.net/showthread.php?t=60899?t=60899.

This is my current code:
Code: Lua
  1. function ConvertSteamID( steamid )
  2.   local parts = string.Explode( ":", steamid )
  3.   local authserver = tonumber( parts[2] ) * 2
  4.   local steamid64 = tonumber( parts[3] )
  5.  
  6.   steamid64 = steamid64 * 2
  7.   steamid64 = steamid64 + 76561197960265728
  8.   steamid64 = steamid64 + authserver
  9.  
  10.   local str = string.format( "%f", steamid64 )
  11.  
  12.   file.Append( "steamid.txt", steamid .. "\n" )
  13.   file.Append( "steamid.txt", authserver .. "\n" )
  14.   file.Append( "steamid.txt", str .. "\n")
  15.  
  16.   str = string.sub( str, 1, string.find(str,'.',1,true)-1 )
  17.  
  18.   file.Append( "steamid.txt", str .. "\n\n")
  19. end
  20.  

It works... almost. I suspect it has something to do with the string.format but it always seems to be off by 1 or 2. In addition, adding anything less than 11 to the steamid64 after 76561197960265728 is added doesn't seem to change the number at all. I'm really confused...
« Last Edit: August 27, 2015, 05:54:15 pm by roastchicken »
Give a man some code and you help him for a day; teach a man to code and you help him for a lifetime.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
Re: How to access UTime hours.
« Reply #12 on: August 27, 2015, 06:33:01 pm »
I would use SteamID32 because it's easier to get than a player's SteamID64 if I want to check his hours and don't have his profile link for steamidfinder or anything like that.

Offline Aaron113

  • Hero Member
  • *****
  • Posts: 803
  • Karma: 102
Re: How to access UTime hours.
« Reply #13 on: August 27, 2015, 07:09:04 pm »
I would use SteamID32 because it's easier to get than a player's SteamID64 if I want to check his hours and don't have his profile link for steamidfinder or anything like that.
You could have Lua convert the regular SteamID easily for you is where I'm getting at.

Offline Aharon Tager

  • Newbie
  • *
  • Posts: 37
  • Karma: 0
    • Barely Legal Gaming
Re: How to access UTime hours.
« Reply #14 on: August 27, 2015, 07:53:02 pm »
Oh ok so to make this I have to read up on sql a bunch but I hope to release this along with Illuminati (Aaron you might remember that one) sometime soon.

  • Print