Author Topic: [code snippet] function parseVarString  (Read 2488 times)

0 Members and 1 Guest are viewing this topic.

Offline Maff^

  • Major(1)
  • Posts: 20
    • Personal page
[code snippet] function parseVarString
« on: June 06, 2007, 09:52:28 am »
Hi, for a MOTD (message of the day) script I was working on I created this function, but it should be usefull to some other scripters so i taught i would post it separately.

Script Name: parseVarString
Script Description: Replaces variables (placed between {}) in a String with servervars.
Original Author(s): Maff^
Core Version: 2.6.1
Code:
Code: [Select]
function parseVarString (const source:String): String;
var
i,vStart,vEnd:Integer;
tmp,varname,value:String;
invar:Boolean;
begin
invar := false;
for i := 1 to length(source) do begin
tmp := tmp + source[i];
case source[i] of
'{': begin
invar := true;
vStart := length(tmp);
end;
'}': begin
vEnd := length(tmp);

if (invar) then begin
Delete(varname,1,1); // remove { at the front of the var

case varname of
'CoreVersion':    value := CoreVersion;
'MaxPlayers':     value := inttostr(MaxPlayers);
'NumPlayers':     value := inttostr(NumPlayers);
'NumBots':        value := inttostr(NumBots);
'CurrentMap':     value := CurrentMap;
'NextMap':        value := NextMap;
'TimeLimit':      value := inttostr(TimeLimit);
'TimeLeft':       value := inttostr(TimeLeft);
'ScoreLimit':     value := inttostr(ScoreLimit);
'GameStyle': begin
case GameStyle of
0: value := 'Death Match';
1: value := 'Point Match';
2: value := 'Team Match';
3: value := 'Capture the Flag';
4: value := 'Rambo';
5: value := 'Infiltration';
6: value := 'Hold the Flag';
end;
end;
'Version':        value := Version;
'ServerVersion':  value := ServerVersion;
'ServerName':     value := ServerName;
'ServerPort':     value := inttostr(ServerPort);
'AlphaPlayers':   value := inttostr(ord(AlphaPlayers));
'BravoPlayers':   value := inttostr(BravoPlayers);
'CharliePlayers': value := inttostr(CharliePlayers);
'DeltaPlayers':   value := inttostr(DeltaPlayers);
'AlphaScore':     value := inttostr(AlphaScore);
'BravoScore':     value := inttostr(BravoScore);
'CharlieScore':   value := inttostr(CharlieScore);
'DeltaScore':     value := inttostr(DeltaScore);

'System':         value := GetSystem();
'Date':           value := FormatDate('mm-dd-yyyy');
'Time':           value := FormatDate('hh:nn:ss');
else
value:= '!--error--!';
end;

if (value > '!--error--!') then begin
Delete(tmp,vStart,(vEnd-vStart+1));
Insert(value,tmp,vStart);
end;
end;

varname := '';
invar := false;
end;
end;

if (invar) then begin
varname := varname + source[i];
end;

end;
Result := tmp;
end;

Example:
Code: [Select]
WriteLn(parseVarString('{NumPlayers} players playing {GameStyle} on {CurrentMap}'));
// could write "12 players playing Death Match on Arena" to console

Notes:
- Only tested it on 2.6.1 dedicated Windows server.
- Check code for available vars.
- I've almost no Pascal experience, so maybe i'm doing this all wrong, but here it works.. ^_^
- I (or someone else) should write some code to format the TimeLeft var.
- I know, there could be a lot more available vars, but for my use this is sufficient . (Add it yourself, you lazy ******* :P)
« Last Edit: June 06, 2007, 10:09:33 am by Maff^ »
Live server info for Zitro-Stats: topic | brokenliquid.net/zitrostats/
Brokenliquid.net DM Serv (soldat://130.89.164.52:23073/) (1.4.2)
now playing: Jungle (2/32) next: Krab

sig updated on 06-10-2008 23:28:45 (GMT+1)

Offline KeYDoN

  • Major
  • *
  • Posts: 60
Re: [code snippet] function parseVarString
« Reply #1 on: June 06, 2007, 04:50:55 pm »
EnEsCe, this shouts for a eval function :D