Author Topic: Time Counter (by ticks) (help)  (Read 585 times)

0 Members and 1 Guest are viewing this topic.

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Time Counter (by ticks) (help)
« on: October 13, 2007, 03:38:16 pm »
Code: [Select]
const
  MaxDays=3;
  AutoOff=true;
var
  TT, SS, MM, HH, DD: integer;

procedure ActivateServer();
begin
  TT:=0;
  SS:=0;
  MM:=0;
  HH:=0;
  DD:=0;
end;

function ResetTime(const Tvar, Svar, Mvar, Hvar, Dvar:variant; const Value:integer; const Return:boolean):integer;
begin
  Result:=Value;
  Tvar:=Result;
  Svar:=Result;
  Mvar:=Result;
  Hvar:=Result;
  Dvar:=Result;
  if Return = true then begin
    WriteLn('Time is now '+inttostr(Dvar)+':'+inttostr(Hvar)+':'+inttostr(Mvar)+':'+inttostr(Svar)+':'+inttostr(Tvar)+'.');
  end else exit;
end;

procedure AppOnIdle(Ticks: integer);
begin
  if Ticks Mod (1 * 1) = 0 then begin
    TT:=TT+1;
  end;
  if TT = 60 then begin
    TT:=0;
    SS:=SS+1;
  end;
  if SS = 60 then begin
    SS:=0;
    MM:=MM+1;
  end;
  if MM = 60 then begin
    MM:=0;
    HH:=HH+1;
  end;
  if HH = 24 then begin
    HH:=0;
    DD:=DD+1;
  end;
  if DD = MaxDays then begin
    if AutoOff = true then begin
      ResetTime('TT', 'SS', 'MM', 'HH', 'DD', 0, false);
      Shutdown();
    end else ResetTime('TT', 'SS', 'MM', 'HH', 'DD', 0, false);
  end;
end;

function OnPlayerCommand(ID: Byte;Text: string):boolean;
var
  Temp1: string;
begin
  Result:=false;
  Temp1:=GetPiece(LowerCase(Text), ' ', 0);
  if Temp1 = '/Time' then begin
    SayToPlayer(ID, 'Total running time is '+inttostr(DD)+':'+inttostr(HH)+':'+inttostr(MM)+':'+inttostr(SS)+':'+inttostr(TT)+'.');
  end;
end;

function OnCommand(ID: byte; Text: string):boolean;
var
  Temp1: string;
  Temp2: integer;
begin
  Result:=false;
  Temp1:=GetPiece(LowerCase(Text), ' ', 0);
  if Temp1 = '/TimeReset' then begin
    if StrPos(Text, ' ') > 0 then begin
      Temp2:=strtoint(GetPiece(Text, ' ', 1));
      ResetTime('TT', 'SS', 'MM', 'HH', 'DD', Temp2, false);
    end else ResetTime('TT', 'SS', 'MM', 'HH', 'DD', 0, false);
  end;
end;

can anyone figure out why my command to say the time the server has been running does not work?

other than that, this script is working (doesn't spit out errors)
(I don't think it's keeping time either...)
« Last Edit: October 13, 2007, 08:11:27 pm by Kavukamari »
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: Time Counter (by ticks) (help)
« Reply #1 on: October 13, 2007, 05:57:17 pm »
You have serious problems coding man. I made a couple of functions that help you measure time intervals with a precision of milliseconds. You might find them usefull.

Code: [Select]
function GetTimeSeconds(): single;
var
arr: TStringArray;
hours, minutes, seconds, milliseconds: integer;
begin
arr := xsplit(FormatDate('h:n:s:z'), ':');

hours := StrToInt(arr[0]);
minutes := StrToInt(arr[1]);
seconds := StrToInt(arr[2]);
milliseconds := StrToInt(arr[3]);

Result := hours * 3600.0 + minutes * 60.0 + seconds + milliseconds/1000.0;
end;

function GetSecondsInterval(seconds1, seconds2: single): single;
begin
if seconds2 < seconds1 then
seconds2 := seconds2 + 24 * 3600.0;
Result := seconds2 - seconds1;
end;

function FormatSeconds(seconds: single; sign: boolean): string;
var
parts: TStringArray;
n, n2: integer;
begin
Result := FloatToStr(RoundTo(seconds, 2));
parts := xsplit(Result, '.');
n := StrToInt(parts[0]);
if n <= 0 then
begin
n := -n;
parts[0] := IntToStr(n);
end;

if GetArrayLength(parts) = 1 then
begin
SetArrayLength(parts, 2);
parts[1] := '00';
end;

if Length(parts[1]) = 1 then
parts[1] := parts[1] + '0';

if n < 10 then
begin
Result := '0' + parts[0] + '.' + parts[1];
end
else if n < 60 then
begin
Result := parts[0] + '.' + parts[1];
end
else
begin
n2 := n div 60;
n := n mod 60;

Result := IntToStr(n2) + ':';
if n2 < 10 then
Result := '0' + IntToStr(n2) + ':';

if n < 10 then
Result := Result + '0' + IntToStr(n) + '.' + parts[1]
else
Result := Result + IntToStr(n) + '.' + parts[1];
end;

if seconds < 0.0 then
Result := '-' + Result
else if sign = true then
Result := '+' + Result;
end;

You use it storing the time you start counting in a variable of type single calling GetTimeSeconds, and when you want to know the amount of time that has passed you call GetSecondsInterval. FormatSeconds is used to convert the resulting value of GetSecondsInterval into a string representation.

Example:
Code: [Select]
var seconds: single;

function OnCommand(ID: byte; Text: string): boolean;
begin
  Result := false;
  if Text = '/resettimer' then
    seconds := GetTimeSeconds()
  else if Text = '/showtimer' then
    WriteConsole(0, 'Time interval: ' + FormatSeconds(GetSecondsInterval(seconds, GetTimeSeconds())), $FFFF0000);
end;

Don't blame me if the example doesn't work, i just wrote it here. The other functions should work fine because I use them in one of my scripts.
urraka

Offline Kavukamari

  • Camper
  • ***
  • Posts: 435
  • 3.14159265358979, mmm... pi
Re: Time Counter (by ticks) (help)
« Reply #2 on: October 13, 2007, 07:59:24 pm »
I'll settle with Ticks, they go better with soldat :P

I still don't get what is wrong with my script tho...

Date Posted: October 13, 2007, 08:47:16 pm
P.S. does oor work?

Date Posted: October 13, 2007, 08:49:03 pm
wait, it doesn't, well, I tried...

I think the script actually DOES work, it's the MapRandom script spitting out the errors, my mistake (this is just like when I forgot that "end;"...)
"Be mindful of fame, show a mighty courage, watch against foes. Nor shalt thou lack what thou desirest, if with thy life thou hast comest out from that heroic task."