Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Calculating Offline Time (https://forums.graalonline.com/forums/showthread.php?t=134264306)

Astram 08-25-2011 03:17 AM

Calculating Offline Time
 
How would I calculate offline time? I mean how can I tell time, like in 2hrs a player would be unjailed even if he/she is offline. (I'm currently not concerned on the "setlevel2();") for when the players unjailed offline. But, I need a way to calculate time while offline. So it invokes something in 2hrs if the player is offline or not. And please explain the way simply. As timevar2 gets me confused.

Tricxta 08-25-2011 03:24 AM

Use timevar, so basically...
you set the time you want the player to get out

PHP Code:

client.jailrelease=timevar+(hours*720); 

then
PHP Code:

if (client.jailrelease<timevar)
{
//release the player


I get 720 since timevar increments by 1 every 5 seconds
so 60*60/5 == 720

Edit: In case you might not fully understand timevar is just a variable that the server increments by 1 ever 5 seconds, so its forever going up. Therefore the way in which you can apply timevar is very straight forward. Timevar2 is a more accurate way of judging time(increments every .1 of a second, maybe faster lol but the basis is its a more accurate presentation of time) but I tend to avoid it when doing big time distance calculations as it just ends up giving you messy numbers. Hope this helped you.

Have fun :)

Switch 08-25-2011 04:42 AM

timevar2 is the one most people use because it's more accurate, as timevar increases by 1 every 5 seconds and timevar2 increases as fast as Graal runs, 1:1 ratio.

0PiX0 08-25-2011 04:46 AM

Why not just remove a person from jail when they log in, rather than while they are offline?

Astram 08-25-2011 05:01 AM

Quote:

Originally Posted by Switch (Post 1665285)
timevar2 is the one most people use because it's more accurate, as timevar increases by 1 every 5 seconds and timevar2 increases as fast as Graal runs, 1:1 ratio.

Explain more

Crow 08-25-2011 12:47 PM

Quote:

Originally Posted by Astram (Post 1665291)
Explain more

timevar2 on the serverside is basically a UNIX time stamp with millisecond precision, but with the milliseconds moved to the decimals. You could do something similar to this:
PHP Code:

player.clientr.jailReleaseTime timevar2 3600// release in an hour 

And then just check periodically and whenever the player logs on:
PHP Code:

if (timevar2 >= player.clientr.jailReleaseTime)
  
// release the player from jail/whatever 


0PiX0 08-25-2011 04:11 PM

Quote:

Originally Posted by Astram (Post 1665199)
So it invokes something in 2hrs if the player is offline or not. And please explain the way simply. As timevar2 gets me confused.

timevar increases by 1 every 5 seconds
timevar2 increases by 1 every 1 second

With a little algebra, we can determine how much time has passed by setting a variable equal to a time variable, then later on, subtracting the variable you set from the time variable.

You can read and write some player variables even when the player is offline by creating a TServerPlayer object.

The 3 vars you are concerned with are "x", "y", and "levelname".

Here are some examples that might help you..
PHP Code:

// ex: getOfflinePlayerVar("Graal123456", "x"); 
// returns the player's x coordinate
function getOfflinePlayerVar(temp.accounttemp.varname) {
  
temp.pl = new TServerPlayer(@account);
  
temp.rtn makevar("pl." varname);
  
pl.destroy();
  return 
rtn;
}

// ex: setOfflinePlayerVar("Graal123456", "x", 30); 
// sets the player's x coordinate to 30. 
// They will login at the new position.
function setOfflinePlayerVar(temp.accounttemp.varnametemp.value) {
  
temp.pl = new TServerPlayer(@account);
  
makevar("pl." varname) = value;
  
pl.saveaccount();
  
pl.destroy();


e.
Depending on how your script works, you might not even need to use timevar or timevar2.
When you jail someone, schedule an event to release their account in however many seconds you wish...
then in the event:
PHP Code:

temp.pl findplayer(acc);
if (
pl == NULL)
  
// edit offline
else
  
// setlevel2 


Astram 08-25-2011 04:45 PM

So does timevar2 go down on its own?

0PiX0 08-25-2011 07:34 PM

It increases automatically as time goes on.

ff7chocoboknight 08-25-2011 08:35 PM

Didn't you already make a thread about timevar, Astram?

fowlplay4 08-25-2011 09:14 PM

To further clarify...

On the server-side timevar2 is the amount of seconds since the unix epoch (January 1st 1970).

On the client-side timevar2 is the amount of seconds the client has been on for.

I believe timevar is the same on both server-side and client-side, it's been forever since I've actually used it though.

cbk1994 08-25-2011 09:57 PM

I wish Stefan would enable timevar3 for non-G3D servers.

Quote:

Originally Posted by fowlplay4 (Post 1665434)
On the client-side timevar2 is the amount of seconds the client has been on for.

Except on Mac (and Linux?), where it's the number of seconds since the unix epoch, but may differ from the server's value due to a bad time setting on the client's computer.

Quote:

Originally Posted by 0PiX0 (Post 1665345)
PHP Code:

// ex: getOfflinePlayerVar("Graal123456", "x"); 
// returns the player's x coordinate
function getOfflinePlayerVar(temp.accounttemp.varname) {
  
temp.pl = new TServerPlayer(@account);
  
temp.rtn makevar("pl." varname);
  
pl.destroy();
  return 
rtn;
}

// ex: setOfflinePlayerVar("Graal123456", "x", 30); 
// sets the player's x coordinate to 30. 
// They will login at the new position.
function setOfflinePlayerVar(temp.accounttemp.varnametemp.value) {
  
temp.pl = new TServerPlayer(@account);
  
makevar("pl." varname) = value;
  
pl.saveaccount();
  
pl.destroy();



You should try to avoid makevar as much as possible

PHP Code:

pl.(@ varname) = value

does the same thing, and doesn't make the object-orientation gods cry.

Quote:

Depending on how your script works, you might not even need to use timevar or timevar2.
When you jail someone, schedule an event to release their account in however many seconds you wish...
then in the event:
PHP Code:

temp.pl findplayer(acc);
if (
pl == NULL)
  
// edit offline
else
  
// setlevel2 


It's best not to use events for stuff like this since they're lost on server restart, and just create another point of failure (e.g. if the script is updated and you introduce a bug, events could not be received/set, ...). Using events is only a good idea if you use it in conjunction with a "release on" time which is checked periodically and/or on login.

0PiX0 08-25-2011 10:13 PM

Quote:

Originally Posted by cbk1994 (Post 1665448)
PHP Code:

pl.(@ varname) = value

does the same thing, and doesn't make the object-orientation gods cry.

No, it doesn't do the same thing.
PHP Code:

makevar("pl.clientr.subvar"); // works
pl.(@ "clientr.subvar"); // does not work 

Quote:

Originally Posted by cbk1994 (Post 1665448)
Using events is only a good idea if you use it in conjunction with a "release on" time which is checked periodically and/or on login.

I agree with checking upon login as well to prevent any issues.

Astram 08-26-2011 04:35 AM

I made this quickly
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
client.blah 0;
  
onTimeOut();
}
function 
onTimeOut() {
  
client.blah timevar2;
  
player.chat client.blah;
  
setTimer(0.05);


But, I can't seem to get it down to 0...

Edit:
I can't get on Graal but would this work.
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
client.timestart client.blah;
  
onTimeOut();
}
function 
onTimeOut() {
  
client.blah timevar2;
  
player.chat client.blah -= client.timestart;
  
setTimer(0.05);



fowlplay4 08-26-2011 04:37 AM

Quote:

Originally Posted by Astram (Post 1665520)
But, I can't seem to get it down to 0...

Please read nearly every post in this thread. It's been said countless times that timevar2 is an auto-incrementing variable based on the amount of seconds since a specific time period.

If you just want a countdown do something like this:

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
this.ticks 100;
  
onTimeout();
}

function 
onCreated() {
  
this.ticks--;
  
player.chat this.ticks;
  
setTimer(1);




All times are GMT +2. The time now is 11:49 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.