Detecting time. - Printable Version
+- SA-MP Forums Archive (
https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (
https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (
https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Detecting time. (
/showthread.php?tid=558955)
Detecting time. -
Arxalan - 20.01.2015
Hello , i have a turf system but i want to detect time . If the zone was attack last time before 1 min ago so he get message this zone was attack 1 min ago and if the zone was attack 2 min ago so he get the message this zone was attack 2 min ago.
Following is my command of on entering Zone.
PHP код:
forward ZoneTimer();
public ZoneTimer()
{
for(new i=0; i < sizeof(ZoneInfo); i++) // loop all zones
{
if(ZoneAttacker[i] != -1) // zone is being attacked
{
if(GetPlayersInZone(i, ZoneAttacker[i]) >= MIN_MEMBERS_TO_START_WAR) // team has enough members in the zone
{
ZoneAttackTime[i]++;
if(ZoneAttackTime[i] == TAKEOVER_TIME) // zone has been under attack for enough time and attackers take over the zone
{
GangZoneStopFlashForAll(ZoneID[i]);
ZoneInfo[i][zTeam] = ZoneAttacker[i];
GangZoneShowForAll(ZoneID[i], GetTeamZoneColor(ZoneInfo[i][zTeam])); // update the zone color for new team
ZoneAttacker[i] = -1;
}
}
else // attackers failed to take over the zone
{
GangZoneStopFlashForAll(ZoneID[i]);
ZoneAttacker[i] = -1;
}
}
else // check if somebody is attacking
{
for(new t=0; t < sizeof(Teams); t++) // loop all teams
{
if(Teams[t] != ZoneInfo[i][zTeam] && GetPlayersInZone(i, Teams[t]) >= MIN_MEMBERS_TO_START_WAR) // if there are enough enemies in the zone
{
ZoneAttacker[i] = Teams[t];
ZoneAttackTime[i] = 0;
GangZoneFlashForAll(ZoneID[i], GetTeamZoneColor(ZoneAttacker[i]));
}
}
}
}
}
Re: Detecting time. -
Threshold - 20.01.2015
Use timestamps.
'gettime' with no parameters returns a timestamp.
Example:
pawn Код:
new currenttime;
public OnPlayerConnect(playerid)
{
currenttime = gettime();
return 1;
}
public OnPlayerDisconnect(playerid, reason)
{
new str[45];
format(str, sizeof(str), "Last player connected %d seconds ago.", gettime() - currenttime);
SendClientMessageToAll(-1, str);
return 1;
}
References:
https://sampwiki.blast.hk/wiki/Gettime
http://en.wikipedia.org/wiki/Unix_time
Re: Detecting time. -
Facerafter - 20.01.2015
pawn Код:
stock timec(timestamp, compare = -1) {
if (compare == -1) {
compare = gettime();
}
new
n,
// on the following line, I have removed the need for the diff() function.
// if you want to use the diff() function in pawn, replace the following with:
// Float:d = diff(timestamp, compare),
Float:d = (timestamp > compare) ? timestamp - compare : compare - timestamp,
returnstr[32];
if (d < 60) {
format(returnstr, sizeof(returnstr), '< 1 minute');
return returnstr;
} else if (d < 3600) { // 3600 = 1 hour
n = floatround(floatdiv(d, 60.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'minute');
} else if (d < 86400) { // 86400 = 1 day
n = floatround(floatdiv(d, 3600.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'hour');
} else if (d < 2592000) { // 2592000 = 1 month
n = floatround(floatdiv(d, 86400.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'day');
} else if (d < 31536000) { // 31536000 = 1 year
n = floatround(floatdiv(d, 2592000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'month');
} else {
n = floatround(floatdiv(d, 31536000.0), floatround_floor);
format(returnstr, sizeof(returnstr), 'year');
}
if (n == 1) {
format(returnstr, sizeof(returnstr), '1 %s', returnstr);
} else {
format(returnstr, sizeof(returnstr), '%d %ss', n, returnstr);
}
return returnstr;
}
You can use this to convert the seconds to minutes/hours/days/month/year
Credits to Blacklite