Forum BigDB How to load randomly

Discussion and help relating to the PlayerIO database solution, BigDB.

How to load randomly

Postby Ariel Valls » May 20th, 2011, 6:27 am

How can I load data randomly from a table, this what I'm trying to achieve:

I've a table with Puzzles then I want to load them randomly for the user but also check if the user have played that puzzle so the user will always get a new different puzzle. So if a player play a puzzle a property called id from that puzzle will be saved on a array within playerObject called puzzlesPlayed, dunno if that is the best to do it.

Thanks
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby Henrik » May 20th, 2011, 11:08 am

Give each table an id that is a number from 1 to as many puzzles as you have.
Make an index on that property.
Pick a random number, x, from between 1 and the number of puzzles you have.
Load puzzle number x from BigDB.

(Or let the id be the row key of the puzzles, that is probably better)
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to load randomly

Postby Ariel Valls » May 20th, 2011, 11:27 am

Thanks for the answer Henrik, actually I've the id as the row key and already made the index, my problem is I dont know how to get a number for the puzzles, i mean how do i get the total rows of that table? so i can make like Math.random()*puzzles.length just to say something... the number of puzzles will change periodically it wont be a fixed number.

Hope you have a good solution for me :D
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby Oliver » May 20th, 2011, 2:44 pm

put the number of puzzles in a database object and load that ?
User avatar
Oliver
.IO
 
Posts: 1159
Joined: January 12th, 2010, 8:29 am

Re: How to load randomly

Postby Ariel Valls » May 20th, 2011, 3:12 pm

I dont think that will work, some puzzles will be added by the users on the client and some will be added by the admin directly on player.io BigDB admin, there is no other more accurate way? ... you should add a new feature like PlayerIO.bigDB.totalRows("MyTable") or something like that hehe
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby Henrik » May 20th, 2011, 3:43 pm

No, we sadly can't add a feature like that. You could keep a number in some other object that you then update every time a puzzle is added or deleted.

Another way you could solve the problem is to give each puzzle a random id between say 0 and 2147483647, and when you want to fetch a random puzzle you pick a random number between those two, and do a loadrange of all puzzles that have an id less than that, with a limit of 1. If your first puzzle then has id 0, that loadrange will always return one random puzzle.
Henrik
.IO
 
Posts: 1880
Joined: January 4th, 2010, 1:53 pm

Re: How to load randomly

Postby Ariel Valls » May 20th, 2011, 6:32 pm

Sounds like a good idea Henrik :) but the bad thing is if I have add a bunch of puzzles manually on bigDB panel Ill have to put that number manually one by one and making sure is not repeat it. :(
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby WindieGames » May 21st, 2011, 9:05 am

I was just about to ask a question like this, so thank you for the post.

When a puzzle is added to the database:
1) give it a random ID property of say 1-10
2) store it's creation date

When you want to load a puzzle:
1) randomly select a number from 1-10
2) randomly select a date beginning from when the database was created, until now.
3) randomly select to check before the date or after the date
4) poll the DB for puzzles with the ID from 1 and the creation date parameters from 2 and 3

I think that should give enough variation to always pull up a fresh selection of puzzles (as long as you have enough content continually being added). From there you can check them against your completed puzzles and poll for new ones if you need too.

I'm going to try it with my puzzle game and I'll let you know the results.
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: How to load randomly

Postby Ariel Valls » May 21st, 2011, 1:24 pm

Yes that sounds good, but im talking about of thousands of puzzles, I was thinking to add them manually on bigDB panel but seems like that wont be possible, Ill have to make some kinda of mini cms for that or just have the person who will be on charge to add them to put that manually and thats it hehe.

The issue about generating random numbers is if the number id got repeat it, so the way you said there will be for example 5 puzzles with same id but different dates, maybe would be better if id its the date so its always different, if bigDB doesnt found an id Ill have to try to load again with different id or i just have to use LoadIndex or LoadRange and give a range, I'm just asking if bigDB always get a valid object lets say i give this range 1 - 10 but 5 and 7 doesnt exist bigDB will look automatically for other id in range that its valid?
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby WindieGames » May 21st, 2011, 9:12 pm

Well, if you're talking about 1000's of puzzles, statisticly you would have at least a hundred in each category. You could always start with a smaller range of 1-3 or 1-5 in the beginning.

I would highly recommend some sort of method that adds puzzles programatically (even if this takes you away from your other features for an extended period of time). Storing that amount of puzzles manually may seem like a big task right now, but will only seem bigger after the first 100, 200, ect. Have players create them client side, send them to the server for validation and store them accordingly.

The reason I said a number and a date is that it will pull up more variety and always pull something up as long as you have a puzzle with that ID in the database. But like you said, if your query doesn't find anything, just retry. Likewise, you can go your method and just use a date, but if you're going to select a single random date, that will be less likely than my method. If you select a range of dates, that's more likely. If you select the first 10 before or after a certain date, that's probably the best method.

I'm in a rush to get to work so I'm sorry if that was vague.
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: How to load randomly

Postby Ariel Valls » May 22nd, 2011, 12:47 am

Yes I had planned make players add puzzles too but I need to add at least 1000 for start, but you are right the best way to do its on the client side, fortunately for me I've people who will be doing that tedious task, Ill just have to build the tool hehe.

So you are working on a puzzle game too?, Ill show up mine here soon its like a mmo of puzzles hehe, its almost done :)
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm

Re: How to load randomly

Postby WindieGames » May 22nd, 2011, 6:32 am

I'm not sure how complex your puzzles are, but 1000 is a very large number. Even getting 20 or 30 in would be enough to pull users into your game (assuming it is addictive) and then they would instantly start developing their own puzzles. Players love to build and share, take a look at everybody edits.

If your editor is simple, clean and fun to use, you will have all the content you need from your users, saving you more time to add features, fix bugs and work on improving your monetization strategy.

I'm looking forward to trying out your game. I love puzzles!

Chris
WindieGames
 
Posts: 35
Joined: January 30th, 2011, 1:36 am

Re: How to load randomly

Postby Ariel Valls » May 22nd, 2011, 7:56 am

Here is why we need lots of puzzles from start, the game will be multiplayer up to 7 players each of them will play 7 puzzles together but cant be puzzles they have played, so if the game on one day its played for 20 players we will need like 100+ puzzles more if they play multiple times etc.. its like if player 1 have played puzzle id 5 but player 2 havent played then need to get another puzzle.

Even if players add them wont work because we need to approve their puzzles first, thats why we need a lot from start.

Anyways as I said wont be me who will have to add 1k of puzzles so I still have the to work on features etc, hope next week Ill post my game for test here, I will be glad to read good advices from you and player.io team.
Ariel Valls
Paid Member
 
Posts: 32
Joined: March 11th, 2011, 12:50 pm


Return to BigDB



cron