How To Get UserName?

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

How To Get UserName?

Postby Guy71173cp » 12 May 2012, 03:16

Ok, I kinda suck at SmartFox. So, anyways, I am making an extension for coins, so it displays how many coins the user has back on the client. So the extension connects to an h2 database with a column called COINS. The extension get the value of that column and sends it to the client. The client displays the coins, but it shows all the coins in the database, and it was obvious it did that because I haven't modified the extension to get the username. I want it to get the user's name, send it to the extension, then the extension checks the database for that username and then identifies the amount of coins that user has, and then it is displayed on the client. Here's the coin extension:

Code: Select all

var dbase;
function init() {
   trace("Initing dbExtension");
   // get a reference to the database manager object
   // This will let you interact the database configure for this zone
   dbase = _server.getDatabaseManager();
}
function destroy() {
   // Release the reference to the dbase manager
   delete dbase;
}
function handleRequest(cmd, params, user, fromRoom) {
   if (cmd == "getData") {
      // create a SQL statement
      var sql = "SELECT * FROM USERS ORDER BY COINS";
      // execute query on DB
      // queryRes is a ResultSet object
      var queryRes = dbase.executeQuery(sql);
      // prepare the response object
      var response = {};
      response._cmd = "getData";
      // Here we create an array for storing the database data
      response.db = [];
      if (queryRes != null) {
         // Cycle through all records in the ResultSet
         for (var i = 0; i<queryRes.size(); i++) {
            // Get a record
            var tempRow = queryRes.get(i);
            // This object will hold the record data that we'll send to the client
            var item = {};
            // From the record object we can get each field value
            item.COINS = tempRow.getItem("COINS");
            response.db.push(item);
         }
         } else {
   trace("DB Query failed");
}
}

}
      _server.sendResponse(response, -1, null, [user]);


function handleInternalEvent(evt) {
   
}


Again, I'm a newbie, So I have no idea how to do this. I'm not sure this is right, but would it go something like this?

Code: Select all

if (cmd == getData){
user.getName()
var sql = "SELECT * FROM USERS ORDER BY COINS";
      // execute query on DB
      // queryRes is a ResultSet object
      var queryRes = dbase.executeQuery(sql);
      // prepare the response object
      var response = {};
      response._cmd = "getData";
   
         }


Yes, I know, I would need a LOT more, but I just don't know how to configure this. Any help is appreciated. Thanks!
User avatar
zachofthegolden1
Posts: 89
Joined: 05 Sep 2011, 19:31

Re: How To Get UserName?

Postby zachofthegolden1 » 12 May 2012, 12:52

If your using a database login you can use the response to handle the coins and the username
-ZG1
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How To Get UserName?

Postby rjgtav » 12 May 2012, 14:16

On the handleClientRequest method, one of the parameters is the sender, which is the user who sent the extension message.
You can get this user's username by simply doing sender.getName();
You are getting every users' coins because of your sql statement. You need to only retrieve the sender's coins, not everyone's.
You can use a simple sql statement like (where username is the name of the username collumn):

var sql = "SELECT * FROM USERS WHERE username='"+sender.getName+"'";
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 12 May 2012, 15:40

Hey,

My SQL statement has changed to var sql = "SELECT * FROM USERS WHERE NAME='"+sender.getName+"'"; like you said, but I get an error in the server console:

Code: Select all

[ WARNING ] > Error in extension [ coinextension.as ]: ReferenceError: "sender" is not defined. (coinextension.as#1603) Internal: 28 -- Line numb
er: 27 in file: coinextension.as


The sql statement is on line 27.

I'm also wondering, if I use that sql statement, how would I be getting the coins since that statement is checking the username only? Would it be from this:

Code: Select all

 item.COINS      = tempRow.getItem("COINS")


?

Thanks and any explanation and help would be appreciated.

~Guy71173
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How To Get UserName?

Postby rjgtav » 12 May 2012, 20:22

It was just an example code. In your case, the sender is the "user" variable.
Well, as you just need the information of one user, and usernames are unique, you can simply get the data from that username, without checking the password.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 13 May 2012, 21:09

Thanks, I changed my extension to:

Code: Select all

var dbase

function init()
{
        trace("Initing dbExtension")
       
        // get a reference to the database manager object
        // This will let you interact the database configure for this zone
        dbase = _server.getDatabaseManager()
}


function destroy()
{
        // Release the reference to the dbase manager
        delete dbase
}


function handleRequest(cmd, params, user, fromRoom)
{
        if (cmd == "getData")
        {
            // create a SQL statement
                var sql = "SELECT * FROM USERS WHERE NAME='"+user.getName+"'";
               
                // execute query on DB
                // queryRes is a ResultSet object
                var queryRes = dbase.executeQuery(sql)

            // prepare the response object
                var response = {}
               
                response._cmd = "getData"
               
                // Here we create an array for storing the database data
                response.db = []
               
                if (queryRes != null)
                {
                        // Cycle through all records in the ResultSet
                        for (var i = 0; i < queryRes.size(); i++)
                        {
                                // Get a record
                                var tempRow = queryRes.get(i)
                               
                                // This object will hold the record data that we'll send to the client
                                var item = {}
                               
                                // From the record object we can get each field value
                                item.name       = tempRow.getItem("name")
                                item.coins = tempRow.getItem("coins");
                               
                                response.db.push( item )
                        }
                }
                else
                   trace("DB Query failed")
               
                _server.sendResponse(response, -1, null, [user])
               
               
        }
}


function handleInternalEvent(evt)
{
        // Simply print the name of the event that was received
        trace("Event received: " + evt.name)
}



But nothing appears on the client. I am 100% sure the columns the database checked were in the table. Here is my client-side onExtensionResponse code:

Code: Select all

smartfox.onExtensionResponse = function(resObj:Object, type:String)
{
   
   if (type == "xml")
   {
      
      if (resObj._cmd == "getData")
      {
         trace(resObj.db[i])
         
         
         for (var i = 0; i < resObj.db.length; i++)
         {
            grid_dg.addItem(resObj.db[i])
         }
      }
   }
}




function getData()
{
   smartfox.sendXtMessage(extensionName, "getData", {}, "xml")
}




It appears in the datagrid because I am testing with the simple db extension example and I'm reading the tutorial. But nothing appears. As you can probably see, I used a trace to debug:

Code: Select all

trace(resObj.db[i])


But the output comes out as:

lt;var n=&apos;_cmd&apos; t=&apos;s&apos;&gt;getData&lt;/var&gt;&lt;/dataObj&gt;</body></msg>
undefined


Any help is appreciated.
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How To Get UserName?

Postby rjgtav » 14 May 2012, 23:01

Hello.
Please pay attention when you're using the API methods. getName() is a method, not a property, so you retrieve it by doing user.getName() and not user.getName.
That information you're getting is probably an empty query. I suggest you to also check if the queryRes size is greater than 0, before sending the response back to the client.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 14 May 2012, 23:29

Code: Select all

var dbase

function init()
{
        trace("Initing dbExtension")
       
        // get a reference to the database manager object
        // This will let you interact the database configure for this zone
        dbase = _server.getDatabaseManager()
}


function destroy()
{
        // Release the reference to the dbase manager
        delete dbase
}


function handleRequest(cmd, params, user, fromRoom)
{
        if (cmd == "getData")
        {
            // create a SQL statement
                var sql = "SELECT * FROM USERS WHERE NAME='"+user.getName()+"'";

               
                // execute query on DB
                // queryRes is a ResultSet object
                var queryRes = dbase.executeQuery(sql)

            // prepare the response object
                var response = {}
               
                response._cmd = "getData"
               
                // Here we create an array for storing the database data
                response.db = []
               
                if (queryRes != null)
                {
                        // Cycle through all records in the ResultSet
                        for (var i = 0; i < queryRes.size(); i++)
                        {
                                // Get a record
                                var tempRow = queryRes.get(i)
                               
                                // This object will hold the record data that we'll send to the client
                                var item = {}
                               
                                // From the record object we can get each field value
                                item.name       = tempRow.getItem("name")
                                item.coins = tempRow.getItem("coins");
                               
                                response.db.push( item )
                        }
                }
                else
                   trace("DB Query failed")
               
                _server.sendResponse(response, -1, null, [user])
               
               
        }
}


function handleInternalEvent(evt)
{
        // Simply print the name of the event that was received
        trace("Event received: " + evt.name)
}



That code still doesn't work. The same thing happens. Thanks.
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How To Get UserName?

Postby rjgtav » 14 May 2012, 23:42

Hmm... Sorry it's already getting late, and I'm already really tired...
Can you please check on the server-side if the data is received correctly? Add some traces to check the values you're dealing with. (like tracing the queryRes size, each Row and its data, etc.)
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 15 May 2012, 22:57

In the handleRequest, I traced out

Code: Select all

trace(item.name);


It returns as null. Got any idea? Here's the server:

Code: Select all

[coinextension.as]: null



Thanks.
User avatar
rjgtav
Posts: 2813
Joined: 19 Apr 2009, 11:31
Location: Lisbon, Portugal

Re: How To Get UserName?

Postby rjgtav » 15 May 2012, 23:22

It looks like your queryRes isn't fine. Try checking the SQL statement on the database directly. Check also what data does the Row object have.
Skills: SFS Pro, SFS2X, AS2.0/AS3.0, Java, HTML5/CSS3/JS, C#
Portfolio: https://rjgtav.wordpress.com/
SFS Tutorials: http://sfs-tutor.blogspot.com/ - Discontinued. Some examples may be bugged.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 16 May 2012, 02:21

I changed the item variables to this:

Code: Select all

 item.NAME = tempRow.getItem("NAME");
           item.COINS = tempRow.getItem("COINS");


The only difference is that I capitalized them. I also changed the sql statement a little:

Code: Select all

var sql = "SELECT COUNT (NAME) FROM USERS WHERE NAME='"+user.getName()+"'";




The extension looks fine, haven't made really any changes:

Code: Select all

var dbase

function init()
{
        trace("Initing dbExtension")
       
        // get a reference to the database manager object
        // This will let you interact the database configure for this zone
        dbase = _server.getDatabaseManager()
}


function destroy()
{
        // Release the reference to the dbase manager
        delete dbase
}


function handleRequest(cmd, params, user, fromRoom)
{
        if (cmd == "getData")
        {
            // create a SQL statement
                var sql = "SELECT COUNT (NAME) FROM USERS WHERE NAME='"+user.getName()+"'";

               
                // execute query on DB
                // queryRes is a ResultSet object
                var queryRes = dbase.executeQuery(sql)

            // prepare the response object
                var response = {}
               
                response._cmd = "getData"
               
                // Here we create an array for storing the database data
                response.db = []
               
                if (queryRes != null)
                {
                        // Cycle through all records in the ResultSet
                        for (var i = 0; i < queryRes.size(); i++)
                        {
                                // Get a record
                                var tempRow = queryRes.get(i)
                               
                                // This object will hold the record data that we'll send to the client
                                var item = {}
                               
                                // From the record object we can get each field value
                                item.NAME = tempRow.getItem("NAME");
                                item.COINS = tempRow.getItem("COINS");
                               
                                response.db.push( item )
                        trace(item.name);
                        
                        }
                }
                else
                   trace("DB Query failed")
               
                _server.sendResponse(response, -1, null, [user])
               
               
      }
}



function handleInternalEvent(evt)
{
        // Simply print the name of the event that was received
        trace("Event received: " + evt.name)
}


The database is in its usual condition. The weird thing is, it works if I just use var sql = "SELECT * FROM USERS ORDER BY NAME" but that was the problem: it displayed ALL of the coins, so after I changed the sql statement, it stopped working and I got the undefined error in the server. And every row has 1000 for the coin column. Thanks.
Guy71173cp
Posts: 148
Joined: 03 Aug 2010, 16:08
Contact:

Re: How To Get UserName?

Postby Guy71173cp » 16 May 2012, 02:37

I fixed it! I found out the problem was on the client, and the data wasn't displaying in the game. I figured that out because it displayed in the output the coins and the name. Thanks a TON!

Return to “SmartFoxServer 1.x Discussions and Help”

Who is online

Users browsing this forum: No registered users and 105 guests