Please help with Parse.com database and Corona SDK

Hey guys,

Im trying to work with parse in corona, I’ve read Jen’s tutorials, but its still a bit difficult for me to understand few things…

Im currently making a multiplayer Card Battle Game, each player has its own card stats, that is stored in a single table object.

The desired effect im looking for is: 

Each player, once registering (to parse) inside the game, is basically adding a row to the table of “user database” (that is accessible from parse via “dashboard” -> “data browser” -> “user table”) that row holds all of its data, including name, email, and all of his “Cards data table”. Now, when the player chooses to battle, I want to make a search in the parse database, and pick random 10 possible players and send their info to the device, then the user (on his device) can choose an opponent from that list of 10 players and battle against him. (the battle from the opponent side is handled by the device)

What is already done? I already set up the registration form in the game, it also sends all the “Card data table” to the parse database. I’m using this code in corona under the function networkListener( event ) for the registration

[lua]if (request == “signup”) then
– pass player account info to LocalAccount
for k,v in pairs(AccountSetup) do
LocalAccount[k] = v
end
– pass parse response to LocalAccount
for k,v in pairs(response) do
LocalAccount[k] = v
end
– set playeremail table in case password reset needs to be called
playerEmail.email = LocalAccount[“email”]
end[/lua]

And for updating the card stats im using this code under the function networkListener( event )

[lua]if (request == “updateObj”) then
–get response
for k,v in pairs(response) do
LocalAccount[k] = v
end
–now update
for k,v in pairs (updateData) do
LocalAccount[k] = v
end
end [/lua]

Moving on to the questions: 

  1. What is the code (in corona) for requesting such a search on the database? 

  2. Since im expecting the database to be quite big, I would think that the “picking 10 random players from the database process” better take place in the cloud, instead of downloading the whole database to the device and make the process on the device itself, im guessing ‘cloud code’ would be involved, any help on how to do this (the code for the search and return result) will be greatly appreciated, cause I have no knowledge other then Lua and c languages, and parse.com doesn’t use them…

Any help will help!

Roy.

hi, I am not sure that you can select ten at random using Parse’s REST API - I would recommend asking on the Parse forum, there are some extremely helpful folks there (and then let us know what happens, it’s very useful knowledge!)

If I had to do something like this I would use Cloud Code.

https://www.parse.com/docs/cloud_code_guide

To keep it simple I would just do a query.count to get the total count of Users. Then create a random number for count-10. Then pull the 10 records from that random point.

Cloud Code uses the Parse Javascript codebase. Here is a starting point on doing it with CC. I did not upload and test this code.

Parse.Cloud.define("getRandomPlayers", function(request, response) { var Users = Parse.Object.extend("User"); var query = new Parse.Query(Users); query.count({ success: function(count) { // The count request succeeded. var jump = Math.floor((Math.random()\*(count-10))+1); var query2 = new Parse.Query(Users); query2.ascending("objectId"); query2.limit(10); query2.skip(jump); query2.find({ success: function(results) { response.success(results); }, error: function(error) { response.success(error); } }); }, error: function(error) { response.success(error); } }); });

I am not 100% sure what the ordering is by default on the User class. So I added in a line to sort it by objectId to make it more random than say, sorting by name . You could do other tricks to make it more random as well. I just wouldn’t recommend creating 10 random numbers and calling 10 different records in one Cloud Code function as it may be too slow. Cloud Code only gets 15 seconds to execute before Parse times out the function.

hi, I am not sure that you can select ten at random using Parse’s REST API - I would recommend asking on the Parse forum, there are some extremely helpful folks there (and then let us know what happens, it’s very useful knowledge!)

If I had to do something like this I would use Cloud Code.

https://www.parse.com/docs/cloud_code_guide

To keep it simple I would just do a query.count to get the total count of Users. Then create a random number for count-10. Then pull the 10 records from that random point.

Cloud Code uses the Parse Javascript codebase. Here is a starting point on doing it with CC. I did not upload and test this code.

Parse.Cloud.define("getRandomPlayers", function(request, response) { var Users = Parse.Object.extend("User"); var query = new Parse.Query(Users); query.count({ success: function(count) { // The count request succeeded. var jump = Math.floor((Math.random()\*(count-10))+1); var query2 = new Parse.Query(Users); query2.ascending("objectId"); query2.limit(10); query2.skip(jump); query2.find({ success: function(results) { response.success(results); }, error: function(error) { response.success(error); } }); }, error: function(error) { response.success(error); } }); });

I am not 100% sure what the ordering is by default on the User class. So I added in a line to sort it by objectId to make it more random than say, sorting by name . You could do other tricks to make it more random as well. I just wouldn’t recommend creating 10 random numbers and calling 10 different records in one Cloud Code function as it may be too slow. Cloud Code only gets 15 seconds to execute before Parse times out the function.