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: 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:
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)