The online racing simulator
How to detect changes in a .txt file?
Hello everyone!

I'm currently working on a new way to store and load user data using .txt files since the current method of storing and loading data in LFSLapper is inefficient and slow.

Here's a small snippet from my source so you can understand what I've done exactly.


<?php 
    
IF ($userName != "")
    
THEN
        $userDataName 
$userName."_database";
        
$dataDirectory "C:\LFS\Server\Insim\bin\default\Databases";
        IF (
FileExist($dataDirectory,$userDataName,".txt") == 0THEN
            
            
#Create new database file.
            
CreateFile($userDataName,$dataDirectory,0);
            
            
#Add $userName's data in the database file.
            
PrivMsg($userName,"creating a new database for ".$userName."...");
            
            
#Necessary internal variables.
            
$defUserData[1] = "ExampleVariable1:".$DefUser["ExampleVariable1"];
            
$defUserData[2] = "ExampleVariable2:".$DefUser["ExampleVariable2"];
            
$defUserData[3] = "ExampleVariable3:".$DefUser["ExampleVariable3"];
            
            
$totalData 3;
            FOR (
$Data 1$Data <= ToNum($totalData); $Data $Data 1)
                
$Text $defUserData[ToNum($Data)];
                
EditFile($userDataName,$dataDirectory,$Text,-1,".txt");
                
PrivMsg($defUserData[ToNum($Data)]."...Created!");
            ENDFOR
            
PrivMsg($userName,"^7Finished creating new data!");
            
            
#Read the database and set the necessary variables.
            
PrivMsg($userName,"^7Reading...");
            
$LinesofFile ReadFile($userDataName,$dataDirectory,".txt");
            
$NrOfLines ToNum($LinesofFile["NumberOfLines"]);
            FOR (
$i 0$i <= $NrOfLines-$i $i 1)
                
$Line $LinesofFile[$i,"Line"];
                
$splitData SplitToArray($Line,":");
                
SetPlayerVar($userName,$splitData[0],$splitData[1]);
                
PrivMsg($userName,$splitData[0]."...Loaded!");
            ENDFOR    
            
PrivMsg($userName,"^7Finished reading!");
        ELSE
            
#If database file exists, read it and set the necessary variables.
            
PrivMsg($userName,"^7Loading user data...");
            
$LinesofFile ReadFile($userDataName,$dataDirectory,".txt");
            
$NrOfLines ToNum($LinesofFile["NumberOfLines"]);
            FOR ( 
$i 0$i <= $NrOfLines-$i $i 1)
                
$Line $LinesofFile[$i,"Line"];
                
$splitData SplitToArray($Line,":");
                
SetPlayerVar($userName,$splitData[0],$splitData[1]);
                
PrivMsg($splitData[0]."...Loaded");
            ENDFOR
            
PrivMsg($userName,"^7Successfully loaded all databases!");
        ENDIF
    ENDIF
?>

Now what I'm wondering is, when I create a new script or a new feature for the insim, I have to create a new variable to manipulate the feature I'm trying to work with, right?

So, I'm wondering how can I make my script detect missing variable in the .txt file and add it to the .txt file so it can correctly store all variables and load them correctly.
I think the userfile will look like this?

UserName = Bass-Driver
Cash = 98987
Cars = UF1+XFG+XRT
etc

You could create a array with all the variables.

GlobalVar $Variable;
$Variable[1] = "userName";
$Variable[2] = "Cash";
$Variable[3] = "Cars";
$Variable[4] = "Distance";

Verify each line of the file with the name of the $variable.
And if it doesnt exist. You create a new line with the new variable.
Forgive me for asking, but why would someone prefer storing data in text file over using a database?
Quote from Yisc[NL] :Forgive me for asking, but why would someone prefer storing data in text file over using a database?

I realized with the original method how painfully slow it is to store and load data, especially during auto-save sessions when there's like 10+ people in a server, it literally freezes the entire insim for a minute (or even more depending on the player count)

While my method of storing and loading data is much much faster (in literal seconds) and it has a lot of flexibility too, such as retrieving offline player data (example: offline bans or offline adjustments to variables on the fly)
Okay, I can't argue with that as I haven't seen the evidence.
When I started my "programming" carreer, I made a few websites with PHP and everything stored in text files, until it became sooooo complex to handle all the data, so that's when I moved on to learn how to use an SQL database.
As said, I haven't seen what you have seen, so I will take your word for it, but from my professional opinion I would believe that using the Lapper database is always faster (and much easier to code) then storing/retrieving things from files.
Quote from Yisc[NL] :I made a few websites with PHP and everything stored in text files, until it became sooooo complex to handle all the data, so that's when I moved on to learn how to use an SQL database.

Funny I kind of did the opposite. Learned SQL first then played around with text files. Both SQL databases and flatfile databases are useful for certain applications. Also if its sqlite, that's still technically text files.

Sorry for bringing it off topic Wink
#7 - expr
Quote from TechEdison :Also if its sqlite, that's still technically text files.

How come? I doubt that Smile.

FGED GREDG RDFGDR GSFDG