11 May 2012

Coursework 2: JavaScript Animation Game Spiced Up – Post 2

Hello there it's me again posting on the second coursework given to me by my lecture. Today's post will not be as I had planned out. Why? Well...if you remember in my previous post on the subject, I decided to make my game store the user's details such as passwords and high scores in an XML file rather than a Database and, well...I ended up learning it the hard way that XML is not suited for that kind of use, but on the plus side I also learnt how to read and write to and from an XML file by using PHP.
Therefore I am still going to post on how to do these two tasks and then conclude by explaining why PHP is not ideal for this kind of use.


Reading from an XML file using PHP
After I had done some research I learnt that you could use the SimpleXML toolset to read from an XML file via PHP.
    
    $xml = simplexml_load_file("userSource.xml");

    foreach ($xml->xpath('//user') as $user) {
        if ($userID == (string) $user->userID) 
            if ($password == (string) $user->userPW) {
                $_SESSION['userName'] = (string)$user->userName;
                $_SESSION['highScore'] = (int)$user->highScore;
                $_SESSION['lastBeatenBy'] = (string)$user->lastBeatenBy;
                $_SESSION['placing'] = (int)$user->placing;
                $_SESSION['Authenticated'] = "yes";

                return TRUE;
            } else {
                return FALSE;
            }
        }
    }
    return FALSE;


Code breakdown:
First the XML file is loaded into an object which I named '$xml'.
$xml = simplexml_load_file("userSource.xml");

Next a For Each loops through all the users, this is achieved by using Xpath with the XML object to identify the specific nodes you are interested in.
foreach ($xml->xpath('//user') as $user)

Comparisons can also be achieved with the XML object. For instance on every iteration, the code checks if the current User's userID child element value matches the User Id stored in a variable named '$userID'.
if ($userID == (string) $user->userID)

Note that I am parsing the XML value into a string. This may not look like much, but it's very easy to forget and I did have some problems with this at the start as I wasn't parsing the values.

Finally to read the values stored from an XML file and into a PHP session variable requires the same technique as when comparing, but instead you simply assign the value to the session.
$_SESSION['userName'] = (string)$user->userName;

Writing to an XML file
Writing to an XML file is as basic as reading, below I have displayed the code I used to carry out this task.

        
        $xml = simplexml_load_file("userSource.xml");
        
        $user = $xml->addChild("user");
        $user->addChild("userName",$_POST['fullName']);
        $user->addChild("userID",$UserID);
        $user->addChild("userPW",$_POST['password']);
        $user->addChild("highScore","0");
        $user->addChild("placing",$UserCount);
        $user->addChild("lastBeatenBy","");
        
        $xml->asXML("userSource.xml");

Code Breakdown:
The first line is exactly like the reading example in which I load the XML file into an object.


The next step is to add an additional <user> node to the already existing XML file and store it as an object in order to add the required child nodes to it.
$user = $xml->addChild("user");
The parameter passed to the 'addChild()' function represents the name of the node to be created.

To add child nodes with values to the <user> node, which is currently in memory, you use the 'addChild()' function once more, but this time pass the value as an additional parameter.
$user->addChild("userName",$_POST['fullName']);
The value parameter in the above example is being retrieved directly from the  POST variable, although variables and hard coded values can also be used. 

Why XML is not ideal for storing info about users on a web application
So, here is how it all went down. after I hanaged to read and write into the XML file, it dawned upon me...This game will probably have more than one user playing it at the same time!

So if, lets say user `A`, opened the XML file to memory in order to write his/her details to the file and user `B` opened the file as well to save his/her new high score, in the meantime user `A` saved the file with the new details, but the version user `B` has opened doesn't have the information entered by user `A`, therefore once user `B` saves, he/she will overwrite the changes made by user `A`.


To solve this problem you could try to devise some kind of collision detection system, but unless you are forced to use only XML, that is not a feasible solution, therefore I will have to go back to the drawing board and make my game store its data on a database.




"Mistakes are a part of being human. Appreciate your mistakes for what they are: precious life lessons that can only be learned the hard way. Unless it's a fatal mistake, which, at least, others can learn from." - Al Franken

No comments:

Post a Comment