Smart gift giver (or how to store data in a database)

Written by Kitsune

 

Written by Kahiro Watanabe

 

In the previous post we’ve seen a basic example of how to exchange data between SL and the Web, but there was not much utility in it. Now we are going to build a more powerful tool:

Let’s assume that we have a store and we want to give a gift to each visitor we have, but only once. If a visitor comes back again the gift will not be given.

How can we do that? The classic LSL option is storing the values in a list, but as I mentioned in the previous post there are memory and stability problems when storing data in LSL scripts, a much better option is to store it in a database so we can assure that our information will be safe and that we can store large amounts of it.

Before starting to code we first need to elaborate the basic concept of our application, if we start coding without having a basic structure of the project, then the result will not be good and will take us much more time to finish it… think about that before starting a big project.

Project name: Smart gift giver

Description: this application gives a gift to the visitor when touched only if the visitor didn’t get the gift in the past otherwise the visitor will receive a “Welcome back” message.

LSL script function: send key and name to the PHP script, wait for a response:

  • Response 1: “exists,userkey” will result in a “Welcome back” message to ‘userkey’
  • Response 2: “new,userkey” will give the object and will give a first welcome message to ‘userkey’

PHP script function: if a parameter key is received it will be checked against the database to see if it already exists.

  • Case 1: It exists, return Response 1 (“exists,userkey”)
  • Case 2: It doesn’t exists. Key and Name are added to the database and return Reponse 2 (“new,userkey”).

The table in the database:

How to design a database structure for an application is a large subject and I will try to give bits of information in each example, what you need to know now is that in a table we must find a way to uniquely identify each record, in this case we are going to use the user’s key, that will the primary key of the table.

Table Name: ‘customers’
Structure:

  • user_key (primary key) datatype: varchar (36)
  • user_name datatype: varchar (100)
    • Database Create - coming soon
    • table create
    • Create Fields - coming soon
    • The LSL script
    • // Object to give
      string giftName = "Shirt";
      
      // Url to send data
      string url = "http://www.mywebsite.com/index.php";
      
      // Request query
      key giftQuery;
      
      default
      {
          touch_start(integer total_number)
          {
              // get key and name from toucher
              key userKey = llDetectedKey(0);
              string name = llKey2Name(userKey);
      
              // build parameters for query
              string parameters = "userkey="+(string)userKey+"&username="+name;
      
              // send it to the url
              giftQuery = llHTTPRequest(url,[HTTP_METHOD,"POST",HTTP_MIMETYPE,"application/x-www-form-urlencoded"],parameters);
          }
      
          http_response(key request_id, integer status, list metadata, string body)
          {
              if (request_id == giftQuery)
              {
                  //we receive the message and the userkey whom the message/object must be sent
                  list result = llParseString2List(body,[","],[]);
      
                  string message = llList2String(result,0);
                  key userKey = llList2Key(result,1);
      
                  if (message == "exists")
                  {
                      llInstantMessage(userKey,"Welcome back");
                  }
                  else if (message == "new")
                  {
                      llInstantMessage(userKey,"Welcome to my land!");
                      llGiveInventory(userKey,giftName);
                  }
              }
          }
      }

       

      • The php script:

        index.php

        • 
          

           

          I will let you digest all this information, if there are some PHP functions or syntax that you don’t understand I highly recommend you to take a look at these two websites:
          PHP Tutorial from W3Schools
          The official php website

          Note: in this post I didn’t cover security issues, this is an important subject and I will adress in future posts as the complexity of the examples advances.