Thursday, 2 June 2011

Second Life (cont.) Building Objects and Scripting

Introduction

In this blog post we go into more detail in Second life and start building some objects in this virtual world. To start building in Second Life first you must go in a place in the world that allows the user to build. These places are called Sandboxes, it is only allowed to build in these Sandboxes and each Sandbox has its own rules such as you cannot build a wall around an avatar caging him being not able to get out without a teleport. Failure to abide these rules could result to a ban from Second life.

Building Objects

To Start building in the sandbox you right click the ground and select build from the menu shown this will make the building menu visible on the screen. Building is done by selecting shapes (prims) and placing them in the world. These shaped can be modified in shape, position and size as the user wants. Also multiple items can be linked together to form one object. This is done so that an Object will have more details to it and will look better.








The first object I created was a couple of boxes linked together to make a very bad looking chair that an avatar can sit on.  Every avatar can sit on your object if you are not currently editing it. I made sure that when an avatar will sit on my object, it will sit properly.






Also you can create objects and wear them on your avatar carrying them around where you go showing, while showing them to other people. For this I decided to create a simple War hammer for my Avatar to carry around. I used the cylinder type prim and a cuboid type prim and linking them together after positioning them carefully at the right place.


To wear the object, just right click it and select wear from the menu. I decided to put the object I created on the right hand of my avatar. When I selected this the hammer didn't slot in my avatar's right hand correctly so I adjusted its position as well as I could to make it look like my avatar is carrying the hammer in its hand while walking.









Scripting


Users in 2nd life use Linden Scripting language to script objects and have them interact with the rest of the virtual world. To script an object, right click it and choose edit to open the edit window. In the edit window you may select "more>>>" to reveal five tabs marked general, object, features, content, and texture. Then choose "content".  This window shows the contents of an object which can hold scripts, notecards, and even other objects. Press "new script" to add a new script. 

When scripting an item you have to tell the object what to do and when will it do it. Simple examples of what an object can do with script include:

  • Rez an object
  • Give something to an avatar
  • Say something in chat
  • Change the object's color
  • Change the object's texture
  • Prompt an avatar to load a URL in a web browser
  • Create hovering text
  • Play a sound
  • Move around



Amongst other situations these scripts could be performed in the following situations:

  • As soon as the script starts
  • When an avatar touches your object
  • On a repeating timer
  • When an avatar is nearby
  • When someone says something


             
Here is an example of a script of an object changing its color to red whenever somebody touches the object the script:


default
{


touch_start(integer total_number) {


// change color!
llSetColor(<1.0,0,0>,ALL_SIDES);


}


}






This following example will have the object say "Hello"  in the chat on every 30 seconds.


default
{


state_entry() {
llSetTimerEvent(30);
}
timer() {


// speak out loud!
llSay(0,"Hello");


}


}





After experimenting with these simple examples on the Hammer I created,  I decided to create some more simple objects.

First I started with a very simple chair that would be used to sit on. I just used a cube to sit on and a thin cuboid to make the back of the chair, I combined both objects and made the avatar sit on it.


Finally as my last 2 scripts I decided to give a new script to my hammer to send emails and make it open an internet browser at a specific webpage. 

The script used for the emails is as follows:


default  
{  
  touch_start(integer total_number) 
  {   
    llEmail( "danielsammut92@gmail.com", "Test", "Test" );  
  }  







And The script used to open a web browser to a webpage I desired is :




default  
{  
      touch_start(integer total_number) 
      {  
             llLoadURL(llDetectedKey(0), "view my blog",   "http://sammutdaniel.blogspot.com/");  
       }  
}




Both scripts are run whenever the object is touched.


Wednesday, 25 May 2011

Introduction to Second LIfe


Introduction


This week, we start out on something completely different from what we have done so far in this topic. After going through so many different Web technologies such as HTML, CSS, JavaScript and PHP we will now begin a few weeks working on Second Life.

Second Life is an online virtual world developed by Linden Lab launched in 2003, This is like a social networking website but instead of profiles people communicate with each other with the use of avatars. An avatar is a graphical representation of the user’s character. This is similar to many Multiplayer Online RPGs (Role-Playing Games) where a user has his own personalized avatar, meets with other people’s avatars on the network and then do stuff together.


What is different in Second Life then other Online Games and Networks is the ability to build stuff in the virtual world and then program it in any way you want. This could be anything from clothing to buildings to cars with each having its own individual use.



Installing and Logging In

To install the client program for Second life an account was created with Second life on the website creating a username and password with the username being used as the name of your avatar and a sample Avatar is chosen to start with which then could be fully customized to the users desires (except the username). After registering the client application is downloaded and installed.


After installing the application, one could log in by using the username and password chosen in the registration. Also you must chose to login in either Basic or Advanced Mode. The difference is that in Advance mode you could code using Linden Scripting Language and the menus are arraigned in a slightly different way. What I found to be quite puzzling is that before you log in if you change from Basic to Advance Mode or vice versa you have to close the application and restart it. Since at first I decided that I will just look a bit around to see how things work in Second Life I decided to run in Basic Mode.


What I found to be quite annoying in Second life is the massive loading time it took to load everyone’s Avatar and the terrain around you and I am using a laptop which I can run games which came out a month ago smoothly so I can only imagine in 2003 how much worse the loading times where at that time since the hardware wasn’t as good. When I saw this I decided to lower the graphics used as much as possible so that it will run smoothly but for some reason I could not find the edit menu item anywhere to change the graphics (see screenshot below) at least I found a shortcut to go to the preferences (ctrl + p) and I lowered the graphics. Although the application now was much smoother when walking around the loading times where still long to load everyone’s Avatar and the terrain when either logging in or teleporting to a new locations.


The World of Second Life

Apart from this when logging in the first time you will be taken to a place to learn the controls. I found that the hotkeys/shortcuts automatically set for the controls are much simpler to use then the actual controls detailed in this part by Second Life. For Example, the key that is used to fly (yes you can fly around in Second life) is told to be “Page Up” and to go back down to the ground is “Page Down”, while the hotkeys for this command is “e” and “c”. Also the same applies for moving, I much prefer the  WASD set up of the hotkeys then the arrow keys since the mouse is used a lot in this to click objects and interact with them.



Teleporting




Since I mentioned teleporting before, let me expand on that a bit.


In Second life you have a search where you can find your friends and add them to your friends list by sending them a request. Once this is done and the request is accepted you could have private chats with anyone in your friends list and also something which is helpful is that you could ask for a teleport to go near friends quickly instead of needing to chat and decide on a specific place to meet and then make sure it’s a place where your fiend will see your avatar.


Conclusion

To conclude this post, I decided to get a change of clothes so that I won’t look like a complete beginner. I right clicked my character and selected edit appearance and started messing around with the shape and clothes of my avatar. I changed his hair, his figure through the multiple menus and features and decided on a short and fat dwarf like shape with a big beard since all the general male avatar seemed tall and well built and I like to be different. Next week we’ll start building some objects and scripting



















Wednesday, 4 May 2011

CourseWork 2 Web space management system

Introduction

This blog post will detail how the coursework of the webspace management system was created and explaining all of its features. The system will consist of 3 main pages: the login page, the mainpage and a registration screen.

Here is the explaination given to us on what we should do.


Coursework Task

Write a remote Web space management system suitable for handling Web site content of various types. It should allow a user to add and organise files to a personal Web area which is secured by password. Again, there will be a connected theme, to be announced in lecture time.

This program should include the following features.


1)  Your program should be written as PHP scripts . The user should be able to work with this program via a Web browser from any computer connected to the Internet.

    2)  When your program starts, it should ask the user to enter a password and username, and not allow the user to access their Web space unless they know the password. Passwords should be stored in an appropriate MySQL database.
   
    3)  Once allowed access the user should be able to:
    a)  Make and delete directories
    b)  Delete files 
    c)  Upload new files
    d)  Only be able to access their own space, which should have space limits imposed upon it.

User data and passwords should be stored in an appropriate MySQL database or file system which the PHP script can access; you must consider security implications. There are no restrictions on the style of the user interface, although you should consider ease of use and design to be issues too.



Login Page

I started off with building on the login screen created in last week’s PHP with MySQL blog post. The main improvement on the Login Screen was that an extra button was created called Register which will be used to redirect the user to the registration screen. I also gave the login screen a bit of a space theme although I am no designer this is the best I could come up with.



Apart from this new button the login form has still basically the same functionality as before using cookies for the “remember me” function  for automated logins and using sessions to remember the username when the user is redirected to the mainpage so that only his page is viewed.


Registration Screen

This page consists of three textfields in which the user will fill in the Username desired to register and the password for the user. The 3rd textfield will be used as a confirm password as most registration form are. Again the Space theme was kept throughout for consistency.

  

When the user has filled in all the fields the user will press the register button which will first check if all the fields are filled in, then it will check if the password and confirm password match. If they do, an SQL connect is created with the localhost server and an SQL query is sent to the server to find out if the username chosen by the user already exists. This is done by selecting all the rows from the Users table where the Username is the username inserted by the user.  If the result of the query has a row it means that the username already exists.

  
If the Username doesn’t exist the username and password chosen are inserted in the table using the “mysql_query”. After the record is inserted the program will try to create a folder on the Server with the username as the folder name. This folder will be used as the folder loaded in the mainpage when this new user is created. If the folder is created a Session is created detailing that the registration is successful and the user is redirected to the login page.

  

The login page is checking for a session called Successful which is only created when a registration is successful, if its value is “Yes” an alert box is shown to let the user know that the Registration was successful. This was done in this way since if the alert box is created in the registration page, it is not shown due to the page is redirected to the login page, even if the alertbox is before the redirecting in the coding.




MainPage

When this page is loaded a check for a session named Username is done to write the Welcome + username. On the page, Also the session is used to open the folder using the “opendir” function in Php, the folder where the user will upload all his files since when this is created the name of the folder is the username chosen by the user. If the session is not found, the page is redirected back to the login.




This page consist of a file input with a button to start the event, a textbox with a button to add a folder with the name inserted in the textbox, a button to go back to the previous directory if you accessed a folder inside your personal space, a logout button to go back to the login screen, and a list of all the files and folders inside the space of the user name with a delete button near each one to be able to delete it. Here is a detailed explanation of each feature in the main page and how it was achieved using PHP.


The feature to load the files and folders of the user is achieved by using the “is_dir” feature to know if the item loaded is a folder or a file. If it’s a folder instead of just showing just showing the name of the file, the name of the folder would be a hyper link that would call a function to show all the files inside that particular folder by sending the name of the folder and adding it to the path used to get all the files from.


  
To know which files and folders to show when using the “opendir” function is that the directory which is opened, is the one created when the registration was done is used by going to the main folder of the webspace. Then adding the Username to the directory, since that is the one created.



Also when showing every file and folder name a delete hypertext field is created to be used to delete the file or folder.  This hyper link sends the name of the file or folder to a function which check if it is actually a file or a folder first using the "is_dir()" and then if its a file, it deletes it using the unlink feature. If its a folder it first has to go into every file and unlink it one-by-one before deleting the directory since PHP would give an error if it tried to delete a folder which had any kind of content.






As explained before another function in the Webspace is the ability to Upload Files. This is achieved by using the file input and HTML; the user will select a file from his computer and then click the upload file button. The PHP code will check if the file size is larger than 2MB the file is not loaded an error is given. Also an error is given if the size of all the items in the folder used by the user plus the size of the file to be uploaded exceeds 10MB. I chose the limit to 10MB so that I did not need to upload a lot of files before the limit is reached and testing could be done properly. When the file is uploaded the page is reloaded using the HEADER function so an alertbox shows the user that the folder is created and it is now shown since the page is reloaded the new folder is now shown as well.

  

To Find out how much space is used, a function was created that will add every files' size to a variable in the main directory of the user, and then before uploading the file adding it to the size of the file that is going to be uploaded now. This is done because It could be that the file that will be uploaded now will exceed the maximum limit.



Another feature is to add new folders in the webspace. A Textbox along with button to add folder exists to make this function properly. This feature is achieved when pressing the “Add folder” button and the PHP script will take the value of the textbox and using the “mkdir” function in PHP a folder is created. Again the program checks if the folder already exists and if it does the folder is not created. After the folder is created the program uses the header so that the user will go back to the current page, so that an alertbox shows the user that the folder is created and it is now shown since the page is reloaded the new folder is now shown as well.



If the User accesses a folder inside his webspace, every item uploaded or folder created will be now inside that particular folder. Also if the user wishes to access the main folder again, he does not need to log out and log back in. The "Back to Previous Directory" will take the user up one level in the directory using the "dirname" function in PHP which gives you the directory name of where an item currently is so by giving it the current path, the directory which it will give us is the folder we need to go back to the previous directory. A validation is created so that the previous will not go further than the main screen of the specific user.




Also a very simple logout button is used to unset and destroy the sessions used in when logging in and the page is redirected back to the login page. 




In the end this is what the main page looks like.




Conclusion

After finishing everything I made sure that the website is compatible in all browsers, since I kept the CSS to a minimum, everything was the same in every browser tried (Google chrome, Firefox 4, Internet Explorer).


Although I have spent a lot of time on this coursework I still believe that a lot of improvement on features could be done such as Downloading files that one has uploaded, creating a folder offline for the user which would sync with the webspace, or having an e-mail sent to the user whenever he registers first and a feature to send him an e-mail with the password whenever he forgets it.

Also I think that some improvements on styling could be done but since I never had any styling or designing experience I tried to keep things simple.

Some limitations I have done creating this Coursework was that every page had only 1 file that had all its PHP scripts. This proved to become quite messy when fixing errors or trying to find specific code. Next time I would make sure to structure the scripts into more files for at least every main function and link them to the main page using the "include" function.

As For my Experienced when using PHP over these last few weeks, coming from .NET Developer who never did any Web Development before was, that PHP although a very powerful scripting language still leaves us some things to be desired such as a good Debugging tool and maybe its strictness over Case-sensitivity can be a little tedious.

Friday, 29 April 2011

Using MySQL with PHP

Introduction

In this blog post, we will take our PHP login page created for the previous post and build on to include functionality of MySQL databases.
The task for this week is to:

  • Log into the SQL server using the command line and perform some commands such as listing the databases
  • Attempt to connect to SQL by using PHPMYADMIN tool
  • Create a Database that stores usernames and passwords
  • Modify your PHP program from the previous lab session to connect to the database to authenticate the user. 

Log into the SQL server using the command line and perform some commands such as listing the databases

First the command line is opened by running the cmd.exe in the windows 7. To login to the MySQL server first you need to find the Bin folder located in the MySQL directory in my case “D:\xampp\mysql\bin”. To go to this directory in the command line first enter "D:" to go to the D drive and then the command “cd D:\xampp\mysql\bin” was entered.
To log into the Server you must use the following command and login as root for the first time since no other users exist (unless more users where created using XAMPP, but more on that later), The command used was : “mysql -uroot –p”. When this command is entered a prompt for the root password appears as shown belong.

As soon as one logs into mysql the path in the command line will be changed to mysql. Here is where one can run commands in the server. To show all the databases you have in the MySQL server “show databases;” command was entered with the result below.
  






Attempt to connect to SQL by using PHPMYADMIN tool

To login into PHPmyAdmin the Xampp homepage was logged into (by typing http://localhost). From the menu here, the PHPmyAdmin was selected. This will prompt the user to insert the login for MySQL (in this case we kept using “root”).


The phpMyAdmin is a tool build in XAMPP to automate the creation of databases and tables without the need of writing any SQL code. As one can see from the screenshot below all the databases are listed on the left, and by clicking on any one of them, you will enter in another screen that allows you to edit the particular database, and also shows all the tables that are in that database.



Create a Database that stores usernames and passwords

To Create a Database using the interface provided in phpMyAdmin is very easy. In the main page of the tool there is a textfield under “MYSQL LOCALHOST” that allows you to enter the name of the database you want to create and by clicking the “create” button the database is now created in the MySQL server.
When this button is clicked the next screen shown is to start creating tables to be used in this database, using the same concept of before, the name of the table was entered and the number of fields in the table was also entered (in this case 2). The go button will take you to the next screen. This screen is used to create the fields of the table by inserting the name, type, and primary keys etc… The Save button in the bottom of the screen will then perform the SQL command to create the table with the information given.






Once the table is created some data of usernames and passwords where entered by using an insert command in the SQL screen or the insert feature from GUI of the Tool.


Modify your PHP program from the previous lab session to connect to the database to 
authenticate the user

Once all of that was set up, it was time to change the Login screen created to incorporate the use on the MySql database.

First to connect to the database a variable with  the "mysql_connect" function was used an it was given the server name and login and password.

Then to select the database "mysql_select_db" was used to select the database.

A query was used to select all the usernames and password where the username and password are the ones inputted in the textboxes. To reduce the chances of SQL injection attacks the "sprintf(" function along with the "mysql_real_escape_string" function to pass the username and password instead of just insert them into the sql query string.

A variable is then used to store the results of the query and another variable will hold the number of rows of the result. 

If there is a row in the results then the username is confirmed to be genuine thus a Session is started to send the username to the next page and the user is now redirected to the mainpage.

In the end the SQL connection is close to reduce the usage of resources.