Author Topic: KD ratio script  (Read 5541 times)

0 Members and 1 Guest are viewing this topic.

Offline iftach

  • Major(1)
  • Posts: 16
KD ratio script
« on: April 13, 2007, 07:49:20 am »
Script Name: KD ratio
Script Description: displays player's kills:deaths ratio. its my first script so don't bash.
Original Author(s): iftach
Core Version: 2.5.4

!kd - displays player's kills:deaths ratio
!kdall - displays kills:deaths ratio of all the players (buggy).

Code: [Select]
var
  PlayerCaps: array[1..32] of integer;
  plflag: array[1..2] of integer;

procedure ResetCaps();
var
i: integer;
begin
for i := 1 to 32 do begin
PlayerCaps[i] := 0;
end;
end;

procedure OnMapChange();
begin
ResetCaps()
end;

procedure OnJoinTeam(IP, Nickname: string;Team: byte);
begin
PlayerCaps[NameToId(Nickname)] := 0;
end;

procedure OnLeaveGame(IP, Nickname: string;Team: byte);
begin
PlayerCaps[NameToId(Nickname)] := 0;
end;

procedure OnFlagGrab(ID: integer;TeamFlag: byte;GrabbedInBase: boolean);
begin
plflag[TeamFlag]:=ID;
end;

procedure OnFlagScore(ID: integer;TeamFlag: byte);
begin
PlayerCaps[plflag[TeamFlag]] := PlayerCaps[plflag[TeamFlag]] + 1;
end;

function IDtoKills(PlayerID: integer):integer;
begin
  case PlayerID of
    1: Result:= Player_Kills_1;
    2: Result:= Player_Kills_2;
    3: Result:= Player_Kills_3;
    4: Result:= Player_Kills_4;
    5: Result:= Player_Kills_5;
    6: Result:= Player_Kills_6;
    7: Result:= Player_Kills_7;
    8: Result:= Player_Kills_8;
    9: Result:= Player_Kills_9;
    10: Result:= Player_Kills_10;
    11: Result:= Player_Kills_11;
    12: Result:= Player_Kills_12;
    13: Result:= Player_Kills_13;
    14: Result:= Player_Kills_14;
    15: Result:= Player_Kills_15;
    16: Result:= Player_Kills_16;
    17: Result:= Player_Kills_17;
    18: Result:= Player_Kills_18;
    19: Result:= Player_Kills_19;
    20: Result:= Player_Kills_20;
    21: Result:= Player_Kills_21;
    22: Result:= Player_Kills_22;
    23: Result:= Player_Kills_23;
    24: Result:= Player_Kills_24;
    25: Result:= Player_Kills_25;
    26: Result:= Player_Kills_26;
    27: Result:= Player_Kills_27;
    28: Result:= Player_Kills_28;
    29: Result:= Player_Kills_29;
    30: Result:= Player_Kills_30;
    31: Result:= Player_Kills_31;
    32: Result:= Player_Kills_32;
  end;
end;

function IDtoDeaths(PlayerID: integer):integer;
begin
  case PlayerID of
    1: Result := Player_Deaths_1;
    2: Result := Player_Deaths_2;
    3: Result := Player_Deaths_3;
    4: Result := Player_Deaths_4;
    5: Result := Player_Deaths_5;
    6: Result := Player_Deaths_6;
    7: Result := Player_Deaths_7;
    8: Result := Player_Deaths_8;
    9: Result := Player_Deaths_9;
    10: Result := Player_Deaths_10;
    11: Result := Player_Deaths_11;
    12: Result := Player_Deaths_12;
    13: Result := Player_Deaths_13;
    14: Result := Player_Deaths_14;
    15: Result := Player_Deaths_15;
    16: Result := Player_Deaths_16;
    17: Result := Player_Deaths_17;
    18: Result := Player_Deaths_18;
    19: Result := Player_Deaths_19;
    20: Result := Player_Deaths_20;
    21: Result := Player_Deaths_21;
    22: Result := Player_Deaths_22;
    23: Result := Player_Deaths_23;
    24: Result := Player_Deaths_24;
    25: Result := Player_Deaths_25;
    26: Result := Player_Deaths_26;
    27: Result := Player_Deaths_27;
    28: Result := Player_Deaths_28;
    29: Result := Player_Deaths_29;
    30: Result := Player_Deaths_30;
    31: Result := Player_Deaths_31;
    32: Result := Player_Deaths_32;
  end;
end;

function IDtoTeam(PlayerID: integer):integer;
begin
  case PlayerID of
    1: Result:= Player_Team_1;
    2: Result:= Player_Team_2;
    3: Result:= Player_Team_3;
    4: Result:= Player_Team_4;
    5: Result:= Player_Team_5;
    6: Result:= Player_Team_6;
    7: Result:= Player_Team_7;
    8: Result:= Player_Team_8;
    9: Result:= Player_Team_9;
    10: Result:= Player_Team_10;
    11: Result:= Player_Team_11;
    12: Result:= Player_Team_12;
    13: Result:= Player_Team_13;
    14: Result:= Player_Team_14;
    15: Result:= Player_Team_15;
    16: Result:= Player_Team_16;
    17: Result:= Player_Team_17;
    18: Result:= Player_Team_18;
    19: Result:= Player_Team_19;
    20: Result:= Player_Team_20;
    21: Result:= Player_Team_21;
    22: Result:= Player_Team_22;
    23: Result:= Player_Team_23;
    24: Result:= Player_Team_24;
    25: Result:= Player_Team_25;
    26: Result:= Player_Team_26;
    27: Result:= Player_Team_27;
    28: Result:= Player_Team_28;
    29: Result:= Player_Team_29;
    30: Result:= Player_Team_30;
    31: Result:= Player_Team_31;
    32: Result:= Player_Team_32;
  end;
end;

procedure KDRatio(ID: Integer);
var
KD: Double;
plkills: Integer;
begin
plkills := IDtoKills(ID)-(PlayerCaps[ID]*20);
if(IDtoTeam(ID)=5) then command('/say '+IDToName(ID)+', Your are spectator')
  else if(IDtoDeaths(ID)=0) then command('/say '+IDToName(ID)+', Your K:D is incalculable' +' ('+intToStr(plkills)+'/0)')
    else begin
    KD := (plkills)/(IDtoDeaths(ID))*100;
    command('/say '+IDToName(ID)+', Your K:D is '+FloatToStr(round(KD)/100)+' ('+FloatToStr(plkills)+'/'+FloatToStr(IDtoDeaths(ID))+')');
    end;
end;

procedure OnPlayerSpeak(Name,Text: string);
var
i: integer;
begin
  if Text = '!kd' then KDRatio(NameToID(Name)); 
  if Text = '!kdall' then for i:=1 to NumPlayers do KDRatio(i); 
end;
« Last Edit: April 19, 2007, 07:42:59 am by iftach »

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: KD ratio script
« Reply #1 on: April 13, 2007, 07:55:05 am »
If its your first script, why is rainrider the author?

Offline iftach

  • Major(1)
  • Posts: 16
Re: KD ratio script
« Reply #2 on: April 13, 2007, 08:07:01 am »
it's more like the text of the /say commands than the script that i took from him.
and his script is for his ESA.

edited

oops this script isn't good. i have to subtract the cap's points from the points.

procedure OnFlagScore(ID: integer;TeamFlag: byte);
the ID always returns 0 so i cant fix this script.

its still good for DM and TDM...
« Last Edit: April 13, 2007, 03:25:06 pm by iftach »

Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: KD ratio script
« Reply #3 on: April 13, 2007, 04:59:37 pm »
it's more like the text of the /say commands than the script that i took from him.
and his script is for his ESA.

edited

oops this script isn't good. i have to subtract the cap's points from the points.

procedure OnFlagScore(ID: integer;TeamFlag: byte);
the ID always returns 0 so i cant fix this script.

its still good for DM and TDM...

Come back and fix it once 2.6 (soldat 1.4) is out, since the ID bug should be fixed then.

Offline iftach

  • Major(1)
  • Posts: 16
Re: KD ratio script
« Reply #4 on: April 13, 2007, 05:30:01 pm »
i fixed the script, now it good (except the !kdall  :-\).

i found a way around the bug :)
using the (working) id of the onFlagGrab.

also, the roundto function doesn't work.
so to get only 2 numbers after the decimal point.
i did: *100 -> round -> /100.

tell me what u think.
« Last Edit: April 14, 2007, 11:58:41 am by iftach »

Offline _ReApEr

  • Major(1)
  • Posts: 5
Re: KD ratio script
« Reply #5 on: April 16, 2007, 02:38:58 pm »
Code: [Select]
(12:43:17) /recompile
(12:43:17)  [*] Compiling Script AdminCore.pas...
(12:43:17)  [*] Compiling Script NetworkCore.pas...
(12:43:18)  [*] (NetworkCore.pas) [Error] (199:4): Unknown identifier 'IDtoTeam'
(12:43:18) Shutting server...
(12:43:19) Shutting down fileserver...
(12:43:19) Shutting down admin server...
(12:43:19) Connection to the server lost

What causes this? I've also had it with other scripts.

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: KD ratio script
« Reply #6 on: April 16, 2007, 05:00:35 pm »
Code: [Select]
(12:43:17) /recompile
(12:43:17)  [*] Compiling Script AdminCore.pas...
(12:43:17)  [*] Compiling Script NetworkCore.pas...
(12:43:18)  [*] (NetworkCore.pas) [Error] (199:4): Unknown identifier 'IDtoTeam'
(12:43:18) Shutting server...
(12:43:19) Shutting down fileserver...
(12:43:19) Shutting down admin server...
(12:43:19) Connection to the server lost

What causes this? I've also had it with other scripts.

Looks like the author forgot to include that function (which is not in the scripting engine).

Offline _ReApEr

  • Major(1)
  • Posts: 5
Re: KD ratio script
« Reply #7 on: April 16, 2007, 05:34:25 pm »
Does this mean it's fixable by the author or, since it's not in the scripting engine, it's not fixable?

Offline chrisgbk

  • Moderator
  • Veteran
  • *****
  • Posts: 1739
Re: KD ratio script
« Reply #8 on: April 16, 2007, 05:59:51 pm »
Does this mean it's fixable by the author or, since it's not in the scripting engine, it's not fixable?

It's fixable, the author just needs to provide the missing code.

Offline _ReApEr

  • Major(1)
  • Posts: 5
Re: KD ratio script
« Reply #9 on: April 16, 2007, 10:22:09 pm »
*glares at author*


<3

Offline mikembm

  • Soldier
  • **
  • Posts: 210
Re: KD ratio script
« Reply #10 on: April 16, 2007, 10:30:53 pm »
Code: [Select]
function IDtoTeam(PlayerID: integer):integer;
begin
  case PlayerID of
    1: Result:= Player_Team_1;
    2: Result:= Player_Team_2;
    3: Result:= Player_Team_3;
    4: Result:= Player_Team_4;
    5: Result:= Player_Team_5;
    6: Result:= Player_Team_6;
    7: Result:= Player_Team_7;
    8: Result:= Player_Team_8;
    9: Result:= Player_Team_9;
    10: Result:= Player_Team_10;
    11: Result:= Player_Team_11;
    12: Result:= Player_Team_12;
    13: Result:= Player_Team_13;
    14: Result:= Player_Team_14;
    15: Result:= Player_Team_15;
    16: Result:= Player_Team_16;
    17: Result:= Player_Team_17;
    18: Result:= Player_Team_18;
    19: Result:= Player_Team_19;
    20: Result:= Player_Team_20;
    21: Result:= Player_Team_21;
    22: Result:= Player_Team_22;
    23: Result:= Player_Team_23;
    24: Result:= Player_Team_24;
    25: Result:= Player_Team_25;
    26: Result:= Player_Team_26;
    27: Result:= Player_Team_27;
    28: Result:= Player_Team_28;
    29: Result:= Player_Team_29;
    30: Result:= Player_Team_30;
    31: Result:= Player_Team_31;
    32: Result:= Player_Team_32;
  end;
end;

Offline _ReApEr

  • Major(1)
  • Posts: 5
Re: KD ratio script
« Reply #11 on: April 16, 2007, 11:03:51 pm »
Code: [Select]
(21:02:54) /recompile
(21:02:54)  [*] Compiling Script AdminCore.pas...
(21:02:55)  [*] Compiling Script NetworkCore.pas...
(21:02:55)  [*] (NetworkCore.pas) [Error] (246:1): Unknown identifier 'ResetCaps'
(21:02:55) Shutting server...
(21:02:56) Shutting down fileserver...
(21:02:56) Shutting down admin server...
(21:02:56) Connection to the server lost

D'oh.

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: KD ratio script
« Reply #12 on: April 16, 2007, 11:07:56 pm »
Code: [Select]
(21:02:54) /recompile
(21:02:54)  [*] Compiling Script AdminCore.pas...
(21:02:55)  [*] Compiling Script NetworkCore.pas...
(21:02:55)  [*] (NetworkCore.pas) [Error] (246:1): Unknown identifier 'ResetCaps'
(21:02:55) Shutting server...
(21:02:56) Shutting down fileserver...
(21:02:56) Shutting down admin server...
(21:02:56) Connection to the server lost

D'oh.
That function is at the very start of the script....

Offline _ReApEr

  • Major(1)
  • Posts: 5
Re: KD ratio script
« Reply #13 on: April 16, 2007, 11:20:30 pm »
I was unaware the order of things mattered, so I was putting them in alphabetical order to make them easier to find.

*bows head in shame*

It works now. Thanks, guys.

Offline iftach

  • Major(1)
  • Posts: 16
Re: KD ratio script
« Reply #14 on: May 04, 2007, 08:09:08 am »
Script Name: KD ratio
Script Description: displays player's kills:deaths ratio.
Original Author(s): iftach
Core Version: 2.6.0

the !kdall part is quite Beta-ish, so if u have a problem tell me.

!kd - displays player's kills:deaths ratio
!kdall - displays kills:deaths ratio of all the players (sorted).

Code: [Select]
const
//KD ratio- console text color
 KDColor = $EED6BFFF;

var
  PlayerCaps: array[1..32] of integer;
  KDarray: array[0..32] of Double;
  KDPos: array[0..32] of Integer;

procedure ResetCaps();
var
i: integer;
begin
for i := 1 to 32 do begin
PlayerCaps[i] := 0;
end;
end;

procedure OnJoinGame(ID, Team: byte);
begin
PlayerCaps[ID] := 0;
end;

procedure OnLeaveGame(ID, Team: byte; Kicked: boolean);
begin
PlayerCaps[ID] := 0;
end;

procedure OnFlagScore(ID, TeamFlag: byte);
begin
inc(PlayerCaps[ID],1);
end;

Procedure KDSort(size : Integer);
Var
i, j: Integer;

Begin
 for i:=0 to 32 do begin
 KDPos[i]:=i;
 end;
 For i := 2 to size do
  Begin
  For j := size downto i do
   begin
   if (KDarray[j] > KDarray[j-1]) then
    Begin
     KDarray[0] := KDarray[j];
     KDarray[j] := KDarray[j-1];
     KDarray[j-1] := KDarray[0];
     KDPos[0] := KDPos[j];
     KDPos[j] := KDPos[j-1];
     KDPos[j-1] := KDPos[0];
    end;
  end;
 end;
End;

function BiggestNick() : Byte;
var
bigNick, i : Byte;
begin
  BigNick:=length(Getplayerstat(1,'name'));
  for i := 2 to maxplayers do
  begin
    If BigNick<length(Getplayerstat(i,'name')) then bigNick:=length(Getplayerstat(i,'name'));
  end;
  Result := BigNick;
end;

procedure KDallRatio();
var
KD: Double;
kills, ID, i: Integer;
BNick : Byte;
spaces: String;
begin
  for ID:=1 to NumPlayers+2 do begin
  kills := GetPlayerStat(ID,'kills')-PlayerCaps[ID]*20;
  if GetPlayerStat(ID,'active') And GetPlayerStat(ID,'team')<>5 then begin
      i := 0;
      if(GetPlayerStat(ID,'deaths')=0) then i:=1
      KD := (kills)/((GetPlayerStat(ID,'deaths'))+i);
      KDarray[ID]:=roundto(KD,2);
    end;
  end;
  KDSort(Maxplayers);
  i:=0
  BNick:=BiggestNick();
  for ID:=1 to Maxplayers do begin
      if (GetPlayerStat(KDPos[ID],'active')=True) then begin
       i := Length(GetPlayerStat(KDPos[ID],'name'));
       spaces := '';
       while i<BNick do begin
        i:=i+1
        spaces := spaces+chr(32);
        end
        WriteConsole(0,GetPlayerStat(KDPos[ID],'name')+Spaces+'  '+FloatToStr(KDarray[ID]),KDColor);
       end;
  end;
end;

procedure KDRatio(ID: Integer);
var
KD: Double;
Kills: integer;
begin
kills:=GetPlayerStat(ID,'kills')-PlayerCaps[ID]*20;
if(GetPlayerStat(ID,'team')=5) then
WriteConsole(0,GetPlayerStat(ID,'name')+', Your are spectator.',KDColor)
  else if(GetPlayerStat(ID,'deaths')=0) then
  WriteConsole(0,GetPlayerStat(ID,'name')+', Your K:D is '+intToStr(kills)+' K:'+intToStr(kills)+' D:0',KDColor)
    else begin
    KD := kills/GetPlayerStat(ID,'deaths');
    WriteConsole(0,GetPlayerStat(ID,'name')+', Your K:D is '+FloatToStr(roundto(KD,2))+' K:'+intToStr(kills)+' D:'+IntToStr(GetPlayerStat(ID,'Deaths')),KDColor);
    end;
end;

procedure OnPlayerSpeak(ID: byte; Text: string);
begin
  if (Text = '!kd') or (Text = 'rate') then KDRatio(ID);
  if Text = '!kdall' then KDallRatio();
end;

procedure OnMapChange(NewMap: string);
begin
ResetCaps();
end;

*updated*
« Last Edit: May 04, 2007, 04:26:30 pm by iftach »

Offline Irlandec

  • Soldier
  • **
  • Posts: 176
Re: KD ratio script
« Reply #15 on: June 12, 2009, 03:23:16 pm »
I think , iftach wouldn't be mad if I post modified version of the script:

Script Name: KD ratio
Script Description: displays player's kills:deaths ratio.
Original Author(s): iftach
Core Version: 2.6.5
Compilation test: Passed
Exmaple code:
Code: [Select]
function BiggestNick() : Byte;
var
bigNick, i : Byte;
begin
  BigNick:=length(Getplayerstat(1,'name'));
  for i := 2 to maxplayers do
  begin
    If BigNick<length(Getplayerstat(i,'name')) then bigNick:=length(Getplayerstat(i,'name'));
  end;
  Result := BigNick;
end;

Whole code added in zip-structute file  :)

All kudos to iftach

« Last Edit: June 12, 2009, 03:32:46 pm by Irlandec »