Author Topic: average ping on server (function), like arsse  (Read 4508 times)

0 Members and 1 Guest are viewing this topic.

Offline szmon

  • Major(1)
  • Posts: 7
average ping on server (function), like arsse
« on: June 11, 2007, 09:01:19 pm »
Code: [Select]
function AveragePing(): integer;
var
i : integer;
ping: integer;
totalping: integer;
begin
  for i := 1 to 32 do begin
  totalping := totalping + GetPlayerStat(i,'Ping')
  i := i + 1
if i = 32 then
begin
ping := totalping / NumPlayers
end;
end;
Result := ping;
end;

Offline colby

  • Major(1)
  • Posts: 42
  • party time!!!
Re: average ping on server (function), like arsse
« Reply #1 on: June 11, 2007, 09:21:55 pm »
 ;) nice script
favorite weapons: Styer, and Ruger

Offline urraka

  • Soldat Developer
  • Flagrunner
  • ******
  • Posts: 703
Re: average ping on server (function), like arsse
« Reply #2 on: June 11, 2007, 09:28:46 pm »
It's a bit confusing i would write it this way:

Code: [Select]
function AveragePing(): integer;
var
    totalping, i: integer;
begin
    for i := 1 to 32 do
        totalping := totalping + GetPlayerStat(i,'Ping');

    Result := totalping / NumPlayers;
end;
urraka

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: average ping on server (function), like arsse
« Reply #3 on: June 11, 2007, 10:27:37 pm »
It's a bit confusing i would write it this way:

Code: [Select]
function AveragePing(): integer;
var
    totalping, i: integer;
begin
    for i := 1 to 32 do
        totalping := totalping + GetPlayerStat(i,'Ping');

    Result := totalping / NumPlayers;
end;

Your way is correct; the code in the original post breaks the rule of "Don't modify FOR counting variables inside a FOR loop", and is incorrect.

Offline szmon

  • Major(1)
  • Posts: 7
Re: average ping on server (function), like arsse
« Reply #4 on: June 12, 2007, 05:35:23 am »
Thanks for tips. I still learning^^

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: average ping on server (function), like arsse
« Reply #5 on: June 12, 2007, 06:50:51 am »
It's a bit confusing i would write it this way:

Code: [Select]
function AveragePing(): integer;
var
    totalping, i: integer;
begin
    for i := 1 to 32 do
        totalping := totalping + GetPlayerStat(i,'Ping');

    Result := totalping / NumPlayers;
end;

Your way is correct; the code in the original post breaks the rule of "Don't modify FOR counting variables inside a FOR loop", and is incorrect.

... except the original code isn't using a for loop.  It is emulating a for-loop with a while-loop.  I don't believe there's anything wrong with his code from a "technically speaking" point of view

but yes, the other code is better

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: average ping on server (function), like arsse
« Reply #6 on: June 12, 2007, 07:48:47 am »
It's a bit confusing i would write it this way:

Code: [Select]
function AveragePing(): integer;
var
    totalping, i: integer;
begin
    for i := 1 to 32 do
        totalping := totalping + GetPlayerStat(i,'Ping');

    Result := totalping / NumPlayers;
end;

Your way is correct; the code in the original post breaks the rule of "Don't modify FOR counting variables inside a FOR loop", and is incorrect.

... except the original code isn't using a for loop.  It is emulating a for-loop with a while-loop.  I don't believe there's anything wrong with his code from a "technically speaking" point of view

but yes, the other code is better
function AveragePing(): integer;
var
i : integer;
ping: integer;
totalping: integer;
begin
  for i := 1 to 32 do begin <<<<<<<<<<<<<<<
  totalping := totalping + GetPlayerStat(i,'Ping')
  i := i + 1
if i = 32 then
begin
ping := totalping / NumPlayers
end;
end;
Result := ping;
end;

wut?

Offline sai`ke

  • Camper
  • ***
  • Posts: 318
  • Can't be arsed to remove christmas avatar
Re: average ping on server (function), like arsse
« Reply #7 on: June 12, 2007, 11:14:35 am »
Does getplayerstat return 0 if there is no player in that slot? If not, both will give a biased measure.
#soldat.ttw #ttw.gather --- Quakenet!
http://ttwforums.com

Offline szmon

  • Major(1)
  • Posts: 7
Re: average ping on server (function), like arsse
« Reply #8 on: June 12, 2007, 12:03:13 pm »
Does getplayerstat return 0 if there is no player in that slot? If not, both will give a biased measure.

corrected ;P

Code: [Select]
function AveragePing(): integer;
var
    totalping, i: integer;
begin
    if Numplayers > 0 then begin
    for i := 1 to 32 do
        totalping := totalping + GetPlayerStat(i,'Ping');

    Result := totalping / NumPlayers;
end;
end;

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: average ping on server (function), like arsse
« Reply #9 on: June 12, 2007, 12:24:10 pm »
LMAO szmon... just give up.