Sending score and time to database

being new I was wondering if it was possible once the user reaches the final scene, to have their time and score sent to a database on another pc? I’m able to have the score and time printed out on the screen but I’m not sure if it’s possible to automatically have them sent to a database (to be viewed)?

I’m assuming I would need to create a table and have them automatically inserted, but how to do that and send it every time a scene is reach is what I’m wondering.

Thank you in advance.

local composer = require ("composer") local scene = composer.newScene() local mainGroup local score = 1 local timePlayedTotal = os.time() - startTime

There are many ways to accomplish this. But before we can suggest the best way, what is the purpose of sending the score and time to a server? Does this server have to be another computer in your possession? Or are you just trying to create a leaderboard?

Rob

I’m actually creating a training simulator and I’d like to see how long it takes for the users to go through the training.

The score is only “1” so that each time they repeat it I’ll know how many times they went through it (they are supposed to go through multiple times).

So I’d like to send it to another computer where I can see the results.

Since you need this data sent to a server in your control and it’s not for leaderboard use, then the best thing would be to setup a web server that can run a scripting language of your choice that can process HTTP POST and GET requests.  These scripts would take the request from a Corona network.request() API Call, pull the various key-value pairs sent and inject the data in to a database of your choice.  A common setup would be a Apache Web Server that supports PHP and a MySQL database, but if your skills are more in line with Microsoft’s IIS Server + .NET for scripting + MSSQL will work to.  Others might run a Node.js server and store data in a nosql type platform.

As long as the server can process HTTP POST and/or GET requests, Corona can talk to it.

Rob

So… I don’t have to change anything in my code? Just have the database request it? Doesn’t Lua have to send the data every time it reaches the final stage/scene?

What if I was going to send it to a leaderboard style page so the users can see and compare their scores?

When you get to your end stage, your Corona app will call the network.request API  with something like:

network.request("https://mywebserver.com/myapp/savescore.php?user=fred&score=10&timestamp=823482346", "GET", onRequestComplete)

If you care to check if this was successful or not or have the server pass back any data you will need a listener function, which I named “onRequestComplete” that will take the data sent by the script and give you an opportunity to parse that data and display it. You also check for errors in that function.

local function onRequestComplete( event )     if not event.isError then         print( event.response )     end end

This of course would need to be coded before you call network.request().

Then your server will have a script (I used PHP) that will have access to those GET variables in the $_GET[] table, i.e.

$user = $\_GET["user"]; $score = $\_GET["score"];

Then the script would inject the data into the database and then use the PHP echo statement to write some success code back that you recognize.

Now you probably want to use “POST” to send data rather than “GET” in particular if you’re passing sensitive data and you can run your server on https: instead of http:

The documentation for network.request() should get you started.

Rob

@Rob… seems like Corona needs a PHP section in the docs!

Thank you.

I’m still not sure where I put the “score” and “time” to be sent?

Check out this plugin for leaderboard management.https://marketplace.coronalabs.com/plugin/dreamlo

@richtornado, I would hope that you are already using HTTP GET because many URL’s you might type into a web browser use this already.  If you use YouTube you use URLs like:

https://www.youtube.com/watch?v=8f6e3e7-gvA

So you’re connecting to the website www.youtube.com using the https protocol. This will run a script called “watch” and it passes a key-value pair after the question mark.  The key is “v” and the value is “8f6e3e7-gvA”

If there are additional key-value pairs, they would be separate by an ampersand (&) like:

https://www.youtube.com/watch?v=8f6e3e7-gvA&rel=0&showinfo=0

In this case, there are two extra key-value pairs:  rel = 0 and showinfo = 0

Depending on how the script is coded, the order of the parameters shouldn’t matter because internally your going to look up the keys in a table by key to get the value. If this was done with PHP then you could do:

$showInfo = $\_GET["showinfo"]; $rel = $\_GET["rel"] $video = $\_GET["v"]

Now you have those parameters. Now YouTube can use GET for this because there isn’t any data in the URL that needs to be secured. But if you’re passing user credentials like usernames and passwords, you don’t want to use GET (this is putting the key-value pairs on the end of the URL) because they can’t be secured.

If your server is running  https instead of http you get free encryption of your data if you use POST instead of GET.  The key-value pairs are not part of the URL in this case, but sent as data to that URL.

Corona lets you pass a table of data to network.request() that ends up being the data that will be sent with POST. It’s still a bunch of key-value pairs.  Consider this code:
 

local params = {} params.body = "v=8f6e3e7-gvA&rel=0&showinfo=0 network.request( "https://www.youtube.com/watch", "POST", networkListener, params )

First, let me say, I doubt this would work because the watch script probably won’t take a POST request, I’m just using it as an example.

Since we are on HTTPS, the POST data will be encrypted and decrypted automatically for you. In your PHP script instead of using $_GET[] to access the variables are in a table named $_POST[] and you can do:

$video = $\_POST["v"]; $rel = $\_POST["rel"]; $showinfo = $\_POST["showinfo"];

Now your script can work with those variables as needed.

There are many ways to accomplish this. But before we can suggest the best way, what is the purpose of sending the score and time to a server? Does this server have to be another computer in your possession? Or are you just trying to create a leaderboard?

Rob

I’m actually creating a training simulator and I’d like to see how long it takes for the users to go through the training.

The score is only “1” so that each time they repeat it I’ll know how many times they went through it (they are supposed to go through multiple times).

So I’d like to send it to another computer where I can see the results.

Since you need this data sent to a server in your control and it’s not for leaderboard use, then the best thing would be to setup a web server that can run a scripting language of your choice that can process HTTP POST and GET requests.  These scripts would take the request from a Corona network.request() API Call, pull the various key-value pairs sent and inject the data in to a database of your choice.  A common setup would be a Apache Web Server that supports PHP and a MySQL database, but if your skills are more in line with Microsoft’s IIS Server + .NET for scripting + MSSQL will work to.  Others might run a Node.js server and store data in a nosql type platform.

As long as the server can process HTTP POST and/or GET requests, Corona can talk to it.

Rob

So… I don’t have to change anything in my code? Just have the database request it? Doesn’t Lua have to send the data every time it reaches the final stage/scene?

What if I was going to send it to a leaderboard style page so the users can see and compare their scores?

When you get to your end stage, your Corona app will call the network.request API  with something like:

network.request("https://mywebserver.com/myapp/savescore.php?user=fred&score=10&timestamp=823482346", "GET", onRequestComplete)

If you care to check if this was successful or not or have the server pass back any data you will need a listener function, which I named “onRequestComplete” that will take the data sent by the script and give you an opportunity to parse that data and display it. You also check for errors in that function.

local function onRequestComplete( event )     if not event.isError then         print( event.response )     end end

This of course would need to be coded before you call network.request().

Then your server will have a script (I used PHP) that will have access to those GET variables in the $_GET[] table, i.e.

$user = $\_GET["user"]; $score = $\_GET["score"];

Then the script would inject the data into the database and then use the PHP echo statement to write some success code back that you recognize.

Now you probably want to use “POST” to send data rather than “GET” in particular if you’re passing sensitive data and you can run your server on https: instead of http:

The documentation for network.request() should get you started.

Rob

@Rob… seems like Corona needs a PHP section in the docs!

Thank you.

I’m still not sure where I put the “score” and “time” to be sent?

Check out this plugin for leaderboard management.https://marketplace.coronalabs.com/plugin/dreamlo

@richtornado, I would hope that you are already using HTTP GET because many URL’s you might type into a web browser use this already.  If you use YouTube you use URLs like:

https://www.youtube.com/watch?v=8f6e3e7-gvA

So you’re connecting to the website www.youtube.com using the https protocol. This will run a script called “watch” and it passes a key-value pair after the question mark.  The key is “v” and the value is “8f6e3e7-gvA”

If there are additional key-value pairs, they would be separate by an ampersand (&) like:

https://www.youtube.com/watch?v=8f6e3e7-gvA&rel=0&showinfo=0

In this case, there are two extra key-value pairs:  rel = 0 and showinfo = 0

Depending on how the script is coded, the order of the parameters shouldn’t matter because internally your going to look up the keys in a table by key to get the value. If this was done with PHP then you could do:

$showInfo = $\_GET["showinfo"]; $rel = $\_GET["rel"] $video = $\_GET["v"]

Now you have those parameters. Now YouTube can use GET for this because there isn’t any data in the URL that needs to be secured. But if you’re passing user credentials like usernames and passwords, you don’t want to use GET (this is putting the key-value pairs on the end of the URL) because they can’t be secured.

If your server is running  https instead of http you get free encryption of your data if you use POST instead of GET.  The key-value pairs are not part of the URL in this case, but sent as data to that URL.

Corona lets you pass a table of data to network.request() that ends up being the data that will be sent with POST. It’s still a bunch of key-value pairs.  Consider this code:
 

local params = {} params.body = "v=8f6e3e7-gvA&rel=0&showinfo=0 network.request( "https://www.youtube.com/watch", "POST", networkListener, params )

First, let me say, I doubt this would work because the watch script probably won’t take a POST request, I’m just using it as an example.

Since we are on HTTPS, the POST data will be encrypted and decrypted automatically for you. In your PHP script instead of using $_GET[] to access the variables are in a table named $_POST[] and you can do:

$video = $\_POST["v"]; $rel = $\_POST["rel"]; $showinfo = $\_POST["showinfo"];

Now your script can work with those variables as needed.