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);



callimuc 08-26-2011 04:51 AM

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
client.two 3600//one hour
  
onTimeOut();
}
function 
onTimeOut() {
  
client.one timevar2;
  
client.two--;
  
client.three client.one client.two;
  
player.chat client.three;
  
settimer(1);


Didn´t test it yet, something looks strange for me (couldn´t find something) but I still think this should work.

DustyPorViva 08-26-2011 04:55 AM

If you want a timer to keep track of how long even while the player is offline you'll simply need to compare timevar2's.

When you jail the player, save a clientr of the current timevar2(on serverside) and time to stay jailed. For example: clientr.jailtime = {timevar2,days*24+hours*60+minutes*60}; Days, hours and minutes are simply representing real time you want them to be jailed.

Then every time they log in(or in the jail level, in a loop) do something like:

PHP Code:

if (timevar2 clientr.jailtime[0] + clientr.jailtime[1]) {
  
// JAIL TIME OVER!


Alternatively you can have a DBNPC that handles all jailings.

Not only would doing this clientside yield undesired results, it would also be extremely insecure. I wouldn't trust the clientside to handle any of it, if it's meant for a jailing system.

cbk1994 08-26-2011 04:59 AM

Quote:

Originally Posted by callimuc (Post 1665524)
PHP Code:

//#CLIENTSIDE
function onCreated() {
  
client.two 3600//one hour
  
onTimeOut();
}
function 
onTimeOut() {
  
client.one timevar2;
  
client.two--;
  
client.three client.one client.two;
  
player.chat client.three;
  
settimer(1);


Didn´t test it yet, something looks strange for me (couldn´t find something) but I still think this should work.

Why are you adding the time the player has remaining to timevar2...?

PHP Code:

function onTimeOut() {
  
player.timeRemaining --;
  
  if (
player.timeRemaining <= 0) {
    
// free player
  
} else {
    
this.setTimer(1);
  }


This is all you need if you're using a timer.

Also, things like this should never be done clientside. If you absolutely must use a timeout for each player, it should probably be done in a serverside class joined to the player.

To use timevar2, you'll want to do something like this (in a player class for this example):

PHP Code:

function onCreated() {
  
// player logs in
  
if (this.isInJail()) {
    
// player is still jailed, warp to jail
    
this.warpToJail();
  } else if (
this.level.name == "jail.nw") {
    
// player is in jail.nw, but time has expired
    
this.unstick();
  }
}

public function 
jailPlayer(temp.seconds) {
  
this.jailedUntil timevar2 temp.seconds;
  
this.warpToJail();
  
  
// schedule an event in case the player is still online when the time
  // expires
  
this.cancelEvents("onJailTimeExpired"); // in case there was already an event
  
this.scheduleEvent(temp.seconds"onJailTimeExpired");
}

public function 
warpToJail() {
  
this.setLevel2("jail.nw"3232);
}

function 
onJailTimeExpired() {
  
this.freeFromJail();
}

public function 
isInJail() {
  return 
this.jailedUntil timevar2;
}

public function 
freeFromJail() {
  
this.jailedUntil 0;
  
this.cancelEvents("onJailTimeExpired"); // in case there was still an event pending
  
this.unstick();


Quote:

Originally Posted by DustyPorViva (Post 1665525)
If you want a timer to keep track of how long even while the player is offline you'll simply need to compare timevar2's.

When you jail the player, save a clientr of the current timevar2(on serverside) and time to stay jailed. For example: clientr.jailtime = {timevar2,days*24+hours*60+minutes*60}; Days, hours and minutes are simply representing real time you want them to be jailed.

Why bother with storing the length at all? Why not just store the time they get out?

Also, that length calculation is totally wrong (days*24+hours*60+minutes*60).

callimuc 08-26-2011 08:51 PM

Quote:

Originally Posted by cbk1994 (Post 1665528)
Why are you adding the time the player has remaining to timevar2...?

Thought after relogging the this.bla get "resetted". Or did I missunderstood something?

DustyPorViva 08-26-2011 08:56 PM

Quote:

Originally Posted by cbk1994 (Post 1665528)
Why bother with storing the length at all? Why not just store the time they get out?

*shrug* could be useful to tell when they were jailed, no?


All times are GMT +2. The time now is 06:00 PM.

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