Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Uptime and reading from files

Uptime and reading from files

It is now over 60 days since the last post. This thread is closed.     Refresh page


Pages: 1 2  

Posted by Zeno   USA  (2,871 posts)  Bio
Date Thu 25 Nov 2004 05:06 AM (UTC)
Message
I'm looking for a way to show the MUD uptime in the "time" command, but not sure how to record it. Increase a variable saved in system data every update?

Also, with the "last" command, I am wondering if there is a way to also display the IP that was last logged on, along with the last logon time. If not, I may just install the finger snippet for Immortals.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Thu 25 Nov 2004 06:15 AM (UTC)
Message
The MUD stores its startup time, doesn't it? Can't you just take current time, subtract startup time, and there you go? Making a whole new variable in update really seems like a lot of effort for a really simple problem - even if the MUD doesn't already store the startup time (and I think it does) you can just store it yourself.

For your modification to 'last', you'll have to go in and actually read the file, I think. Just make sure you don't save it or anything, because that will break the last command, which, IIRC, works by looking at when the file was last written.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #2 on Thu 25 Nov 2004 08:21 PM (UTC)
Message
Hmm, not sure how to do that if the time is read as a string. str_boot_time is also a string. Could you provide an example of how to do this?

Meh. Might as well just install finger, more useful.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Sat 27 Nov 2004 12:16 AM (UTC)
Message
Here's what I have in comm.cpp:
secBootTime = time(0);
strcpy( str_boot_time, ctime( &secCurrentTime ) );

Now, I've edited it all, so it's probably not called secBootTime on your version... but I'm pretty darn sure it's still there one way or another.

So, to do the uptime, you would use time(0), subtract from that the boot time, and you obtain the number of seconds you have been running. (To be perfectly correct, you should use the difftime function, but since a time_t is a number on any POSIX system, normal subtraction is fine.)

From the number of seconds, getting the number of days/hours/minutes should be fairly straightforward.

In fact, I think I'm going to add this functionality to my MUD - good idea. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #4 on Sat 27 Nov 2004 02:25 AM (UTC)
Message
Right, with a little extra help from a friend, I've got it working for those who also wish to use it. I put this in my time function:

    time_t current;
    time_t diff;

    current = time(0);
    diff = current - boot_time;
    ch_printf( ch, "The MUD has been up:      %d hours, %d minutes, %d seconds.\n\r", diff/3600, diff/60, diff%60 );



Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #5 on Sat 27 Nov 2004 02:29 AM (UTC)

Amended on Sat 27 Nov 2004 02:47 AM (UTC) by David Haley

Message
This won't work...
ch_printf( ch, "The MUD has been up:      %d hours, %d minutes, %d seconds.\n\r", diff/3600, diff/60, diff%60 );


If time = 3671, you will get:
hours = 1
minutes = 61
seconds = 11

As you can see, the minutes are wrong. What you actually want would be more like:
ch_printf( ch, "The MUD has been up:      %d hours, %d minutes, %d seconds.\n\r", diff/3600, (diff-3600*(diff/3600))/60, diff%60 );


This way, you'll get:
hours = 1
minutes = 1 (EDIT #2: oops, I had this as 0)
seconds = 11
Which is what you'd expect.

EDIT:
Or, you could use:
ch_printf( ch, "The MUD has been up:      %d hours, %d minutes, %d seconds.\n\r", diff/3600, (diff%3600)/60, diff%60 );

This is perhaps a little clearer than the above version.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #6 on Sat 27 Nov 2004 02:40 AM (UTC)
Message
Ah, didn't even look into that. Would have realized it though, when it would have happened. Thanks.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Horus   (1 post)  Bio
Date Reply #7 on Sat 27 Nov 2004 02:44 AM (UTC)

Amended on Sat 27 Nov 2004 02:45 AM (UTC) by Horus

Message
I just came up with the code above (the one with the mistake) off the top of my head. In any case, it would probably be easier to just do:

ch_printf( ch, "The MUD has been up:      %d hours, %d minutes, %d seconds.\n\r", diff/3600, (diff/60)%60, diff%60 );


All solutions to the same problem...
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #8 on Wed 08 Mar 2006 12:50 AM (UTC)
Message
If I wanted to also display days, what would be the changes? I'm not entirely sure, and can't really see the immediate solution. It's simple I know.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #9 on Wed 08 Mar 2006 03:52 AM (UTC)
Message
This should help you see what's going on:
-- diff.lua
-- 4 seconds and 3 minutes and 2 hours and 7 days
time = 4 + 60*3 + 60*60*2 + 60*60*24*7
timeorig = time

days = math.floor(time/86400)

time = math.mod(time, 86400)
hours = math.floor(time/3600)

time = math.mod(time, 3600)
minutes = math.floor(time/60)

seconds = math.mod(time, 60)


print("Days", days, "Hours", hours)
print("Minutes", minutes, "Seconds", seconds)

time = timeorig
print("Days", math.floor(time/86400), "Hours", math.mod(math.floor(time/3600), 24) )
print("Minutes", math.mod(math.floor(time/60), 60), "Seconds", math.mod(time, 60))
The answer to your question is in the last two lines.

Days = time/86400
Hours = (time/3600) % 24
Minutes = (time/60) % 60
Seconds = time % 60

Note that math.floor is needed in Lua to make sure we have integer division, which is automatic in C.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #10 on Wed 08 Mar 2006 02:29 PM (UTC)
Message
Yeah, I figured it out while I was about to go to sleep. I love it how I couldn't figure it out while staring at the problem/code, but got it when I was trying to sleep. I thought "Wait, I just multiplay 24 by 3600, and divide the number by that." Thanks for the detailed explaination.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #11 on Wed 08 Mar 2006 03:50 PM (UTC)
Message
Well, you can't just get the number of days. If you compute the number of days, you have to adjust the number of hours as well (since 25 hours is no longer 25 but instead 1 day and 1 hour).

But yes, as they say, sometimes problems are best solved when you're sleeping, or about to sleep. Which is why I wish I could get some sleep these days, and not be forced to be up for nearly the entire night. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #12 on Wed 08 Mar 2006 07:54 PM (UTC)
Message
Quote:
Well, you can't just get the number of days. If you compute the number of days, you have to adjust the number of hours as well (since 25 hours is no longer 25 but instead 1 day and 1 hour).

Yeah, I didn't mention that because I had thought it was obvious/assumed.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

Posted by Conner   USA  (381 posts)  Bio
Date Reply #13 on Wed 08 Mar 2006 09:56 PM (UTC)
Message
Ok, now I'm very curious what you've come up with as the final product so that do_time will display the mud's uptime correctly even if it's been multiple days.. would you be willing to post it?

Also, Zeno, in answer to your other question about having the IP display in your last command, I use Xerves' logon history snippet on my mud and it works very well. I can do 'last 10' to see the last 10 logons (with IP) or 'last today' to see everyone who's logged on since midnight, or 'last <player> #' to see a particular player's last # of logons (up to the limit of my last log file which I have set to 5000, but the default in the snippet is 500) or I can do 'last -1' to see the last 5000 logons or I can do 'last <player>' to see the old version that you're used to. Was a fairly easy snippet and has never given me any problems at all.

-=Conner=-
--
Come test your mettle in the Land of Legends at telnet://tcdbbs.zapto.org:4000
or, for a little family oriented medieval fun, come join us at The Castle's Dungeon BBS at telnet://tcdbbs.zapto.org
or, if you just want information about either, check our web page at http://tcdbbs.zapto.org
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #14 on Wed 08 Mar 2006 10:08 PM (UTC)
Message
This is what I have:
    current = time(0);
    diff = current - boot_time;
    ch_printf( ch, "The MUD has been up  :    %d day%s, %d hour%s, %d minute%s and %d second%s.\n\r",
         diff/86400, diff/86400 == 1 ? "" : "s", (diff/3600)%24, (diff/3600)%24 == 1 ? "" : "s",
        (diff%3600)/60, (diff%3600)/60 == 1 ? "" : "s", diff%60, diff%60 == 1 ? "" : "s" );

It should work, but if anyone notices a problem let me know.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


72,519 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.