PDA

View Full Version : save/load overall statistic of the script work to inifile



CynicRus
06-26-2012, 07:37 AM
Hello. I apologize in advance for the quality of English language, but when writing I will try to make quality was as good as possible.

Studying SIMBA documentation I stumbled on the function of writing and reading from ini files. And the first question I had was 'Why there is a function of writing info to a file INI in SIMBA?' What can one apply it for? Storing settings? But it is easier to store settings in a script because they can often be changed. Thus, keeping settings in the file would be impractical. And I came up with an idea of how this feature can be used. ini files may be very convenient to store the statistics of script usage. Any statistics for the time of its work. I think it will be useful not only for me but for other developers. And if it seems useless to you, I'm just sharing with you my experience of working with ini files in Simba.
To storage the statistics I came up with the following structure: //just simple
type TStat = record//stats struct
TotalRunTime: integer;//Total running time
TotalExp: integer;//Total experience gained
TotalRunCount: integer;//The total number of launches
end;
You can use any other structure, it was coined solely as an example. Unfortunately, Simba can save only a string in an ini file. So I had to extend the standard features 'read\write' to ini files to be able to record integer and Boolean values to an ini file.

//internal function for convert boolean to int
function BoolToInt(ABool: boolean): integer;
begin
if ABool then
Result := 1
else
Result := 0;
end;
//internal function for convert int to boolean
function IntToBool(i: integer): boolean;
begin
if i=1 then result:=true else result:=false;
end;
//Write Integer value to ini
procedure WriteInteger(Section,KeyName,Filename: string; value: integer);
begin
WriteINI(Section,Keyname,toStr(value),Filename);
end;
//Write boolean value to ini
procedure WriteBool(Section,KeyName,Filename: string; value: boolean);
begin
WriteInteger(Section,KeyName,Filename,BoolToInt(va lue));
end;
//Read integer value from ini
function ReadInteger(Section,Keyname,filename: string): integer;
var
i: integer;
s: string;
begin
s:=ReadINI(Section,Keyname,Filename);
i:=StrToInt(s);
result:=i;
end;
//Read boolean value from ini
function ReadBoolean(Section,Keyname,filename: string): boolean;
var
s: string;
i: integer;
b: boolean;
begin
s:=ReadINI(Section,Keyname,Filename);
i:=StrToInt(s);
b:=IntToBool(i);
result:=b;
end;

After such an extension of standard mechanisms ini files work, the task of storing statistics regarding the use of script is reduced to writing\reading the values from the ini file to your structure and then using this structure in the script.

//save stat struct
procedure SaveHistory(hist: TStat;ininame: string);
begin
if not FileExists(ininame) then CreateFile(ininame);
WriteInteger('TOTAL','TotalTime',ininame,hist.Tota lRunTime);
WriteInteger('TOTAL','TotalExp',ininame,hist.Total Exp);
WriteInteger('TOTAL','RunCount',ininame,hist.Total RunCount);
end;
//function for loading stat history, return TStat struct
function LoadHistory(ininame: string):TStat;
var
hist: TStat;
begin
if not FileExists(ininame) then
begin
WriteLn('File'+'_'+ininame+'_'+'not found');
end;
hist.TotalRunTime:=ReadInteger('TOTAL','TotalTime' ,ininame);
hist.TotalExp:=ReadInteger('TOTAL','TotalExp',inin ame);
hist.TotalRunCount:=ReadInteger('TOTAL','RunCount' ,ininame);
result:=hist;
end;

Testing script:

var
RStat,WStat: TStat;
FileName: string;
begin
Filename:=AppPath + 'Scripts\MyScriptStats.ini';
WStat.TotalRunTime:=2400;
WStat.TotalExp:=13000000;
WStat.TotalRunCount:=200;
SaveHistory(WStat,Filename);
RStat:=LoadHistory(Filename);
WriteLn('Total Time:'+toStr(RStat.TotalRunTime)+'/'+'Total Exp:'+ToStr(RStat.TotalExp)+'/'+'Total Run Count:'+ToStr(RStat.TotalRunCount));
end.

Test output:

Compiled successfully in 31 ms.
Total Time:2400/Total Exp:13000000/Total Run Count:200


Thus, this algorithm can be used not only for the storage of statistics, but also to store whatever information. For example, storing form settings, the state of global variables, etc. I hope it was useful for you.Thank you for your attention. Enjoy!

See the full test script in the attachment.

Full test script see in attachment.

Le Jingle
07-31-2012, 10:17 PM
Huh, I was browsing the tutorial section and found this.
I like your work, it's a good fit for intermediate learning in my opinion, especially introducing people to utilizing .ini files in scripts.
Reminds me of a thing in msi;


C:\Simba\Includes\MSI\MSI\Extras\Accounts\Characte rLevels.html

Brandon
07-31-2012, 10:30 PM
There really isn't a use for Bool To Int :S You can use StrToBool, and BoolToStr.

Also I do not see the usefulness in wrapping a function just to add an integer :S

That entire wrapper can easily be:

WriteINI(Section, Key, ToStr(...), FilePath);


and

StrToType(ReadINI(Section, Key, Path));

// OR

StrToTypeDef(ReadINI(Section, Key, Path), DefaultValue);


Where StrToType can be:

StrToInt
StrToBool
StrToIntDef(Val, DefaultVal)

etc..

Also an INI file can be made just as useful as an XML file. It's a way of storing Data for accessing later. I used it to store default settings of a Form. Someone loads a form to enter player info and stuff, you don't want them to have to enter it into the form every single time. Thus an INI file can help there as you can Initialize the form with previous data upon start.

masterBB
07-31-2012, 11:11 PM
I used ini files before to store the statistics of my loops before. Average time taken and such. This way my script would pick out extra ordinary waits itself.

Also in lape:
procedure WriteINI(Section, Keyname:String; value:Variant; Filename:String); overload;
begin
WriteINI(Section, Keyname, ToStr(value), Filename);
end;

In pascalscript:
procedure WriteINIUnTyped(Section, Keyname:String; value:Variant; Filename:String);
begin
WriteINI(Section, Keyname, ToStr(value), Filename);
end;